GPIO

GPIO Demo Code Support List

This chapter introduces the details of the GPIO demo code.

GPIO Demo for Input

The description of GPIO demo code 1 is shown in the following table.

GPIO Demo Code 1 Description

Demo 1

gpio_input_demo.c

Sample Purpose

Demonstrates GPIO input mode.

Brief Introduction

This sample code demonstrates how to get the input level value of GPIO.

File Path

sdk\src\sample\io_demo\gpio\input\gpio_input_demo.c

Function Entry

gpio_input_demo()

GPIO Direction

Input mode

Pin Definition

#define TEST_Pin     ADC_1

Expected Result

Press the Reset button on the EVB, the string gpio_test: gpio_value 1 will be printed in Debug Analyzer.

GPIO Demo for Output

The description of GPIO demo code 2 is shown in the following table.

GPIO Demo Code 2 Description

Demo 2

gpio_output_demo.c

Sample Purpose

Demonstrates GPIO output mode.

Brief Introduction

This sample code demonstrates how to use GPIO to output high and low levels.

File Path

sdk\src\sample\io_demo\gpio\output\gpio_output_demo.c

Function Entry

gpio_output_demo()

GPIO Direction

Output mode

Hardware Connection

As shown in GPIO Demo Code Hardware Connection Diagram. On EVB, connect P0_1 and LED1, and connect P0_2 and LED2.

Pin Definition

#define TEST_PIN     P0_1

#define TEST_PIN_2   P0_2

Expected Result

Press the Reset button on the EVB. LED1 and LED2 will light up or off when the output level is selected as high or low.

The hardware connection of GPIO demo code 2 is shown in the figure below.

../../../_images/GPIO_Hardware_Connection_Diagram.png

GPIO Demo Code Hardware Connection Diagram

GPIO Demo for Different Port (Single Pin Output)

The description of GPIO demo code 3 is shown in the following table.

GPIO Demo Code 3 Description

Demo 3

gpioab_output_demo.c

Sample Purpose

Demonstrates GPIOx output mode whether the pin belongs to GPIOA or B.

Brief Introduction

This sample code demonstrates the GPIOx output function. The output level value can be detected by connecting with LED.

File Path

sdk\src\sample\io_demo\gpio\output\gpio_a_b\gpioab_output_demo.c

Function Entry

gpioab_output_demo()

GPIO Direction

Output mode

Hardware Connection

As shown in GPIO Demo Code Hardware Connection Diagram. On EVB, connect P4_3 and LED1.

Pin Definition

#define TEST_Pin     P4_3

Expected Result

Press the Reset button on the EVB. LED1 will light up or off when the gpio_pin_bit is selected to set or reset.

GPIO Demo for Different Port (Multiple Pin Output)

The description of GPIO demo code 4 is shown in the following table.

GPIO Demo Code 4 Description

Demo 4

gpioab_output_group_demo.c

Sample Purpose

Demonstrates GPIOx group output mode whether the pins belong to GPIOA or B.

Brief Introduction

This sample code demonstrates the GPIOx group output function. These output level values can be detected by connecting with LED.

File Path

sdk\src\sample\io_demo\gpio\output\gpio_a_b\gpioab_output_group_demo.c

Function Entry

gpioab_output_group_demo()

GPIO Direction

Output mode

Hardware Connection

As shown in GPIO Demo Code Hardware Connection Diagram. On EVB, connect LED and test pin.

Expected Result

Press the Reset button on the EVB. The LED will light up or off when the gpio_pin_bit is selected to set or reset.

GPIO Demo for Interrupt Mode

The description of GPIO demo code 5 is shown in the following table.

GPIO Demo Code 5 Description

Demo 5

gpio_int_demo.c

Sample Purpose

Demonstrates GPIO used as a key by edge trigger mode with hardware debounce.

Brief Introduction

This sample code demonstrates the detection of GPIO input through interrupt. When the button is pressed (such as P1_0 changing from high level to low level), the falling edge of GPIO is detected, and the GPIO interrupt is triggered. Then switch the edge trigger, after the button is released (P1_0 changes from low level to high level), the GPIO interrupt is triggered again.

File Path

sdk\src\sample\io_demo\gpio\interrupt\gpio_int_demo.c

Function Entry

gpio_int_demo()

GPIO Direction

Input mode

Hardware Connection

As shown in GPIO Demo Code Hardware Connection Diagram. On EVB, connect P1_0, P1_1, P2_1, P2_2 to KEY1 ~ KEY4 respectively.

