STM32 power-off detection + Flash access

Publisher:创意火舞Latest update time:2016-09-26 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
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

Previous article:STM32 GPIO register ODR BSRR BRR
Next article:STM32 Quick Learning 6——SysTick Timing 1s Control LED

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号