BPR — 入侵检测
固件库: AT32F403A_407_Firmware_Library V2.2.2
芯片: AT32
源文件: AT32/AT32F403A_407_Firmware_Library_V2.2.2/project/at_start_f403a/examples/bpr/tamper/src/main.c
功能简介
本示例演示备份寄存器(BPR)的读写。BPR 由独立电池供电,MCU 复位后数据不丢失,适合存储校准参数、运行计数等关键数据。
完整代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
#include "at32f403a_407_board.h"
#include "at32f403a_407_clock.h"
#define BPR_DR_NUMBER 42
int main(void)
{
/* config nvic priority group */
nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
system_clock_config();
at32_board_init();
/* nvic configuration */
nvic_irq_enable(TAMPER_IRQn, 0, 0);
/* enable pwc and bpr clock */
crm_periph_clock_enable(CRM_PWC_PERIPH_CLOCK, TRUE);
crm_periph_clock_enable(CRM_BPR_PERIPH_CLOCK, TRUE);
/* enable write access to bpr domain */
pwc_battery_powered_domain_access(TRUE);
/* disable tamper pin */
bpr_tamper_pin_enable(FALSE);
/* disable tamper interrupt */
bpr_interrupt_enable(FALSE);
/* tamper pin active on low level */
bpr_tamper_pin_active_level_set(BPR_TAMPER_PIN_ACTIVE_LOW);
/* clear tamper pin event pending flag */
bpr_flag_clear(BPR_TAMPER_EVENT_FLAG);
/* write data to bpr dt registers */
bpr_reg_write();
/* enable tamper interrupt */
bpr_interrupt_enable(TRUE);
/* enable tamper pin */
bpr_tamper_pin_enable(TRUE);
/* check if the written data are correct */
if(bpr_reg_check() == TRUE)
{
at32_led_on(LED2);
}
else
{
at32_led_off(LED2);
}
while(1)
{
}
}
|
代码讲解
系统时钟初始化:配置 MCU 的主频和时钟树。不同芯片的时钟配置不同,一般由工具生成。
板级初始化:初始化开发板上的 LED、按键等基础外设。
中断优先级配置:NVIC 设置中断的抢占优先级和子优先级。数字越小优先级越高。