Pin Definition

#define GPIO_DEMO_INPUT_PIN0     P1_0

#define GPIO_DEMO_INPUT_PIN1     P1_1

#define GPIO_DEMO_INPUT_PIN2     P2_1

#define GPIO_DEMO_INPUT_PIN3     P2_2

Expected Result

Press the Reset button on the EVB. Press KEY1 ~ KEY4: gpio_isr_cb: pin_name P1_0, gpio_level 0 gpio_isr_cb: pin_name P1_1, gpio_level 1

gpio_isr_cb: pin_name P2_1, gpio_level 0 gpio_isr_cb: pin_name P2_2, gpio_level 1 will be printed in Debug Analyzer.

Release KEY1 ~ KEY4: gpio_isr_cb: pin_name P1_0, gpio_level 1 gpio_isr_cb: pin_name P1_1, gpio_level 0

gpio_isr_cb: pin_name P2_1, gpio_level 1 gpio_isr_cb: pin_name P2_2, gpio_level 0 will be printed in Debug Analyzer.

GPIO Demo for Key Detection by Level Trigger Mode

The description of GPIO demo code 6 is shown in the following table.

GPIO Demo Code 6 Description

Demo 6

gpio_key.c

Sample Purpose

Demonstrates GPIO used as a key by level trigger mode with TIMER debounce.

Brief Introduction

This sample code demonstrates the detection of GPIO input through interrupt. When the button is pressed (P0_0 changes from high level to low level), the low level of GPIO is detected, and the GPIO interrupt is triggered. Then restart the TIMER in the GPIO handler, after the 30ms debounce time, enter the TIMER handler to switch the level trigger, when the button is released (P1_0 changes from low level to high level), the GPIO interrupt is triggered again. Then the TIMER restarts again.

File Path

sdk\src\sample\io_demo\gpio\key\level_tim\gpio_key.c

Function Entry

gpio_key()

GPIO Direction

Input mode

Hardware Connection

As shown in GPIO Demo Code Hardware Connection Diagram. On EVB, connect P0_0 and KEY1.

Pin Definition

#define KEY_PIN     ADC_0

Expected Result

Press the Reset button on the EVB. Press KEY1: key_handler: key_status 0 debounce_hw_timer_callback: Key press will be printed in Debug Analyzer. Release KEY1: key_handler: key_status 1 debounce_hw_timer_callback: Key release will be printed in Debug Analyzer.

GPIO Demo for Key Detection by Edge Trigger Mode

The description of GPIO demo code 7 is shown in the following table.

GPIO Demo Code 7 Description

Demo 7

gpio_edge_key.c

Sample Purpose

Demonstrates GPIO used as a key by edge trigger mode with hardware debounce.

Brief Introduction

This sample code demonstrates the detection of GPIO input through interrupt. When the button is pressed (P0_0 changes from high level to low level), the falling edge of GPIO is detected, and the GPIO interrupt is triggered. Then switch the edge trigger, after the button is released (P0_0 changes from low level to high level), the GPIO interrupt is triggered again.

File Path

sdk\src\sample\io_demo\gpio\key\edge\gpio_edge_key.c

Function Entry

gpio_edge_key()

GPIO Direction

Input mode

Hardware Connection

As shown in GPIO Demo Code Hardware Connection Diagram. On EVB, connect P0_0 and KEY1.

Pin Definition

#define KEY_PIN     ADC_0

Expected Result

Press the Reset button on the EVB. Press KEY1: key_handler: Key press will be printed in Debug Analyzer. Release KEY1: key_handler: Key release will be printed in Debug Analyzer.

GPIO Demo for GDMA Mode

The description of GPIO demo code 8 is shown in the following table.

GPIO Demo Code 8 Description

Demo 8

dma_tim_demo.c

Sample Purpose

Demonstrates how GDMA controls GPIO output.

Brief Introduction

This sample code demonstrates how to use TIMER to toggle GDMA to control GPIO to output high and low levels of specified lengths.

File Path

sdk\src\sample\io_demo\gdma\multiblock_tim_gpio\dma_tim_demo.c

GPIO Direction

Output mode

GDMA Direction

Memory to Peripheral

Function Entry

ma_tim_demo()

Pin Definition

#define PIN_OUT     ADC_2

Expected Result

Press the Reset button on the EVB. Use a logic analyzer to capture the waveform of P0_2, and the output waveform results of single-block or multi-block are shown in GPIO Demo Code 8 Expected Result Diagram.

