Overflow

This example demonstrates the counting overflow function using RTC.

The RTC timer is a 32-bit timer, and the counting overflow time is 232=4294967296, 4294967296/32000≈134217s.

When the RTC count overflows, it triggers an RTC interrupt.

Requirements

The sample supports the following development kits:

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\Overflow\mdk

Project file: board\evb\io_sample\RTC\Overflow\gcc

Please follow these steps to build and run the example:

  1. Open sample project file.

  2. To build the target, follow the steps listed on the Generating App Image in Quick Start.

  3. After a successful compilation, the app bin app_MP_xxx.bin will be generated in the directory mdk\bin or gcc\bin.

  4. To download app bin into EVB board, follow the steps listed on the MP Tool Download in Quick Start.

  5. Press reset button on EVB board and it will start running.

Experimental Verification

After the RTC counter overflows, it triggers an RTC interrupt, and information is printed within the interrupt function.

[main]RTC_Handler: RTC_INT_OVF

Code Overview

This chapter will be introduced according to the following several parts:

  1. Source Code Directory.

  2. Peripheral initialization will be introduced in chapter Initialization.

  3. Functional implementation after initialization will be introduced in chapter Function Implementation.

Source Code Directory

  • Project directory: sdk\board\evb\io_sample\RTC\Overflow

  • Source code directory: sdk\src\sample\io_sample\RTC\Overflow

Source files are currently categorized into several groups as below.

└── 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

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, which includes the following steps:

  1. Reset the RTC peripheral.

  2. Set the RTC’s prescaler coefficient to (1-1), with an RTC clock frequency of 32kHz.

  3. Enable the RTC counter overflow interrupt RTC_INT_OVF.

  4. Configure and enable the RTC IRQ channel. Enable interrupt signal to CPU NVIC.

  5. Reset the RTC count value.

  6. Start the RTC peripheral.

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);
}

Functional Implementation

When the counter overflows (count greater than 232=4294967296, 4294967296/32000≈134217s), an interrupt is triggered. In the interrupt function, print the relevant information and clear the interrupt flag.

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();
    }
}