[RTT] SPI Flash and file system: DFS and EasyFlash

  
Reference documents (domestic): DFS reference documentation
Reference documents (domestic): EasyFlash reference documentation

  

1, DFS overview

   DFS is a virtual file system component provided by RT thread. Its full name is Device File System, that is, device virtual file system.
  for detailed description, please refer to the reference manual above.

  

2, EasyFlash overview

   EasyFlash is an open source lightweight embedded Flash memory library, which makes it easier for developers to develop common applications based on Flash memory.
  it turns Flash into a small key value storage database of NoSQL (non relational database) model.
  for detailed description, please refer to the reference manual above.

  

3, DFS and EasyFlash relationship

  as shown in the figure below.

  

4, Use of DFS, EasyFlash and SD card

  

1. Use of DFS

  because we use W25Q128 to mount the elm FAT of DFS, the default sector size of elm FAT is 512, but the sector size of W25Q128 is 4096, so it needs to be modified accordingly.

  moreover, DFS needs to be modified accordingly.

/* components/dfs/include/dfs.h */

#ifndef SECTOR_SIZE
#define SECTOR_SIZE              4096	//512
#endif

  in the FAL article, we introduced the DFS mounting process in the summary of "use of FAL partitions", which will not be introduced here.
  note: DFS must be mounted on a block device.

  

2. Use of EasyFlash

   EasyFlash does not need to be mounted on the block device. It only needs to find the partition specified in the FAL partition table, and then directly operate the partition using the API provided by FAL.
  therefore, we need to specify the partition to be used by EasyFlash.

/* bsp/stm32/stm32l475-atk-pandora/board/ports/ef_fal_port.c */

/* "easyflash" partition in FAL used */
#define FAL_EF_PART_NAME               "easyflash"

  next, we can add the initialization of EasyFlash to the code provided by "DFS use", and then we can use EasyFlash.

#include <fal.h>
#include <dfs_fs.h>
#include <easyflash.h>

/* Mount initialization */
void mnt_init(void)
{
	struct rt_device *rootfs = RT_NULL;
	
	/* Create a block device using the filesystem partition. The block device name is filesystem */
	rootfs = fal_blk_device_create("filesystem");
	if(rootfs == RT_NULL)
        return;

	/* Mount the elm fat file system on the filesystem partition of W25Q128 */
	if (dfs_mount("filesystem", "/", "elm", 0, 0) == 0)
    {
        rt_kprintf("file system initialization done!\n");
    }
    else
    {
        if(dfs_mkfs("elm", "filesystem") == 0)
        {
            if (dfs_mount("filesystem", "/", "elm", 0, 0) == 0)
            {
                rt_kprintf("file system initialization done!\n");
            }
            else
            {
                rt_kprintf("file system initialization failed!\n");
            }
        }
    }
	
	/* easyflash Initialization of */
    easyflash_init();
}

  

3. Use of SD card

   here's the idea: because we use the "filesystem" partition of off chip Flash to create the corresponding block device to mount the root directory /, we create a mnt directory under the root directory / as the mount point to mount the SD card.
  before mounting, we need to set the number of file systems that DFS supports mounting (from 2 to 3), otherwise an error will be reported.

  because RT thread has provided the SD card driver code, we need to shield the mounting part of the SD card driver code, and then mount it ourselves.
   similarly, we can add SD card mounting on the code provided by "use of EasyFlash".

void mnt_init(void)
{
    struct rt_device *rootfs = RT_NULL;

    /* FAL initialization */
    fal_init();

    /* Create a block device using the filesystem partition. The block device name is filesystem */
    rootfs = fal_blk_device_create("filesystem");
    if(rootfs == RT_NULL)
        return;

	/* Mount the elm fat file system on the filesystem partition of W25Q128 */
    if (dfs_mount("filesystem", "/", "elm", 0, 0) == 0)
    {
        rt_kprintf("file system initialization done!\n");
    }
    else
    {
        if(dfs_mkfs("elm", "filesystem") == 0)
        {
            if (dfs_mount("filesystem", "/", "elm", 0, 0) == 0)
            {
                rt_kprintf("file system initialization done!\n");
            }
            else
            {
                rt_kprintf("file system initialization failed!\n");
            }
        }
    }

    /* easyflash Initialization of */
    easyflash_init();

    /* Create / mnt directory for mounting SD card */
    if (opendir("/mnt") == RT_NULL)
    {
        if (mkdir("mnt", 0x777) == -1)
            return;
    }    
        
    if(rt_device_find("sd0") == RT_NULL)
    {
        rt_kprintf("failed to find sd card device.\n");
        return;
    }

    if (dfs_mount("sd0", "/mnt", "elm", 0, 0) == RT_EOK)
    {
        rt_kprintf("sd card mount to '/mnt' success.\n");
    }
    else
    {
        rt_kprintf("sd card mount to '/mnt' failed!\n");
    }
}

Keywords: rt-thread

Added by fleabay on Fri, 21 Jan 2022 09:48:07 +0200