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 * 2023 Otto-von-Guericke-Universität Magdeburg
5 *
6 * This file is subject to the terms and conditions of the GNU Lesser
7 * General Public License v2.1. See the file LICENSE in the top level
8 * directory for more details.
9 */
10
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/* Provide base address of the GPIO peripheral via APB */
35#if defined(PORT_SEC)
36# define GPIO_APB_BASE PORT_SEC
37#else
38# define GPIO_APB_BASE PORT
39#endif
40
41/* Provide base address of the GPIO peripheral via IOBUS */
42#if defined(PORT_IOBUS_SEC)
43# define GPIO_IOBUS_BASE PORT_IOBUS_SEC
44#elif defined(PORT_IOBUS)
45# define GPIO_IOBUS_BASE PORT_IOBUS
46#else
47# define GPIO_IOBUS_BASE GPIO_APB_BASE /* no IOBUS present, fall back to APB */
48#endif
49
50#define GPIO_PORT_NUMBERING_ALPHABETIC 1
51
52#if PORT_GROUPS >= 1
53# define GPIO_PORT_0 ((uintptr_t)&GPIO_IOBUS_BASE->Group[0])
54#endif
55#if PORT_GROUPS >= 2
56# define GPIO_PORT_1 ((uintptr_t)&GPIO_IOBUS_BASE->Group[1])
57#endif
58#if PORT_GROUPS >= 3
59# define GPIO_PORT_2 ((uintptr_t)&GPIO_IOBUS_BASE->Group[2])
60#endif
61#if PORT_GROUPS >= 4
62# define GPIO_PORT_3 ((uintptr_t)&GPIO_IOBUS_BASE->Group[3])
63#endif
64#if PORT_GROUPS >= 5
65# define GPIO_PORT_4 ((uintptr_t)&GPIO_IOBUS_BASE->Group[4])
66#endif
67#if PORT_GROUPS >= 5
68# define GPIO_PORT_4 ((uintptr_t)&GPIO_IOBUS_BASE->Group[4])
69#endif
70#if PORT_GROUPS >= 6
71# define GPIO_PORT_5 ((uintptr_t)&GPIO_IOBUS_BASE->Group[5])
72#endif
73#if PORT_GROUPS >= 7
74# define GPIO_PORT_6 ((uintptr_t)&GPIO_IOBUS_BASE->Group[6])
75#endif
76#if PORT_GROUPS >= 8
77# define GPIO_PORT_7 ((uintptr_t)&GPIO_IOBUS_BASE->Group[7])
78#endif
79
83#define GPIO_PORT(num) ((uintptr_t)&GPIO_IOBUS_BASE->Group[(num)])
84
88#define GPIO_PORT_NUM(port) \
89 (((port) - (uintptr_t)&GPIO_IOBUS_BASE->Group[0]) / sizeof(GPIO_IOBUS_BASE->Group[0]))
90
91static inline gpio_port_t gpio_port(uword_t num)
92{
93 return (uintptr_t)&GPIO_IOBUS_BASE->Group[num];
94}
95
96static inline uword_t gpio_port_num(gpio_port_t port)
97{
98 return (port - (uintptr_t)&GPIO_IOBUS_BASE->Group[0]) / sizeof(GPIO_IOBUS_BASE->Group[0]);
99}
100
101static inline PortGroup *sam0_gpio_iobus2ap(PortGroup *iobus)
102{
103 const uintptr_t iobus_base = (uintptr_t)GPIO_IOBUS_BASE;
104 const uintptr_t apb_base = (uintptr_t)GPIO_APB_BASE;
105
106 return (PortGroup *)((uintptr_t)iobus - (iobus_base - apb_base));
107}
108
109static inline uword_t gpio_ll_read(gpio_port_t port)
110{
111 PortGroup *p = (PortGroup *)port;
112 if (!IS_USED(MODULE_PERIPH_GPIO_FAST_READ)) {
113 p = sam0_gpio_iobus2ap(p);
114 }
115 return p->IN.reg;
116}
117
118static inline uword_t gpio_ll_read_output(gpio_port_t port)
119{
120 PortGroup *p = (PortGroup *)port;
121 return p->OUT.reg;
122}
123
124static inline void gpio_ll_set(gpio_port_t port, uword_t mask)
125{
126 PortGroup *p = (PortGroup *)port;
127 p->OUTSET.reg = mask;
128}
129
130static inline void gpio_ll_clear(gpio_port_t port, uword_t mask)
131{
132 PortGroup *p = (PortGroup *)port;
133 p->OUTCLR.reg = mask;
134}
135
136static inline void gpio_ll_toggle(gpio_port_t port, uword_t mask)
137{
138 PortGroup *p = (PortGroup *)port;
139 p->OUTTGL.reg = mask;
140}
141
142static inline void gpio_ll_write(gpio_port_t port, uword_t mask)
143{
144 PortGroup *p = (PortGroup *)port;
145 p->OUT.reg = mask;
146}
147
148static inline void gpio_ll_switch_dir_output(gpio_port_t port, uword_t outputs)
149{
150 PortGroup *p = (PortGroup *)port;
151 p->DIRSET.reg = outputs;
152}
153
154static inline void gpio_ll_switch_dir_input(gpio_port_t port, uword_t inputs)
155{
156 PortGroup *p = (PortGroup *)port;
157 p->DIRCLR.reg = inputs;
158}
159
160static inline gpio_port_t gpio_get_port(gpio_t pin)
161{
162 return (gpio_port_t)(pin & ~(0x1f));
163}
164
165static inline uint8_t gpio_get_pin_num(gpio_t pin)
166{
167 return pin & 0x1f;
168}
169
170static inline gpio_port_t gpio_port_pack_addr(void *addr)
171{
172 return (gpio_port_t)addr;
173}
174
175static inline void * gpio_port_unpack_addr(gpio_port_t port)
176{
177 if (port < GPIO_PORT(0)) {
178 return (void *)port;
179 }
180 if (port > GPIO_PORT(ARRAY_SIZE(GPIO_IOBUS_BASE->Group))) {
181 return (void *)port;
182 }
183
184 return NULL;
185}
186
187static inline bool is_gpio_port_num_valid(uint_fast8_t num)
188{
189 return (num < ARRAY_SIZE(GPIO_IOBUS_BASE->Group));
190}
191
192#endif /* DOXYGEN */
193#ifdef __cplusplus
194}
195#endif
196
197#endif /* GPIO_LL_ARCH_H */
Platform-independent access to architecture details.
#define ARRAY_SIZE(a)
Calculate the number of elements in a static array.
Definition container.h:83
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 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.
#define IS_USED(module)
Checks whether a module is being used or not.
Definition modules.h:71