Output - Open Drain Mode
This sample implements the open drain output mode for GPIO.
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 P2_1 #define TEST_PIN_2 P2_2 #define TEST_PIN_3 P1_0 #define TEST_PIN_4 P1_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.hal_gpio_open_drain_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.
hal_gpio_open_drain_demo: start
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\output\hal_gpio_open_drain_demo.c
.
Initialization
The initialization flow for peripherals can refer to Initialization Flow.
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 |
GPIO Hardware Parameters |
GPIO |
---|---|
Pin Number |
|
GPIO Type |
|
GPIO Mode |
|
GPIO Pull Value |
GPIO Hardware Parameters |
GPIO |
---|---|
Pin Number |
|
GPIO Type |
|
GPIO Mode |
|
GPIO Pull Value |
GPIO Hardware Parameters |
GPIO |
---|---|
Pin Number |
|
GPIO Type |
|
GPIO Mode |
|
GPIO Pull Value |
Functional Implementation
Call
hal_gpio_open_drian_output_high
to output a high level in open drain mode.Call
hal_gpio_change_direction()
to configure the GPIO to input mode.
void hal_gpio_open_drian_output_high(uint8_t pin_index) { hal_gpio_change_direction(pin_index, GPIO_DIR_INPUT); }
Call
hal_gpio_open_drian_output_low
to output a low level in open drain mode.Call
hal_gpio_change_direction()
to configure the GPIO to output mode.Call
hal_gpio_set_level()
to output low level.
void hal_gpio_open_drian_output_low(uint8_t pin_index) { hal_gpio_change_direction(pin_index, GPIO_DIR_OUTPUT); hal_gpio_set_level(pin_index, GPIO_LEVEL_LOW); }