cancel
Showing results for 
Search instead for 
Did you mean: 

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

mahmuudesam
Associate II
Posted on March 22, 2015 at 17:57

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

5 REPLIES 5
Posted on March 22, 2015 at 18:12

[DEAD LINK /public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/problem%20with%20restoring%20value%20written%20to%20user%20configured%20flash%20after%20power%20reset&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B&TopicsView=https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/AllItems.aspx?Paged%3DTRUE%26p_StickyPost%3D%26p_DiscussionLastUpdated%3D20150319%252018%253a05%253a33%26p_ID%3D51603%26View%3D%257bF47A9ED8%252dE726%252d42BE%252dACED%252d732F13B66581%257d%26FolderCTID%3D0x012001%26PageFirstRow%3D41&currentviews=9]Earlier duplicate thread

Not sure there's much HAL support here in the pro user space. Might I suggest you check the value first and not write it every pass, review the error status at each step, printing the result to a serial console, and not trying to write if the memory isn't the 0xFF state, or the erase fails..

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
childresss
Associate II
Posted on March 23, 2015 at 06:57

I have more elaborate code to read/write/rewrite data into flash using the HAL calls. It works well.

The linker file I use allocates the data-in-flash sectors as such so code cannot go in those sectors.

A quick look at your code - I see inconsistencies - casting an address to a uint8_t * but then dereferencing that to a variable that is not a uint8_t.

mahmuudesam
Associate II
Posted on March 23, 2015 at 11:04

I detected that the sector isn't erased successfully while running the optimized version of the code by running a buzzer if the memory isn't 0xFF

what to do if the erase fails. But while running the non optimized version of the code the sector is erased succssessfully.

Posted on March 23, 2015 at 12:23

What toolchain are you using? Are you sure you can't provide some serial connectivity so you can get more informative feedback than a buzzer?

I think you need to make sure you clear any error/status states from the flash controller after you unlock it for the first time. In SPL terms this would look like

FLASH_Unlock();
FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
mahmuudesam
Associate II
Posted on March 23, 2015 at 12:38

I'm using the toolchain ARM_GNU_GCC.

In the start and after each operation,

I clear the flags, execute

and check

for errors.

  This is the up_to_date version of the test_write_flash() function   void testwriteFlash(void)  {  status1 = HAL_FLASH_Unlock();  FLASH->CR |= 0x3000000; // Enable End of operation and errors interrupts  FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);  FLASH_Erase_Sector(FLASH_SECTOR_2, VOLTAGE_RANGE_1);  if(__HAL_FLASH_GET_FLAG((FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | \  FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR | FLASH_FLAG_RDERR)) != RESET)   {  Run_Buzzer();// No sound emitted here if put alone without next conditions  }  if(testreadvalue!=255)  

{  Run_Buzzer(); // sound emitted here if put alone without other conditions  }  else  {  __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR |FLASH_FLAG_PGSERR );  status2 = HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, (uint32_t) userConfig, (uint64_t)testwritevalue);  if(__HAL_FLASH_GET_FLAG((FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | \  FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR | FLASH_FLAG_RDERR)) != RESET)   {  Run_Buzzer();// No sound emitted here if put alone without the previous conditions  }   __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR |FLASH_FLAG_PGSERR );  }  status3 = HAL_FLASH_Lock();  } }