This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

fwrite of "cnt < 4-byte" data results in ARM_Flash_ProgramData (cnt = 4-byte)

Problem Code:

/* main.c */
uint8_t  testBuf[] = {1, 2, 3, 4, 5},

FILE * fwr = fopen("tst1.txt", "w");
fwrite(testBuf, sizeof(uint8_t), sizeof(testBuf), fwr);
fflush(fwr);
fclose(fwr);

/* flash_cmsis_driver.c */
int32_t ARM_Flash_ProgramData(uint32_t addr, const void *data, uint32_t cnt)
{
    /* Problem: cnt is 8 while it should be 5 */
    /* cnt is next nearest multiple of 4 */
}


Line 5, in this case, I am writing (sizeof(testBuf)) 1-byte sized items in "tst.txt" file.

The problem I get is when the code reaches Line 10, where fwrite tries to write the testBuf data in the file. I receive the value of cnt as 8 which is not correct.
This problem is uniform: If testBuf size is 1-byte the cnt received is 4, if testBuf size is 10-byte the cnt received it 12. Hence the cnt is next nearest multple of 4.

These are some of the related configurations to support 1-byte write
/* Driver Capabilities */
static const ARM_FLASH_CAPABILITIES DriverCapabilities = {
    1,  /* Supports event ready status */
    0,  /* data_width = 0:8-bit, 1:16-bit, 2:32-bit */
    0,  /* Don't support chip erase */
    0U  /* Reserved */
};

/* Flash Information */
static ARM_FLASH_INFO FlashInfo = 
{
    FLASH_SECTOR_INFO,
    FLASH_SECTOR_COUNT,
    FLASH_SECTOR_SIZE,
    8, /* FLASH_PAGE_SIZE */
    1, /*FLASH_PROGRAM_UNIT */
    0xFF, /* FLASH_ERASED_VALUE */
    { 0U, 0U, 0U }      /* Reserved */
};


I am using Embedded File System (EFS) for my NOR Flash.


I used the Event Recorder to look at the Debug Events. Interesting things is that FsEFS: FileWrite shows the len as 5.

Additionally, another observation was this:
From the previous mentioned testBuf = {1, 2, 3, 4, 5}. If I issue fwrite only for 2-bytes i.e 1,2. The data written in Flash is 1,2,0,0.
The File system copies the data that needs to be written in the stack and analysing the data copied it was only 1,2 contrary to 1,2,3,4 for this scenario.
This means it does understand that 2-bytes needs to be written. But because the Flash_ProgramData cnt as 4 Flash drivers write the data 1,2 and next available data 0,0.

What could be the reason for getting cnt value as multiple of 4?
If I have forgotten to present some important information, do let me know!

Thank you!!