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
The following macros can be configured to modify GPIO pin definitions.
#define TEST_Pin ADC_1
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
Press the Reset button on the EVB and the message will be displayed in the Debug Analyzer.
gpio_input_demo
When a high level input is detected, the following message is printed in the Debug Analyzer.
gpio_test: gpio_value 1
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.

GPIO Initialization Flow Chart
Call
hal_gpio_init()
to enable GPIO clock.hal_gpio_init();
Call
hal_gpio_init_pin()
to initialize the GPIO peripheral. The GPIO initialization parameters are configured as shown in the table below.
GPIO Hardware Parameters |
GPIO |
---|---|
Pin Number |
|
GPIO Type |
|
GPIO Mode |
|
GPIO Pull Value |
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);
}