Loading...
Searching...
No Matches
gpio_ll_arch.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2016 Freie Universität Berlin
3 * 2017 OTA keys S.A.
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
22#ifndef GPIO_LL_ARCH_H
23#define GPIO_LL_ARCH_H
24
25#include "architecture.h"
26#include "periph_cpu.h"
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
32#ifndef DOXYGEN /* hide implementation specific details from Doxygen */
33
34#define GPIO_PORT_NUMBERING_ALPHABETIC 1
35
36#ifdef GPIOA_BASE
37# define GPIO_PORT_0 GPIOA_BASE
38#endif
39
40#ifdef GPIOB_BASE
41# define GPIO_PORT_1 GPIOB_BASE
42#endif
43
44#ifdef GPIOC_BASE
45# define GPIO_PORT_2 GPIOC_BASE
46#endif
47
48#ifdef GPIOD_BASE
49# define GPIO_PORT_3 GPIOD_BASE
50#endif
51
52#ifdef GPIOE_BASE
53# define GPIO_PORT_4 GPIOE_BASE
54#endif
55
56#ifdef GPIOF_BASE
57# define GPIO_PORT_5 GPIOF_BASE
58#endif
59
60#ifdef GPIOG_BASE
61# define GPIO_PORT_6 GPIOG_BASE
62#endif
63
64#ifdef GPIOH_BASE
65# define GPIO_PORT_7 GPIOH_BASE
66#endif
67
68#ifdef GPIOI_BASE
69# define GPIO_PORT_8 GPIOI_BASE
70#endif
71
72#ifdef GPIOJ_BASE
73# define GPIO_PORT_9 GPIOJ_BASE
74#endif
75
76#ifdef GPIOK_BASE
77# define GPIO_PORT_10 GPIOK_BASE
78#endif
79
80static inline gpio_port_t gpio_port(uword_t num)
81{
82#if defined(CPU_FAM_STM32MP1)
83 return GPIOA_BASE + (num << 12);
84#else
85 return GPIOA_BASE + (num << 10);
86#endif
87}
88
89static inline uword_t gpio_port_num(gpio_port_t port)
90{
91#if defined(CPU_FAM_STM32MP1)
92 return (port - GPIOA_BASE) >> 12;
93#else
94 return (port - GPIOA_BASE) >> 10;
95#endif
96}
97
98static inline uword_t gpio_ll_read(gpio_port_t port)
99{
100 GPIO_TypeDef *p = (GPIO_TypeDef *)port;
101 return p->IDR;
102}
103
104static inline uword_t gpio_ll_read_output(gpio_port_t port)
105{
106 GPIO_TypeDef *p = (GPIO_TypeDef *)port;
107 return p->ODR;
108}
109
110static inline void gpio_ll_set(gpio_port_t port, uword_t mask)
111{
112 GPIO_TypeDef *p = (GPIO_TypeDef *)port;
113 p->BSRR = mask;
114}
115
116static inline void gpio_ll_clear(gpio_port_t port, uword_t mask)
117{
118 GPIO_TypeDef *p = (GPIO_TypeDef *)port;
119 /* The STM32F4 vendor header files do include defines for accessing the
120 * BRR register, but do not have a BRR register.
121 * See https://github.com/STMicroelectronics/cmsis_device_f4/pull/7 */
122#if defined(GPIO_BRR_BR0) && !defined(CPU_FAM_STM32F4)
123 p->BRR = mask;
124#else
125 /* The first half-word sets GPIOs, the second half-world clears GPIOs */
126 volatile uint16_t *brr = (volatile uint16_t *)&(p->BSRR);
127 brr[1] = (uint16_t)mask;
128#endif
129}
130
131static inline void gpio_ll_toggle(gpio_port_t port, uword_t mask)
132{
133 GPIO_TypeDef *p = (GPIO_TypeDef *)port;
134 unsigned irq_state = irq_disable();
135 p->ODR ^= mask;
136 irq_restore(irq_state);
137}
138
139static inline void gpio_ll_write(gpio_port_t port, uword_t value)
140{
141 GPIO_TypeDef *p = (GPIO_TypeDef *)port;
142 p->ODR = value;
143}
144
145#ifdef MODULE_PERIPH_GPIO_LL_SWITCH_DIR
147{
148 /* implementation too large to always inline */
149 extern uword_t gpio_ll_prepare_switch_dir_impl(uword_t mask);
150 return gpio_ll_prepare_switch_dir_impl(mask);
151}
152
153static inline void gpio_ll_switch_dir_output(gpio_port_t port, uword_t pins)
154{
155 GPIO_TypeDef *p = (GPIO_TypeDef *)port;
156 unsigned irq_state = irq_disable();
157 p->MODER |= pins;
158 irq_restore(irq_state);
159}
160
161static inline void gpio_ll_switch_dir_input(gpio_port_t port, uword_t pins)
162{
163 GPIO_TypeDef *p = (GPIO_TypeDef *)port;
164 unsigned irq_state = irq_disable();
165 p->MODER &= ~pins;
166 irq_restore(irq_state);
167}
168#endif
169
170static inline gpio_port_t gpio_get_port(gpio_t pin)
171{
172 return pin & 0xfffffff0LU;
173}
174
175static inline uint8_t gpio_get_pin_num(gpio_t pin)
176{
177 return pin & 0xfLU;
178}
179
180static inline gpio_port_t gpio_port_pack_addr(void *addr)
181{
182 return (gpio_port_t)addr;
183}
184
185static inline void * gpio_port_unpack_addr(gpio_port_t port)
186{
187 if (port < GPIOA_BASE) {
188 return (void *)port;
189 }
190
191 return NULL;
192}
193
194static inline bool is_gpio_port_num_valid(uint_fast8_t num)
195{
196 switch (num) {
197 default:
198 return false;
199#ifdef GPIOA_BASE
200 case 0:
201#endif
202#ifdef GPIOB_BASE
203 case 1:
204#endif
205#ifdef GPIOC_BASE
206 case 2:
207#endif
208#ifdef GPIOD_BASE
209 case 3:
210#endif
211#ifdef GPIOE_BASE
212 case 4:
213#endif
214#ifdef GPIOF_BASE
215 case 5:
216#endif
217#ifdef GPIOG_BASE
218 case 6:
219#endif
220#ifdef GPIOH_BASE
221 case 7:
222#endif
223#ifdef GPIOI_BASE
224 case 8:
225#endif
226#ifdef GPIOJ_BASE
227 case 9:
228#endif
229#ifdef GPIOK_BASE
230 case 10:
231#endif
232#ifdef GPIOL_BASE
233 case 11:
234#endif
235#ifdef GPIOM_BASE
236 case 12:
237#endif
238#ifdef GPION_BASE
239 case 13:
240#endif
241#ifdef GPIOO_BASE
242 case 14:
243#endif
244#ifdef GPIOP_BASE
245 case 15:
246#endif
247#ifdef GPIOQ_BASE
248 case 16:
249#endif
250#ifdef GPIOR_BASE
251 case 17:
252#endif
253#ifdef GPIOS_BASE
254 case 18:
255#endif
256#ifdef GPIOT_BASE
257 case 19:
258#endif
259#ifdef GPIOU_BASE
260 case 20:
261#endif
262#ifdef GPIOV_BASE
263 case 21:
264#endif
265#ifdef GPIOW_BASE
266 case 22:
267#endif
268#ifdef GPIOX_BASE
269 case 23:
270#endif
271#ifdef GPIOY_BASE
272 case 24:
273#endif
274#ifdef GPIOZ_BASE
275 case 25:
276#endif
277 return true;
278 }
279}
280
281#endif /* DOXYGEN */
282#ifdef __cplusplus
283}
284#endif
285
286#endif /* GPIO_LL_ARCH_H */
Platform-independent access to architecture details.
MAYBE_INLINE void irq_restore(unsigned state)
This function restores the IRQ disable bit in the status register to the value contained within passe...
MAYBE_INLINE unsigned irq_disable(void)
This function sets the IRQ disable bit in the status register.
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 void gpio_ll_switch_dir_output(gpio_port_t port, uword_t pins)
Turn GPIO pins specified by pins (obtained from gpio_ll_prepare_switch_dir) to outputs.
static void gpio_ll_switch_dir_input(gpio_port_t port, uword_t pins)
Turn GPIO pins specified by pins (obtained from gpio_ll_prepare_switch_dir) to inputs.
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 uword_t gpio_ll_prepare_switch_dir(uword_t mask)
Prepare bitmask for use with gpio_ll_switch_dir_output and gpio_ll_switch_dir_input.
Definition gpio_ll.h:743
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.