Last days I have been facing an issue with the LittleFS library. I need to mount my external flash AT45DB041E, which has the following characteristics:
(I can communicate with the flash properly)
FLASH SIZE: 4MBit, Block size: 2048 bytes, Page size: 256 Bytes, Num_Blocks = 256, Num_Pages = 2048, Num_Sectors = 8
After mounting the main purpose of my task is to find a binary file that had been written to this external flash with LittleFS as well, in order to store its contents to a buffer.
For that reason I have implemented some functions based on AT45DB041E_datasheet where I can read-write-erase a page properly(I have test it and it works properly). Also I have a function that erases a block(8 pages, also tested). All I am doing
lfs_t lfs;
const struct lfs_config cfg = {
.read = block_device_read,
.prog = block_device_prog,
.erase = block_device_erase,
.sync = block_device_sync,
// block device configuration
.read_size = 256,
.prog_size = 256,
.block_size = 2048,
.block_count = 256,
.cache_size = 256,
.lookahead_size = 2048,
.block_cycles = 500,
}
Subsequently, I am connecting the implemented functions which are interacting properly with the external flash memory with that way (Those are the functions of littlefs where I implement them with my functions):
(The status register doesn’t report any error when programming operation finishes).
int block_device_prog(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, const void *buffer, lfs_size_t size)
{
uint8_t status_register[2];
/*Calculate the page address based on block and offset*/
uint16_t page_address = (block * c->block_size) + off;
/*Write the page to flash memory*/
at45db_write_page(page_address, (uint8_t *)buffer, size);
/*Read the status register*/
at45db_read_status(status_register);
/*Check the 5bit of second byte*/
uint16_t res = READ_BIT(status_register[1], (1U<<5));
/*Check for fault operation*/
if (res == 1)
{
/*Error detected while programming the specified page*/
printf("An error has been detected while programming the page address: %Xnr", page_address);
return -1;
} else
{
/*No errors detected while programming the flash memory*/
printf("Successful programming the page address: 0x%08Xnr", page_address);
return 0;
}
}
(The erase operation doesn’t report any error when it finishes as well)
int block_device_erase(const struct lfs_config *c, lfs_block_t block)
{
/*Calculate the address*/
uint32_t address = block * c->block_size;
uint16_t res = at45db_block_erase(address);
/*Check for fault conditions*/
if (res == 1) {
/*Success*/
return LFS_ERR_OK;
} else {
printf("Error in erase operation of address: 0x%Xnr", address);
/*False*/
return -1;
}
}
This is the read operation
int block_device_read(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size)
{
uint16_t page_address = (block * c->block_size) +off;
at45db_read_page(page_address, buffer);
return LFS_ERR_OK;
}
Now the problem is when I am using that code:
at45db_init();
at45db_chip_erase();
int res = lfs_mount(&lfs, &cfg);
if (res != LFS_ERR_OK) {
printf("Errornr");
lfs_format(&lfs, &cfg);
lfs_mount(&lfs, &cfg);
}
The first mount return -84, which is an indication of corruption. More specifically it returns this:
trace: lfs_mount(...)
Corrupted dir pair at {0x0, 0x1}
lfs_mount -> -84
Error
After that the format function returns -28 which is (No space left on device indication). I tries to add to the cfg struct some buffers one for reading one for writing and another one where each buffer had the same size 256, but I was getting the same error again). More specifically it returns this :
lfs_format()
Successful programming the page address: 0x00000000
Bad block at 0x0
Superblock 0x0 has become unwritable
lfs_format -> -28
And finally, when it uses the mount function again it also fails with the same error as the first time.
Can anyone help me with that?
My questions are:
- Lfs doc had block_device_read/write/erase is that a problem that I use read/write/erase to pages?
- Is the values at the configurations correct based on the characteristics of the external flash memory?
- Am I missing something before mounting?
I need to tell you that the external flash memory which I am trying to mount is not corrupted since I am able to mount it with the mbed library. But I need to make this program work with CubeIDE.