其他分享
首页 > 其他分享> > 将LittleFS Porting到STM32 G431RB板子上

将LittleFS Porting到STM32 G431RB板子上

作者:互联网

想在STM32 G431RB板子上实现一个小的Log系统。G431RB的板子只有128KB的Flash. 加上代码段,留给Log系统的空间就不多了。

如果使用FATFS系统,系统开销有些大,很难搞一个20KB的文件系统。选择了开销比较小的LittleFS 文件系统。

G431RB板子的Page size = 2KB,也就意味着文件系统中的Block Size需要设置为2KB, Flash的写(Prog)最小是64bit。

我从100KB的地方开始创建LittleFS文件系统,Block Count = 14 (28KB)。

代码实现参考Github (https://github.com/magicduan/STM_LittleFS_Log)

实现起来还是比较简单的,配置好LittleFS configuration struct,实现好四个Interface Function(read, prog, erase, sync)就可以了。

// configuration of the filesystem is provided by this struct
const struct lfs_config cfg = {
    // block device operations
    .read  = stm32_interl_flash_block_read,
    .prog  = stm32_interl_flash_block_prog,
    .erase = stm32_interl_flash_block_erase,
    .sync  = stm32_interl_flash_block_sync,

    // block device configuration
    .read_size = STM32_G431RB_READ_SIZE, //16
    .prog_size = STM32_G431RB_PROG_SIZE, //64
    .block_size = STM32_G431RB_BLOCK_SIZE,  //2K = 2048
    .block_count = STM32_G431RB_BLOCK_COUNT, //14
    .cache_size = 128,
    .lookahead_size = 8,
    .block_cycles = 500,
};

四个Interface Function:

int stm32_interl_flash_block_read(const struct lfs_config *c, lfs_block_t block,
            lfs_off_t off, void *buffer, lfs_size_t size)
{
    uint32_t src_addr = FS_BASE_ADDR + c->block_size*block +off;
    uint32_t read_size = 0;
    uint16_t* pDest = (uint16_t*)buffer;
    uint16_t* pSrc = (uint16_t*)src_addr;
    while(read_size < size){
        *pDest = *(pSrc);
        pDest++;
        pSrc++;
        read_size += 2;
    }

    return 0;
}            

int stm32_interl_flash_block_prog(const struct lfs_config *c, lfs_block_t block,
            lfs_off_t off, const void *buffer, lfs_size_t size)
{
    HAL_FLASH_Unlock();
    uint32_t  dest_addr = FS_BASE_ADDR + c->block_size*block +off;
    uint64_t *pSrc = (uint64_t*)buffer;
    uint32_t  write_size = 0;
    while(write_size < size){
        if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD,dest_addr,*(pSrc)) != HAL_OK){
            HAL_FLASH_Lock();
            return HAL_FLASH_GetError();
        }
        pSrc++;
        dest_addr += 8;
        write_size += 8;
    }
    HAL_FLASH_Lock();
    return 0;
}

int stm32_interl_flash_block_erase(const struct lfs_config *c, lfs_block_t block)
{
    static FLASH_EraseInitTypeDef EraseInitStruct;
    uint32_t PageError;
    HAL_FLASH_Unlock();

    EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
    EraseInitStruct.Banks = FLASH_BANK_1;
    EraseInitStruct.Page = FS_BASE_PAGE_START + block;
    EraseInitStruct.NbPages = 1;
    
    if (HAL_FLASHEx_Erase(&EraseInitStruct,&PageError)!= HAL_OK){
        return HAL_FLASH_GetError();
    }

    HAL_FLASH_Lock();
    return 0;
}

int stm32_interl_flash_block_sync(const struct lfs_config *c)
{
    return 0;
}

 

标签:HAL,LittleFS,read,FLASH,STM32,lfs,Porting,block,size
来源: https://www.cnblogs.com/magicduan/p/15731076.html