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
208 lines
4.8 KiB
C
208 lines
4.8 KiB
C
/*
|
|
* Copyright (C) 2022 Christian Amsüss
|
|
*
|
|
* 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_efm32
|
|
* @ingroup drivers_periph_gpio_ll
|
|
* @{
|
|
*
|
|
* @file
|
|
* @brief CPU specific part of the Peripheral GPIO Low-Level API
|
|
*
|
|
* Noteworthy aspects of this implementation:
|
|
*
|
|
* * The platform has no "GPIO_USED_BY_PERIPHERAL" -- instead, the pin needs to
|
|
* be configured as by the using peripheral's properties (eg. push-pull for
|
|
* UART, disabled for ADC to minimize GPIO influence, etc.).
|
|
*
|
|
* * Alternative drive strengths are supported by the hardware, but not
|
|
* implemented in the driver. Adding them would be possible, but is tedious
|
|
* to implement: As each port can only have one alternative drive strength,
|
|
* changing that would require iterating over all pins, decide whether they
|
|
* are using an alternative drive strength, and refuse changing it if any are
|
|
* found.
|
|
*
|
|
* * There is an optional glitch suppression filter after the Schmitt trigger;
|
|
* no custom API (which would be a local extension to @ref gpio_conf_t) is
|
|
* implemented yet to enable the filters.
|
|
*
|
|
* @author Christian Amsüss <chrysn@fsfe.org>
|
|
*/
|
|
|
|
#ifndef GPIO_LL_ARCH_H
|
|
#define GPIO_LL_ARCH_H
|
|
|
|
#include "cpu.h"
|
|
#include "periph_cpu.h"
|
|
|
|
#include "em_gpio.h"
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
#ifndef DOXYGEN /* hide implementation specific details from Doxygen */
|
|
|
|
#define GPIO_PORT_NUMBERING_ALPHABETIC 1
|
|
|
|
/* Note: The pin count may be defined as zero to indicate the port not existing.
|
|
* Hence, don't to `#if defined(foo)` but only `#if foo`
|
|
*/
|
|
#if _GPIO_PORT_A_PIN_COUNT
|
|
# define GPIO_PORT_0 0
|
|
#endif
|
|
|
|
#if _GPIO_PORT_B_PIN_COUNT
|
|
# define GPIO_PORT_1 1
|
|
#endif
|
|
|
|
#if _GPIO_PORT_C_PIN_COUNT
|
|
# define GPIO_PORT_2 2
|
|
#endif
|
|
|
|
#if _GPIO_PORT_D_PIN_COUNT
|
|
# define GPIO_PORT_3 3
|
|
#endif
|
|
|
|
#if _GPIO_PORT_E_PIN_COUNT
|
|
# define GPIO_PORT_4 4
|
|
#endif
|
|
|
|
#if _GPIO_PORT_F_PIN_COUNT
|
|
# define GPIO_PORT_6 6
|
|
#endif
|
|
|
|
#if _GPIO_PORT_G_PIN_COUNT
|
|
# define GPIO_PORT_7 7
|
|
#endif
|
|
|
|
#if _GPIO_PORT_H_PIN_COUNT
|
|
# define GPIO_PORT_8 8
|
|
#endif
|
|
|
|
#if _GPIO_PORT_I_PIN_COUNT
|
|
# define GPIO_PORT_9 9
|
|
#endif
|
|
|
|
#if _GPIO_PORT_J_PIN_COUNT
|
|
# define GPIO_PORT_10 10
|
|
#endif
|
|
|
|
#if _GPIO_PORT_K_PIN_COUNT
|
|
# define GPIO_PORT_11 11
|
|
#endif
|
|
|
|
/* We could do
|
|
*
|
|
* static inline gpio_port_t gpio_port(uword_t num)
|
|
* {
|
|
* return GPIO->P[num];
|
|
* }
|
|
*
|
|
* which works for some operations, but at latest when _ll_set needs to fan out
|
|
* for some EFM32 families to
|
|
*
|
|
#if defined(_GPIO_P_DOUTSET_MASK)
|
|
GPIO->P[port].DOUTSET = pins;
|
|
#elif defined(GPIO_HAS_SET_CLEAR)
|
|
GPIO->P_SET[port].DOUT = pins;
|
|
#else
|
|
(some bit-banding-style interaction on P)
|
|
#endif
|
|
*
|
|
* that approach becomes an unbearable burden, because P_SET is not necessarily
|
|
* as large as P, and getting from a P pointer to a P_SET pointer would involve
|
|
* division and multiplication. Instead, falling back to addressing ports by
|
|
* their index number, which does require an additional multiplication for most
|
|
* accesses, but at least does that consistently.
|
|
*
|
|
* (It also makes things easier because it allows going through the helper
|
|
* functions).
|
|
*
|
|
* There appears to be one truly viable alternative: implementing gpio_ll only
|
|
* for those EFM32 that do have DOUTSET etc. in P, with no way of having such
|
|
* an implementation for other EFM32 families. For the time being, the
|
|
* suboptimal-but-works-for-all version is the best we have.
|
|
*/
|
|
static inline gpio_port_t gpio_port(uword_t num)
|
|
{
|
|
return num;
|
|
}
|
|
|
|
static inline uword_t gpio_port_num(gpio_port_t port)
|
|
{
|
|
return port;
|
|
}
|
|
|
|
static inline uword_t gpio_ll_read(gpio_port_t port)
|
|
{
|
|
return GPIO_PortInGet(port);
|
|
}
|
|
|
|
static inline uword_t gpio_ll_read_output(gpio_port_t port)
|
|
{
|
|
return GPIO_PortOutGet(port);
|
|
}
|
|
|
|
static inline void gpio_ll_set(gpio_port_t port, uword_t mask)
|
|
{
|
|
GPIO_PortOutSet(port, mask);
|
|
}
|
|
|
|
static inline void gpio_ll_clear(gpio_port_t port, uword_t mask)
|
|
{
|
|
GPIO_PortOutClear(port, mask);
|
|
}
|
|
|
|
static inline void gpio_ll_toggle(gpio_port_t port, uword_t mask)
|
|
{
|
|
GPIO_PortOutToggle(port, mask);
|
|
}
|
|
|
|
static inline void gpio_ll_write(gpio_port_t port, uword_t value)
|
|
{
|
|
GPIO->P[port].DOUT = value;
|
|
}
|
|
|
|
static inline gpio_port_t gpio_get_port(gpio_t pin)
|
|
{
|
|
return (pin >> 4);
|
|
}
|
|
|
|
static inline uint8_t gpio_get_pin_num(gpio_t pin)
|
|
{
|
|
return (pin & 0x0f);
|
|
}
|
|
|
|
static inline gpio_port_t gpio_port_pack_addr(void *addr)
|
|
{
|
|
return (gpio_port_t)addr;
|
|
}
|
|
|
|
static inline bool is_gpio_port_num_valid(uint_fast8_t num)
|
|
{
|
|
return GPIO_PORT_VALID(num);
|
|
}
|
|
|
|
static inline void * gpio_port_unpack_addr(gpio_port_t port)
|
|
{
|
|
if ((port & ~0xff) == 0 && is_gpio_port_num_valid(port)) {
|
|
return NULL;
|
|
}
|
|
|
|
return (void *)port;
|
|
}
|
|
|
|
#endif /* DOXYGEN */
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|
|
#endif /* GPIO_LL_ARCH_H */
|
|
/** @} */
|