摘要:
本文讲述Contiki文件系统Coffee的移植,包括参数配置和宏映射,并解释了Coffee一些宏的含义。
一、概述
Coffee文件系统实现代码在contiki/core/cfs/cfs-coffee.c,与平台无关。移植Coffee文件系统主要是修改contiki/cpu/platform(比如我的是arm/stm32f103)/cfs-coffee-arch.h文件,包括针对特定硬件进行一些参数配置和将宏COFFEE_WRITE、COFFEE_READ、COFFEE_ERASE映射到对应的函数。
二、参数配置
2.1 硬件相关
我板子的MCU是STM32F103RBT6,片上Flash是128KB(0x0800 0000 ~ 0x0801 FFFF),页大小是1KB。如果不知道怎么看,可参见博文《STM32片上Flash内存映射、页面大小、寄存器映射》。
- #define FLASH_START 0x8000000 /*Flash起始地址*/
-
#define FLASH_PAGE_SIZE 1024 /*最小擦除单元*/
-
#define FLASH_PAGES 125 /*Last 3 pages reserved for NVM*/
最后3页留给NVM,NVM是指非易失性存储器(non-volatile memory)还是指别管(Never Mind)?个人认为,应该预留不使用,因为在链接映像文件contiki.map(Contiki\EWARM\Debug\List\)找不到该范围的地址。在IAR链接脚本iar-cfg-coffee.icf(Contiki\cpu\stm32w108),给了内存区域(Memory Regions)的定义,如下:
- /*-Memory Regions-*/
-
define symbol __ICFEDIT_region_NVM_start__ = 0x0801F800;
-
define symbol __ICFEDIT_region_NVM_end__ = 0x0801FFFF;
2.2 COFFEE_PAGE_SIZE和COFFEE_SECTOR_SIZE
尽管FLASH按页写,按块擦除,但文件系统不一定非得按这个来(当然,落实到实际写还是按页写)。Coffee逻辑页大小和逻辑区大小可以自定义,COFFEE_SECTOR_SIZE用于应付大的存储设备(比如SD卡),在这种情况下,将其设置大一点可加快顺序扫描速度。[1]建议将COFFEE_PAGE_SIZE和COFFEE_SECTOR_SIZE设小一点,可以获得更合理的性能(reasonable performance)。我的设置如下:
- /* Minimum reservation unit for Coffee. It can be changed by the user. */
-
#define COFFEE_PAGE_SIZE (FLASH_PAGE_SIZE/4) //即256B
-
-
/* These must agree with the parameters passed to makefsdata */
-
#define COFFEE_SECTOR_SIZE FLASH_PAGE_SIZE //即1KB
2.3 COFFEE_ADDRESS
COFFEE_ADDRESS,指文件系统从FLASH地址COFFEE_ADDRESS处开始管理,尽管FLASH大小是128KB(以STM32F103RBT6为例),但需要腾出一部分空间来存放代码。从链接映射文件(Generate linker map file,更具体信息得参考《IAR C/C++ Development Guide for ARM》),我编译链接完代码段占用空间为0x0800 0000 ~ 0x08004210。源码如下:
- /* If using IAR, COFFEE_ADDRESS reflects the static value in the linker script iar-cfg-coffee.icf, so it can't be passed as a parameter for Make.*/
-
#ifdef __ICCARM__
-
#define COFFEE_ADDRESS 0x8010c00
-
#endif
在链接脚本iar-cfg-coffee.icf中,有这么一项,如下:
- /*-Memory Regions-*/
-
define symbol __ICFEDIT_region_CFS_start__ = 0x08010c00; /* Reserved for contiki flash file system. COFFEE_ADDRESS must be changed also in cfs-coffee-arch.h */
-
-
define symbol __ICFEDIT_region_CFS_end__ = 0x0801F3FF;
2.4 若干宏含义
- #define COFFEE_PAGES ((FLASH_PAGES*FLASH_PAGE_SIZE-(COFFEE_ADDRESS-FLASH_START))/COFFEE_PAGE_SIZE)
-
#define COFFEE_START (COFFEE_ADDRESS & ~(COFFEE_PAGE_SIZE-1))
-
#define COFFEE_SIZE (COFFEE_PAGES*COFFEE_PAGE_SIZE)
COFFEE_PAGES(Coffee页面总数):(125*1024)-(0x8010c00-0x8000000),再除以256,最后结果是232
COFFEE_START(Coffee起始地址):即屏蔽掉COFFEE_ADDRESS后8位(COFFEE_PAGE_SIZE为256B),结果还是0x08010c00
COFFEE_SIZE(Coffee总大小):COFFEE_PAGES*COFFEE_PAGE_SIZE,等于59392Byte,即58KB
以下是在Linux运行(单独把cfs-coffee-arch.h抽出来,略在修改),打印出来的结果:
2.5 其他选项
以下这些选项根据实际应用设置:
- #define COFFEE_NAME_LENGTH 20
-
-
#define COFFEE_MAX_OPEN_FILES 4
-
#define COFFEE_FD_SET_SIZE 8
-
#define COFFEE_DYN_SIZE (COFFEE_PAGE_SIZE*1)
-
#define COFFEE_MICRO_LOGS 0
-
#define COFFEE_LOG_TABLE_LIMIT 16 //It doesnt' matter as COFFEE_MICRO_LOGS is 0.
-
#define COFFEE_LOG_SIZE 128
2.6 约束条件
尽管用户可以自定义这些变量,但需符合一定的约束条件。
(1)COFFEE_ADDRESS
COFFEE_ADDRESS必须是1KB边界对齐,这点比较好理解,预编译源码如下:
- #if (COFFEE_ADDRESS & 0x3FF) !=0
- #error "COFFEE_ADDRESS not aligned to a 1024-bytes page boundary."
- #endif
(2)COFFEE_START与COFFEE_SECTOR_SIZE- #if COFFEE_START & (COFFEE_SECTOR_SIZE - 1)
- #error COFFEE_START must point to the first byte in a sector.
- #endif
三、映射宏
将Coffee文件系统读、写、擦除映射到平台相关的函数,如下所示,并在cfs-coffee-arch.c实现。
- #define COFFEE_WRITE(buf, size, offset) stm32_flash_write(COFFEE_START + offset, buf, size)
-
-
#define COFFEE_READ(buf, size, offset) stm32_flash_read(COFFEE_START + offset, buf, size)
-
-
#define COFFEE_ERASE(sector) stm32_flash_erase(sector)
注:
本文只是根据[1]和源代码分析,旨在对Coffee若干宏有个了解,方便日后分析。具体移植,可能还需更多工作。
参考资料:
[1]
阅读(5559) | 评论(0) | 转发(0) |