Overflow
该示例通过使用 RTC 实现计数溢出功能。
RTC 定时器为 32 位定时器,计数溢出的时间为 232 = 4294967296 ,4294967296/32000≈134217s 。
当 RTC 计数溢出时,会触发 RTC 中断。
环境需求
该示例支持以下开发套件:
Hardware Platforms |
Board Name |
---|---|
RTL8752H HDK |
RTL8752H EVB |
更多信息请参考 快速入门。
编译和下载
该示例的工程路径如下:
Project file: board\evb\io_sample\RTC\Overflow\mdk
Project file: board\evb\io_sample\RTC\Overflow\gcc
请按照以下步骤操作构建并运行该示例:
打开工程文件。
按照 快速入门 中 编译 APP Image 给出的步骤构建目标文件。
编译成功后,在路径
mdk\bin
或gcc\bin
下会生成 app binapp_MP_xxx.bin
文件。按下 reset 按键,开始运行。
测试验证
RTC 计数溢出后,触发 RTC 中断,在中断函数内打印信息。
[main] RTC_Handler: RTC_INT_OVF
代码介绍
该章节分为以下几个部分:
源码路径
工程路径:
sdk\board\evb\io_sample\RTC\Overflow
源码路径:
sdk\src\sample\io_sample\RTC\Overflow
该工程的工程文件代码结构如下:
└── Project: rtc_overflow
└── secure_only_app
└── include
├── app_define.h
└── rom_uuid.h
├── cmsis includes CMSIS header files and startup files
├── overlay_mgr.c
├── system_rtl876x.c
└── startup_rtl876x.s
├── lib includes all binary symbol files that user application is built on
├── rtl8752h_sdk.lib
├── gap_utils.lib
└── ROM.lib
├── peripheral includes all peripheral drivers and module code used by the application
├── rtl876x_rcc.c
├── rtl876x_nvic.c
└── rtl876x_rtc.c
├── profile
└── app includes the ble_peripheral user application implementation
└── main.c
初始化
当 EVB 复位启动时,执行 main
函数。在 rtc_demo
中,包含 RTC 外设的初始化。
int main(void)
{
extern uint32_t random_seed_value;
srand(random_seed_value);
__enable_irq();
rtc_demo();
while (1)
{
__NOP();
__NOP();
__NOP();
__NOP();
__NOP();
__NOP();
}
}
void rtc_demo(void)
{
/* Initialize RTC peripheral */
driver_rtc_init();
}
driver_rtc_init
为 RTC 外设的初始化,包含如下流程:
复位 RTC 外设。
设置 RTC 的分频系数为(1-1),RTC 时钟频率为 32kHz。
使能 RTC 计数器溢出中断
RTC_INT_OVF
。配置并使能 RTC 的 IRQ 通道,开启 CPU 的 NVIC 中断。
复位 RTC 计数值。
启动 RTC 外设。
void driver_rtc_init(void) { RTC_DeInit(); RTC_SetPrescaler(RTC_PRESCALER_VALUE); RTC_INTConfig(RTC_INT_OVF, ENABLE); /* Config RTC interrupt */ NVIC_InitTypeDef NVIC_InitStruct; NVIC_InitStruct.NVIC_IRQChannel = RTC_IRQn; NVIC_InitStruct.NVIC_IRQChannelPriority = 2; NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStruct); RTC_NvCmd(ENABLE); /* Start RTC */ RTC_ResetCounter(); RTC_Cmd(ENABLE); }
功能实现
当计数器溢出时(计数大于 232=4294967296 ,4294967296/32000≈134217s),触发中断,在中断函数内打印相关信息,清除中断标志位。
void RTC_Handler(void)
{
/* RTC overflow interrupt handle */
if (RTC_GetINTStatus(RTC_INT_OVF) == SET)
{
/* Notes: DBG_DIRECT function is only used for debugging demonstrations, not for application projects.*/
DBG_DIRECT("[main]RTC_Handler: RTC_INT_OVF");
// Add application code here
RTC_ClearOverFlowINT();
}
}