Input Polling

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

It is necessary to configure the GPIO as input mode and print the input level status within the while loop.

Users can modify pin information through different macro configurations. For specific macro configurations, refer to Configurations.

Requirements

For requirements, please refer to the Requirements.

Wiring

Connect P1_0 and VDDIO to detect input high level. Connect P1_0 and GND to detect input low level.

Note

The VDDIO can be connected by referring to the instructions for IC Power Voltage Selection in EVB Guide .

Configurations

  1. The following macro can be configured to modify the pin definition.

    #define INPUT_PIN           P1_0
    

Building and Downloading

For building and downloading, please refer to the Building and Downloading.

Experimental Verification

  1. After resetting the EVB, observe the log messages as shown in the Debug Analyzer.

    Start input polling test!
    
  2. When a high level input is detected, the following message is printed in the Debug Analyzer every 2 seconds.

    gpio_input_data = 1
    gpio_input_data = 1
    gpio_input_data = 1
    ...
    
  3. When a low level input is detected, the following message is printed in the Debug Analyzer every 2 seconds.

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

Code Overview

This section mainly introduces the code and process description for initialization and corresponding function implementation in the example.

Source Code Directory

The directory for project file and source code are as follows:

  • Project directory: sdk\samples\peripheral\gpio\input_polling\proj

  • Source code directory: sdk\samples\peripheral\gpio\input_polling\src

Initialization

The initialization flow for peripherals can refer to Initialization Flow in General Introduction.

  1. Call Pad_Config() and Pinmux_Config() to configure the PAD and PINMUX of the corresponding pins.

    void board_gpio_init(void)
    {
        Pad_Config(INPUT_PIN, PAD_PINMUX_MODE, PAD_IS_PWRON, PAD_PULL_NONE, PAD_OUT_ENABLE, PAD_OUT_HIGH);
    
        Pinmux_Config(INPUT_PIN, DWGPIO);
    }
    
  2. Call RCC_PeriphClockCmd() to enable the GPIO clock.

  3. Initialize the GPIO peripheral:

    1. Define the GPIO_InitTypeDef type GPIO_InitStruct, and call GPIO_StructInit() to pre-fill GPIO_InitStruct with default values.

    2. Modify the GPIO_InitStruct parameters as needed. The initialization parameter configurations for GPIO are shown in the table below. Call GPIO_Init() to initialize the GPIO peripheral.

GPIO Initialization Parameters

GPIO Hardware Parameters

Setting in the GPIO_InitStruct

GPIO

GPIO pin

GPIO_InitTypeDef::GPIO_Pin

GPIO_PIN

GPIO direction

GPIO_InitTypeDef::GPIO_Dir

GPIO_DIR_IN

GPIO interrupt

GPIO_InitTypeDef::GPIO_ITCmd

DISABLE

Functional Implementation

After initialization, call the GPIO_ReadInputDataBit() function within the while loop and print the read input level information.

while (1)
{
   uint8_t gpio_input_data = GPIO_ReadInputDataBit(GPIO_PORT, GPIO_PIN);
   DBG_DIRECT("gpio_input_data = %d", gpio_input_data);
   platform_delay_ms(2000);
}