The expected result of GPIO demo code 8 is shown in the figure below.

../../../_images/GPIO_DMA_Result_Diagram.png

GPIO Demo Code 8 Expected Result Diagram

Functional Overview

The GPIO integrated core is programmable general-purpose input/output. Each GPIO pin can be configured by software as an output, input, or interrupt peripheral function.

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 both-edge-triggered interrupts.

  • Support hardware debounce function.

  • Support GPIO+TIMER+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.

Program Examples

Initialization Flow

The GPIO initialization flow is shown in the following figure.

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

GPIO Initialization Flow Chart

Input Mode

The codes below demonstrate the operation flow of GPIO in input mode.

hal_gpio_init();
hal_gpio_init_pin(TEST_Pin, GPIO_TYPE_AUTO, GPIO_DIR_INPUT, GPIO_PULL_UP);
gpio_value = hal_gpio_get_input_level(TEST_Pin);

Output Mode

The codes below demonstrate the operation flow of GPIO in output mode.

hal_gpio_init();
hal_gpio_init_pin(TEST_PIN, GPIO_TYPE_CORE, GPIO_DIR_OUTPUT, GPIO_PULL_UP);
hal_gpio_init_pin(TEST_PIN_2, GPIO_TYPE_AON, GPIO_DIR_OUTPUT, GPIO_PULL_UP);

for (uint16_t i = 0; i < 10; i++)
{
   hal_gpio_set_level(TEST_PIN, GPIO_LEVEL_LOW);
   hal_gpio_set_level(TEST_PIN_2, GPIO_LEVEL_LOW);
   hal_gpio_set_level(TEST_PIN, GPIO_LEVEL_HIGH);
   hal_gpio_set_level(TEST_PIN_2, GPIO_LEVEL_HIGH);
}

Interrupt Mode

The codes below demonstrate the operation flow of GPIO in interrupt mode.

hal_gpio_init();
hal_gpio_int_init();
hal_gpio_set_debounce_time(30);

hal_gpio_init_pin(GPIO_DEMO_INPUT_PIN0, GPIO_TYPE_AUTO, GPIO_DIR_INPUT, GPIO_PULL_UP);
hal_gpio_init_pin(GPIO_DEMO_INPUT_PIN1, GPIO_TYPE_AUTO, GPIO_DIR_INPUT, GPIO_PULL_DOWN);
hal_gpio_init_pin(GPIO_DEMO_INPUT_PIN2, GPIO_TYPE_AUTO, GPIO_DIR_INPUT, GPIO_PULL_UP);
hal_gpio_init_pin(GPIO_DEMO_INPUT_PIN3, GPIO_TYPE_AUTO, GPIO_DIR_INPUT, GPIO_PULL_DOWN);

hal_gpio_set_up_irq(GPIO_DEMO_INPUT_PIN0, GPIO_IRQ_EDGE, GPIO_IRQ_ACTIVE_LOW, true);
hal_gpio_set_up_irq(GPIO_DEMO_INPUT_PIN1, GPIO_IRQ_EDGE, GPIO_IRQ_ACTIVE_HIGH, true);
hal_gpio_set_up_irq(GPIO_DEMO_INPUT_PIN2, GPIO_IRQ_EDGE, GPIO_IRQ_ACTIVE_LOW, true);
hal_gpio_set_up_irq(GPIO_DEMO_INPUT_PIN3, GPIO_IRQ_EDGE, GPIO_IRQ_ACTIVE_HIGH, true);

hal_gpio_register_isr_callback(GPIO_DEMO_INPUT_PIN0, gpio_isr_cb, GPIO_DEMO_INPUT_PIN0);
hal_gpio_register_isr_callback(GPIO_DEMO_INPUT_PIN1, gpio_isr_cb, GPIO_DEMO_INPUT_PIN1);
hal_gpio_register_isr_callback(GPIO_DEMO_INPUT_PIN2, gpio_isr_cb, GPIO_DEMO_INPUT_PIN2);
hal_gpio_register_isr_callback(GPIO_DEMO_INPUT_PIN3, gpio_isr_cb, GPIO_DEMO_INPUT_PIN3);

hal_gpio_irq_enable(GPIO_DEMO_INPUT_PIN0);
hal_gpio_irq_enable(GPIO_DEMO_INPUT_PIN1);
hal_gpio_irq_enable(GPIO_DEMO_INPUT_PIN2);
hal_gpio_irq_enable(GPIO_DEMO_INPUT_PIN3);