Input Polling

This sample uses the GPIO input function to detect the GPIO input signal by polling.

This sample starts with configuring the GPIO as input to detect changes in the GPIO input. Then it loops while printing information about the GPIO input.

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 P2_3 to the external input signal.

Building and Downloading

This sample can be found in the SDK folder:

Project file: board\evb\io_sample\GPIO\Input_polling\mdk

Project file: board\evb\io_sample\GPIO\Input_polling\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. When detecting a GPIO input high level, the following information is continuously printed in the Debug Analyzer.

    gpio_input_data = 1
    gpio_input_data = 1
    gpio_input_data = 1
    ...
    
  2. When detecting a GPIO input low level, the following information is continuously printed in the Debug Analyzer.

    gpio_input_data = 0
    gpio_input_data = 0
    gpio_input_data = 0
    ...
    

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\GPIO\Input_polling

  • Source code directory: sdk\src\sample\io_sample\GPIO\Input_polling

Source files are currently categorized into several groups as below.

└── Project: input_polling
    └── 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_pinmux.c
            ├── rtl876x_nvic.c
            └── rtl876x_gpio.c
        ├── profile
        └── app                      includes the ble_peripheral user application implementation
            └── main.c

Initialization

When the EVB reset, the main function is executed, following these steps:

int main(void)
{
    extern uint32_t random_seed_value;
    srand(random_seed_value);
    gpio_demo();

    ...
}

In gpio_demo, it includes the PAD/PINMUX setup, and GPIO peripheral initialization.

void gpio_demo(void)
{
    /* Configure pad and pinmux firstly! */
    board_gpio_init();

    /* Initialize gpio peripheral */
    driver_gpio_init();

}

board_gpio_init sets up the PAD/PINMUX with the following process:

  1. Configure PAD: Set the pins, PINMUX mode, PowerOn, internal pull-up, and disable output.

  2. Configure PINMUX: Assign the pins to the GPIO function.

driver_gpio_init initializes the GPIO peripheral with the following process:

  1. Enable the RCC clock.

  2. Configure the GPIO mode to input mode.

  3. Disable GPIO interrupts.

void driver_gpio_init(void)
{
    /* Initialize GPIO peripheral */
    RCC_PeriphClockCmd(APBPeriph_GPIO, APBPeriph_GPIO_CLOCK, ENABLE);

    GPIO_InitTypeDef GPIO_InitStruct;
    GPIO_StructInit(&GPIO_InitStruct);
    GPIO_InitStruct.GPIO_Pin    = GPIO_PIN_INPUT;
    GPIO_InitStruct.GPIO_Mode   = GPIO_Mode_IN;
    GPIO_InitStruct.GPIO_ITCmd  = DISABLE;
    GPIO_Init(&GPIO_InitStruct);
}

Functional Implementation

In the main function, after initialization, execute a while loop, and within the loop body, execute the GPIO_ReadInputDataBit() function and print the read input level information.

int main(void)
{
    extern uint32_t random_seed_value;
    srand(random_seed_value);
    gpio_demo();

    while (1)
    {
        /* Read GPIO input value: gpio_input_data */
        uint8_t gpio_input_data = GPIO_ReadInputDataBit(GPIO_PIN_INPUT);

        DBG_DIRECT("gpio_input_data = %d", gpio_input_data);
        for (uint32_t i = 0 ; i < 10000; i++);
    }
}