stm32 BKP register operation [operation register + library function]

Publisher:JFETLatest update time:2017-02-06 Source: eefocusKeywords:stm32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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):

 

BKP_CSR.png


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

#include 

void 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();

}


Keywords:stm32 Reference address:stm32 BKP register operation [operation register + library function]

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

SMS short message sending and receiving system based on STM32
As a basic digital service provided by GSM network to users, Short Message Service (SMS) has become a communication means for remote monitoring in many fields. In many applications, the short message transceiver module is directly controlled by PC to complete the system data collection or remote information transmis
[Microcontroller]
SMS short message sending and receiving system based on STM32
STM32 interrupt learning Interrupt/Evens
1. The concept of NVIC priority     Pre-emption priority:     Interrupt events with high pre-emption priority will interrupt the current main program/interrupt program - preemptive priority response, commonly known as interrupt nesting.     Sub-priority:     Under the same pre-emption priority, interrupts with high su
[Microcontroller]
STM32 independent watchdog principle
The independent watchdog of STM32 is driven by a dedicated internal 40Khz low-speed clock, that is, it is still effective even if the main clock fails. Here we need to note that the clock of the independent watchdog is not an accurate 40Khz, but a clock that varies between 30 and 60Khz. It is just that we estimate it
[Microcontroller]
STM32 IIC Difficulties and Errors
Let's go off topic first~ It's said on the Internet that the IIC of STM32F103 has defects! Just treat it as some shortcomings. Personally, I think it's definitely fine to use, but it's just not easy to use. People say that ST company considered patent issues and did not follow Philips' standards. As a result, the IIC
[Microcontroller]
STM32 IIC Difficulties and Errors
STM32 single-line serial port control of bus servo
1 Introduction to bus servos Bus servo servos are serial bus intelligent servos, which can actually be understood as derivatives of digital servos . Compared with analog servos , digital servos are a subversion of control system design, while bus servo servos are a subversion of servos in terms of function and applic
[Microcontroller]
STM32 single-line serial port control of bus servo
STM32 - SystemInit trap during simulation debugging
STM32 - SystemInit trap during simulation debugging When I started the simulation debugging of STM32, I encountered a problem. During debugging, the program kept stopping in the waiting crystal oscillator in SystemInit() and could not get out. The code in the first part of SystemInit() can be executed, but the pr
[Microcontroller]
STM32 - SystemInit trap during simulation debugging
CAN network development notes based on STM32: filter configuration and ID setting
    I have read many articles about configuring filters and setting IDs (StdID ExtID), but found that they all have problems. After my own experimental tests, the results are as follows: (1) Set ID    If you want to use StdID, ExtID can be set at will, just configure StdID correctly. At the same time, you need to set
[Microcontroller]
CAN network development notes based on STM32: filter configuration and ID setting
STM32 timer timing
The STM32 timer is a powerful module, and the frequency of the timer is also very high. The timer can do some basic timing, and can also do PWM output or input capture functions. Clock source problem: There are eight TIMx, of which TIM1 and TIM8 are connected to the APB2 bus, while TIM2-TIM7 are connected to the On th
[Microcontroller]
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
Guess you like
    502 Bad Gateway

    502 Bad Gateway


    openresty

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号