Skip to main content
Explorer
September 20, 2024
Solved

Store values in STM32G0 internal flash

  • September 20, 2024
  • 2 replies
  • 1085 views

(was initially a reply to https://community.st.com/t5/stm32-mcus-products/store-values-in-internal-flash/m-p/666974

 

I am store variable in flash adders and after reset read but variable values is not store in flash adders.& using stm32g030f6p6 microcontroller and here is my code:-

void flash unlock(void)

{

 FLASH->KEYR = 0x45670123;

 FLASH->KEYR = 0xCDEF89AB;

}

 

 

void Erase_Flash(void)

{

 //HRTIM1->sMasterRegs.MICR |= 0x51;  /*clear pending bit*/

 // HRTIM1->sMasterRegs.MDIER = HRTIM_MASTER_IT_NONE; /*DISABLE HRTIMER INTERRUPT*/

  

  flash_unlock();

   Delay(200); 

 /*if((FLASH->CR &0x80)== 0x80)

 {

  flash_unlock();

  delay2(200);

 }

  

  

 else*/

 {

  SET_BIT(FLASH->CR, FLASH_CR_PER);  

    Delay(200); 

  FLASH->WRP1AR = 0x08000500;

    Delay(200); 

  FLASH->CR |= 0x40;

  FLASH->WRP1AR = 0x08000800;

    Delay(200); 

  FLASH->CR |= 0x40;

  /* FLASH->WRP1AR = 0x08007C10;

    Delay(200); 

  FLASH->CR |= 0x40;

  FLASH->WRP1AR = 0x08007C14;

    Delay(200); 

  FLASH->CR |= 0x40;

    Delay(200); */

  CLEAR_BIT(FLASH->CR, FLASH_CR_PER);   

 } 

 // HRTIM1->sMasterRegs.MDIER = HRTIM_MDIER_MUPDIE;  /*ENABLE HRTIMER INTERRUPT*/ 

 

void flash_write(void)

{

 // HRTIM1->sMasterRegs.MICR |= 0x51;  /*clear pending bit*/

 // HRTIM1->sMasterRegs.MDIER = HRTIM_MASTER_IT_NONE; /*DISABLE HRTIMER INTERRUPT*/

 Erase_Flash();   

 //HRTIM1->sMasterRegs.MICR |= 0x51;  /*clear pending bit*/

 //HRTIM1->sMasterRegs.MDIER = HRTIM_MASTER_IT_NONE; /*DISABLE HRTIMER INTERRUPT*/

 /*if((FLASH->CR &0x80)== 0x80)

 {

  flash_unlock();

  delay2(200);

 }

 else*/

 {

  //flash_unlock();

    Delay(10000); 

  SET_BIT(FLASH->CR, FLASH_CR_PG);  

    Delay(10000); 

  *(__IO uint16_t*)0x08000500 = 596; ///PV_cal

    Delay(10000); 

  *(__IO uint16_t*)0x08000800 = 456; ///BV_cal

    Delay(10000); 

 /* *(__IO uint16_t*)0x08007C10 = VAR3; //BI_cal

    Delay(10000); 

  *(__IO uint16_t*)0x08007C14 = VAR4; //Induct_cal

    Delay(10000); 

  *(__IO uint16_t*)0x08007C18 = VAR5; //Induct_cal

    Delay(10000); 

  // *(__IO uint16_t*)0x08007C1C = 0xAD; //first time

   Delay(10000); */

  CLEAR_BIT(FLASH->CR, FLASH_CR_PG);  

 }  

 // HRTIM1->sMasterRegs.MDIER = HRTIM_MDIER_MUPDIE;  /*ENABLE HRTIMER INTERRUPT*/  

}

 

 

void flash(void)

 {  

 VAR1  = *(__IO uint16_t*)0x08000500;

  Delay(20); 

 VAR2  = *(__IO uint16_t*)0x08000800;

  Delay(20); 

 /* VAR3  = *(__IO uint16_t*)0x08007C10;

  Delay(20); 

 VAR4 = *(__IO uint16_t*)0x08007C14;

  Delay(20); 

  VAR5 = *(__IO uint16_t*)0x08007C18;

  Delay(20); 

  //First = *(__IO uint16_t*)0x08007C1C;

   Delay(20); */

}

    This topic has been closed for replies.
    Best answer by Tesla DeLorean

    Use </> code formatting tool to post source code.

    Add comments

    Does library code work? Low addresses likely to contain the executing code, can't just erase 

    2 replies

    Graduate II
    September 20, 2024

    Use </> code formatting tool to post source code.

    Add comments

    Does library code work? Low addresses likely to contain the executing code, can't just erase 

    Explorer
    September 21, 2024
    void flash unlock(void)
    {
     FLASH->KEYR = 0x45670123;
     FLASH->KEYR = 0xCDEF89AB;
    }
    void Erase_Flash(void)
    { flash_unlock();
     Delay(200); 
     /*if((FLASH->CR &0x80)== 0x80)
     {
     flash_unlock();
     delay2(200);
     }
     else*/
     {
     SET_BIT(FLASH->CR, FLASH_CR_PER); 
     Delay(200); 
     FLASH->WRP1AR = 0x08000500;
     Delay(200); 
     FLASH->CR |= 0x40;
     FLASH->WRP1AR = 0x08000800;
     Delay(200); 
     FLASH->CR |= 0x40;
     /* FLASH->WRP1AR = 0x08007C10;
     Delay(200); 
     FLASH->CR |= 0x40;
     FLASH->WRP1AR = 0x08007C14;
     Delay(200); 
     FLASH->CR |= 0x40;
     Delay(200); */
     CLEAR_BIT(FLASH->CR, FLASH_CR_PER); 
     } 
     } 
     
    void flash_write(void)
    {
     Erase_Flash(); 
     /*if((FLASH->CR &0x80)== 0x80)
     {
     flash_unlock();
     delay2(200);
     }
     else*/
     {
     //flash_unlock();
     Delay(10000); 
     SET_BIT(FLASH->CR, FLASH_CR_PG); 
     Delay(10000); 
     *(__IO uint16_t*)0x08000500 = 596; ///PV_cal
     Delay(10000); 
     *(__IO uint16_t*)0x08000800 = 456; ///BV_cal
     Delay(10000); 
     /* *(__IO uint16_t*)0x08007C10 = VAR3; //BI_cal
     Delay(10000); 
     *(__IO uint16_t*)0x08007C14 = VAR4; //Induct_cal
     Delay(10000); 
     *(__IO uint16_t*)0x08007C18 = VAR5; //Induct_cal
     Delay(10000); 
     // *(__IO uint16_t*)0x08007C1C = 0xAD; //first time
     Delay(10000); */
     CLEAR_BIT(FLASH->CR, FLASH_CR_PG); 
     } 
     }
     void flash(void)
     { 
     VAR1 = *(__IO uint16_t*)0x08000500;
     Delay(20); 
     VAR2 = *(__IO uint16_t*)0x08000800;
     Delay(20); 
     /* VAR3 = *(__IO uint16_t*)0x08007C10;
     Delay(20); 
     VAR4 = *(__IO uint16_t*)0x08007C14;
     Delay(20); 
     VAR5 = *(__IO uint16_t*)0x08007C18;
     Delay(20); 
     //First = *(__IO uint16_t*)0x08007C1C;
     Delay(20); */
    }
    Super User
    September 20, 2024

    It looks like you're trying to erase individual sections of flash, but your code is misguided and won't work. Perhaps go from a HAL example.

    Look at how flash operates--particularly that you can only erase an entire sector at a time.

    Also note that erasing flash that is currently in use by the program will cause your program to stop working.

    Explorer
    September 21, 2024

    yes when i am erase flasg entire program is stop working , may be my flash adders is not correct or correct ,please refer any addears to replace it.