GPIO

Last Updated on : 2022-11-24 09:20:18download

API list

Function name Description
gpio_raw_init Configure GPIO pins.
gpio_raw_input_read_status Read a GPIO pin.
gpio_raw_output_write_status Write to a GPIO pin.
gpio_output_init Initialize a GPIO pin as an output.
gpio_button_init Initialize a GPIO pin as an input.

API description

gpio_raw_init

Function prototype

extern void gpio_raw_init(gpio_config_t pin);

Parameter description

Parameter name Parameter type Description
pin gpio_config_t The values in the gpio_config_t enum.

gpio_raw_input_read_status

Function prototype

extern uint8_t gpio_raw_input_read_status( GPIO_PORT_T port, GPIO_PIN_T pin);;

Parameter description

Parameter name Parameter type Description
port GPIO_PORT_T The values in the GPIO_PORT_T enum.
pin GPIO_PIN_T The values in the GPIO_PIN_T enum.

gpio_raw_output_write_status

Function prototype

extern void gpio_raw_output_write_status(  GPIO_PORT_T port, GPIO_PIN_T pin, uint8_t value);

Parameter description

Parameter name Parameter type Description
port GPIO_PORT_T The values in the GPIO_PORT_T enum.
pin GPIO_PIN_T The values in the GPIO_PIN_T enum.
pin uint8_t
  • 0: low level
  • 1: high level

gpio_output_init

Function prototype

void gpio_output_init(gpio_config_t *config, uint8_t sum);

Parameter description

Parameter name Parameter type Description
*config gpio_config_t GPIO configuration.
sum uint8 The number of configured GPIO pins.

gpio_button_init

Function prototype

void gpio_button_init(gpio_config_t *config, uint8_t sum, uint32_t jitter_time, key_func_t key_func);

Parameter description

Parameter name Parameter type Description
*config gpio_config_t GPIO configuration.
sum uint8 The number of configured GPIO pins.
jitter_time uint32_t Debounce time for buttons.
key_func key_func_t Key press interrupt callback.

Struct description

gpio_port_t

Struct prototype

// GPIO port definition.
typedef enum {
    PORT_A = 0x00,
    PORT_B,
    PORT_C,
    PORT_D,
    PORT_E,
    PORT_F,
    PORT_H,
    PORT_I,
    PORT_J,
    PORT_K
}GPIO_PORT_T;

gpio_pin_t

Struct prototype

// GPIO pin definition.
typedef enum {
    PIN_0 = 0x00,
    PIN_1,
    PIN_2,
    PIN_3,
    PIN_4,
    PIN_5,
    PIN_6,
    PIN_7,
    PIN_8,
    PIN_9,
    PIN_10,
    PIN_11,
    PIN_12,
    PIN_13,
    PIN_14,
    PIN_15,
}GPIO_PIN_T;

gpio_mode_t

Struct prototype

// GPIO mode definition.
typedef enum {
    GPIO_MODE_INPUT_HIGH_IMPEDANCE = 0,
    GPIO_MODE_INPUT_PULL,
    GPIO_MODE_OUTPUT_PP,
    GPIO_MODE_OUTPUT_OD,
    GPIO_MODE_OUTPUT_OD_PULL_UP,
    GPIO_MODE_OUTPUT_OD_PULL_DOWN,
}GPIO_MODE_T;

gpio_config_t;

Struct prototype

// GPIO configuration.
typedef struct {
    GPIO_PORT_T port;//
    GPIO_PIN_T pin;//
    GPIO_MODE_T mode;// Input or output mode.
    GPIO_DOUT_T out;// Output level.
    GPIO_LEVEL_T drive_flag; // Active high or low.
} gpio_config_t;

Sample code

The hal_gpio.h file details the API definitions. You can refer to the following code snippets for some examples of how to configure the GPIOs.

Basic configuration

#define LED_1_PORT  PORT_A
#define LED_1_PIN   PIN_4
#define LED_1_MODE  GPIO_MODE_OUTPUT_PP /// Configure the GPIO as output.
#define LED_1_DOUT  GPIO_DOUT_LOW       /// Output the default level on the first initialization.
#define LED_1_DRIVE GPIO_DOUT_HIGH      /// Active high, used for advanced configuration.

#define KEY_1_PORT  PORT_A
#define KEY_1_PIN   PIN_3
#define KEY_1_MODE  GPIO_MODE_INPUT_PULL /// Input pull-up or pull-down, depending on `KEY_1_DOUT`.
#define KEY_1_DOUT  GPIO_DOUT_HIGH       /// Input pull-up
#define KEY_1_DRIVE GPIO_LEVEL_LOW       /// Active low, used for advanced configuration.

static void __gpio_int_func_t(GPIO_PORT_T port, GPIO_PIN_T pin)
{
    uint8_t vol_level = gpio_raw_input_read_status(KEY_1_PORT, KEY_1_PIN); /// Read the input signal.
}

