备份域侵入检测测试
void TamperEventTest()
{
//设置时钟PWR和BKP
RCC_APB1PeriphClockCmd(RCC_APB1Periph_BKP,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR,ENABLE);
PWR_BackupAccessCmd(ENABLE); //使能访问备份域和RTC
//侵入引脚时钟设置
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
//初始化备份域
RCC_BackupResetCmd(ENABLE);
RCC_BackupResetCmd(DISABLE);
//设置备份域时钟
RCC_LSEConfig(RCC_LSE_ON); //Configures the External Low Speed oscillator (LSE).
RCC_RTCCLKCmd(ENABLE); //Enables or disables the RTC clock.
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); //Configures the RTC clock (RTCCLK).
while(!(RCC->BDCR & 0x1<<1)); //External low-speed clock ready flag.
BKP_ClearFlag(); //Set CTE bit to clear Tamper Pin Event flag
//配置信号控制(PA1)侵入检测的信号(PC13)
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//write backup registers
BKP_WriteBackupRegister(BKP_DR1, 0xA5A5);
BKP_WriteBackupRegister(BKP_DR2, 0x8888);
BKP_WriteBackupRegister(BKP_DR3, 0x6666);
BKP_WriteBackupRegister(BKP_DR4, 0x9999);
BKP_WriteBackupRegister(BKP_DR5, 0x9999);
BKP_WriteBackupRegister(BKP_DR6, 0x9999);
BKP_WriteBackupRegister(BKP_DR7, 0x9999);
BKP_WriteBackupRegister(BKP_DR8, 0x9999);
BKP_WriteBackupRegister(BKP_DR9, 0x9999);
BKP_WriteBackupRegister(BKP_DR10, 0x9999);
BKP->CR=0x01;
GPIOA->ODR=1;//set PA[0]=1 >> set PC[13]
while(1)
{
if((BKP->CSR &0x100)==0x100)//tef tamper event flag
{
//A write to the BKP->DRx register is performed while tef is set the value will not be stored
BKP->DR1=0x6666;
BKP->DR2=0x6666;
delay(100);
BKP->CSR=0x1;//clear tamper event flag;侵入检测,将使备份域复位
break;
}
}
|