fileX移植

ThreadX RTOS在被微软收购后最近开源了,相关介绍在:https://docs.microsoft.com/en-us/azure/rtos/, 源码在github。

之前在使用threadX RTOS,但是没有用过它的文件系统fileX,从介绍可以看是FAT兼容(fat12,fat16,fat32,exfat),支持fail safe等。功能介绍:https://docs.microsoft.com/en-us/azure/rtos/filex/overview-filex

Azure RTOS FileX is a high-performance, file allocation table (FAT)-compatible file system that’s fully integrated with Azure RTOS ThreadX and available for all supported processors. Like Azure RTOS ThreadX, Azure RTOS FileX is designed to have a small footprint and high performance, making it ideal for today’s deeply embedded applications that require file management operations. FileX supports most physical media, including RAM, Azure RTOS USBX, SD CARD, and NAND/NOR flash memories via Azure RTOS LevelX.

  • FAT 12/16/32 and exFAT support
  • Multiple partition support
  • Automatic scaling
  • Endian neutral
  • Long file name and 8.3 support
  • Optional fault tolerance support
  • Logical sector cache
  • FAT entry cache
  • Pre-allocation of clusters
  • Contiguous file support
  • Optional performance metrics
  • Azure RTOS TraceX system analysis support

我是在SD卡使用FAT文件系统,fileX文档很齐全,但是对移植方面没有详细介绍,具体步骤需要通过看例子和代码摸索,源码中包括了一个使用ram使用fileX的简单例子,我们可以根据它写与IO driver对接的代码。以下给出了移植方面的过程以注意的地方。

通常在fx_port.h中定义移置 fileX所需的基本宏和函数,如mutex函数,时间更新周期,以及功能宏。

  1. 调用fx_system_initialize()初始化fileX,其中date及time可以同步为系统时间,默认10s秒更新一次date/time。
  2. 根据sample实现I/O driver。我们使用的是sd卡,需要实现FX_DRIVER_READ/FX_DRIVER_WRITE/FX_DRIVER_INIT/FX_DRIVER_BOOT_READ/FX_DRIVER_BOOT_WRITE,这里需要注意是FX_DRIVER_BOOT_READ/FX_DRIVER_BOOT_WRITE,它是读写boot区域,对应的是DBR(dos boot record),我们可以在FX_DRIVER_INIT中去增加代码获取boot区域所在的物理sector。
  3. 查找sd卡DBR地址:(包括两种情况:1卡上无分区,2卡上有分区),无分区没有MBR,有分区存在MBR。看下图说明。流程:
    1. 读取sector 0, 第一个字节是否为有效的跳转指令,或是否为exfat
    2. 若是,则sector 0即为有效的fat分区。
    3. 若不是,则去读sector 0上DPT(disk partition table),从446字节开始为DPT,DPT共64个字节,由4个分区表组成,每个分区表16个字节,从每一个分表的第8字节—第12字节是该分区的所在的起始sector地址(即为DBR地址)。
    4. 读取DPT的每个分区起始sector(DBR地址),判断第一个字节是否为有效的跳转指令,或是否为exfat,若是则为有效fat分区,记录起始sector地址(即为DBR地址)。

fileX移植

fileX移植

fileX移植

FX_DRIVER_INIT中获取文件系统起始物理sector代码:

fileX移植

4.  fileX内部使用的是逻辑sector(从文件系统的0号sector(即DBR)开始),需要在I/O driver中转成物理sector。

物理sector地址 = fileX的逻辑sector地址 + 文件系统起始的物理sector地址(DBR地址,步聚3获取的地址)

fileX移植

5.实现FX_DRIVER_BOOT_READ/FX_DRIVER_BOOT_WRITE,根据上面得到的bootsector,调用sd卡IO接口去读写。

fileX移植

fileX移植 

6. 实现FX_DRIVER_READ/FX_DRIVER_WRITE,根据上面得到物理sector就可以去调用sd卡IO接口去读写。

7. 以上完成后,对接IO的部分就完成了,可以使用sample中的例子测试。

8. 如果要支持exfat,需要在定义FX_ENABLE_EXFAT。