static void gpio_demo(void)
{
    const gpio_config_t gpio_ouput_config = {
        LED_1_PORT, LED_1_PIN, LED_1_MODE, LED_1_DOUT, LED_1_DRIVE,
    };  

        const gpio_config_t gpio_input_config = {
        KEY_1_PORT, KEY_1_PIN, KEY_1_MODE, KEY_1_DOUT, KEY_1_DRIVE,
    };

    gpio_raw_init(gpio_ouput_config);       /// Configure PA4 as output, defaulting to output low.

    gpio_raw_output_write_status(LED_1_PORT, LED_1_PIN, 1); /// Change the output level to high level.

    gpio_raw_init(gpio_inputput_config);    /// Configure PA3 as input, input pull-up, and non-interrupt mode.
    gpio_int_register(&gpio_inputput_config, __gpio_int_func_t); /// Configure PA3 as input, input pull-up, and falling edge interrupt mode.
}

Advanced configuration


#define LED_1_PORT  PORT_A
#define LED_1_PIN   PIN_4
#define LED_1_MODE  GPIO_MODE_OUTPUT_PP /// Configure the GPIO as output.
#define LED_1_DOUT  GPIO_DOUT_LOW       /// Output the default level on the first initialization.
#define LED_1_DRIVE GPIO_DOUT_HIGH      /// Active high, used for advanced configuration.

#define LED_2_PORT  PORT_D
#define LED_2_PIN   PIN_15
#define LED_2_MODE  GPIO_MODE_OUTPUT_PP /// Configure the GPIO as output.
#define LED_2_DOUT  GPIO_DOUT_LOW       /// Output the default level on the first initialization.
#define LED_2_DRIVE GPIO_DOUT_HIGH      /// Active high, used for advanced configuration.

#define KEY_1_PORT  PORT_A
#define KEY_1_PIN   PIN_3
#define KEY_1_MODE  GPIO_MODE_INPUT_PULL /// Input pull-up or pull-down, depending on `KEY_1_DOUT`.
#define KEY_1_DOUT  GPIO_DOUT_HIGH       /// Input pull-up
#define KEY_1_DRIVE GPIO_LEVEL_LOW       /// Active low, used for advanced configuration.

#define KEY_2_PORT  PORT_A
#define KEY_2_PIN   PIN_5
#define KEY_2_MODE  GPIO_MODE_INPUT_PULL /// Input pull-up or pull-down, depending on `KEY_1_DOUT`.
#define KEY_2_DOUT  GPIO_DOUT_HIGH       ///  Input pull-up
#define KEY_2_DRIVE GPIO_LEVEL_LOW       /// Active low, used for advanced configuration.

#define KEY_1_INDEX 0   /// Index of initializing array.
#define KEY_2_INDEX 1

#define LED_1_INDEX 0   /// Index of initializing array.
#define LED_2_INDEX 1

static void __dev_key_handle(uint32_t key_id, key_st_t key_st, uint32_t push_time)
{
    switch(key_id) {
        case KEY_1_INDEX: {
            if(key_st == KEY_ST_PUSH) { /// The button is being pressed.
                if(push_time == 3000) { /// After the button is pressed and held for 3,000 ms, the LED2 starts blinking.
                     dev_led_start_blink(LED_2_INDEX, 500, 500, DEV_LED_BLINK_FOREVER, DEV_IO_OFF); /// The LED1 alternately outputs high for 500 ms and low for 300 ms to emit blinking signals.
                }
            }
            else {
                if(push_time < 3000) { /// If the button is pressed and held for less than 3,000 ms, it is recognized as a short press.
                    //TODO:
                }
                else {
                    dev_led_stop_blink(LED_2_INDEX, DEV_IO_OFF); /// The LED2 stops blinking and comes off.
                    //TODO:
                }
            }
            break;
        }

                case KEY_2_INDEX: {
            //TODO:
            break;
        }

                default: {
            break;
        }
    }
}

static void gpio_demo(void)
{
    gpio_config_t gpio_ouput_config[] = {
        {LED_1_PORT, LED_1_PIN, LED_1_MODE, LED_1_DOUT, LED_1_DRIVE},
        {LED_2_PORT, LED_2_PIN, LED_2_MODE, LED_2_DOUT, LED_2_DRIVE},
    };  

        gpio_config_t gpio_input_config[] = {
        {KEY_1_PORT, KEY_1_PIN, KEY_1_MODE, KEY_1_DOUT, KEY_1_DRIVE},
        {KEY_2_PORT, KEY_2_PIN, KEY_2_MODE, KEY_2_DOUT, KEY_2_DRIVE},
    };

    gpio_button_init((gpio_config_t *)gpio_input_config, get_array_len(gpio_input_config), 50, __dev_key_handle); /// Initialize the button, with 50 ms dithering time.
    gpio_output_init((gpio_config_t *)gpio_ouput_config, get_array_len(gpio_ouput_config)); /// Initialize the output. You can use advanced functions as needed.

    dev_led_start_blink(LED_1_INDEX, 500, 300, DEV_LED_BLINK_FOREVER, DEV_IO_OFF); /// The LED1 alternately outputs high for 500 ms and low for 300 ms to emit blinking signals.
}