gpio_ll_arch.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2022 Christian Amsüss
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
37#ifndef GPIO_LL_ARCH_H
38#define GPIO_LL_ARCH_H
39
40#include "cpu.h"
41#include "periph_cpu.h"
42
43#include "em_gpio.h"
44
45#ifdef __cplusplus
46extern "C" {
47#endif
48
49#ifndef DOXYGEN /* hide implementation specific details from Doxygen */
50
51/* We could do
52 *
53#define GPIO_PORT(num) (GPIO->P[num])
54#define GPIO_PORT_NUM(port) ((port - &GPIO->P))
55 *
56 * which forks for some operations, but at latest when _ll_set needs to fan out
57 * for some EFM32 families to
58 *
59#if defined(_GPIO_P_DOUTSET_MASK)
60 GPIO->P[port].DOUTSET = pins;
61#elif defined(GPIO_HAS_SET_CLEAR)
62 GPIO->P_SET[port].DOUT = pins;
63#else
64 (some bit-banding-style interaction on P)
65#endif
66 *
67 * that approach becomes an unbearable burden, because P_SET is not necessarily
68 * as large as P, and getting from a P pointer to a P_SET pointer would involve
69 * division and multiplication. Instead, falling back to addressing ports by
70 * their index number, which does require an additional multiplication for most
71 * accesses, but at least does that consistently.
72 *
73 * (It also makes things easier because it allows going through the helper
74 * functions).
75 *
76 * There appears to be one truly viable alternative: implementing gpio_ll only
77 * for those EFM32 that do have DOUTSET etc. in P, with no way of having such
78 * an implementation for other EFM32 families. For the time being, the
79 * suboptimal-but-works-for-all version is the best we have.
80 */
81
82#define GPIO_PORT(num) (num)
83#define GPIO_PORT_NUM(port) (port)
84
85static inline uword_t gpio_ll_read(gpio_port_t port)
86{
87 return GPIO_PortInGet(port);
88}
89
90static inline uword_t gpio_ll_read_output(gpio_port_t port)
91{
92 return GPIO_PortOutGet(port);
93}
94
95static inline void gpio_ll_set(gpio_port_t port, uword_t mask)
96{
97 GPIO_PortOutSet(port, mask);
98}
99
100static inline void gpio_ll_clear(gpio_port_t port, uword_t mask)
101{
102 GPIO_PortOutClear(port, mask);
103}
104
105static inline void gpio_ll_toggle(gpio_port_t port, uword_t mask)
106{
107 GPIO_PortOutToggle(port, mask);
108}
109
110static inline void gpio_ll_write(gpio_port_t port, uword_t value)
111{
112 GPIO->P[port].DOUT = value;
113}
114
115static inline gpio_port_t gpio_get_port(gpio_t pin)
116{
117 return (pin >> 4);
118}
119
120static inline uint8_t gpio_get_pin_num(gpio_t pin)
121{
122 return (pin & 0x0f);
123}
124
125static inline gpio_port_t gpio_port_pack_addr(void *addr)
126{
127 return (gpio_port_t)addr;
128}
129
130static inline bool is_gpio_port_num_valid(uint_fast8_t num)
131{
132 return GPIO_PORT_VALID(num);
133}
134
135static inline void * gpio_port_unpack_addr(gpio_port_t port)
136{
137 if ((port & ~0xff) == 0 && is_gpio_port_num_valid(port)) {
138 return NULL;
139 }
140
141 return (void *)port;
142}
143
144#endif /* DOXYGEN */
145#ifdef __cplusplus
146}
147#endif
148
149#endif /* GPIO_LL_ARCH_H */
#define GPIO
GPIO register bank.
static uint8_t gpio_get_pin_num(gpio_t pin)
Extract the pin number from a gpio_t
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.
static gpio_port_t gpio_port_pack_addr(void *addr)
Pack a pointer into a gpio_port_t.
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 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.
uintptr_t gpio_port_t
GPIO port type.
Definition: gpio_ll.h:87
uint< num > _t uword_t
Word sized unsigned integer.
Definition: architecture.h:69
Shared CPU specific definitions for the STM32 family.