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