由 小何 贡献,Ai-Thinker 进行整理
AI-WB2的SPIFFS文件系统,刷写程序、OTA后不掉数据
查看项目下的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 ?=
(info ****** Please SET BL60X_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
(APP_BIN) SDK_BOARD= (PROJECT_NAME) SDK_MEDIA_BIN= (APP_ROMFS_DIR) SDK_DTS= (PROJECT_BOARD_XTAL) BL_FLASH_TOOL_INPUT_PATH_cfg2_bin_input=$(APP_BIN) (SERIAL_BAUDRATE) --port= (BL60X_SDK_PATH)/tools/flash_tool/chips/bl602/partition/partition_cfg_4M.toml --dts= (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 = 0x100000compressed 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 = 0x8000compressed 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 = 0compressed 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 = 0compressed 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 = 0compressed 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 = 0compressed 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 = 0compressed 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 = 0compressed 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 = 0compressed 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, 下载次数: 1)
附近增加了例程。

