2020-07-30 11:44 PM
I have migrated my keil code generated from HAL cube to stm32cubeide because of size constraint. The code is working fine when used in debugging. But when run code stand alone using st link utility, it is working fine for the first time but when perform reset its first address erased and controller gone to undefined state. Can anyone have any idea regarding this problem and how can i fix it.
2020-07-31 08:28 AM
Do you use flash write somewhere in your code (for example emulated EEPROM)?
2020-07-31 08:34 PM
Hi Prain,
Yes i have used the flash write in my code. After some research in code i found that problem is occurring due to my erase function. But can get out of it. Functions used are written below:
uint32_t EraseFlash(uint32_t sector, uint32_t NbrOfPages)
{
volatile uint32_t pages=NbrOfPages;
ERASE.TypeErase=FLASH_TYPEERASE_PAGES;
ERASE.NbPages=pages;
ERASE.PageAddress=sector;
HAL_FLASH_Unlock();
CState=HAL_FLASHEx_Erase(&ERASE,(uint32_t*)PGERROR);
if(CState==HAL_OK)
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR);
else
return HAL_FLASH_GetError();
HAL_FLASH_Lock();
return CState;
}
/********************************************** **
Function to SAVE in the internal flash memory
****************************************************/
char SaveInFlash(uint32_t FLASH_STORAGE, uint32_t *Packetdata)
{
volatile uint32_t dataLen=(strlen((char*)Packetdata)/4)+(int)((strlen((char*)Packetdata)%4)!=0);
volatile uint32_t write_cnt=0, index=0;
volatile uint32_t end_sector_addr=FLASH_STORAGE+dataLen*4+1;
volatile uint32_t number_of_sector=end_sector_addr-FLASH_STORAGE+1;
FLASH_ERASE=EraseFlash(FLASH_STORAGE, number_of_sector);
HAL_FLASH_Unlock();
if(FLASH_ERASE==HAL_OK)
{
while(index < dataLen)
{
current=HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, FLASH_STORAGE+write_cnt, Packetdata[index]);
if(current == HAL_OK)
{
write_cnt += 4;
index++;
}
}
}
LastFlashdataLocation=write_cnt;
HAL_FLASH_Lock();
return 1;
}
2020-08-01 12:03 AM
I guess you are writing to the zero address or 0x08000000 mistakenly.
Put a break point at every line of the code that attempts writing to flash; example:
current=HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, FLASH_STORAGE+write_cnt, Packetdata[index]);
when break point hits, check the address you are writing to; in this example second argument is address so check the value of FLASH_STORAGE+write_cnt in debugger. It must not equal zero or 0x08000000. Also complete this test for every where you are writing to flash.
If you are using flash for data storage, try to use proven ST EEPROM emulation library and assign last two flash pages for this purpose.