Loading...
Searching...
No Matches
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#define GPIO_PORT_NUMBERING_ALPHABETIC 1
52
53/* Note: The pin count may be defined as zero to indicate the port not existing.
54 * Hence, don't to `#if defined(foo)` but only `#if foo`
55 */
56#if _GPIO_PORT_A_PIN_COUNT
57# define GPIO_PORT_0 0
58#endif
59
60#if _GPIO_PORT_B_PIN_COUNT
61# define GPIO_PORT_1 1
62#endif
63
64#if _GPIO_PORT_C_PIN_COUNT
65# define GPIO_PORT_2 2
66#endif
67
68#if _GPIO_PORT_D_PIN_COUNT
69# define GPIO_PORT_3 3
70#endif
71
72#if _GPIO_PORT_E_PIN_COUNT
73# define GPIO_PORT_4 4
74#endif
75
76#if _GPIO_PORT_F_PIN_COUNT
77# define GPIO_PORT_6 6
78#endif
79
80#if _GPIO_PORT_G_PIN_COUNT
81# define GPIO_PORT_7 7
82#endif
83
84#if _GPIO_PORT_H_PIN_COUNT
85# define GPIO_PORT_8 8
86#endif
87
88#if _GPIO_PORT_I_PIN_COUNT
89# define GPIO_PORT_9 9
90#endif
91
92#if _GPIO_PORT_J_PIN_COUNT
93# define GPIO_PORT_10 10
94#endif
95
96#if _GPIO_PORT_K_PIN_COUNT
97# define GPIO_PORT_11 11
98#endif
99
100/* We could do
101 *
102 * static inline gpio_port_t gpio_port(uword_t num)
103 * {
104 * return GPIO->P[num];
105 * }
106 *
107 * which works for some operations, but at latest when _ll_set needs to fan out
108 * for some EFM32 families to
109 *
110#if defined(_GPIO_P_DOUTSET_MASK)
111 GPIO->P[port].DOUTSET = pins;
112#elif defined(GPIO_HAS_SET_CLEAR)
113 GPIO->P_SET[port].DOUT = pins;
114#else
115 (some bit-banding-style interaction on P)
116#endif
117 *
118 * that approach becomes an unbearable burden, because P_SET is not necessarily
119 * as large as P, and getting from a P pointer to a P_SET pointer would involve
120 * division and multiplication. Instead, falling back to addressing ports by
121 * their index number, which does require an additional multiplication for most
122 * accesses, but at least does that consistently.
123 *
124 * (It also makes things easier because it allows going through the helper
125 * functions).
126 *
127 * There appears to be one truly viable alternative: implementing gpio_ll only
128 * for those EFM32 that do have DOUTSET etc. in P, with no way of having such
129 * an implementation for other EFM32 families. For the time being, the
130 * suboptimal-but-works-for-all version is the best we have.
131 */
132static inline gpio_port_t gpio_port(uword_t num)
133{
134 return num;
135}
136
137static inline uword_t gpio_port_num(gpio_port_t port)
138{
139 return port;
140}
141
142static inline uword_t gpio_ll_read(gpio_port_t port)
143{
144 return GPIO_PortInGet(port);
145}
146
147static inline uword_t gpio_ll_read_output(gpio_port_t port)
148{
149 return GPIO_PortOutGet(port);
150}
151
152static inline void gpio_ll_set(gpio_port_t port, uword_t mask)
153{
154 GPIO_PortOutSet(port, mask);
155}
156
157static inline void gpio_ll_clear(gpio_port_t port, uword_t mask)
158{
159 GPIO_PortOutClear(port, mask);
160}
161
162static inline void gpio_ll_toggle(gpio_port_t port, uword_t mask)
163{
164 GPIO_PortOutToggle(port, mask);
165}
166
167static inline void gpio_ll_write(gpio_port_t port, uword_t value)
168{
169 GPIO->P[port].DOUT = value;
170}
171
172static inline gpio_port_t gpio_get_port(gpio_t pin)
173{
174 return (pin >> 4);
175}
176
177static inline uint8_t gpio_get_pin_num(gpio_t pin)
178{
179 return (pin & 0x0f);
180}
181
182static inline gpio_port_t gpio_port_pack_addr(void *addr)
183{
184 return (gpio_port_t)addr;
185}
186
187static inline bool is_gpio_port_num_valid(uint_fast8_t num)
188{
189 return GPIO_PORT_VALID(num);
190}
191
192static inline void * gpio_port_unpack_addr(gpio_port_t port)
193{
194 if ((port & ~0xff) == 0 && is_gpio_port_num_valid(port)) {
195 return NULL;
196 }
197
198 return (void *)port;
199}
200
201#endif /* DOXYGEN */
202#ifdef __cplusplus
203}
204#endif
205
206#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.
gpio_port_t gpio_port(uword_t num)
Get the gpio_port_t value of the port number num.
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
uword_t gpio_port_num(gpio_port_t port)
Get the number of the GPIO port port refers to.
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.
Shared CPU specific definitions for the STM32 family.