AnsweredAssumed Answered

problem using (hal_stm32f4xx library) to restore value written to user configured flash after power reset

Question asked by essam.mahmoud on Mar 19, 2015
can any one help me with this problem? I try to  save value in the user configured section of flash memory and restore it after power reset through hal_stm32f4xx library. I started debugging by reading the value from the programmed address without power reset and it was right. But then I tried to check the value after power reset so I ran this code . If the value is read correct (the led connected to GPIO_10) should turn on, but this didn't happen .


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

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

void testwriteFlash(void)
{
    HAL_StatusTypeDef status;
    status =    HAL_FLASH_Unlock();   // returns  HAL_OK
    FLASH->CR |= 0x1000000;
    FLASH_Erase_Sector(FLASH_SECTOR_2, VOLTAGE_RANGE_1);
    __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGSERR );
    status = HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, (uint32_t) userConfig, (uint64_t)testwritevalue);
    // returns  HAL_OK
    while((*(uint8_t* )(userConfig))==0);
    status = HAL_FLASH_Lock();        // returns  HAL_OK

}


int main(void)
{
    HAL_Init();
    MX_GPIO_Init();
    testreadFlash();
    if(testreadvalue==6)
      { HAL_GPIO_WritePin(GPIOB,GPIO_PIN_10,0);}            //power cycle the device  //TESTING
    else
      { testwriteFlash();}
       SystemClock_Config();
  
     while (1)
        {}
 }

and this is the part of the linker for the user configured flash
MEMORY   
{
// RAM  default
  FLASH1 (rx) : ORIGIN = 0x08000000, LENGTH = 32k
  DATA (xrw) : ORIGIN = 0x08008000, LENGTH = 32k        
  FLASH (rx) : ORIGIN = 0x08010000, LENGTH = 512K-64k 
//others default
}

SECTIONS
{
    .user_data :
    {
      . = ALIGN(4);
         *(.user_data)
      . = ALIGN(4);
    } > DATA
}


Outcomes