RTC

RTC Demo Code Support List

This chapter introduces the details of the RTC demo code.

RTC Demo for Comparator Function

The description of RTC demo code 1 is shown in the following table.

RTC Demo Code 1 Description

Demo 1

rtc_demo.c

Sample Purpose

Demonstrate RTC comparator function.

File Path

sdk\src\sample\io_demo\rtc\comparator\rtc_demo.c

Function Entry

rtc_demo()

Expected Result

Print string rtc_handler in Debug Analyzer every 1536ms.

RTC Demo for Overflow Function

The description of RTC demo code 2 is shown in the following table.

RTC Demo Code 2 Description

Demo 2

rtc_overflow_demo.c

Sample Purpose

Demonstrate RTC count overflow function.

File Path

sdk\src\sample\io_demo\rtc\overflow\rtc_overflow_demo.c

Function Entry

rtc_overflow_demo()

Expected Result

For RTL87x3D, rtc_handler function is executed every 134217.728s. For RTL87x3E, rtc_handler function is executed every 524.288s.

RTC Demo for Tick Function

The description of RTC demo code 3 is shown in the following table.

RTC Demo Code 3 Description

Demo 3

rtc_tick_demo.c

Sample Purpose

Demonstrate RTC tick function.

File Path

sdk\src\sample\io_demo\rtc\tick\rtc_tick_demo.c

Function Entry

rtc_tick_demo()

Expected Result

rtc_handler function is executed every 31.25us.

RTC Demo for Calendar Function

The description of RTC demo code 4 is shown in the following table.

RTC Demo Code 4 Description

Demo 4

rtc_clock_demo.c rtc_clock_demo.h

Sample Purpose

Demonstrate RTC calendar functionality.

File Path

sdk\src\sample\io_demo\rtc\clock\rtc_clock_demo.c

sdk\src\sample\io_demo\rtc\clock\rtc_clock_demo.h

Function Entry

rtc_clock_demo()

Expected Result

  1. Press the Reset button on the EVB, string rtc_clock_sys_update: year 2022, month 9, day 29, hour 0, minute 1, second 0 will be printed in Debug Analyzer.

  2. Print the time information log in Debug Analyzer every one minute.

RTC Demo in Power Down Mode

The description of RTC demo code 5 is shown in the following table.

RTC Demo Code 5 Description

Demo 5

rtc_in_power_down_mode.c

Sample Purpose

Demonstrate RTC work in power down mode.

File Path

sdk\src\sample\io_demo\rtc\power_down\rtc_in_power_down_mode.c

Function Entry

rtc_in_power_down_mode()

Expected Result

  1. When system is in idle state, it will enter power down mode automatically and print string app_dlps_enter_callback: 10 sec in Debug Analyzer.

  2. After 10 seconds, the system wakes up and prints string rtc_handler: RTC_INT_CMP0 and string rtc_handler: RTC_INT_CMP_1 in Debug Analyzer.

Functional Overview

RTC is an independent timer. RTC has a set of continuous counting counters to provide a calendar clock. It can be converted into the current time and date of the system by querying the counter value.

Feature List

  • 32KHz clock source.

  • 24-bit read-only counter (RTL87x3E and RTL87x3EP), 32-bit read-only counter (RTL87x3D).

  • 12-bit prescaler.

  • Support 4 comparator interrupts.

  • Support overflow interrupt, indicating that the counter overflows and wraps around to 0.

  • Support tick interrupt.

  • Support wake-up system from low power mode.

  • Support 8 comparators.

  • 4 comparators can only be used to trigger wake-up.

  • 4 comparators can be used to trigger wake-up and CPU NVIC.

Program Examples

Comparator Function Operation Flow

RTC comparator function initialization flow is shown in the following figure.

../../../_images/RTC_Comparator_Function_Initialization_Flow_Chart.png

RTC Comparator Function Initialization Flow Chart

The codes below demonstrate the RTC comparator function initialization flow.

void driver_rtc_init(void)
{
   RTC_DeInit();
   RTC_SetPrescaler(RTC_PRESCALER_VALUE);
   RTC_SetComp(RTC_COMP_INDEX, RTC_COMP_VALUE);
   RTC_MaskINTConfig(RTC_INT_CMP_1, DISABLE);
   RTC_CompINTConfig(RTC_INT_CMP_1, ENABLE);
   RamVectorTableUpdate(RTC_VECTORn, RTC_Handler);

   /* Configure 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);

   /* Start RTC */
   RTC_SystemWakeupConfig(ENABLE);
   RTC_RunCmd(ENABLE);
}

RTC interrupt handle flow is shown in the following figure.

../../../_images/RTC_Interrupt_Handle_Flow_Chart_1.png

RTC Interrupt Handle Flow Chart

The codes below demonstrate the RTC interrupt handle flow.

void rtc_handler(void)
{
   IO_PRINT_TRACE0("rtc_handler");
   if (RTC_GetINTStatus(RTC_INT_CMP_1) == SET)
   {
      RTC_SetComp(RTC_COMP_INDEX, RTC_GetCounter() + RTC_COMP_VALUE);
      RTC_ClearCompINT(RTC_COMP_INDEX);
   }
}

Overflow Function Operation Flow

RTC overflow function initialization flow is shown in the following figure.

../../../_images/RTC_Overflow_Function_Initialization_Flow_Chart.png

RTC Overflow Function Initialization Flow Chart

The codes below demonstrate the RTC overflow function initialization flow.

void driver_rtc_init(void)
{
   RTC_DeInit();
   RTC_SetPrescaler(RTC_PRESCALER_VALUE);
   RTC_MaskINTConfig(RTC_INT_OVF, DISABLE);

   RamVectorTableUpdate(RTC_VECTORn, rtc_handler);

   /* 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);

   /* Start RTC */
   RTC_RunCmd(ENABLE);
}

RTC interrupt handle flow is shown in the following figure.

../../../_images/RTC_Interrupt_Handle_Flow_Chart_2.png

RTC Interrupt Handle Flow Chart

The codes below demonstrate the RTC interrupt handle flow.

void rtc_handler(void)
{
   /* RTC overflow interrupt handle */
   if (RTC_GetINTStatus(RTC_INT_OVF) == SET)
   {
      // Add application code here

      RTC_ClearOverFlowINT();
   }
}

Tick Function Operation Flow

RTC tick function initialization flow is shown in the following figure.

../../../_images/RTC_Tick_Function_Initialization_Flow_Chart.png

RTC Tick Function Initialization Flow Chart

The codes below demonstrate the RTC tick function initialization flow.

void driver_rtc_init(void)
{
   RTC_DeInit();
   RTC_SetPrescaler(RTC_PRESCALER_VALUE);
   RTC_MaskINTConfig(RTC_INT_TICK, DISABLE);
   RTC_TickINTConfig(ENABLE);

   RamVectorTableUpdate(RTC_VECTORn, rtc_handler);

   /* 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);

   /* Start RTC */
   RTC_RunCmd(ENABLE);
}

RTC interrupt handle flow is shown in the following figure.

../../../_images/RTC_Interrupt_Handle_Flow_Chart_3.png

RTC Interrupt Handle Flow Chart

The codes below demonstrate the RTC interrupt handle flow.

void rtc_handler(void)
{
   /* RTC overflow interrupt handle */
   if (RTC_GetINTStatus(RTC_INT_TICK) == SET)
   {
      // Add application code here

      RTC_ClearTickINT();
   }
}