AnsweredAssumed Answered

problem in using (hal_stm32f4xx library) to read value written to user configured sector of flash

Question asked by essam.mahmoud on Mar 22, 2015
Latest reply on Mar 23, 2015 by essam.mahmoud
can any one help me with this problem? I try to make sure that I stored a value correctly in the user _configured section of flash .So I  read value from a specific address of flash memory immediately after  programming  it through hal_stm32f4xx library. For debugging I make condition on the read value if it equals the value written to the address a led turns on. I have no problem while debugging and running without optimizations. But when I chose to run (the most optimized version of the code without any debugging) from the project settings , the led didn't turn on.

and this is the source code


__attribute__((__section__(".user_data"))) const uint32_t userConfig[64];
 uint32_t testreadvalue=0;
uint32_t testwritevalue=6;
 HAL_StatusTypeDef  status = HAL_ERROR;

void testreadFlash(void)
{
    testreadvalue =*(uint8_t *)userConfig;
}

void testwriteFlash(void)
{
    HAL_StatusTypeDef status;
    status =    HAL_FLASH_Unlock();   // returns  HAL_OK
    FLASH_Erase_Sector(FLASH_SECTOR_2, VOLTAGE_RANGE_1);
    status = HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, (uint32_t) userConfig, (uint64_t)testwritevalue);
    // returns  HAL_OK
    status = HAL_FLASH_Lock();        // returns  HAL_OK

}


int main(void)
{
    HAL_Init();
    SystemClock_Config();
    MX_GPIO_Init();
    testwriteFlash();
    testreadFlash();
    if(testreadvalue==6)
      { HAL_GPIO_WritePin(GPIOB,GPIO_PIN_10,0);}            // turn on the yellow led
    else
      { HAL_GPIO_WritePin(GPIOB,GPIO_PIN_10,1);}            // turn off the yellow led


     while (1)
        {}
 }


Thanks in advance


Outcomes