Loading...
Searching...
No Matches
gpio_ll_arch.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2016 Freie Universität Berlin
3 * SPDX-FileCopyrightText: 2017 OTA keys S.A.
4 * SPDX-FileCopyrightText: 2023 Otto-von-Guericke-Universität Magdeburg
5 * SPDX-License-Identifier: LGPL-2.1-only
6 */
7
8#pragma once
9
20
21#include "architecture.h"
22#include "periph_cpu.h"
23
24#ifdef __cplusplus
25extern "C" {
26#endif
27
28#ifndef DOXYGEN /* hide implementation specific details from Doxygen */
29
30/* Provide base address of the GPIO peripheral via APB */
31#if defined(PORT_SEC)
32# define GPIO_APB_BASE PORT_SEC
33#else
34# define GPIO_APB_BASE PORT
35#endif
36
37/* Provide base address of the GPIO peripheral via IOBUS */
38#if defined(PORT_IOBUS_SEC)
39# define GPIO_IOBUS_BASE PORT_IOBUS_SEC
40#elif defined(PORT_IOBUS)
41# define GPIO_IOBUS_BASE PORT_IOBUS
42#else
43# define GPIO_IOBUS_BASE GPIO_APB_BASE /* no IOBUS present, fall back to APB */
44#endif
45
46#define GPIO_PORT_NUMBERING_ALPHABETIC 1
47
48#if PORT_GROUPS >= 1
49# define GPIO_PORT_0 ((uintptr_t)&GPIO_IOBUS_BASE->Group[0])
50#endif
51#if PORT_GROUPS >= 2
52# define GPIO_PORT_1 ((uintptr_t)&GPIO_IOBUS_BASE->Group[1])
53#endif
54#if PORT_GROUPS >= 3
55# define GPIO_PORT_2 ((uintptr_t)&GPIO_IOBUS_BASE->Group[2])
56#endif
57#if PORT_GROUPS >= 4
58# define GPIO_PORT_3 ((uintptr_t)&GPIO_IOBUS_BASE->Group[3])
59#endif
60#if PORT_GROUPS >= 5
61# define GPIO_PORT_4 ((uintptr_t)&GPIO_IOBUS_BASE->Group[4])
62#endif
63#if PORT_GROUPS >= 5
64# define GPIO_PORT_4 ((uintptr_t)&GPIO_IOBUS_BASE->Group[4])
65#endif
66#if PORT_GROUPS >= 6
67# define GPIO_PORT_5 ((uintptr_t)&GPIO_IOBUS_BASE->Group[5])
68#endif
69#if PORT_GROUPS >= 7
70# define GPIO_PORT_6 ((uintptr_t)&GPIO_IOBUS_BASE->Group[6])
71#endif
72#if PORT_GROUPS >= 8
73# define GPIO_PORT_7 ((uintptr_t)&GPIO_IOBUS_BASE->Group[7])
74#endif
75
79#define GPIO_PORT(num) ((uintptr_t)&GPIO_IOBUS_BASE->Group[(num)])
80
84#define GPIO_PORT_NUM(port) \
85 (((port) - (uintptr_t)&GPIO_IOBUS_BASE->Group[0]) / sizeof(GPIO_IOBUS_BASE->Group[0]))
86
87static inline gpio_port_t gpio_port(uword_t num)
88{
89 return (uintptr_t)&GPIO_IOBUS_BASE->Group[num];
90}
91
92static inline uword_t gpio_port_num(gpio_port_t port)
93{
94 return (port - (uintptr_t)&GPIO_IOBUS_BASE->Group[0]) / sizeof(GPIO_IOBUS_BASE->Group[0]);
95}
96
97static inline PortGroup *sam0_gpio_iobus2ap(PortGroup *iobus)
98{
99 const uintptr_t iobus_base = (uintptr_t)GPIO_IOBUS_BASE;
100 const uintptr_t apb_base = (uintptr_t)GPIO_APB_BASE;
101
102 return (PortGroup *)((uintptr_t)iobus - (iobus_base - apb_base));
103}
104
105static inline uword_t gpio_ll_read(gpio_port_t port)
106{
107 PortGroup *p = (PortGroup *)port;
108 if (!IS_USED(MODULE_PERIPH_GPIO_FAST_READ)) {
109 p = sam0_gpio_iobus2ap(p);
110 }
111 return p->IN.reg;
112}
113
114static inline uword_t gpio_ll_read_output(gpio_port_t port)
115{
116 PortGroup *p = (PortGroup *)port;
117 return p->OUT.reg;
118}
119
120static inline void gpio_ll_set(gpio_port_t port, uword_t mask)
121{
122 PortGroup *p = (PortGroup *)port;
123 p->OUTSET.reg = mask;
124}
125
126static inline void gpio_ll_clear(gpio_port_t port, uword_t mask)
127{
128 PortGroup *p = (PortGroup *)port;
129 p->OUTCLR.reg = mask;
130}
131
132static inline void gpio_ll_toggle(gpio_port_t port, uword_t mask)
133{
134 PortGroup *p = (PortGroup *)port;
135 p->OUTTGL.reg = mask;
136}
137
138static inline void gpio_ll_write(gpio_port_t port, uword_t mask)
139{
140 PortGroup *p = (PortGroup *)port;
141 p->OUT.reg = mask;
142}
143
144static inline void gpio_ll_switch_dir_output(gpio_port_t port, uword_t outputs)
145{
146 PortGroup *p = (PortGroup *)port;
147 p->DIRSET.reg = outputs;
148}
149
150static inline void gpio_ll_switch_dir_input(gpio_port_t port, uword_t inputs)
151{
152 PortGroup *p = (PortGroup *)port;
153 p->DIRCLR.reg = inputs;
154}
155
156static inline gpio_port_t gpio_get_port(gpio_t pin)
157{
158 /* GPIO LL and legacy GPIO API may disagree on what is the GPIO base
159 * address if one is using the IOBUS and the other is using the APB for
160 * access. In this case, we need to do impedance matching by adding the
161 * offset. */
162 const uintptr_t gpio_ll_base = GPIO_PORT_0;
163 const uintptr_t gpio_legacy_base = GPIO_PIN(0, 0) & ~(0x1f);
164 uintptr_t addr = (pin & ~(0x1f));
165
166 return addr + (gpio_ll_base - gpio_legacy_base);
167}
168
169static inline uint8_t gpio_get_pin_num(gpio_t pin)
170{
171 return pin & 0x1f;
172}
173
174static inline gpio_port_t gpio_port_pack_addr(void *addr)
175{
176 return (gpio_port_t)addr;
177}
178
179static inline void * gpio_port_unpack_addr(gpio_port_t port)
180{
181 if (port < GPIO_PORT(0)) {
182 return (void *)port;
183 }
184 if (port > GPIO_PORT(ARRAY_SIZE(GPIO_IOBUS_BASE->Group))) {
185 return (void *)port;
186 }
187
188 return NULL;
189}
190
191static inline bool is_gpio_port_num_valid(uint_fast8_t num)
192{
193 return (num < ARRAY_SIZE(GPIO_IOBUS_BASE->Group));
194}
195
196#endif /* DOXYGEN */
197#ifdef __cplusplus
198}
199#endif
200
Platform-independent access to architecture details.
#define GPIO_PIN(x, y)
Define a CPU specific GPIO pin generator macro.
Definition periph_cpu.h:42
#define ARRAY_SIZE(a)
Calculate the number of elements in a static array.
Definition container.h:82
#define GPIO_PORT_0
Get the gpio_port_t value of the port labeled 0.
Definition gpio_ll.h:129
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:95
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:70