发帖
0 0 0

忙里偷闲发个AI-WB2的SPIFFS文件系统,刷写程序、OTA后不掉数据哦

小何
论坛元老

7

主题

21

回帖

3519

积分

论坛元老

积分
3519
Ai-WB2系列 12 0 3 小时前
查看项目下的Make File
#
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
# project subdirectory.
#

PROJECT_NAME := LivingRoomCock
PROJECT_PATH := $(abspath .)
PROJECT_BOARD := evb
export PROJECT_PATH PROJECT_BOARD
#CONFIG_TOOLPREFIX :=

-include ./proj_config.mk

ifeq ($(origin BL60X_SDK_PATH), undefined)
BL60X_SDK_PATH_GUESS ?= $(shell pwd)
BL60X_SDK_PATH ?= $(BL60X_SDK_PATH_GUESS)/../../../..
$(info ****** Please SET BL60X_SDK_PATH ******)
$(info ****** Trying SDK PATH [$(BL60X_SDK_PATH)])
endif

COMPONENTS_NETWORK := sntp dns_server
COMPONENTS_BLSYS   := bltime blfdt blmtd blota bloop loopadc looprt loopset
COMPONENTS_VFS     := romfs cjson

INCLUDE_COMPONENTS += freertos_riscv_ram bl602 bl602_std newlibc wifi wifi_manager wpa_supplicant bl602_os_adapter wifi_hosal hosal mbedtls_lts lwip lwip_dhcpd vfs yloop utils cli aws-iot httpc httpd dns_server netutils blog blog_testc
INCLUDE_COMPONENTS += easyflash4 coredump lwip_altcp_tls_mbedtls  blcrypto_suite bl_os_adapter rfparam_adapter_tmp spiffs

INCLUDE_COMPONENTS += $(COMPONENTS_NETWORK)
INCLUDE_COMPONENTS += $(COMPONENTS_BLSYS)
INCLUDE_COMPONENTS += $(COMPONENTS_VFS)
INCLUDE_COMPONENTS += $(PROJECT_NAME)

include $(BL60X_SDK_PATH)/make_scripts_riscv/project.mk
最终调用make_scripts_riscv/project.mk

flash-only:
        cd $(BL60X_SDK_PATH)/tools/flash_tool && env SDK_APP_BIN=$(APP_BIN) SDK_BOARD=$(PROJECT_BOARD) SDK_NAME=$(PROJECT_NAME) SDK_MEDIA_BIN=$(APP_MEDIA_BIN) SDK_ROMFS_DIR=$(APP_ROMFS_DIR) SDK_DTS=$(PROJECT_DTS) SDK_XTAL=$(PROJECT_BOARD_XTAL) BL_FLASH_TOOL_INPUT_PATH_cfg2_bin_input=$(APP_BIN) $(FLASH_TOOL_TYPE) --chipname=BL602 --baudrate=$(SERIAL_BAUDRATE) --port=$(SERIAL_PORT) --pt=$(BL60X_SDK_PATH)/tools/flash_tool/chips/bl602/partition/partition_cfg_4M.toml --dts=$(BL60X_SDK_PATH)/tools/flash_tool/chips/bl602/device_tree/04-bl_factory_params_IoTKitA_40M-20220625.dts --boot2=$(BL60X_SDK_PATH)/tools/flash_tool/chips/bl602/builtin_imgs/boot2_isp_bl602_v6.5.1/boot2_iap_release.bin --firmware=$(APP_BIN)
最终得出Flash分区文是/tools/flash_tool/chips/bl602/partition/partition_cfg_4M.toml
备份文partition_cfg_4M.toml文件

                               
登录/注册后可看大图

增加spiffs文件系统分区
[pt_table]
#partition table is 4K in size
address0 = 0xE000
address1 = 0xF000

[[pt_entry]]
type = 0
name = "FW"
device = 0
address0 = 0x10000
size0 = 0x100000
address1 = 0x110000
size1 = 0x100000
# compressed image must set len,normal image can left it to 0
len = 0

[[pt_entry]]
type = 0
name = "spiffs"
device = 0
address0 = 0x210000
size0 = 0x28000
address1 = 0x238000
size1 = 0x8000
# compressed image must set len,normal image can left it to 0
len = 0


[[pt_entry]]
type = 2
name = "mfg"
device = 0
address0 = 0x240000
size0 = 0x32000
address1 = 0
size1 = 0
# compressed image must set len,normal image can left it to 0
len = 0

[[pt_entry]]
type = 3
name = "media"
device = 0
address0 = 0x272000
size0 = 0x1000
address1 = 0
size1 = 0
# compressed image must set len,normal image can left it to 0
len = 0

[[pt_entry]]
type = 4
name = "PSM"
device = 0
address0 = 0x273000
size0 = 0x33000
address1 = 0
size1 = 0
# compressed image must set len,normal image can left it to 0
len = 0

[[pt_entry]]
type = 5
name = "KEY"
device = 0
address0 = 0x2A6000
size0 = 0x2000
address1 = 0
size1 = 0
# compressed image must set len,normal image can left it to 0
len = 0

[[pt_entry]]
type = 6
name = "DATA"
device = 0
address0 = 0x2A8000
size0 = 0x5000
address1 = 0
size1 = 0
# compressed image must set len,normal image can left it to 0
len = 0


[[pt_entry]]
type = 7
name = "factory"
device = 0
address0 = 0x2AD000
size0 = 0x7000
address1 = 0
size1 = 0
# compressed image must set len,normal image can left it to 0
len = 0

