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

  1. 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
    
  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.

    hal_gpio_open_drain_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.

    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.

  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 Output0 Initialization Parameters

GPIO Hardware Parameters

GPIO

Pin Number

TEST_PIN

GPIO Type

GPIO_TYPE_CORE

GPIO Mode

GPIO_DIR_OUTPUT

GPIO Pull Value

GPIO_PULL_NONE

GPIO Output1 Initialization Parameters

GPIO Hardware Parameters

GPIO

Pin Number

TEST_PIN_2

GPIO Type

GPIO_TYPE_CORE

GPIO Mode

GPIO_DIR_OUTPUT

GPIO Pull Value

GPIO_PULL_NONE

GPIO Output2 Initialization Parameters

GPIO Hardware Parameters

GPIO

Pin Number

TEST_PIN_3

GPIO Type

GPIO_TYPE_AON

GPIO Mode

GPIO_DIR_OUTPUT

GPIO Pull Value

GPIO_PULL_NONE

GPIO Output3 Initialization Parameters

GPIO Hardware Parameters

GPIO

Pin Number

TEST_PIN_4

GPIO Type

GPIO_TYPE_AON

GPIO Mode

GPIO_DIR_OUTPUT

GPIO Pull Value

GPIO_PULL_NONE

Functional Implementation

  1. Call hal_gpio_open_drian_output_high to output a high level in open drain mode.

    1. 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);
    }
    
  2. Call hal_gpio_open_drian_output_low to output a low level in open drain mode.

    1. Call hal_gpio_change_direction() to configure the GPIO to output mode.

    2. 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);
    }