I am trying to emulate EEPROM via flash on my STM32F1, as described here (for STM32F4) for example, but I am struggling to change the code for STM32F1RB (medium density) specifications. I am using SW4STM32, CubeMX and the HAL API.
__attribute__((__section__(".user_data"))) const char userConfig[64];
//data type have to be consistent with the TYPEPROGRAM, i.e:
//TYPEPROGRAM_BYTE uint8_t data
//TYPEPROGRAM_HALFWORD uint16_t data
//TYPEPROGRAM_WORD uint32_t data
//TYPEPROGRAM_DOUBLEWORD uint64_t data
void Write_Flash(uint32_t data[],uint8_t flashTypeProgram)
{
uint8_t addressGap;
HAL_FLASH_Unlock();
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGSERR );
FLASH_Erase_Sector(FLASH_SECTOR_6, VOLTAGE_RANGE_3);
for (i=0;i<64/pow(2, flashTypeProgram);i++)
{
addressGap=pow(2, flashTypeProgram)*i;
HAL_FLASH_Program(flashTypeProgram, &userConfig[0]+addressGap, data[i]);
}
HAL_FLASH_Lock();
//TYPEPROGRAM_BYTE Program byte (8-bit) at a specified address $0
//TYPEPROGRAM_HALFWORD Program a half-word (16-bit) at a specified address $1
//TYPEPROGRAM_WORD Program a word (32-bit) at a specified address $2
//TYPEPROGRAM_DOUBLEWORD Program a double word (64-bit) at a specified address $3
}
[...]
flashTypeProgram=TYPEPROGRAM_WORD;
dataSize=(sizeof dataBuffer) / (sizeof *dataBuffer);
for (i=0;i<dataSize;i++) {
dataBuffer[i]=0x1010101; //0x1010101 puts 1 in each byte of userConfig
}
Write_Flash(dataBuffer,flashTypeProgram);
I looked into the way of writing data into flash memory with the HAL API, but as I don't want to "ruin" my board, messing with wrong memory part, I want to make sure I understand everything before trying on my own.
First of all, let's say I want to have 5 kbytes of data to be stored. How should I allow the Data zone ? starting from the end of the main memory zone ? For example I could assign pages 127 to 122 for my Data ? like this ?
MEMORY
{
DATA (RWX) : ORIGIN = 0x08048800, LENGTH = 5k /* 0x08048000 is the beginning of page 107 */
...
}
that would be good to allow 5 pages for flash storage ?
Next, I don't exactly understand this line :
__attribute__((__section__(".user_data"))) const char userConfig[64];
I get that it's for the user to be able to read the Flash stored data, but why is it assigned this way ? I mean, 64*sizeof(char) = 64 bytes, right ? How is he assigning only 64 bytes for reading purposes while he allowed 128 kB of storage ?
In advance, thanks !