Input

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

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

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

Requirements

For hardware requirements, please refer to the Requirements.

Configurations

  1. The following macros can be configured to modify GPIO pin definitions.

    #define TEST_Pin            ADC_1
    
  2. The entry function is as follows, call this function in main() to run this sample code. For more details, please refer to the Initialization.

    gpio_input_demo();
    

Building and Downloading

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

Experimental Verification

  1. Press the Reset button on the EVB and the message will be displayed in the Debug Analyzer.

    gpio_input_demo
    
  2. When a high level input is detected, the following message is printed in the Debug Analyzer.

    gpio_test: gpio_value 1
    
  3. When a low level input is detected, the following message is printed in the Debug Analyzer.

    gpio_test: gpio_value 0
    

Code Overview

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

Source Code Directory

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

  • For project directory, please refer to Source Code Directory.

  • Source code directory: sdk\sample\io_demo\gpio\input\gpio_input_demo.c.

Initialization

The initialization flow for peripherals can refer to Initialization Flow.

The GPIO initialization flow is shown in the following figure.

../../../_images/GPIO_Initialization_Flow_Chart.png

GPIO Initialization Flow Chart

  1. Call hal_gpio_init() to enable GPIO clock.

    hal_gpio_init();
    
  2. Call hal_gpio_init_pin() to initialize the GPIO peripheral. The GPIO initialization parameters are configured as shown in the table below.

GPIO Initialization Parameters

GPIO Hardware Parameters

GPIO

Pin Number

TEST_Pin

GPIO Type

GPIO_TYPE_AUTO

GPIO Mode

GPIO_DIR_INPUT

GPIO Pull Value

GPIO_PULL_UP

Functional Implementation

After initialization, call the hal_gpio_get_input_level() function to read the input level.

static void gpio_test(void)
{
    T_GPIO_LEVEL gpio_value;

    gpio_value  = hal_gpio_get_input_level(TEST_Pin);
    IO_PRINT_INFO1("gpio_test: gpio_value %d", gpio_value);
}