#if user want to put RF calibration data on flash, uncomment following pt entry
#[[pt_entry]]
#type = 8
#name = "rf_para"
#device = 0
#address0 = 0x1FF000
#size0 = 0x1000
#address1 = 0
#size1 = 0
## compressed image must set len,normal image can left it to 0
#len = 0
修改已移植好的spiffs.c文件
/*
* @Author: Sean Kwok 1026025056@qq.com
* @Date: 2022-10-09 10:53:46
* @LastEditors: Sean Kwok 1026025056@qq.com
* @LastEditTime: 2022-10-28 17:37:50
* @FilePath: /Ai-Thinker-WB2/applications/storage/spiffs_demo/spiffs_demo/main.c
* @Description: SPIFFS Example
*/

#include <stdio.h>
#include <hosal_flash.h>
#include <stdlib.h>
#include <blog.h>
#include <vfs.h>
#include <bl_spiffs.h>
#include "spiffs_app.h"
#include <hal_boot2.h>

static spiffs fs;

/*
partition_cfg_4M.toml

[[pt_entry]]
type = 2
name = "mfg"
device = 0
address0 = 0x240000
size0 = 0x32000
address1 = 0
size1 = 0
# compressed image must set len,normal image can left it to 0
len = 0

*/
#define spiffs_size 0x00047000
#define spiffs_start_addr (0x240000 - spiffs_size)
#define flash_PAGE_SIZE 256
#define flash_block_SIZE 4096

static u8_t spiffs_work_buf[flash_PAGE_SIZE * 2];
static u8_t spiffs_fds[32 * 4];
static u8_t spiffs_cache_buf[(flash_PAGE_SIZE + 32) * 4];

spiffs_config cfg = {
    .phys_size = spiffs_size,                 // use all spi flash
    .phys_addr = spiffs_start_addr, // start spiffs at start of spi flash
    .phys_erase_block = flash_block_SIZE,                // according to datasheet
    .log_block_size = flash_block_SIZE,                  // let us not complicate things
    .log_page_size = flash_PAGE_SIZE,          // as we said
    .hal_read_f = spiffs_read_flash,
    .hal_write_f = spiffs_write_flash,
    .hal_erase_f = spiffs_erase_flash,
};

static void sys_spiffs_format(void)
{
    blog_info(">---format spiffs coreflash");
    SPIFFS_unmount(&fs);
    SPIFFS_format(&fs);

    int res = SPIFFS_mount(&fs,
                           &cfg,
                           spiffs_work_buf,
                           spiffs_fds,
                           sizeof(spiffs_fds),
                           spiffs_cache_buf,
                           sizeof(spiffs_cache_buf),
                           0);
    blog_info("mount res: %i", res);
    blog_info(">---format spiffs coreflash finish");
}

void spiffs_mount(void)
{
    uint32_t addr0, addr1;
    uint32_t size0, size1;
    int active, ret;

    ret = hal_boot2_partition_addr("spiffs",&addr0,&addr1,&size0,&size1,&active);
    //bl_os_printf("spiffs_app spiffs;ret:%x;active:%x;addr0:%x;addr1:%x;size0:%x;size1:%x;\r\n",ret,active,addr0,addr1,size0,size1);
    cfg.phys_addr = addr0;
    cfg.phys_size = size0;


    int res = SPIFFS_mount(&fs,
                           &cfg,
                           spiffs_work_buf,
                           spiffs_fds,
                           sizeof(spiffs_fds),
                           spiffs_cache_buf,
                           sizeof(spiffs_cache_buf),
                           0);
    if (SPIFFS_ERR_NOT_A_FS == res)
    {
        blog_warn("spiffs start format");
        sys_spiffs_format();
    }
    else
    {
        blog_info("spiffs mount success");
    }
}

void spiffs_write_read_test(void)
{
    char buf[13] = {0};
    spiffs_file fd;

    blog_info("--> spiffs write/read demo <--\r\n");
/*
    // Surely, I've mounted spiffs before entering here
    fd = SPIFFS_open(&fs, "my_file", SPIFFS_CREAT | SPIFFS_TRUNC | SPIFFS_RDWR, 0);
    if (SPIFFS_write(&fs, fd, (u8_t *)"Hello world", 12) < 0){
        blog_info("errno %i\n", SPIFFS_errno(&fs));
    }
    SPIFFS_close(&fs, fd);
*/
    fd = SPIFFS_open(&fs, "my_file", SPIFFS_RDWR, 0);
    if (SPIFFS_read(&fs, fd, (u8_t *)buf, 12) < 0){
        blog_info("errno %i\n", SPIFFS_errno(&fs));
    }
    SPIFFS_close(&fs, fd);
    blog_info("--> %s <--\n", buf);
    blog_info("--> spiffs write/read demo end<--\r\n");
}
hal_boot2_partition_addr("spiffs",&addr0,&addr1,&size0,&size1,&active);这个函数可以得到分区文件partition_cfg_4M.toml的开始地址和大小。修改spiffs的起始地址和大小,cfg.phys_addr = addr0;cfg.phys_size = size0;spiffs文件系统就存在flash这个空间,烧写程序和OAT升级都不会破坏这个空间,也就是spiffs数据不会丢失。     
上传的附件: 新建文件夹.rar (528.67 KB, 下载次数: 0)

附近增加了例程。


──── 0人觉得很赞 ────

使用道具 举报

您需要登录后才可以回帖 立即登录
高级模式
返回
统计信息
  • 会员数: 28078 个
  • 话题数: 39675 篇