hal_gpio.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2020 Inria
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
9 #pragma once
10 
22 #include "periph/gpio.h"
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
31 enum {
38 };
43 
47 enum {
48 #ifdef GPIO_NONE
49  HAL_GPIO_TRIG_NONE = GPIO_NONE,
50 #endif
52  HAL_GPIO_TRIG_RISING = GPIO_RISING,
54  HAL_GPIO_TRIG_FALLING = GPIO_FALLING,
56  HAL_GPIO_TRIG_BOTH = GPIO_BOTH,
58 #ifdef GPIO_LOW
59  HAL_GPIO_TRIG_LOW = GPIO_LOW,
60 #endif
62 #ifdef GPIO_HIGH
63  HAL_GPIO_TRIG_HIGH = GPIO_HIGH
64 #endif
65 };
70 
75 
84 static inline int hal_gpio_init_in(gpio_t pin, hal_gpio_pull_t pull)
85 {
86  return gpio_init(pin, pull);
87 }
88 
98 static inline int hal_gpio_init_out(gpio_t pin, int val)
99 {
100  int res = gpio_init(pin, GPIO_OUT);
101  gpio_write(pin, val);
102  return res;
103 }
104 
111 static inline void hal_gpio_write(gpio_t pin, int val)
112 {
113  gpio_write(pin, val);
114 }
115 
123 static inline int hal_gpio_read(gpio_t pin)
124 {
125  return gpio_read(pin);
126 }
127 
135 static inline int hal_gpio_toggle(gpio_t pin)
136 {
137  gpio_toggle(pin);
138  return gpio_read(pin);
139 }
140 
152 static inline int hal_gpio_irq_init(gpio_t pin,
153  hal_gpio_irq_handler_t handler,
154  void *arg,
155  hal_gpio_irq_trig_t trig,
156  hal_gpio_pull_t pull)
157 {
158  return gpio_init_int(pin, pull, trig, handler, arg);
159 }
160 
166 static inline void hal_gpio_irq_release(gpio_t pin)
167 {
168  /* can't release the interrupt so ar least disable it */
169  gpio_irq_disable(pin);
170 }
171 
177 static inline void hal_gpio_irq_enable(gpio_t pin)
178 {
179  gpio_irq_enable(pin);
180 }
181 
187 static inline void hal_gpio_irq_disable(gpio_t pin)
188 {
189  gpio_irq_disable(pin);
190 }
191 
192 #ifdef __cplusplus
193 }
194 #endif
gpio_flank_t
Definition: periph_cpu.h:176
@ GPIO_OUT
select GPIO MASK as output
Definition: periph_cpu.h:161
@ GPIO_IN
select GPIO MASK as input
Definition: periph_cpu.h:160
Low-level GPIO peripheral driver interface definitions.
gpio_mode_t
Available pin modes.
Definition: periph_cpu.h:88
void gpio_toggle(gpio_t pin)
Toggle the value of the given pin.
void(* gpio_cb_t)(void *arg)
Signature of event callback functions triggered from interrupts.
Definition: gpio.h:146
void gpio_write(gpio_t pin, bool value)
Set the given pin to the given value.
int gpio_init_int(gpio_t pin, gpio_mode_t mode, gpio_flank_t flank, gpio_cb_t cb, void *arg)
Initialize a GPIO pin for external interrupt usage.
void gpio_irq_disable(gpio_t pin)
Disable the pin interrupt if configured as interrupt source.
void gpio_irq_enable(gpio_t pin)
Enable pin interrupt if configured as interrupt source.
bool gpio_read(gpio_t pin)
Get the current value of the given pin.
int gpio_init(gpio_t pin, gpio_mode_t mode)
Initialize the given pin as general purpose input or output.
@ GPIO_IN_PU
configure as input with pull-up resistor
Definition: gpio.h:121
@ GPIO_IN_PD
configure as input with pull-down resistor
Definition: gpio.h:120
static int hal_gpio_init_in(gpio_t pin, hal_gpio_pull_t pull)
Initializes the specified pin as an input.
Definition: hal_gpio.h:84
static int hal_gpio_irq_init(gpio_t pin, hal_gpio_irq_handler_t handler, void *arg, hal_gpio_irq_trig_t trig, hal_gpio_pull_t pull)
Initialize a given pin to trigger a GPIO IRQ callback.
Definition: hal_gpio.h:152
static void hal_gpio_irq_enable(gpio_t pin)
Enable IRQs on the passed pin.
Definition: hal_gpio.h:177
static void hal_gpio_irq_release(gpio_t pin)
Release a pin from being configured to trigger IRQ on state change.
Definition: hal_gpio.h:166
static int hal_gpio_read(gpio_t pin)
Reads the specified pin.
Definition: hal_gpio.h:123
gpio_cb_t hal_gpio_irq_handler_t
Function proto for GPIO irq handler functions.
Definition: hal_gpio.h:74
static void hal_gpio_write(gpio_t pin, int val)
Write a value (either high or low) to the specified pin.
Definition: hal_gpio.h:111
gpio_flank_t hal_gpio_irq_trig_t
hal_gpio_irq_trig type
Definition: hal_gpio.h:69
static int hal_gpio_toggle(gpio_t pin)
Toggles the specified pin.
Definition: hal_gpio.h:135
static int hal_gpio_init_out(gpio_t pin, int val)
Initialize the specified pin as an output, setting the pin to the specified value.
Definition: hal_gpio.h:98
@ HAL_GPIO_TRIG_FALLING
IRQ occurs on falling edge.
Definition: hal_gpio.h:54
@ HAL_GPIO_TRIG_RISING
IRQ occurs on rising edge.
Definition: hal_gpio.h:52
@ HAL_GPIO_TRIG_BOTH
IRQ occurs on either edge.
Definition: hal_gpio.h:56
@ HAL_GPIO_PULL_NONE
Pull-up/down not enabled.
Definition: hal_gpio.h:33
@ HAL_GPIO_PULL_DOWN
Pull-down enabled.
Definition: hal_gpio.h:37
@ HAL_GPIO_PULL_UP
Pull-up enabled.
Definition: hal_gpio.h:35
static void hal_gpio_irq_disable(gpio_t pin)
Disable IRQs on the passed pin.
Definition: hal_gpio.h:187
gpio_mode_t hal_gpio_pull_t
hal_gpio_pull type
Definition: hal_gpio.h:42