BKP is the abbreviation of "BACKUP". The stm32f103RCTE is equipped with 10 16-bit BKP registers. When the main power is cut off or the system generates a reset time, the BKP register can still maintain its content with the support of the backup power supply.
In practical applications, BKP can store important data to prevent malicious viewing, or be used for power outage recovery, etc.
This example implements the read and write operations of the BKP register, as well as intrusion detection and processing. After writing the register in the main program, 10 BKP register data are printed out in sequence, and then the intrusion interrupt of GPIOC13 is triggered (input low level), and the register content after the intrusion event occurs is printed out in the interrupt (reset to 0).
Direct access to registers
The registers used are described as follows:
Backup data register x (BKP_DRx) (x = 1 … 10): The lower 16 bits [15:0] are valid and are used to write or read backup data.
Backup Control Register (BKP_CR):
The lower two bits are valid.
TPAL[1]: TAMPER pin active level for intrusion detection
0: A high level on the intrusion detection TAMPER pin will clear all data backup registers (if the TPE bit is 1)
1: A low level on the intrusion detection TAMPER pin will clear all data backup registers (if the TPE bit is 1)
TPE[0]: Enable TAMPER pin for intrusion detection (TAMPER pin enable)
0: The intrusion detection TAMPER pin is used as a general IO port
1: Enable the intrusion detection pin for use as intrusion detection
Backup Control/Status Register (BKP_CSR):
TIF[9]: Tamper interrupt flag 0: No tamper interrupt 1: Generate tamper interrupt
This bit is set to 1 by hardware when an intrusion event is detected and the TPIE bit is 1.
This flag is cleared by writing 1 to the CTI bit (which also clears the interrupt). This bit is also cleared if the TPIE bit is cleared.
TEF[8]: Tamper event flag 0: No tamper event 1: Tamper event detected
This bit is set to 1 by hardware when an intrusion event is detected. This flag can be cleared by writing 1 to the CTE bit.
TPIE[2]: Enable TAMPER pin interrupt (TAMPER pin interrupt enable)
0: Disable intrusion detection interrupt 1: Enable intrusion detection interrupt (the TPE bit of the BKP_CR register must also be set to 1)
Note 1: Intrusion interrupts cannot wake up the system core from low power modes. Note 2: This bit is reset only when the system is reset or waked up from standby mode.
CTI[1]: Clear tamper interrupt
0: Invalid 1: Clear intrusion detection interrupt and TIF intrusion detection interrupt flag
CTE[0]: Clear tamper event
0: Invalid 1: Clear the TEF intrusion detection event flag (and reset the intrusion detector).
To write BKP register data, you must cancel the backup area write protection in PWR->CR before writing BKP data. There is no need to set the GPIOC clock and input/output mode when STM32 turns on intrusion detection.
The code is as follows: (system.h and stm32f10x_it.h and other related codes refer to the stm32 direct operation register development environment configuration )
User/main.c
#include#include "system.h" #include "usart.h" #include "bkp.h" #define LED1 PAout(4) #define LED2 PAout(5) void Gpio_Init(void); int main(void) { u16 data,i=10; Rcc_Init(9); //System clock settings Usart1_Init(72,9600); Bkp_Init(); Tamper_Init(); Nvic_Init(0,0,TAMPER_IRQChannel,0); //Set up interrupt Gpio_Init(); while(i){ Write_Bkp(i,i); data = Read_Bkp(i); printf("\n DR%u = 0x%04X\n",i,data); delay(30000); //delay 30ms i--; } while(1); } void Gpio_Init(void) { RCC->APB2ENR|=1<<2; //Enable PORTA clock GPIOA->CRL&=0x0000FFFF; // PA0~3 are set as floating input, PA4~7 are set as push-pull output GPIOA->CRL|=0x33334444; //USART1 serial port I/O settings GPIOA -> CRH&=0xFFFFF00F; //Set USART1's Tx (PA.9) to the second function push-pull, 50MHz; Rx (PA.10) to floating input GPIOA -> CRH|=0x000008B0; }
User/stm32f103x_it.c
#include "stm32f10x_it.h" #include "system.h" #include "stdio.h" #define LED1 PAout(4) #define LED2 PAout(5) #define LED3 PAout(6) #define LED4 PAout(7) extern u16 Read_Bkp(u8 reg); void TAMPER_IRQHandler(void) { u16 i=10,data; LED4 =1 ; printf("\r\n A Tamper is coming .\r\n"); while(i){ data = Read_Bkp(i); printf("\n DR%u = 0x%04X\n",i,data); delay(30000); //delay 30ms i--; } BKP->CSR |= 3<<0; //Clear event interrupt flag }
Library/src/bkp.c
#include#include "bkp.h" void Bkp_Init(void) { RCC->APB1RSTR |= 1<<27; //Reset BKP register RCC->APB1RSTR &= ~(1<<27); RCC->APB1ENR|=1<<28; //Enable power clock RCC->APB1ENR|=1<<27; //Enable BKP clock } /** * *Backup register write operation *reg: register number *data: the value to be written * **/ void Write_Bkp(u8 reg,u16 data) { PWR->CR|=1<<8; //Cancel the write protection of the backup area switch(reg) { case 1: BKP->DR1=data; break; case 2: BKP->DR2=data; break; case 3: BKP->DR3=data; break; case 4: BKP->DR4=data; break; case 5: BKP->DR5=data; break; case 6: BKP->DR6=data; break; case 7: BKP->DR7=data; break; case 8: BKP->DR8=data; break; case 9: BKP->DR9=data; break; case 10: BKP->DR10=data; break; } } u16 Read_Bkp(u8 reg) { u16 data; switch(reg) { case 1: data = BKP->DR1; break; case 2: data = BKP->DR2; break; case 3: data = BKP->DR3; break; case 4: data = BKP->DR4; break; case 5: data = BKP->DR5; break; case 6: data = BKP->DR6; break; case 7: data = BKP->DR7; break; case 8: data = BKP->DR8; break; case 9: data = BKP->DR9; break; case 10: data = BKP->DR10; break; } return data; } // Enable intrusion detection. The detection pin is GPIOC13, but there is no need to turn on its clock and set the pin mode void Tamper_Init() { BKP->CSR |= 3<<0; //Clear event interrupt flag BKP->CR |= 1<<1; //Set the intrusion level to low level BKP->CSR |= 1<<2; //Allow intrusion interrupt BKP->CR |= 1<<0; // Enable intrusion detection }
Library/inc/bkp.h
#includevoid Bkp_Init(void); void Write_Bkp(u8 reg,u16 data); u16 Read_Bkp(u8 reg); void Tamper_Init(void);
Library function operation
main.c
#include "stm32f10x.h" #include "stdio.h" #define PRINTF_ON 1 #define CHECK_CODE 0xAE86 void RCC_Configuration(void); void GPIO_Configuration(void); void NVIC_Configuration(void); void USART_Configuration(void); void BKP_Configuration(void); void PrintBKP(void); void WriteBKP(u16 Data,u8 DRNumber); u8 CheckBKP(void); int main(void) { RCC_Configuration(); GPIO_Configuration(); NVIC_Configuration(); USART_Configuration(); BKP_Configuration(); if(CheckBKP()) { printf("\r\n The datas are as their initial status. \r\n"); WriteBKP(0xA522,2); PrintBKP(); }else{ printf("\r\n The datas have been changed . \r\n"); WriteBKP(0xA53C,1); PrintBKP(); } while(1); } void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA , &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA , &GPIO_InitStructure); } void BKP_Configuration(void) { PWR_BackupAccessCmd(ENABLE); BKP_ClearFlag(); BKP_TamperPinLevelConfig(BKP_TamperPinLevel_Low); BKP_ITConfig(ENABLE); BKP_TamperPinCmd(ENABLE); } void RCC_Configuration(void) { /* Define enumeration type variable HSEStartUpStatus */ ErrorStatus HSEStartUpStatus; /* Reset system clock settings */ RCC_DeInit(); /* Enable HSE*/ RCC_HSEConfig(RCC_HSE_ON); /* Wait for HSE to start oscillating and stabilize*/ HSEStartUpStatus = RCC_WaitForHSEStartUp(); /* Determine whether the HSE is successfully started, if yes, enter if() */ if(HSEStartUpStatus == SUCCESS) { /* Select HCLK (AHB) clock source as SYSCLK divided by 1 */ RCC_HCLKConfig(RCC_SYSCLK_Div1); /* Select PCLK2 clock source as HCLK (AHB) divided by 1 */ RCC_PCLK2Config(RCC_HCLK_Div1); /* Select PCLK1 clock source as HCLK (AHB) divided by 2 */ RCC_PCLK1Config(RCC_HCLK_Div2); /* Set the FLASH delay cycle number to 2 */ FLASH_SetLatency(FLASH_Latency_2); /* Enable FLASH prefetch cache */ FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); /* Select the phase-locked loop (PLL) clock source as HSE 1 division, the multiplier is 9, then the PLL output frequency is 8MHz * 9 = 72MHz */ RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); /* Enable PLL */ RCC_PLLCmd(ENABLE); /* Wait for PLL output to stabilize */ while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET); /* Select SYSCLK clock source as PLL */ RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); /* Wait for PLL to become the SYSCLK clock source */ while(RCC_GetSYSCLKSource() != 0x08); } /* Turn on the GPIOA clock on the APB2 bus */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR|RCC_APB1Periph_BKP, ENABLE); } void USART_Configuration(void) { USART_InitTypeDef USART_InitStructure; USART_ClockInitTypeDef USART_ClockInitStructure; USART_ClockInitStructure.USART_Clock = USART_Clock_Disable; USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low; USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge; USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable; USART_ClockInit(USART1 , &USART_ClockInitStructure); USART_InitStructure.USART_BaudRate = 9600; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx|USART_Mode_Tx; USART_Init(USART1,&USART_InitStructure); USART_Cmd(USART1,ENABLE); } void NVIC_Configuration(void) { NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitStructure.NVIC_IRQChannel = TAMPER_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } void WriteBKP(u16 Data,u8 DRNumber) // You can also add some encryption algorithms; DRNumber (1-9) { switch(DRNumber) { case 0x01: BKP_WriteBackupRegister(BKP_DR1,Data); break; case 0x02: BKP_WriteBackupRegister(BKP_DR2,Data); break; case 0x03: BKP_WriteBackupRegister(BKP_DR3,Data); break; case 0x04: BKP_WriteBackupRegister(BKP_DR4,Data); break; case 0x05: BKP_WriteBackupRegister(BKP_DR5,Data); break; case 0x06: BKP_WriteBackupRegister(BKP_DR6,Data); break; case 0x07: BKP_WriteBackupRegister(BKP_DR7,Data); break; case 0x08: BKP_WriteBackupRegister(BKP_DR8,Data); break; case 0x09: BKP_WriteBackupRegister(BKP_DR9,Data); break; default: BKP_WriteBackupRegister(BKP_DR1,Data); } BKP_WriteBackupRegister(BKP_DR10,CHECK_CODE); } u8 CheckBKP(void) { if( BKP_ReadBackupRegister(BKP_DR10) == 0xAE86 ) // If this bit of data is lost, the BPK data is lost return 1; else return 0; } void PrintBKP(void) { printf("DR1 = 0x%04X\t",BKP_ReadBackupRegister(BKP_DR1)); printf("DR2 = 0x%04X\t",BKP_ReadBackupRegister(BKP_DR2)); printf("DR3 = 0x%04X\t",BKP_ReadBackupRegister(BKP_DR3)); printf("DR4 = 0x%04X\t",BKP_ReadBackupRegister(BKP_DR4)); printf("DR5 = 0x%04X\t",BKP_ReadBackupRegister(BKP_DR5)); printf("DR6 = 0x%04X\t",BKP_ReadBackupRegister(BKP_DR6)); printf("DR7 = 0x%04X\t",BKP_ReadBackupRegister(BKP_DR7)); printf("DR8 = 0x%04X\t",BKP_ReadBackupRegister(BKP_DR8)); printf("DR9 = 0x%04X\t",BKP_ReadBackupRegister(BKP_DR9)); printf("DR10 = 0x%04X\t",BKP_ReadBackupRegister(BKP_DR10)); } #if PRINTF_ON int fputc(int ch,FILE *f) { USART_SendData(USART1,(u8) ch); while(USART_GetFlagStatus(USART1,USART_FLAG_TC) == RESET); return ch; } #endif
stm12f10x_it.c
#include "stm32f10x_it.h" #include "stdio.h" extern void PrintBKP(void); void TAMPER_IRQHandler(void) { printf("\r\n A Tamper is coming .\r\n"); PrintBKP(); BKP_ClearITPendingBit(); BKP_ClearFlag(); }
Previous article:STM32 AD analog-to-digital conversion [operation register + library function]
Next article:stm32 RTC real-time clock [operation register + library function]
Recommended ReadingLatest update time:2024-11-16 12:52
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications