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

  1. The following macros can be configured to modify the RTC prescaler value.

    • #define RTC_PRESCALER_VALUE         4095

  2. 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

  1. Press the Reset button on the EVB, the system is in idle state and will enter DLPS mode.

  2. 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

  1. Call power_down_check_wake_up_reason() to check power down wake up reason.

  2. If the system is not woken up from power down:

    1. Call RTC_DeInit() to reset the RTC peripheral.

    2. Call RTC_SetPrescaler() to set the RTC prescaler.

    3. Call RTC_SetComp() to set RTC comparator channel 1 value.

    4. Call RTC_MaskINTConfig() to unmask the RTC comparator channel 1 interrupt _RTC_INT::RTC_INT_CMP1.

    5. Call RTC_CompINTConfig() to enable the RTC comparator channel 1 interrupt _RTC_INT::RTC_INT_CMP1.

    6. Call set_clk_32k_power_in_powerdown to open 32k clock.

    7. Call NVIC_Init() to enable NVIC of RTC.

    8. Call RTC_RunCmd() to enable the RTC.

  3. If the system is woken up from power down:

    1. Call set_clk_32k_power_in_powerdown to open 32k clock.

    2. Call NVIC_Init() to enable NVIC of RTC.

Power Down Mode Initialization

  1. Call bt_power_mode_set() to set Bluetooth MAC deep sleep mode.

  2. Call io_dlps_register() to initialize IO store/restore and do not need to worry about which IO peripheral requires specific handling.

  3. Call io_dlps_register_enter_cb() to register callbacks to power down mode enter stage. Function app_dlps_enter_callback will be executed while entering from power down mode:

    1. Call power_mode_get() to get power mode.

    2. If power mode is power down mode:

      1. 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.

      2. 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.

      3. Call RTC_SystemWakeupConfig() to enable the RTC system wakeup function.

      4. Call RTC_GetCounter() to get counter value of RTC.

      5. Call RTC_SetComp() to set the RTC comparator channel 0 value.

  4. 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:

  1. Call RTC_GetINTStatus() to check interrupt status flag.

    1. If RTC_INT_CMP0 interrupt status flag is set:

      1. Call RTC_ClearCompINT() to clear the RTC comparator channel 0 interrupt.

      2. Call RTC_ClearCompWkINT() to clear wake up interrupt of the comparator channel 0 of RTC.

    2. If RTC_INT_CMP1 interrupt status flag is set:

      1. Call RTC_SetComp() to set RTC comparator channel 1 value.

      2. Call RTC_ClearCompINT() to clear the RTC comparator channel 1 interrupt.