mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
36e8526046
The API was based on the assumption that GPIO ports are mapped in memory sanely, so that a `GPIO_PORT(num)` macro would work allow for constant folding when `num` is known and still be efficient when it is not. Some MCUs, however, will need a look up tables to efficiently translate GPIO port numbers to the port's base address. This will prevent the use of such a `GPIO_PORT(num)` macro in constant initializers. As a result, we rather provide `GPIO_PORT_0`, `GPIO_PORT_1`, etc. macros for each GPIO port present (regardless of MCU naming scheme), as well as `GPIO_PORT_A`, `GPIO_PORT_B`, etc. macros if (and only if) the MCU port naming scheme uses letters rather than numbers. These can be defined as macros to the peripheral base address even when those are randomly mapped into the address space. In addition, a C function `gpio_port()` replaces the role of the `GPIO_PORT()` and `gpio_port_num()` the `GPIO_PORT_NUM()` macro. Those functions will still be implemented as efficient as possible and will allow constant folding where it was formerly possible. Hence, there is no downside for MCUs with sane peripheral memory mapping, but it is highly beneficial for the crazy ones. There are also two benefits for the non-crazy MCUs: 1. We can now test for valid port numbers with `#ifdef GPIO_PORT_<NUM>` - This directly benefits the test in `tests/periph/gpio_ll`, which can now provide a valid GPIO port for each and every board - Writing to invalid memory mapped I/O addresses was treated as triggering undefined behavior by the compiler and used as a optimization opportunity 2. We can now detect at compile time if the naming scheme of the MCU uses letters or numbers, and produce more user friendly output. - This is directly applied in the test app
199 lines
4.7 KiB
C
199 lines
4.7 KiB
C
/*
|
|
* Copyright (C) 2016 Freie Universität Berlin
|
|
* 2017 OTA keys S.A.
|
|
* 2023 Otto-von-Guericke-Universität Magdeburg
|
|
*
|
|
* This file is subject to the terms and conditions of the GNU Lesser
|
|
* General Public License v2.1. See the file LICENSE in the top level
|
|
* directory for more details.
|
|
*/
|
|
|
|
/**
|
|
* @ingroup cpu_sam0_common
|
|
* @ingroup drivers_periph_gpio_ll
|
|
* @{
|
|
*
|
|
* @file
|
|
* @brief CPU specific part of the Peripheral GPIO Low-Level API
|
|
*
|
|
* @author Marian Buschsieweke <marian.buschsieweke@posteo.net>
|
|
*/
|
|
|
|
#ifndef GPIO_LL_ARCH_H
|
|
#define GPIO_LL_ARCH_H
|
|
|
|
#include "architecture.h"
|
|
#include "periph_cpu.h"
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
#ifndef DOXYGEN /* hide implementation specific details from Doxygen */
|
|
|
|
/* Provide base address of the GPIO peripheral via APB */
|
|
#if defined(PORT_SEC)
|
|
# define GPIO_APB_BASE PORT_SEC
|
|
#else
|
|
# define GPIO_APB_BASE PORT
|
|
#endif
|
|
|
|
/* Provide base address of the GPIO peripheral via IOBUS */
|
|
#if defined(PORT_IOBUS_SEC)
|
|
# define GPIO_IOBUS_BASE PORT_IOBUS_SEC
|
|
#elif defined(PORT_IOBUS)
|
|
# define GPIO_IOBUS_BASE PORT_IOBUS
|
|
#else
|
|
# define GPIO_IOBUS_BASE GPIO_APB_BASE /* no IOBUS present, fall back to APB */
|
|
#endif
|
|
|
|
#define GPIO_PORT_NUMBERING_ALPHABETIC 1
|
|
|
|
#if PORT_GROUPS >= 1
|
|
# define GPIO_PORT_0 ((uintptr_t)&GPIO_IOBUS_BASE->Group[0])
|
|
#endif
|
|
#if PORT_GROUPS >= 2
|
|
# define GPIO_PORT_1 ((uintptr_t)&GPIO_IOBUS_BASE->Group[1])
|
|
#endif
|
|
#if PORT_GROUPS >= 3
|
|
# define GPIO_PORT_2 ((uintptr_t)&GPIO_IOBUS_BASE->Group[2])
|
|
#endif
|
|
#if PORT_GROUPS >= 4
|
|
# define GPIO_PORT_3 ((uintptr_t)&GPIO_IOBUS_BASE->Group[3])
|
|
#endif
|
|
#if PORT_GROUPS >= 5
|
|
# define GPIO_PORT_4 ((uintptr_t)&GPIO_IOBUS_BASE->Group[4])
|
|
#endif
|
|
#if PORT_GROUPS >= 5
|
|
# define GPIO_PORT_4 ((uintptr_t)&GPIO_IOBUS_BASE->Group[4])
|
|
#endif
|
|
#if PORT_GROUPS >= 6
|
|
# define GPIO_PORT_5 ((uintptr_t)&GPIO_IOBUS_BASE->Group[5])
|
|
#endif
|
|
#if PORT_GROUPS >= 7
|
|
# define GPIO_PORT_6 ((uintptr_t)&GPIO_IOBUS_BASE->Group[6])
|
|
#endif
|
|
#if PORT_GROUPS >= 8
|
|
# define GPIO_PORT_7 ((uintptr_t)&GPIO_IOBUS_BASE->Group[7])
|
|
#endif
|
|
|
|
/**
|
|
* @brief Get a GPIO port by number
|
|
*/
|
|
#define GPIO_PORT(num) ((uintptr_t)&GPIO_IOBUS_BASE->Group[(num)])
|
|
|
|
/**
|
|
* @brief Get a GPIO port number by gpio_port_t value
|
|
*/
|
|
#define GPIO_PORT_NUM(port) \
|
|
(((port) - (uintptr_t)&GPIO_IOBUS_BASE->Group[0]) / sizeof(GPIO_IOBUS_BASE->Group[0]))
|
|
|
|
static inline gpio_port_t gpio_port(uword_t num)
|
|
{
|
|
return (uintptr_t)&GPIO_IOBUS_BASE->Group[num];
|
|
}
|
|
|
|
static inline uword_t gpio_port_num(gpio_port_t port)
|
|
{
|
|
return (port - (uintptr_t)&GPIO_IOBUS_BASE->Group[0]) / sizeof(GPIO_IOBUS_BASE->Group[0]);
|
|
}
|
|
|
|
static inline PortGroup *sam0_gpio_iobus2ap(PortGroup *iobus)
|
|
{
|
|
const uintptr_t iobus_base = (uintptr_t)GPIO_IOBUS_BASE;
|
|
const uintptr_t apb_base = (uintptr_t)GPIO_APB_BASE;
|
|
|
|
return (PortGroup *)((uintptr_t)iobus - (iobus_base - apb_base));
|
|
}
|
|
|
|
static inline uword_t gpio_ll_read(gpio_port_t port)
|
|
{
|
|
PortGroup *p = (PortGroup *)port;
|
|
if (!IS_USED(MODULE_PERIPH_GPIO_FAST_READ)) {
|
|
p = sam0_gpio_iobus2ap(p);
|
|
}
|
|
return p->IN.reg;
|
|
}
|
|
|
|
static inline uword_t gpio_ll_read_output(gpio_port_t port)
|
|
{
|
|
PortGroup *p = (PortGroup *)port;
|
|
return p->OUT.reg;
|
|
}
|
|
|
|
static inline void gpio_ll_set(gpio_port_t port, uword_t mask)
|
|
{
|
|
PortGroup *p = (PortGroup *)port;
|
|
p->OUTSET.reg = mask;
|
|
}
|
|
|
|
static inline void gpio_ll_clear(gpio_port_t port, uword_t mask)
|
|
{
|
|
PortGroup *p = (PortGroup *)port;
|
|
p->OUTCLR.reg = mask;
|
|
}
|
|
|
|
static inline void gpio_ll_toggle(gpio_port_t port, uword_t mask)
|
|
{
|
|
PortGroup *p = (PortGroup *)port;
|
|
p->OUTTGL.reg = mask;
|
|
}
|
|
|
|
static inline void gpio_ll_write(gpio_port_t port, uword_t mask)
|
|
{
|
|
PortGroup *p = (PortGroup *)port;
|
|
p->OUT.reg = mask;
|
|
}
|
|
|
|
static inline void gpio_ll_switch_dir_output(gpio_port_t port, uword_t outputs)
|
|
{
|
|
PortGroup *p = (PortGroup *)port;
|
|
p->DIRSET.reg = outputs;
|
|
}
|
|
|
|
static inline void gpio_ll_switch_dir_input(gpio_port_t port, uword_t inputs)
|
|
{
|
|
PortGroup *p = (PortGroup *)port;
|
|
p->DIRCLR.reg = inputs;
|
|
}
|
|
|
|
static inline gpio_port_t gpio_get_port(gpio_t pin)
|
|
{
|
|
return (gpio_port_t)(pin & ~(0x1f));
|
|
}
|
|
|
|
static inline uint8_t gpio_get_pin_num(gpio_t pin)
|
|
{
|
|
return pin & 0x1f;
|
|
}
|
|
|
|
static inline gpio_port_t gpio_port_pack_addr(void *addr)
|
|
{
|
|
return (gpio_port_t)addr;
|
|
}
|
|
|
|
static inline void * gpio_port_unpack_addr(gpio_port_t port)
|
|
{
|
|
if (port < GPIO_PORT(0)) {
|
|
return (void *)port;
|
|
}
|
|
if (port > GPIO_PORT(ARRAY_SIZE(GPIO_IOBUS_BASE->Group))) {
|
|
return (void *)port;
|
|
}
|
|
|
|
return NULL;
|
|
}
|
|
|
|
static inline bool is_gpio_port_num_valid(uint_fast8_t num)
|
|
{
|
|
return (num < ARRAY_SIZE(GPIO_IOBUS_BASE->Group));
|
|
}
|
|
|
|
#endif /* DOXYGEN */
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|
|
#endif /* GPIO_LL_ARCH_H */
|
|
/** @} */
|