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/gpio_ll.h"
27#include "periph_cpu.h"
28
29#ifdef __cplusplus
30extern "C" {
31#endif
32
33#ifndef DOXYGEN /* hide implementation specific details from Doxygen */
34
38#if defined(CPU_FAM_STM32MP1)
39#define GPIO_PORT(num) (GPIOA_BASE + ((num) << 12))
40#else
41#define GPIO_PORT(num) (GPIOA_BASE + ((num) << 10))
42#endif
43
47#if defined(CPU_FAM_STM32MP1)
48#define GPIO_PORT_NUM(port) (((port) - GPIOA_BASE) >> 12)
49#else
50#define GPIO_PORT_NUM(port) (((port) - GPIOA_BASE) >> 10)
51#endif
52
53static inline uword_t gpio_ll_read(gpio_port_t port)
54{
55 GPIO_TypeDef *p = (GPIO_TypeDef *)port;
56 return p->IDR;
57}
58
59static inline uword_t gpio_ll_read_output(gpio_port_t port)
60{
61 GPIO_TypeDef *p = (GPIO_TypeDef *)port;
62 return p->ODR;
63}
64
65static inline void gpio_ll_set(gpio_port_t port, uword_t mask)
66{
67 GPIO_TypeDef *p = (GPIO_TypeDef *)port;
68 p->BSRR = mask;
69}
70
71static inline void gpio_ll_clear(gpio_port_t port, uword_t mask)
72{
73 GPIO_TypeDef *p = (GPIO_TypeDef *)port;
74 /* The STM32F4 vendor header files do include defines for accessing the
75 * BRR register, but do not have a BRR register.
76 * See https://github.com/STMicroelectronics/cmsis_device_f4/pull/7 */
77#if defined(GPIO_BRR_BR0) && !defined(CPU_FAM_STM32F4)
78 p->BRR = mask;
79#else
80 /* The first half-word sets GPIOs, the second half-world clears GPIOs */
81 volatile uint16_t *brr = (volatile uint16_t *)&(p->BSRR);
82 brr[1] = (uint16_t)mask;
83#endif
84}
85
86static inline void gpio_ll_toggle(gpio_port_t port, uword_t mask)
87{
88 GPIO_TypeDef *p = (GPIO_TypeDef *)port;
89 unsigned irq_state = irq_disable();
90 p->ODR ^= mask;
91 irq_restore(irq_state);
92}
93
94static inline void gpio_ll_write(gpio_port_t port, uword_t value)
95{
96 GPIO_TypeDef *p = (GPIO_TypeDef *)port;
97 p->ODR = value;
98}
99
100static inline gpio_port_t gpio_get_port(gpio_t pin)
101{
102 return pin & 0xfffffff0LU;
103}
104
105static inline uint8_t gpio_get_pin_num(gpio_t pin)
106{
107 return pin & 0xfLU;
108}
109
110static inline gpio_port_t gpio_port_pack_addr(void *addr)
111{
112 return (gpio_port_t)addr;
113}
114
115static inline void * gpio_port_unpack_addr(gpio_port_t port)
116{
117 if (port < GPIOA_BASE) {
118 return (void *)port;
119 }
120
121 return NULL;
122}
123
124static inline bool is_gpio_port_num_valid(uint_fast8_t num)
125{
126 switch (num) {
127 default:
128 return false;
129#ifdef GPIOA_BASE
130 case 0:
131#endif
132#ifdef GPIOB_BASE
133 case 1:
134#endif
135#ifdef GPIOC_BASE
136 case 2:
137#endif
138#ifdef GPIOD_BASE
139 case 3:
140#endif
141#ifdef GPIOE_BASE
142 case 4:
143#endif
144#ifdef GPIOF_BASE
145 case 5:
146#endif
147#ifdef GPIOG_BASE
148 case 6:
149#endif
150#ifdef GPIOH_BASE
151 case 7:
152#endif
153#ifdef GPIOI_BASE
154 case 8:
155#endif
156#ifdef GPIOJ_BASE
157 case 9:
158#endif
159#ifdef GPIOK_BASE
160 case 10:
161#endif
162#ifdef GPIOL_BASE
163 case 11:
164#endif
165#ifdef GPIOM_BASE
166 case 12:
167#endif
168#ifdef GPION_BASE
169 case 13:
170#endif
171#ifdef GPIOO_BASE
172 case 14:
173#endif
174#ifdef GPIOP_BASE
175 case 15:
176#endif
177#ifdef GPIOQ_BASE
178 case 16:
179#endif
180#ifdef GPIOR_BASE
181 case 17:
182#endif
183#ifdef GPIOS_BASE
184 case 18:
185#endif
186#ifdef GPIOT_BASE
187 case 19:
188#endif
189#ifdef GPIOU_BASE
190 case 20:
191#endif
192#ifdef GPIOV_BASE
193 case 21:
194#endif
195#ifdef GPIOW_BASE
196 case 22:
197#endif
198#ifdef GPIOX_BASE
199 case 23:
200#endif
201#ifdef GPIOY_BASE
202 case 24:
203#endif
204#ifdef GPIOZ_BASE
205 case 25:
206#endif
207 return true;
208 }
209}
210
211#endif /* DOXYGEN */
212#ifdef __cplusplus
213}
214#endif
215
216#endif /* GPIO_LL_ARCH_H */
Platform-independent access to architecture details.
Peripheral GPIO Low-Level API.
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.
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.
Shared CPU specific definitions for the STM32 family.