RTC in Power Down Mode
This sample code guide is designed to help users easily and comprehensively understand RTC sample. This sample demonstrates RTC works in power down mode.
Requirements
For hardware requirements, please refer to the Requirements.
Configurations
The following macros can be configured to modify the RTC prescaler value.
#define RTC_PRESCALER_VALUE 4095
The entry function is as follows, call this function in
main()
to run this sample code. For more details, please refer to the Initialization.rtc_in_power_down_mode();
Building and Downloading
For building and downloading, please refer to the Building and Downloading.
Experimental Verification
Press the Reset button on the EVB, the system is in idle state and will enter DLPS mode.
After 10 seconds, the system will be woken up and prints the following log in Debug Analyzer.
rtc_handler: RTC_INT_CMP0 rtc_handler: RTC_INT_CMP_1
Code Overview
This section introduces the code and process description for initialization and corresponding function implementation in the sample.
Source Code Directory
For project directory, please refer to Source Code Directory.
Source code directory:
sdk\src\sample\io_demo\rtc\power_down\rtc_in_power_down_mode.c
.
RTC Initialization
Call
power_down_check_wake_up_reason()
to check power down wake up reason.If the system is not woken up from power down:
Call
RTC_DeInit()
to reset the RTC peripheral.Call
RTC_SetPrescaler()
to set the RTC prescaler.Call
RTC_SetComp()
to set RTC comparator channel 1 value.Call
RTC_MaskINTConfig()
to unmask the RTC comparator channel 1 interrupt_RTC_INT::RTC_INT_CMP1
.Call
RTC_CompINTConfig()
to enable the RTC comparator channel 1 interrupt_RTC_INT::RTC_INT_CMP1
.Call
set_clk_32k_power_in_powerdown
to open 32k clock.Call
NVIC_Init()
to enable NVIC of RTC.Call
RTC_RunCmd()
to enable the RTC.
If the system is woken up from power down:
Call
set_clk_32k_power_in_powerdown
to open 32k clock.Call
NVIC_Init()
to enable NVIC of RTC.
Power Down Mode Initialization
Call
bt_power_mode_set()
to set Bluetooth MAC deep sleep mode.Call
io_dlps_register()
to initialize IO store/restore and do not need to worry about which IO peripheral requires specific handling.Call
io_dlps_register_enter_cb()
to register callbacks to power down mode enter stage. Functionapp_dlps_enter_callback
will be executed while entering from power down mode:Call
power_mode_get()
to get power mode.If power mode is power down mode:
Call
RTC_CompINTConfig()
to enable the RTC comparator channel 0 interrupt_RTC_INT::RTC_INT_CMP0
and wake up interrupt_RTC_INT::RTC_CMP0_WK_INT
.Call
RTC_MaskINTConfig()
to unmask the RTC comparator channel 0 interrupt_RTC_INT::RTC_INT_CMP0
and wake up interrupt_RTC_INT::RTC_CMP0_WK_INT
.Call
RTC_SystemWakeupConfig()
to enable the RTC system wakeup function.Call
RTC_GetCounter()
to get counter value of RTC.Call
RTC_SetComp()
to set the RTC comparator channel 0 value.
Call
power_mode_set()
to switch the system to power down mode.
Functional Implementation
Interrupt Handle
When the comparator setting time (10s) is reached, it triggers an RTC interrupt:
Call
RTC_GetINTStatus()
to check interrupt status flag.If
RTC_INT_CMP0
interrupt status flag is set:Call
RTC_ClearCompINT()
to clear the RTC comparator channel 0 interrupt.Call
RTC_ClearCompWkINT()
to clear wake up interrupt of the comparator channel 0 of RTC.
If
RTC_INT_CMP1
interrupt status flag is set:Call
RTC_SetComp()
to set RTC comparator channel 1 value.Call
RTC_ClearCompINT()
to clear the RTC comparator channel 1 interrupt.