Skip to main content
Jake2451
Associate
December 18, 2018
Solved

Problems when reading from FLASH; STM32F4

  • December 18, 2018
  • 3 replies
  • 1281 views

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:0690X000006CrQeQAK.png

Hera are some screens from debug perspective:

This is after reading from keyboard and writing to FLASH:

0690X000006CrQyQAK.png

Data in userConfig[] are correct but it writes 0 to "inter" variable

0690X000006CrRDQA0.png

And here are FLASH registers:

The core is clocked by 100MHz so the latency is set correctly.

There is no read protection.

0690X000006CrT4QAK.png

I have no idea so I'm asking for help.

    This topic has been closed for replies.
    Best answer by Tesla DeLorean

    Compiler looks to be folding.

    Check the address reported in .MAP file

    Make this volatile not const

    3 replies

    Tesla DeLorean
    Tesla DeLoreanBest answer
    Guru
    December 18, 2018

    Compiler looks to be folding.

    Check the address reported in .MAP file

    Make this volatile not const

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    Jake2451
    Jake2451Author
    Associate
    December 18, 2018

    Thanks a lot! I've made this volatile and it works.

    The mistake comes from here: https://stackoverflow.com/a/28505272

    Tesla DeLorean
    Guru
    December 18, 2018

    Thankfully my understanding of this predates the internet..

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..