Store values in STM32G0 internal flash
(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); */
}
