Voltage Detection

This example verifies the LPC voltage detection function. When the input voltage of P2_4 is higher than the set threshold, it will trigger an LPC interrupt.

Note

  • Pins that can be used for voltage detection are P2_0 ~ P2_7, Vbat. The detection voltage threshold range is 80mV~3200mV.

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.

Wiring

Connect the LPC voltage comparator pin P2_4 to an external input voltage.

Building and Downloading

This sample can be found in the SDK folder:

Project file: board\evb\io_sample\LPC\VolatgeDetection\mdk

Project file: board\evb\io_sample\LPC\VolatgeDetection\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

  1. After EVB restart and during LPC initialization, observe the log in the Debug Analyzer.

    driver_lpc_init
    
  2. When the voltage detected by P2_4 is less than 2000 mV, it triggers the LPC_INT_LPCOMP_VOL interrupt, and the log is printed in the Debug Analyzer as follows.

    LPCOMP_Handler
    [app] app_handle_io_msg: LPC low voltage detection msg.
    

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\LPC\VolatgeDetection

  • Source code directory: sdk\src\sample\io_sample\LPC\VolatgeDetection

Source files are currently categorized into several groups as below.

└── Project: voltage_detect
    └── 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_pinmix.c
            └── rtl876x_lpc.c
        ├── profile
        └── app                      includes the ble_peripheral user application implementation
            ├── main.c
            ├── ancs.c
            ├── app.c
            ├── app_task.c
            └── io_lpc.c

Initialization

After the EVB resets, the main() function is called, and the following process will be executed:

int main(void)
{
    extern uint32_t random_seed_value;
    srand(random_seed_value);
    board_init();
    le_gap_init(APP_MAX_LINKS);
    gap_lib_init();
    app_le_gap_init();
    app_le_profile_init();
    pwr_mgr_init();
    task_init();
    os_sched_start();

    return 0;
}

Note

le_gap_init(), gap_lib_init(), app_le_gap_init, and app_le_profile_init are related to the initialization of the privacy management module. Refer to the initialization process description in LE Peripheral Privacy.

The specific initialization process related to peripherals is as follows:

  1. In board_init, execute board_lpc_init, which is a function for PAD/PINMUX settings related to LPC pins and includes the following processes:

    1. Configure PAD: set pin, SW mode, PowerOn, internal pull-none, output disable.

    2. Configure PINMUX: set pin to IDLE mode.

    void board_lpc_init(void)
    {
        Pad_Config(LPC_CAPTURE_PIN, PAD_SW_MODE, PAD_IS_PWRON, PAD_PULL_NONE, PAD_OUT_DISABLE,
                PAD_OUT_HIGH);
        Pinmux_Config(LPC_CAPTURE_PIN, IDLE_MODE);
    }
    
  2. After executing os_sched_start() to start task scheduling, in the app_main_task main task, execute driver_init to initialize and configure the peripheral drivers.

  3. In driver_init, execute driver_lpc_init, which is the initialization function for the LPC peripheral, including the following process:

    1. Set the LPC comparison channel to the P2_4 pin channel.

    2. Set the LPC voltage detection polarity to LPC_Vin_Below_Vth, which triggers the LPC comparison interrupt when the voltage is below the set threshold.

    3. Set the voltage threshold to 2000mV.

    4. Enable LPC voltage detection and enable the LPC voltage comparison interrupt LPC_INT_LPCOMP_VOL.

    void driver_lpc_init(void)
    {
        DBG_DIRECT("driver_lpc_init");
        LPC_DeInit();
        LPC_InitTypeDef LPC_InitStruct;
        LPC_StructInit(&LPC_InitStruct);
    
        LPC_InitStruct.LPC_Channel   = LPC_CAPTURE_CHANNEL;
        LPC_InitStruct.LPC_Edge      = LPC_VOLTAGE_DETECT_EDGE;
        LPC_InitStruct.LPC_Threshold = LPC_VOLTAGE_DETECT_THRESHOLD;
        LPC_Init(&LPC_InitStruct);
        LPC_Cmd(ENABLE);
    
        LPC_INTConfig(LPC_INT_LPCOMP_VOL, ENABLE);
    }
    

Functional Implementation

  1. Execute os_sched_start() to start task scheduling. When the stack is ready, execute app_handle_dev_state_evt and execute nvic_lpc_init to configure and enable the LPC IRQ channel.

    void nvic_lpc_init(void)
    {
        /* Config LPC interrupt */
        NVIC_InitTypeDef NVIC_InitStruct;
        NVIC_InitStruct.NVIC_IRQChannel = LPCOMP_IRQn;
        NVIC_InitStruct.NVIC_IRQChannelPriority = 3;
        NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
        NVIC_Init(&NVIC_InitStruct);
    
        LPC_INTCmd(ENABLE);
    }
    
  2. When the input voltage of P2_4 is detected to be below 2000mV, it triggers the LPC_INT_LPCOMP_VOL interrupt and enters the interrupt handler function LPCOMP_Handler.

    1. Disable the LPC_INT_LPCOMP_VOL interrupt.

    2. Define the message type IO_MSG_TYPE_BAT_LPC, and send the message to the app task. The main task, upon detecting the message, prints the corresponding information.

    void LPCOMP_Handler(void)
    {
        LPC_INTConfig(LPC_INT_LPCOMP_VOL, DISABLE);
        APP_PRINT_INFO0("LPCOMP_Handler\r\n");
        T_IO_MSG int_lpc_msg;
    
        int_lpc_msg.type = IO_MSG_TYPE_BAT_LPC;
        if (false == app_send_msg_to_apptask(&int_lpc_msg))
        {
            APP_PRINT_ERROR0("[io_lpc] LPCOMP_Handler: Send int_lpc_msg failed!");
            return;
        }
    
    }