Loading...
Searching...
No Matches
gpio_ll.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2020 Gunar Schorcht
3 * 2021 Otto-von-Guericke-Universität Magdeburg
4 *
5 * This file is subject to the terms and conditions of the GNU Lesser
6 * General Public License v2.1. See the file LICENSE in the top level
7 * directory for more details.
8 */
9
69#ifndef PERIPH_GPIO_LL_H
70#define PERIPH_GPIO_LL_H
71
72#include <assert.h>
73#include <stdbool.h>
74#include <stdint.h>
75
76#include "architecture.h"
77#include "periph/gpio.h"
78#include "periph_cpu.h"
79
80#ifdef __cplusplus
81extern "C" {
82#endif
83
87typedef uintptr_t gpio_port_t;
88
89#if !defined(GPIO_PORT_UNDEF) || defined(DOXYGEN)
93#define GPIO_PORT_UNDEF UINTPTR_MAX
94#endif
95
96#ifdef DOXYGEN
106#define GPIO_PORT(num) implementation_specific
107#endif
108
109#ifdef DOXYGEN
126#define GPIO_PORT_NUM(port) implementation_specific
127#endif
128
129#if !defined(HAVE_GPIO_STATE_T) || defined(DOXYGEN)
219#endif
220
221#if !defined(HAVE_GPIO_PULL_T) || defined(DOXYGEN)
232#endif
233
234#if !defined(HAVE_GPIO_PULL_STRENGTH_T) || defined(DOXYGEN)
249#endif
250
261#define GPIO_PULL_NUMOF (1U + (GPIO_PULL_WEAKEST != GPIO_PULL_WEAK) \
262 + (GPIO_PULL_WEAK != GPIO_PULL_STRONG) \
263 + (GPIO_PULL_STRONG != GPIO_PULL_STRONGEST))
264
265#if !defined(HAVE_GPIO_DRIVE_STRENGTH_T) || defined(DOXYGEN)
280#endif
281
290#define GPIO_DRIVE_NUMOF (1U + (GPIO_DRIVE_WEAKEST != GPIO_DRIVE_WEAK) \
291 + (GPIO_DRIVE_WEAK != GPIO_DRIVE_STRONG) \
292 + (GPIO_DRIVE_STRONG != GPIO_DRIVE_STRONGEST))
293
294#if !defined(HAVE_GPIO_SLEW_T) || defined(DOXYGEN)
315#endif
316
325#define GPIO_SLEW_NUMOF (1U + (GPIO_SLEW_SLOWEST != GPIO_SLEW_SLOW) \
326 + (GPIO_SLEW_SLOW != GPIO_SLEW_FAST) \
327 + (GPIO_SLEW_FAST != GPIO_SLEW_FASTEST))
328
347 uint8_t bits;
348 struct {
371 uint8_t : 2; /*< padding */
372 };
373};
374
375#if !defined(HAVE_GPIO_CONF_T) && !defined(DOXYGEN)
376typedef union gpio_conf_minimal gpio_conf_t;
377#endif
378
379#ifdef DOXYGEN
391typedef /* implementation specific */ gpio_conf_t;
392#endif
393
394#ifndef __cplusplus
417static const gpio_conf_t gpio_ll_in = {
418 .state = GPIO_INPUT,
419 .pull = GPIO_FLOATING,
420};
421
427 .state = GPIO_INPUT,
428 .pull = GPIO_PULL_DOWN,
429};
430
436 .state = GPIO_INPUT,
437 .pull = GPIO_PULL_UP,
438};
439
451 .state = GPIO_INPUT,
452 .pull = GPIO_PULL_KEEP,
453};
454
460static const gpio_conf_t gpio_ll_out = {
461 .state = GPIO_OUTPUT_PUSH_PULL,
462 .initial_value = false,
463};
464
471static const gpio_conf_t gpio_ll_od = {
472 .state = GPIO_OUTPUT_OPEN_DRAIN,
473 .pull = GPIO_FLOATING,
474 .initial_value = true,
475};
476
485 .state = GPIO_OUTPUT_OPEN_DRAIN,
486 .pull = GPIO_PULL_UP,
487 .initial_value = true,
488};
490#endif
491
499static inline bool is_gpio_port_num_valid(uint_fast8_t num);
500
538int gpio_ll_init(gpio_port_t port, uint8_t pin, gpio_conf_t conf);
539
555
565
571
587static inline uword_t gpio_ll_read(gpio_port_t port);
588
605
621static inline void gpio_ll_set(gpio_port_t port, uword_t mask);
622
638static inline void gpio_ll_clear(gpio_port_t port, uword_t mask);
639
655static inline void gpio_ll_toggle(gpio_port_t port, uword_t mask);
656
657#if defined(DOXYGEN) || !defined(HAVE_GPIO_LL_PREPARE_WRITE_ALL_PINS)
681 uword_t value)
682{
683 (void)port;
684 return value;
685}
686#endif
687
688#if defined(DOXYGEN) || !defined(HAVE_GPIO_LL_PREPARE_WRITE)
703 uword_t value)
704{
705 return value | (gpio_ll_read_output(port) & (~mask));
706}
707#endif
708
724static inline void gpio_ll_switch_dir_output(gpio_port_t port, uword_t outputs);
725
748static inline void gpio_ll_switch_dir_input(gpio_port_t port, uword_t inputs);
749
791static inline void gpio_ll_write(gpio_port_t port, uword_t state);
792
796static inline gpio_port_t gpio_get_port(gpio_t pin);
797
801static inline uint8_t gpio_get_pin_num(gpio_t pin);
802
814static inline gpio_port_t gpio_port_pack_addr(void *addr);
815
828static inline void * gpio_port_unpack_addr(gpio_port_t port);
829
830#ifndef DOXYGEN
831#if !MODULE_PERIPH_GPIO_LL_SWITCH_DIR
832static inline void gpio_ll_switch_dir_output(gpio_port_t port, uword_t outputs)
833{
834 (void)port;
835 (void)outputs;
836 /* Hack: If this function is only used guarded by some
837 *
838 * if (IS_USED(MODULE_PERIPH_GPIO_LL_SWITCH_DIR)) {
839 * ...
840 * }
841 *
842 * as intended, all calls to the following fake function will be optimized
843 * due to the elimination of dead branches. If used incorrectly, a linking
844 * failure will be the result. The error message will not be ideal, but a
845 * compile time error is much better than a runtime error.
846 */
847 extern void gpio_ll_switch_dir_output_used_but_feature_gpio_ll_switch_dir_is_not_provided(void);
848 gpio_ll_switch_dir_output_used_but_feature_gpio_ll_switch_dir_is_not_provided();
849}
850
851static inline void gpio_ll_switch_dir_input(gpio_port_t port, uword_t inputs)
852{
853 (void)port;
854 (void)inputs;
855 /* Same hack as above */
856 extern void gpio_ll_switch_dir_input_used_but_feature_gpio_ll_switch_dir_is_not_provided(void);
857 gpio_ll_switch_dir_input_used_but_feature_gpio_ll_switch_dir_is_not_provided();
858}
859
860#endif /* !MODULE_PERIPH_GPIO_LL_SWITCH_DIR */
861#endif /* !DOXYGEN */
862
863#ifdef __cplusplus
864}
865#endif
866
867/* the hardware specific implementation relies on the types such as gpio_port_t
868 * to be provided */
869#include "gpio_ll_arch.h" /* IWYU pragma: export */
870
871#endif /* PERIPH_GPIO_LL_H */
Platform-independent access to architecture details.
POSIX.1-2008 compliant version of the assert macro.
Low-level GPIO peripheral driver interface definitions.
static const gpio_conf_t gpio_ll_in_pk
A standard configuration for a generic input pin with pull resistor to keep signal at bus level.
Definition gpio_ll.h:450
static uint8_t gpio_get_pin_num(gpio_t pin)
Extract the pin number from a gpio_t
static void gpio_ll_switch_dir_output(gpio_port_t port, uword_t outputs)
Turn GPIO pins specified by the bitmask outputs to outputs.
static void gpio_ll_set(gpio_port_t port, uword_t mask)
Perform an reg |= mask operation on the I/O register of the port.
gpio_pull_t
Enumeration of pull resistor configurations.
Definition gpio_ll.h:225
static void gpio_ll_switch_dir_input(gpio_port_t port, uword_t inputs)
Turn GPIO pins specified by the bitmask inputs to inputs.
gpio_pull_strength_t
Enumeration of pull resistor values.
Definition gpio_ll.h:243
static const gpio_conf_t gpio_ll_in_pd
A standard configuration for a generic input pin with pull down resistor.
Definition gpio_ll.h:426
static const gpio_conf_t gpio_ll_out
A standard configuration for a generic push-pull output pin.
Definition gpio_ll.h:460
gpio_state_t
Enumeration of GPIO states (direction)
Definition gpio_ll.h:133
gpio_slew_t
Enumeration of slew rate settings.
Definition gpio_ll.h:307
int gpio_ll_init(gpio_port_t port, uint8_t pin, gpio_conf_t conf)
Initialize the given GPIO pin as specified.
static uword_t gpio_ll_prepare_write(gpio_port_t port, uword_t mask, uword_t value)
Helper to use gpio_ll_write side-effect free.
Definition gpio_ll.h:702
static gpio_port_t gpio_port_pack_addr(void *addr)
Pack a pointer into a gpio_port_t.
gpio_conf_t gpio_ll_query_conf(gpio_port_t port, uint8_t pin)
Retrieve the current configuration of a GPIO pin.
void gpio_ll_print_conf_common(const gpio_conf_t conf)
INTERNAL, use gpio_ll_print_conf instead.
gpio_drive_strength_t
Enumeration of drive strength options.
Definition gpio_ll.h:274
static const gpio_conf_t gpio_ll_in_pu
A standard configuration for a generic input pin with pull up resistor.
Definition gpio_ll.h:435
static uword_t gpio_ll_read(gpio_port_t port)
Get the current input value of all GPIO pins of the given port as bitmask.
static gpio_port_t gpio_get_port(gpio_t pin)
Extract the gpio_port_t from a gpio_t
static uword_t gpio_ll_prepare_write_all_outputs(gpio_port_t port, uword_t value)
Same as gpio_ll_prepare_write(port, UWORD_MAX, value), but faster.
Definition gpio_ll.h:680
typedef gpio_conf_t
GPIO pin configuration.
Definition gpio_ll.h:391
static const gpio_conf_t gpio_ll_in
A standard configuration for a generic floating input pin.
Definition gpio_ll.h:417
static const gpio_conf_t gpio_ll_od_pu
A standard configuration for a generic open drain output with pull up.
Definition gpio_ll.h:484
static void * gpio_port_unpack_addr(gpio_port_t port)
Extract a data pointer that was packed by gpio_port_pack_addr.
static bool is_gpio_port_num_valid(uint_fast8_t num)
Check if the given number is a valid argument for GPIO_PORT.
static uword_t gpio_ll_read_output(gpio_port_t port)
Get the current output value of all GPIO pins of the given port as bitmask.
static void gpio_ll_clear(gpio_port_t port, uword_t mask)
Perform an reg &= ~mask operation on the I/O register of the port.
static void gpio_ll_toggle(gpio_port_t port, uword_t mask)
Perform an reg ^= mask operation on the I/O register of the port.
static void gpio_ll_write(gpio_port_t port, uword_t state)
Perform a masked write operation on the I/O register of the port.
static const gpio_conf_t gpio_ll_od
A standard configuration for a generic floating open drain output.
Definition gpio_ll.h:471
uintptr_t gpio_port_t
GPIO port type.
Definition gpio_ll.h:87
void gpio_ll_print_conf(const gpio_conf_t conf)
Utility function to print a given GPIO configuration to stdio.
@ GPIO_FLOATING
No pull ups nor pull downs enabled.
Definition gpio_ll.h:226
@ GPIO_PULL_KEEP
Keep the signal at current logic level with pull up/down resistors.
Definition gpio_ll.h:229
@ GPIO_PULL_DOWN
Pull down resistor enabled.
Definition gpio_ll.h:228
@ GPIO_PULL_UP
Pull up resistor enabled.
Definition gpio_ll.h:227
@ GPIO_PULL_WEAKEST
Use the weakest (highest Ohm value) resistor.
Definition gpio_ll.h:244
@ GPIO_PULL_WEAK
Use a weak pull resistor.
Definition gpio_ll.h:245
@ GPIO_PULL_STRONG
Use a strong pull resistor.
Definition gpio_ll.h:246
@ GPIO_PULL_STRONGEST
Use the strongest pull resistor.
Definition gpio_ll.h:247
@ GPIO_OUTPUT_OPEN_SOURCE
Use pin as output in open emitter configuration.
Definition gpio_ll.h:170
@ GPIO_USED_BY_PERIPHERAL
The GPIO pin is used by a peripheral.
Definition gpio_ll.h:189
@ GPIO_OUTPUT_OPEN_DRAIN
Use pin as output in open collector configuration.
Definition gpio_ll.h:157
@ GPIO_OUTPUT_PUSH_PULL
Use pin as output in push-pull configuration.
Definition gpio_ll.h:144
@ GPIO_DISCONNECT
Disconnect pin from all peripherals.
Definition gpio_ll.h:217
@ GPIO_INPUT
Use pin as input.
Definition gpio_ll.h:176
@ GPIO_SLEW_SLOWEST
let the output voltage level rise/fall as slow as possible
Definition gpio_ll.h:308
@ GPIO_SLEW_FAST
let the output voltage level rise/fall fast
Definition gpio_ll.h:311
@ GPIO_SLEW_SLOW
let the output voltage level rise/fall slowly
Definition gpio_ll.h:310
@ GPIO_SLEW_FASTEST
let the output voltage level rise/fall as fast as possible
Definition gpio_ll.h:312
@ GPIO_DRIVE_STRONG
Use a strong drive strength.
Definition gpio_ll.h:277
@ GPIO_DRIVE_WEAK
Use a weak drive strength.
Definition gpio_ll.h:276
@ GPIO_DRIVE_STRONGEST
Use the strongest drive strength.
Definition gpio_ll.h:278
@ GPIO_DRIVE_WEAKEST
Use the weakest drive strength.
Definition gpio_ll.h:275
uint< NUM > _t uword_t
Word sized unsigned integer.
Public members of gpio_conf_t
Definition gpio_ll.h:346
uint8_t bits
The raw bits of the configuration.
Definition gpio_ll.h:347
gpio_pull_t pull
Pull resistor configuration.
Definition gpio_ll.h:356
bool initial_value
Initial value of the output.
Definition gpio_ll.h:370
gpio_state_t state
State of the pin.
Definition gpio_ll.h:352