Comparator
This example implements the timing function by using the RTC comparator.
After the RTC clock source is pre-divided (32kHz/3200 = 10Hz in this example), an interrupt is triggered by the comparator for every set number of periods (10 in this example, i.e., 0.1s*10=1s).
RTC supports four channels of comparators.
Requirements
The sample supports the following development kits:
Hardware Platforms |
Board Name |
---|---|
RTL8752H HDK |
RTL8752H EVB |
For more requirements, please refer to Quick Start.
Building and Downloading
This sample can be found in the SDK folder:
Project file: board\evb\io_sample\RTC\Comparator\mdk
Project file: board\evb\io_sample\RTC\Comparator\gcc
Please follow these steps to build and run the example:
Open sample project file.
To build the target, follow the steps listed on the Generating App Image in Quick Start.
After a successful compilation, the app bin
app_MP_xxx.bin
will be generated in the directorymdk\bin
orgcc\bin
.To download app bin into EVB board, follow the steps listed on the MP Tool Download in Quick Start.
Press reset button on EVB board and it will start running.
Experimental Verification
Comparator set time (1s) reached, triggers RTC interrupt, print interrupt info and count value in interrupt function, increment count value by 10 each time.
[main]RTC_Handler: RTC_COMP_INDEX1
[main]RTC_Handler: RTC counter current value = 10
[main]RTC_Handler: RTC_COMP_INDEX1
[main]RTC_Handler: RTC counter current value = 20
...
Code Overview
This chapter will be introduced according to the following several parts:
Peripheral initialization will be introduced in chapter Initialization.
Functional implementation after initialization will be introduced in chapter Function Implementation.
Source Code Directory
Project directory:
sdk\board\evb\io_sample\RTC\Comparator
Source code directory:
sdk\src\sample\io_sample\RTC\Comparator
Source files are currently categorized into several groups as below.
└── Project: rtc_comparator
└── 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
Initialization
When the EVB is reset and started, the main
function is executed. In rtc_demo
, it includes the initialization process for the RTC peripheral.
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
is the initialization of the RTC peripheral, including the following steps:
Reset the RTC peripheral.
Set the RTC prescaler to (3200-1), resulting in an RTC clock frequency of 10Hz (32K/3200).
Set the comparator value of RTC comparator channel 1 to 10.
Enable the RTC comparator channel 1 count interrupt
RTC_COMP_INDEX_INT
.Configure and enable the RTC IRQ channel. Enable interrupt signal to CPU NVIC.
Reset the RTC count value.
Start the RTC peripheral.
void driver_rtc_init(void) { RTC_DeInit(); RTC_SetPrescaler(RTC_PRESCALER_VALUE); RTC_SetCompValue(RTC_COMP_INDEX, RTC_COMP_VALUE); RTC_INTConfig(RTC_COMP_INDEX_INT, ENABLE); /* Config RTC interrupt */ NVIC_InitTypeDef NVIC_InitStruct; NVIC_InitStruct.NVIC_IRQChannel = RTC_IRQn; NVIC_InitStruct.NVIC_IRQChannelPriority = 3; NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStruct); RTC_NvCmd(ENABLE); /* Start RTC */ RTC_ResetCounter(); RTC_Cmd(ENABLE); }
Functional Implementation
The comparator setting time (0.1s*10 = 1s) has elapsed, triggering an RTC interrupt and entering the interrupt handler function RTC_Handler
.
Print interrupt information and comparator value information.
Reset the comparator value to the current count value +10, i.e., continue counting 10 cycles from the current time.
Clear the comparator channel 1 count interrupt.
void RTC_Handler(void) { if (RTC_GetINTStatus(RTC_COMP_INDEX_INT) == SET) { DBG_DIRECT("[main]RTC_Handler: RTC_COMP_INDEX%d", RTC_COMP_INDEX); DBG_DIRECT("[main]RTC_Handler: RTC counter current value = %d", RTC_GetCounter()); RTC_SetCompValue(RTC_COMP_INDEX, RTC_GetCounter() + RTC_COMP_VALUE); RTC_ClearCompINT(RTC_COMP_INDEX); } }