cancel
Showing results for 
Search instead for 
Did you mean: 

STM32H753 littleFS Integration

FarukSozuer
Associate III

Helle everyone,

I want to use the littleFS file system library that I used in my previous projects in STM32H753 Internal Flash Memory. But the fact that the block size in STM32H7 is 128K made me think a little. I normally use littleFS on 1K blocks and get great results. Has anyone used littleFS on STM32H7 platforms before? 

Or if a problem occurs while integrating STM32H7 and littleFS;

 

 

 

lfsConfig.block_size = 1K
lfsConfig.block_count = 128

 

 

 

do you think I can edit it like this? 

 

Sincelery

2 REPLIES 2
hans86
Associate III

Yes, I think that will work. The block (or sector) size is the minimum erase size. For most flash devices this is 4096 bytes.

I suspect you already tried this and it is not working? in this case add some printf to your .read/.write/.erase routines and see how they are being called.

Here is a code snippet what I use:

#define SPIDEBUG 1

#ifdef SPIDEBUG
   #define dprintf(...) printf(__VA_ARGS__) // Debug messages on UART0
#else
   #define dprintf(...)
#endif


const struct lfs_config stmconfig = {

.read = stmlfs_hal_read,
.prog = stmlfs_hal_prog,
.erase = stmlfs_hal_erase,
.sync = stmlfs_hal_sync,

// block device configuration
.read_size = FS_PAGE_SIZE,
.prog_size = FS_PAGE_SIZE, // 256
.block_size = FS_SECTOR_SIZE, // 4096
.block_count = FS_SIZE/FS_SECTOR_SIZE,
.cache_size = FS_SECTOR_SIZE/4,
.lookahead_size = 32, // must be multiple of 8
.block_cycles = 100, // 100(better wear levelling)-1000(better performance)
};


int stmlfs_hal_read(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, void* buffer, lfs_size_t size)
{

assert(block < c->block_count);
assert(off + size <= c->block_size);

dprintf("stmlfs_hal_read(block=%ld off=%ld size=%ld\n",block,off,size);

W25Q_Read(block,off,size,buffer);

#ifdef SPIDEBUG
uint8_t buf[4096]={0};
W25Q_Read(block,off,size,buf);
for (int i=0;i<size;i++) {
if (((uint8_t *)buffer)[i]!=buf[i]) {
printf("*** error: stmlfs_hal_read diff buff[%d] %02x %02x\n",i,((uint8_t *)buffer)[i],buf[i]);
}
}
#endif

return LFS_ERR_OK;
}


Good Luck,
Hans.

Hello @hans86 

Thanks for reply. But I am using Internal Flash Memory.  I'm thinking of leaving Internal Flash's last 2 sector (128*2 256K) for LittleFS. My read/program/lfsinit function in below.

lfs_mout error status is LFS_ERR_CORRUPT.

lfs_format error status is LFS_ERR_NOSPC.

 

 

 

 

 

int flash_read(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, void *buffer,lfs_size_t size)
{
   uint32_t start_address;
   uint32_t *buf;
   
   buf = (uint32_t*)buffer;
   start_address = SECTOR13_START_ADDR + (block*c->block_size)+off;
   for(uint32_t i=0 i<(size/4);i++)
   {
     *buf = *(__IO uint32_t *)start_address;
      start_address +=4;
      buf++;
   }
   return LFS_ERR_OK;
}

int flash_program(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, void *buffer,lfs_size_t size)
{
    uint32_t start_address;
    uint32_t *buf;
    buf = (uint32_t *)(buffer);

    start_address = SECTOR13_START_ADDR + (block*c->block_size)+off;

    for(uint32_t i=0;i<size/8;i++)
    {
      HAL_FLASH_Unlock();
      HAL_FLASH_Program(FLASH_TYPEPROGRAM_FLASHWORD,start_address,(uint32_t)&buf[0]);
      HAL_FLASH_Lock();
      start_address += 32; // 1 cycle 256 bit write. The address address is  32 increment
      buf +=8;
    }
    return LFS_ERR_OK;
}

void init_lfs_config(void)
{
  int32_t error;
  lfs_cfg.read_size = 256;
  lfs_cfg.page_size = 256;
  lfs_cfg.block_size = 0x00020000; // 128K sector size
  lfs_cfg.block_count = 2;        // 2 sector
  lfs_cfg.cache_size = 1024;
  lfs_cfg.lookhead_size = 32;
  lfs_cfg.block_cycle = 100; 

  error = lfs_mount(&lfs,&lfs_cfg);
  if(error != LFS_ERR_OK)
  { 
    error = lfs_format(&lfs,&lfs_cfg);
    error = lfs_mount(&lfs,&lfs_cfg);
  }
}

 

 

 

 

 

 

Sincelery