cancel
Showing results for 
Search instead for 
Did you mean: 

problem writing to flash for the second time

alireza nazem
Associate II
Posted on May 12, 2018 at 13:57

Hello everyone,

I have used the following codes to (erase and then ) write to the flash in STM32F103RCT6, the problem is every time i call this function it writes every page only the first time. meaning when called for the second time, writing to the same page makes no effect (nothing is erased even). but when i hard reset (power Off and On) i can erase and write to that page again but only once again. please help me to understand what have I overlooked?!

this is my code:

  HAL_FLASH_Unlock(); 

while((FLASH->SR&FLASH_SR_BSY));

FLASH->CR |= FLASH_CR_PER; //Page Erase Set

FLASH->AR = Page_start_address; //Page Address

FLASH->CR |= FLASH_CR_STRT; //Start Page Erase

while((FLASH->SR&FLASH_SR_BSY));

FLASH->CR &= ~FLASH_CR_PER; //Page Erase Clear

FLASH->CR |= FLASH_CR_PG;

for(i=0; i<2048; i+=2)

{

while((FLASH->SR&FLASH_SR_BSY));

*(__IO uint16_t*)(Page_start_address + i) = DATA[i/2];          //FLASH_Program HalfWord

}

HAL_FLASH_Lock();  

Thanks 

0 REPLIES 0