GPIO

Sample List

This chapter introduces the details of the GPIO sample. The SDK provides the following samples for the GPIO peripheral.

Functional Overview

The general purpose input/output pins (GPIOs) are grouped as one or more ports with each port having up to 32 GPIOs. Each GPIO pin can be configured by software as an output, input, or interrupt peripheral function.

The interrupt can be triggered by changes in the external level signal when in input mode.

GPIO supports debounce functionality.

Feature List

  • 32 independently configured GPIO signals in every port, with independently controllable signal bits.

  • Support hardware and software control.

  • Support input and output control.

  • Support level-triggered and edge-triggered interrupts.

  • Support low and high level-triggered interrupts.

  • Support rising and falling edge-triggered interrupts.

  • Support hardware debounce function.

  • Support GPIO GDMA function.

Note

  1. RTL87x3D supports 3 ports. RTL87x3E and RTL87x3EP support 2 ports.

  2. When the GPIO is controlled by GDMA, it should be configured in hardware mode.

GPIO Output

Each GPIO can be configured into output mode by setting the parameter in hal_gpio_init_pin() to GPIO_DIR_OUTPUT.

The output level of each GPIO can be configured by calling the function hal_gpio_set_level(), where GPIO_LEVEL_HIGH indicates a high level output and GPIO_LEVEL_LOW indicates a low level output.

For more details, please refer to Output.

GPIO Input

Each GPIO can be configured into input mode by setting the parameter in hal_gpio_init_pin() to GPIO_DIR_INPUT.

In input mode, the state of the GPIO can be read. By calling the function hal_gpio_get_input_level() to read the GPIO input state of the corresponding pin.

The GPIO can detect external level changes and meet the specified conditions, it can trigger an interrupt. By calling the hal_gpio_irq_enable(), GPIO interrupts can be enabled.

The interrupt trigger conditions support both level-triggered and edge-triggered configurations. You can configure it to level-triggered by setting the parameter in hal_gpio_set_up_irq() to GPIO_IRQ_LEVEL, or configure it to edge-triggered by setting the parameter to GPIO_IRQ_EDGE. The interrupt trigger polarity supports rising edge (high level) and falling edge (low level) triggers. You can configure it to rising edge (high level) by setting the parameter in hal_gpio_set_up_irq() to GPIO_IRQ_ACTIVE_HIGH, or configure it to falling edge (low level) by setting the parameter to GPIO_IRQ_ACTIVE_LOW.

For more details, please refer to Input - Interrupt.

GPIO Type

When initializing, GPIO can choose from three types: GPIO_TYPE_AUTO, GPIO_TYPE_CORE, and GPIO_TYPE_AON.

  1. GPIO_TYPE_AUTO

    1. When configured in GPIO_DIR_INPUT mode, the wake-up function will be automatically configured based on whether the current interrupt function is enabled when entering low power mode.

    2. When configured in GPIO_DIR_OUTPUT mode, the current level of output will be maintained when entering low power mode.

  2. GPIO_TYPE_CORE

    1. The input and output functions only work in active mode.

  3. GPIO_TYPE_AON

    1. It can be configured in GPIO_DIR_OUTPUT mode to maintain the level in low power mode, but the execution time will be longer compared to GPIO_TYPE_AUTO and GPIO_TYPE_CORE.

Power Manager

The GPIO peripheral is located in the core domain and will be powered off in low power mode. GPIO peripheral store/restore will be automatically performed based on whether the GPIO clock is active. Users can call the API io_dlps_register() to initialize GPIO peripheral store/restore and do not need to worry about GPIO peripheral requiring specific handling.

When the GPIO is configured in input mode, if wake-up functionality is required for this GPIO, it can be set by configuring the parameter in hal_gpio_init_pin() to GPIO_TYPE_AUTO. For more details, please refer to GPIO in DLPS - Auto Config.

When the GPIO is configured in output mode and needs to continue output during DLPS or power down mode, it can be set by configuring the parameter in hal_gpio_init_pin() to GPIO_TYPE_AUTO or GPIO_TYPE_AON, with GPIO_TYPE_AUTO being the recommended option. For more details, please refer to Output.