GPIO
Examples
#include "abov/hal/gpio.h"
#include "abov/delay.h"
#define LED_PORT PERIPH_GPIOD
#define LED_PIN 1
int main(void)
{
#if 0
gpio_open_output(LED_PORT, LED_PIN, GPIO_MODE_PUSHPULL);
#else
struct gpio_cfg cfg = { GPIO_MODE_PUSHPULL, };
gpio_open(LED_PORT, LED_PIN, &cfg);
#endif
while (1) {
gpio_write(LED_PORT, LED_PIN, 1);
udelay(500000);
gpio_write(LED_PORT, LED_PIN, 0);
udelay(500000);
}
return 0;
}
HAL
Functions
-
void gpio_open(periph_t port, uint32_t pin, const struct gpio_cfg *cfg)
Initialize the given GPIO pin to the specified mode.
- Parameters
port – [in] GPIO port enumerated in periph_t
pin – [in] GPIO number starting from 0
cfg – [in] configuration
- Returns
true on success
-
void gpio_open_output(periph_t port, uint32_t pin, gpio_mode_t mode)
Initialize the given GPIO pin to the specified mode.
- Parameters
port – [in] GPIO port enumerated in periph_t
pin – [in] GPIO number starting from 0
mode – [in] sets gpio operation mode
- Returns
true on success
-
struct gpio_cfg
- #include <gpio.h>
GPIO configuration
Public Members
-
gpio_mode_t mode
-
gpio_irq_t irq
-
gpio_speed_t speed
-
bool altfunc
-
int altfunc_number
-
uint32_t debounce
-
gpio_mode_t mode
LL
Enums
-
enum gpio_mode_t
GPIO mode
Values:
-
enumerator GPIO_MODE_PUSHPULL
Output only mode
-
enumerator GPIO_MODE_INPUT
Input only mode
-
enumerator GPIO_MODE_INPUT_PULLUP
Input only mode with pull-up function
-
enumerator GPIO_MODE_INPUT_PULLDOWN
Input only mode with pull-down function
-
enumerator GPIO_MODE_OPENDRAIN
Input/output open-drain mode
-
enumerator GPIO_MODE_OPENDRAIN_PULLUP
Input/output open-drain mode with pull-up function
-
enumerator GPIO_MODE_OPENDRAIN_PULLDOWN
Input/output open-drain mode with pull-down function
-
enumerator GPIO_MODE_ANALOG
Analog function
-
enumerator GPIO_MODE_PUSHPULL
-
enum gpio_irq_t
GPIO interrupt type
Values:
-
enumerator GPIO_IRQ_NONE
-
enumerator GPIO_IRQ_EDGE_RISING
Rising edge interrupt
-
enumerator GPIO_IRQ_EDGE_FALLING
Falling edge interrupt
-
enumerator GPIO_IRQ_EDGE_ANY
Both rising and falling edge interrupts
-
enumerator GPIO_IRQ_LEVEL_HIGH
Logic level high interrupt
-
enumerator GPIO_IRQ_LEVEL_LOW
Logic level low interrupt
-
enumerator GPIO_IRQ_NONE
Functions
-
void gpio_reset(periph_t port)
Reset GPIO port.
This function makes GPIO port the reset state.
- Parameters
port – [in] GPIO port enumerated in periph_t
-
void gpio_enable_port(periph_t port)
Enable the given port.
- Parameters
port – [in] GPIO port enumerated in periph_t
-
void gpio_disable_port(periph_t port)
Disable the given port.
- Parameters
port – [in] GPIO port enumerated in periph_t
-
void gpio_set_mode(periph_t port, uint32_t pin, gpio_mode_t mode)
-
void gpio_set_altfunc(periph_t port, uint32_t pin, int altfunc)
Select GPIO alternate function.
Call this function after :c:func:
gpio_set_mode- Parameters
port – [in] GPIO port enumerated in periph_t
pin – [in] GPIO number starting from 0
altfunc – [in] alternate function number
-
void gpio_set_speed(periph_t port, uint32_t pin, gpio_speed_t speed)
Note
Call this function after gpio_set_altfunc
-
void gpio_set_debouncer(periph_t port, uint32_t pin, uint32_t pclk_clocks)
- Parameters
pclk_clocks – [in] pass 0 clock cycle to disable
-
void gpio_enable_irq(periph_t port, uint32_t pin, gpio_irq_t irq_type)
Enable interrupt on the given GPIO pin.
- Parameters
port – [in] GPIO port enumerated in periph_t
pin – [in] GPIO number starting from 0
irq_type – [in] sets interrupt trigger type
-
void gpio_disable_irq(periph_t port, uint32_t pin)
Disable interrupt on the given GPIO pin.
- Parameters
port – [in] GPIO port enumerated in periph_t
pin – [in] GPIO number starting from 0
-
void gpio_clear_event(periph_t port, uint32_t pin)
Clear interrupt flag on the given GPIO pin.
- Parameters
port – [in] GPIO port enumerated in periph_t
pin – GPIO number starting from 0
-
void gpio_write_pin(periph_t port, uint32_t pin, int value)
Write output level to the given GPIO pin.
- Parameters
port – [in] GPIO port enumerated in periph_t
pin – [in] GPIO number starting from 0
value – [out] logic output level. Only 0(low) and 1(high) are possible
-
int gpio_read_pin(periph_t port, uint32_t pin)
Read the current input level from the given GPIO pin.
- Parameters
port – [in] GPIO port enumerated in periph_t
pin – [in] GPIO number starting from 0
- Returns
0 when the logic level is low. 1 when the logic level is high