Tick

This example demonstrates the TICK function using the RTC peripheral.

The RTC clock source (32kHz) after prescaling (in this example 32kHz/3200 = 10Hz), the time for each cycle is the TICK time (in this example 0.1s).

Each TICK timing triggers an RTC interrupt, and the relevant log information is printed within the RTC interrupt function.

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

Project file: board\evb\io_sample\RTC\Tick\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

When the set time is reached, trigger the RTC TICK interrupt and print the interrupt information.

[main]RTC_Handler: RTC_INT_TICK

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\Tick

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

Source files are currently categorized into several groups as below.

└── Project: rtc_tick
    └── 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 reset, the main function is executed. In rtc_demo, it includes the initialization process of the RTC peripheral and other related steps.

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 for the RTC peripheral, including the following steps:

  1. Reset the RTC peripheral.

  2. Set the RTC prescaler to (3200-1), resulting in an RTC clock frequency of 10Hz (32K/3200), i.e., the TICK time is 0.1s.

  3. Enable the RTC tick interrupt RTC_INT_TICK.

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

  5. Reset the RTC counter value.

  6. Start the RTC peripheral.

void driver_rtc_init(void)
{
    RTC_DeInit();
    RTC_SetPrescaler(RTC_PRESCALER_VALUE);

    RTC_INTConfig(RTC_INT_TICK, 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 TICK set time is reached, an RTC interrupt is triggered, entering the interrupt handler function RTC_Handler. Clear the tick interrupt and print relevant information.

void RTC_Handler(void)
{
    /* RTC tick interrupt handle */
    if (RTC_GetINTStatus(RTC_INT_TICK) == SET)
    {
        /* Notes: DBG_DIRECT function is only used for debugging demonstrations, not for application projects.*/
        DBG_DIRECT("[main]RTC_Handler: RTC_INT_TICK");
        // Add application code here
        RTC_ClearTickINT();
    }
}