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
170 lines
3.1 KiB
C
170 lines
3.1 KiB
C
/*
|
|
* Copyright (C) 2023 Gunar Schorcht <gunar@schorcht.net>
|
|
*
|
|
* 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_gd32v
|
|
* @ingroup drivers_periph_gpio_ll
|
|
* @{
|
|
*
|
|
* @file
|
|
* @brief GPIO Low-level API implementation for the GD32V GPIO peripheral
|
|
*
|
|
* @author Gunar Schorcht <gunar@schorcht.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 */
|
|
|
|
/**
|
|
* @brief Number of ports available on GD32VF103
|
|
*/
|
|
#define GPIO_PORT_NUMOF 5
|
|
|
|
#define GPIO_PORT_NUMBERING_ALPHABETIC 1
|
|
|
|
#ifdef GPIOA_BASE
|
|
# define GPIO_PORT_0 GPIOA_BASE
|
|
#endif
|
|
|
|
#ifdef GPIOB_BASE
|
|
# define GPIO_PORT_1 GPIOB_BASE
|
|
#endif
|
|
|
|
#ifdef GPIOC_BASE
|
|
# define GPIO_PORT_2 GPIOC_BASE
|
|
#endif
|
|
|
|
#ifdef GPIOD_BASE
|
|
# define GPIO_PORT_3 GPIOD_BASE
|
|
#endif
|
|
|
|
#ifdef GPIOE_BASE
|
|
# define GPIO_PORT_4 GPIOE_BASE
|
|
#endif
|
|
|
|
#ifdef GPIOF_BASE
|
|
# define GPIO_PORT_5 GPIOF_BASE
|
|
#endif
|
|
|
|
#ifdef GPIOG_BASE
|
|
# define GPIO_PORT_6 GPIOG_BASE
|
|
#endif
|
|
|
|
#ifdef GPIOH_BASE
|
|
# define GPIO_PORT_7 GPIOH_BASE
|
|
#endif
|
|
|
|
#ifdef GPIOI_BASE
|
|
# define GPIO_PORT_8 GPIOI_BASE
|
|
#endif
|
|
|
|
#ifdef GPIOJ_BASE
|
|
# define GPIO_PORT_9 GPIOJ_BASE
|
|
#endif
|
|
|
|
#ifdef GPIOK_BASE
|
|
# define GPIO_PORT_10 GPIOK_BASE
|
|
#endif
|
|
|
|
static inline gpio_port_t gpio_port(uword_t num)
|
|
{
|
|
#if defined(CPU_FAM_STM32MP1)
|
|
return GPIOA_BASE + (num << 12);
|
|
#else
|
|
return GPIOA_BASE + (num << 10);
|
|
#endif
|
|
}
|
|
|
|
static inline uword_t gpio_port_num(gpio_port_t port)
|
|
{
|
|
#if defined(CPU_FAM_STM32MP1)
|
|
return (port - GPIOA_BASE) >> 12;
|
|
#else
|
|
return (port - GPIOA_BASE) >> 10;
|
|
#endif
|
|
}
|
|
|
|
static inline uword_t gpio_ll_read(gpio_port_t port)
|
|
{
|
|
return ((GPIO_Type *)port)->ISTAT;
|
|
}
|
|
|
|
static inline uword_t gpio_ll_read_output(gpio_port_t port)
|
|
{
|
|
return ((GPIO_Type *)port)->OCTL;
|
|
}
|
|
|
|
static inline void gpio_ll_set(gpio_port_t port, uword_t mask)
|
|
{
|
|
((GPIO_Type *)port)->BOP = mask;
|
|
}
|
|
|
|
static inline void gpio_ll_clear(gpio_port_t port, uword_t mask)
|
|
{
|
|
((GPIO_Type *)port)->BOP = mask << 16;
|
|
}
|
|
|
|
static inline void gpio_ll_toggle(gpio_port_t port, uword_t mask)
|
|
{
|
|
unsigned irq_state = irq_disable();
|
|
((GPIO_Type *)port)->OCTL ^= mask;
|
|
irq_restore(irq_state);
|
|
}
|
|
|
|
static inline void gpio_ll_write(gpio_port_t port, uword_t value)
|
|
{
|
|
((GPIO_Type *)port)->OCTL = value;
|
|
}
|
|
|
|
static inline gpio_port_t gpio_get_port(gpio_t pin)
|
|
{
|
|
return pin & 0xfffffff0UL;
|
|
}
|
|
|
|
static inline uint8_t gpio_get_pin_num(gpio_t pin)
|
|
{
|
|
return pin & 0xfUL;
|
|
}
|
|
|
|
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 < GPIOA_BASE) {
|
|
return (void *)port;
|
|
}
|
|
|
|
return NULL;
|
|
}
|
|
|
|
static inline bool is_gpio_port_num_valid(uint_fast8_t num)
|
|
{
|
|
return num < GPIO_PORT_NUMOF;
|
|
}
|
|
|
|
#endif /* DOXYGEN */
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|
|
#endif /* GPIO_LL_ARCH_H */
|
|
/** @} */
|