鄭州網(wǎng)站建設網(wǎng)站制作百度官網(wǎng)推廣平臺電話
GD32F103VE的TAMPER引腳(PC13),當PC13輸入低電平時,會產(chǎn)生一個侵入檢測事件。它會將所有“數(shù)據(jù)備份寄存器”內(nèi)容清除。
這個功能有什么用?
一是防止被人開殼,抄襲。二是自毀功能。
直奔主題,多一句就是浪費時間。測試程序如下:
#include "TamperDetectionFunction.h"
#include "stdio.h" //使能printf(),sprintf()
//#include "LED.h"/*
當TAMPER引腳上的信號從0到1或從1到0
取決于備份控制寄存器BKP_TPCTL的TPAL位,
會產(chǎn)生一個侵入檢測事件;
侵入檢測事件會將所有數(shù)據(jù)備份寄存器內(nèi)容清除。
*/#define BKP_DATA_REG_NUM 42 //備份寄存器有42個void write_backup_register(uint16_t data);
uint32_t check_backup_register(uint16_t data);
uint32_t is_backup_register_clear(void);
void TamperDetectionFunction_Init(void);//函數(shù)功能:TAMPER引腳(PC13)輸入低電平時,會產(chǎn)生一個侵入檢測事件,它會將所有數(shù)據(jù)備份寄存器內(nèi)容清除。
//為防止侵入事件丟失,PC13引腳檢測到邊沿檢測信號與TPEN位的邏輯與作為侵入檢測信號
void TamperDetectionFunction_Init(void)
{//MCU提供侵入檢測功能以保護重要的用戶數(shù)據(jù),可通過設置BKP_TPCTL寄存器中的TPEN位來使能TAMPER引腳對應的功能。nvic_irq_enable(TAMPER_IRQn,0,0);//設置TAMPER_IRQn的中斷優(yōu)先級,搶占優(yōu)先級為0,子優(yōu)先級為0rcu_periph_clock_enable(RCU_PMU);//使能RCU_PMU時鐘rcu_periph_clock_enable(RCU_BKPI);//使能RCU_BKPI時鐘pmu_backup_write_enable(); //使能對備份域寄存器的寫訪問bkp_tamper_active_level_set(TAMPER_PIN_ACTIVE_LOW); //配置TAMPER引腳(PC13)輸入低電平有效bkp_tamper_detection_disable();//不使能"TAMPER引腳(PC13)實現(xiàn)備份復位功能"bkp_interrupt_disable(); //不使能"TAMPER-RTC引腳(PC13)侵入中斷"bkp_flag_clear(); //清除"TAMPER-RTC引腳(PC13)侵入事件標志"bkp_interrupt_enable(); //使能"TAMPER引腳(PC13)侵入中斷"bkp_tamper_detection_enable(); //使能"TAMPER引腳(PC13)可實現(xiàn)備份復位功能"/*bkp_data_write(BKP_DATA_41,0xA0A0);//將0xA0A0寫入備份數(shù)據(jù)寄存器41if(bkp_data_read(BKP_DATA_41)==0xA0A0) LED2_On();else LED3_On();
*/write_backup_register(0x1226);//將0x1226寫入地址為BKP_DATA_0的備份寄存器if(0x00 == check_backup_register(0x1226))//寫入備份數(shù)據(jù)寄存器正確{
// MCU_Led_On();//寫入備份數(shù)據(jù)寄存器正確printf("\r\nwrite_backup_register OK!!!");}else//寫入備份數(shù)據(jù)寄存器不正確{
// MCU_Led_Off();//寫入備份數(shù)據(jù)寄存器不正確printf("\r\nwrite_backup_register Error!!!");}
}//函數(shù)功能:將data,data+0x50,data+0x50*2,......data+0x50*41,寫入備份寄存器
void write_backup_register(uint16_t data)
{uint32_t temp = 0;/* write data to backup data registers */for (temp = 0; temp < BKP_DATA_REG_NUM; temp++){bkp_data_write( (bkp_data_register_enum)(temp+1),(data + (temp * 0x50)) );//BKP_DATA_0的初始值為1,所以這里要用(temp+1)
// if(temp < 10)
// {
// BKP_DATA0_9(temp) = data + (temp * 0x50);
// }
// else
// {
// BKP_DATA10_41(temp) = data + (temp * 0x50);
// }}
}//函數(shù)功能:從備份寄存器讀取數(shù)據(jù),并比較;若發(fā)現(xiàn)錯誤,則返回
uint32_t check_backup_register(uint16_t data)
{uint32_t temp = 0;for(temp = 0; temp < BKP_DATA_REG_NUM; temp++){if(bkp_data_read( (bkp_data_register_enum)(temp+1) ) != (data + (temp * 0x50)) ){//BKP_DATA_0的初始值為1,所以這里要用(temp+1)return temp+1;//發(fā)現(xiàn)錯誤}// if(temp < 10)
// {
// //get data from data register 0-9
// if(data + (temp * 0x50) != BKP_DATA_GET(BKP_DATA0_9(temp)))
// {
// return temp+1;
// }
// }
// else
// {
// //get data from data register 10-41
// if(data + (temp * 0x50) != BKP_DATA_GET(BKP_DATA10_41(temp)))
// {
// return temp+1;
// }
// }}return 0;
}//函數(shù)功能:檢查"備份寄存器"的數(shù)據(jù)是否為0x0000,并比較;若發(fā)現(xiàn)不是0x0000,則返回
uint32_t is_backup_register_clear(void)
{uint32_t temp = 0;for(temp = 0; temp < BKP_DATA_REG_NUM; temp++){if(bkp_data_read((bkp_data_register_enum)(temp+1))!=0x0000){//BKP_DATA_0的初始值為1,所以這里要用(temp+1)return temp+1;//發(fā)現(xiàn)錯誤}
// if(temp < 10)
// {
// //check if the data of data register 0-9 is 0x0000
// if(0x0000 != BKP_DATA_GET(BKP_DATA0_9(temp)))
// {//BKP_DATA_GET(BKP_DATA0_9(temp)和bkp_data_read( (temp+1))等價
// return temp+1;
// }
// }
// else
// {
// // check if the data of data register 10-41 is 0x0000
// if(0x0000 != BKP_DATA_GET(BKP_DATA10_41(temp)))
// {//BKP_DATA_GET(BKP_DATA10_41(temp)和bkp_data_read( (temp+1))等價
// return temp+1;
// }
// }}return 0;
}//函數(shù)功能:"TAMPER引腳(PC13)侵入中斷服務函數(shù)
//bkp_tamper_active_level_set(TAMPER_PIN_ACTIVE_LOW)配置TAMPER引腳(PC13)輸入低電平有效
//TAMPER引腳(PC13)輸入低電平時,會產(chǎn)生一個侵入檢測事件,它會將所有數(shù)據(jù)備份寄存器內(nèi)容清除。
void TAMPER_IRQHandler(void)
{if(RESET != bkp_interrupt_flag_get())//讀取"TAMPER-RTC引腳(PC13)侵入中斷標志"{if(0 == is_backup_register_clear())//發(fā)現(xiàn)"侵入事件"清除了"備份數(shù)據(jù)寄存器"{//"備份數(shù)據(jù)寄存器"中的數(shù)據(jù)被清除了
// MCU_Led_On();printf("\r\nClear backup_register!!!");}else//發(fā)現(xiàn)"侵入事件"沒有清除"備份數(shù)據(jù)寄存器"{//"備份數(shù)據(jù)寄存器"中的數(shù)據(jù)沒有被清除
// MCU_Led_On();printf("\r\nDon't Clear backup_register!!!");}bkp_interrupt_flag_clear();//清除"TAMPER-RTC引腳(PC13)侵入中斷標志",clear the interrupt bit flag of tamper interruptbkp_flag_clear();//清除"TAMPER-RTC引腳(PC13)侵入事件標志",clear the bit flag of tamper event bkp_interrupt_disable();//不使能"TAMPER-RTC引腳(PC13)侵入中斷",disable the tamper pinbkp_interrupt_enable();//TAMPER-RTC引腳(PC13)侵入中斷使能,enable the tamper pinbkp_tamper_active_level_set(TAMPER_PIN_ACTIVE_LOW);//配置TAMPER引腳(PC13)輸出低電平有效,tamper pin active level set}
}
#include "UART3.h"
#include "stdio.h" //使能printf(),sprintf()void UART3_Init(unsigned int bound);//函數(shù)功能:初始化串口3,這個和STM32F103VET6的UART4兼容
void UART3_Init(unsigned int bound)
{rcu_periph_clock_enable(RCU_GPIOC); //使能GPIOC時鐘,enable GPIO clock rcu_periph_clock_enable(RCU_UART3); //使能UART3時鐘,enable USART clockgpio_init(GPIOC, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_10);//將GPIOC10設置為AFIO口(復用IO口),輸出上拉gpio_init(GPIOC, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_11);//將GPIOC11設置為浮空輸入口usart_deinit(UART3); //復位UART3,USART configureusart_baudrate_set(UART3, bound); //設置UART3的波特率usart_word_length_set(UART3, USART_WL_8BIT); //設置UART3數(shù)據(jù)傳輸格式為8位usart_stop_bit_set(UART3, USART_STB_1BIT); //設置UART3停止位為1位usart_parity_config(UART3, USART_PM_NONE); //設置UART3無需奇偶校驗usart_hardware_flow_rts_config(UART3, USART_RTS_DISABLE); //設置不使能UART3的RTS引腳功能usart_hardware_flow_cts_config(UART3, USART_CTS_DISABLE); //設置不使能UART3的CTS引腳功能usart_receive_config(UART3, USART_RECEIVE_ENABLE); //使能UART3接收usart_transmit_config(UART3, USART_TRANSMIT_ENABLE); //使能UART3發(fā)送usart_enable(UART3); //使能UART3
}/* retarget the C library printf function to the USART */
int fputc(int ch, FILE *f)
{usart_data_transmit(UART3, (uint8_t) ch);while( RESET == usart_flag_get(UART3, USART_FLAG_TBE) ){//等待串口0發(fā)送結束}return ch;
}
?main.c程序如下:
#include "gd32f10x.h" //使能uint8_t,uint16_t,uint32_t,uint64_t,int8_t,int16_t,int32_t,int64_t,bool
#include "UART3.h"
#include "stdio.h" //使能printf(),sprintf()#include "LED.h"
#include "TamperDetectionFunction.h"const char CPU_Reset_REG[]="\r\nCPU reset!\r\n";
int main(void)
{//NVIC_PRIGROUP_PRE4_SUB0:搶占優(yōu)先級為4bit(取值為0~15),子優(yōu)先級為0bit(沒有響應優(yōu)先級)//NVIC_PRIGROUP_PRE3_SUB1:搶占優(yōu)先級為3bit(取值為0~7),子優(yōu)先級為1bit(取值為0~1)//NVIC_PRIGROUP_PRE2_SUB2:搶占優(yōu)先級為2bit(取值為0~3),子優(yōu)先級為2bit(取值為0~3)//NVIC_PRIGROUP_PRE1_SUB3:搶占優(yōu)先級為1bit(取值為0~1),子優(yōu)先級為3bit(取值為0~7)//NVIC_PRIGROUP_PRE0_SUB4:搶占優(yōu)先級為0bit(沒有搶占優(yōu)先級),子優(yōu)先級為3bit(取值為0~15)nvic_priority_group_set(NVIC_PRIGROUP_PRE4_SUB0);//設置系統(tǒng)中斷優(yōu)先級"搶占優(yōu)先級為4bit,子優(yōu)先級為0bit"UART3_Init(115200);//初始化UART3printf("%s",CPU_Reset_REG);//調(diào)試串口輸出"\r\nCPU reset!\r\n"INTX_ENABLE();//開啟所有中斷LED_Init();//初始化MCU_LedTamperDetectionFunction_Init();
//TAMPER引腳(PC13)輸入低電平時,會產(chǎn)生一個侵入檢測事件,它會將所有數(shù)據(jù)備份寄存器內(nèi)容清除。
//當將PC13輸入低電平時,串口輸出"Clear backup_register!!!"while(1){}
}
?