STM32 has a PVD power-off detection function, and the flash can also be directly accessed; so the idea of using the flash to save parameters when power is off came to mind.
1. Get the flash erase and write functions.
void F_ErasePage(u32 Page_Address)
{
U32 tmp;
while((FLASH->SR&FLASH_FLAG_BSY)==FLASH_FLAG_BSY)
{ //PutCnstStr2Usart1("waiting\n"); }
FLASH->SR |= FLASH_FLAG_PGERR;
FLASH->CR |= CR_PER_Set;
FLASH->AR=Page_Address;
>CR |= CR_STRT_Set;
while((FLASH->SR&FLASH_FLAG_BSY)==FLASH_FLAG_BSY)
{ //PutCnstStr2Usart1("waiting\n"); }
FLASH->CR &= ~CR_PER_Set; //Don’t forget this
}
void FLASH_Program(u32 Address, u16 Data)
{
U32 tmp;
Fstart:
if((FLASH->CR&CR_LOCK_Set)==CR_LOCK_Set) //If the write is locked
{
FLASH->KEYR = FLASH_KEY1;
FLASH->KEYR = FLASH_KEY2;
//PutCnstStr2Usart1("Unclock\n");
}
while((FLASH->SR&FLASH_FLAG_BSY)==FLASH_FLAG_BSY)
{
//PutCnstStr2 Usart1("waiting\n");
}
FLASH->CR |= CR_PG_Set;
*(vu16*)Address = Data;
FLASH->CR &= ~CR_PG_Set;
if((FLASH->SR&FLASH_FLAG_PGERR)==FLASH_FLAG_PGERR)
{
//PutCnstStr2Usart1("No earase\n");
F_ErasePage(Address)
; Fstart;//Goto is used, cough cough^_^
}
}
With these two functions, it is basically OK. Anyway, we need to erase one page at a time, so we can save all the parameters. It is more reliable.
U16 const flashdata[1024]__at(0x08001000);//Because flash can and can only write 16 bits at a time, and accessing odd addresses will cause errors.
And don't forget to #include, absolute positioning depends on it.
Put the parameters to be saved into a structure, load it every time the power is reset, and write it to flash when it needs to be stored.
2. Get power-off detection done
void PVD_Init(void)
{
NVIC_InitTypeDef NVIC_InitStruct;
NVIC_InitStruct.NVIC_IRQChannel = PVD_IRQChannel;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0x00;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0x00;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM2 | RCC_APB1Periph_TIM3 |
RCC_APB1Periph_TIM4 | RCC_APB1Periph_USART2|RCC_APB1Periph_PWR, ENABLE); //_PWR, ENABLE); // This is required
PWR->CR |= 1<<4;//Enable
PWR->CR |= 0xE0;//2. 9V detection
NVIC_Init(&NVIC_InitStruct);//Borrow the library to allow interrupts
}
Register settings are valid, manually set the corresponding interrupt flag to enter the interrupt
3. Joint work, failure
void PVD_IRQHandler(void)
{
U16 i;
U32 tmp;
tmp = 0x08001000;
if((PWR->CSR & PWR_FLAG_PVDO)==PWR_FLAG_PVDO) //Below the threshold (voltage recovery will also interrupt)
{
F_ErasePage(tmp);
for(i=0;i<2000;i++)
{
FLASH_Program(tmp,i);
tmp+=2;
}
}
}
Analysis: 1. Vdda may drop slower than Vdd after LC filtering, and it is said that stm32 will have detection problems because of this. 2. The power supply circuit has small capacitance and there is not enough time.
Attempt: Removed a 1000uF capacitor from the ATX power supply and connected it, but there was no improvement.
Thinking: It doesn’t make much sense to combine the power failure detection with the power supply pin. In general, the power supply of the MCU is stabilized by LDO. The power failure detection uses a dedicated IO, and the voltage before the regulator can be used for detection.
Since destructive modifications to the circuit board cannot be made at present, and there is no oscilloscope or adjustable power supply on hand, further research on this will be temporarily abandoned.
Applying the logic of a certain big shot, this attempt was a partial failure .
Keywords:STM32
Reference address:STM32 power-off detection + Flash access
1. Get the flash erase and write functions.
void F_ErasePage(u32 Page_Address)
{
U32 tmp;
while((FLASH->SR&FLASH_FLAG_BSY)==FLASH_FLAG_BSY)
{ //PutCnstStr2Usart1("waiting\n"); }
FLASH->SR |= FLASH_FLAG_PGERR;
FLASH->CR |= CR_PER_Set;
FLASH->AR=Page_Address;
>CR |= CR_STRT_Set;
while((FLASH->SR&FLASH_FLAG_BSY)==FLASH_FLAG_BSY)
{ //PutCnstStr2Usart1("waiting\n"); }
FLASH->CR &= ~CR_PER_Set; //Don’t forget this
}
void FLASH_Program(u32 Address, u16 Data)
{
U32 tmp;
Fstart:
if((FLASH->CR&CR_LOCK_Set)==CR_LOCK_Set) //If the write is locked
{
FLASH->KEYR = FLASH_KEY1;
FLASH->KEYR = FLASH_KEY2;
//PutCnstStr2Usart1("Unclock\n");
}
while((FLASH->SR&FLASH_FLAG_BSY)==FLASH_FLAG_BSY)
{
//PutCnstStr2 Usart1("waiting\n");
}
FLASH->CR |= CR_PG_Set;
*(vu16*)Address = Data;
FLASH->CR &= ~CR_PG_Set;
if((FLASH->SR&FLASH_FLAG_PGERR)==FLASH_FLAG_PGERR)
{
//PutCnstStr2Usart1("No earase\n");
F_ErasePage(Address)
; Fstart;//Goto is used, cough cough^_^
}
}
With these two functions, it is basically OK. Anyway, we need to erase one page at a time, so we can save all the parameters. It is more reliable.
U16 const flashdata[1024]__at(0x08001000);//Because flash can and can only write 16 bits at a time, and accessing odd addresses will cause errors.
And don't forget to #include
Put the parameters to be saved into a structure, load it every time the power is reset, and write it to flash when it needs to be stored.
2. Get power-off detection done
void PVD_Init(void)
{
NVIC_InitTypeDef NVIC_InitStruct;
NVIC_InitStruct.NVIC_IRQChannel = PVD_IRQChannel;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0x00;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0x00;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM2 | RCC_APB1Periph_TIM3 |
RCC_APB1Periph_TIM4 | RCC_APB1Periph_USART2|RCC_APB1Periph_PWR, ENABLE); //_PWR, ENABLE); // This is required
PWR->CR |= 1<<4;//Enable
PWR->CR |= 0xE0;//2. 9V detection
NVIC_Init(&NVIC_InitStruct);//Borrow the library to allow interrupts
}
Register settings are valid, manually set the corresponding interrupt flag to enter the interrupt
3. Joint work, failure
void PVD_IRQHandler(void)
{
U16 i;
U32 tmp;
tmp = 0x08001000;
if((PWR->CSR & PWR_FLAG_PVDO)==PWR_FLAG_PVDO) //Below the threshold (voltage recovery will also interrupt)
{
F_ErasePage(tmp);
for(i=0;i<2000;i++)
{
FLASH_Program(tmp,i);
tmp+=2;
}
}
}
Analysis: 1. Vdda may drop slower than Vdd after LC filtering, and it is said that stm32 will have detection problems because of this. 2. The power supply circuit has small capacitance and there is not enough time.
Attempt: Removed a 1000uF capacitor from the ATX power supply and connected it, but there was no improvement.
Thinking: It doesn’t make much sense to combine the power failure detection with the power supply pin. In general, the power supply of the MCU is stabilized by LDO. The power failure detection uses a dedicated IO, and the voltage before the regulator can be used for detection.
Since destructive modifications to the circuit board cannot be made at present, and there is no oscilloscope or adjustable power supply on hand, further research on this will be temporarily abandoned.
Applying the logic of a certain big shot, this attempt was a partial failure .
Previous article:STM32 GPIO register ODR BSRR BRR
Next article:STM32 Quick Learning 6——SysTick Timing 1s Control LED
- Popular Resources
- Popular amplifiers
Recommended Content
Latest Microcontroller Articles
He Limin Column
Microcontroller and Embedded Systems Bible
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
MoreSelected Circuit Diagrams
MorePopular Articles
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
MoreDaily News
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
Guess you like
- Design of AC voltage measurement based on MSP430 microcontroller
- 【Beetle ESP32-C3】4. Luat-OS environment deployment
- A brief discussion on the differences between x86, DSP and SPARC
- ESP32-C3 RISC-V microcontroller information leak
- Review summary: Flathead RVB2601
- EEWORLD University ---- Using lithium-ion batteries more safely and efficiently - Battery Management System (BMS) Solution
- Talk: Small capital investment
- Where does the EG8010 dead time go?
- Downloads|Selected Courseware & Latest Materials from Tektronix 2019 Semiconductor Seminar
- GaN Power HEMT > 650 V: Parametric Analysis and Comparison with SiC MOSFET