cancel
Showing results for 
Search instead for 
Did you mean: 

hello, Im using stm32f103 microcontroller. Im trying flash programming. while unlocking flash program erase controller by writing key values to register, controller not comming out of that loop. Is their any changes required in code????

manjunath r
Associate II
Posted on March 24, 2018 at 05:43

#define FLASH_KEY1               ((u32)0x45670123)

#define FLASH_KEY2               ((u32)0xCDEF89AB)

void main (void)

{

   //FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);

  

   /*unlock the flash to enable the flash control register*/

   //while((FLASH->SR & FLASH_FLAG_BSY) != 0)

   {

   

   }   

   FLASH_Unlock();

  

   /*Clear pending flag*/

   //FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR);

  

   /*Define number of pages to be erassed*/

   Nbrofpage = (Flash_user_end_addr - Flash_user_strt_addr)/ Flash_page_size;

  

   for(Erasecounter = 0; (Erasecounter < Nbrofpage ); Erasecounter++)

   {

    if(FLASH_ErasePage(Flash_user_strt_addr + (Flash_page_size * Erasecounter)) != FLASH_COMPLETE)

    {

     //while(1){}

    }

   }

 

 address = Flash_user_strt_addr;

 

 while(address < Flash_user_end_addr)

 {

  if(FLASH_ProgramWord(address,data_32) == FLASH_COMPLETE)

  {

   address = address+4;

  } 

 }

 address1 = Flash_user_strt_addr;

 while(address < Flash_user_end_addr)

 {

    data = *address1;

 } 

     

}

void FLASH_Unlock(void)

{

  /* Authorize the FPEC Access */

  FLASH->KEYR = FLASH_KEY1;

  FLASH->KEYR = FLASH_KEY2

}
2 REPLIES 2
Posted on March 24, 2018 at 13:57

Please don't post entire question into summary.

Suggest you use subroutines for your code and don't dump it entirely in main()

Instrument the code so it doesn't need a debugger and so you can observe behaviour externally. Use a USART or other available interface.

It is important to clear errors/status from the controller.

The main() function should never exit, use a while(1) loop to make it stop at the end. Nothing good will happen when it exits, only bad things.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on March 25, 2018 at 03:01

hi

Turvey.Clive.002

But the problem is after writingkey2 value to the register, the controller is not coming out of that FLASH_Unlock function and it goes to hard fault exception. Is their any problem with starting address or I need to configure something before main ????