2018-12-18 05:38 AM
I successfully write to FLASH but when I try to read I get 0.
First I read some data from keyboard to initialize "inter" variable.
Then I store it in FLASH, but I can't read it.
Here is what I've added to linker .ld file:
/* Specify the memory areas */
MEMORY
{
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 128K
FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 384K
DATA (rw) : ORIGIN = 0x8060000, LENGTH = 128K
}
.user_data :
{
. = ALIGN(4);
KEEP(*(.user_data))
. = ALIGN(4);
} > DATA
.preinit_array :
{
Obviously I've inserted this in main.c:
__attribute__((__section__(".user_data"))) const uint32_t userConfig[32];
This is how I'm dealing with writing to FLASH:
/* Writing to memory */
HAL_FLASH_Unlock();
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGSERR );
FLASH_EraseInitTypeDef EraseInitStruct;
EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;
EraseInitStruct.Sector = FLASH_SECTOR_7; //Specify sector number
EraseInitStruct.NbSectors = 1;
uint32_t SectorError = 0;
if(HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK) {
//Erase error!
}
HAL_FLASH_Program(TYPEPROGRAM_WORD,userConfig,inter->moder);
HAL_FLASH_Program(TYPEPROGRAM_WORD,userConfig+1,inter->prc);
HAL_FLASH_Program(TYPEPROGRAM_WORD,userConfig+2,inter->cpr);
HAL_FLASH_Program(TYPEPROGRAM_WORD,userConfig+3,inter->pos_mul_factor);
HAL_FLASH_Program(TYPEPROGRAM_WORD,userConfig+4,inter->speed_mul_factor);
HAL_FLASH_Program(TYPEPROGRAM_WORD,userConfig+5,inter->PWM_prc);
HAL_FLASH_Lock();
and that is how I try to read:
/*function that reads stored data*/
void ReadConfiguration(Interface * inter)
{
inter->moder=userConfig[0];
inter->prc=userConfig[1];
inter->cpr=userConfig[2];
inter->pos_mul_factor= userConfig[3];
inter->speed_mul_factor=userConfig[4];
inter->PWM_prc=userConfig[5];
}
But it returns 0 on each field of "inter" variable.
Data are stored in FLASH and I can check it in STM32 ST-LINK Utility:
Hera are some screens from debug perspective:
This is after reading from keyboard and writing to FLASH:
Data in userConfig[] are correct but it writes 0 to "inter" variable
And here are FLASH registers:
The core is clocked by 100MHz so the latency is set correctly.
There is no read protection.
I have no idea so I'm asking for help.
Solved! Go to Solution.
2018-12-18 06:05 AM
Compiler looks to be folding.
Check the address reported in .MAP file
Make this volatile not const
2018-12-18 06:05 AM
Compiler looks to be folding.
Check the address reported in .MAP file
Make this volatile not const
2018-12-18 07:50 AM
Thanks a lot! I've made this volatile and it works.
The mistake comes from here: https://stackoverflow.com/a/28505272
2018-12-18 08:25 AM
Thankfully my understanding of this predates the internet..