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
179 lines
3.8 KiB
C
179 lines
3.8 KiB
C
/*
|
|
* Copyright (C) 2015 Jan Wagner <mail@jwagner.eu>
|
|
* 2015-2016 Freie Universität Berlin
|
|
* 2019 Inria
|
|
*
|
|
* 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_nrf5x_common
|
|
* @ingroup drivers_periph_gpio_ll
|
|
* @{
|
|
*
|
|
* @file
|
|
* @brief CPU specific part of the Peripheral GPIO Low-Level API
|
|
*
|
|
* @note This GPIO driver implementation supports only one pin to be
|
|
* defined as external interrupt.
|
|
*
|
|
* @author Christian Kühling <kuehling@zedat.fu-berlin.de>
|
|
* @author Timo Ziegler <timo.ziegler@fu-berlin.de>
|
|
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
|
* @author Jan Wagner <mail@jwagner.eu>
|
|
* @author Alexandre Abadie <alexandre.abadie@inria.fr>
|
|
*/
|
|
|
|
#ifndef GPIO_LL_ARCH_H
|
|
#define GPIO_LL_ARCH_H
|
|
|
|
#include <assert.h>
|
|
|
|
#include "cpu.h"
|
|
#include "irq.h"
|
|
#include "periph_cpu.h"
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
#ifndef DOXYGEN /* hide implementation specific details from Doxygen */
|
|
|
|
#define PORT_BIT (1 << 5)
|
|
#define PIN_MASK (0x1f)
|
|
#define NRF5X_IO_AREA_START (0x40000000UL)
|
|
|
|
/* Compatibility wrapper defines for nRF9160 */
|
|
#ifdef NRF_P0_S
|
|
#define NRF_P0 NRF_P0_S
|
|
#endif
|
|
|
|
#if defined(CPU_FAM_NRF51)
|
|
# define GPIO_PORT_0 ((gpio_port_t)NRF_GPIO)
|
|
#else
|
|
# if defined(NRF_P1)
|
|
# define GPIO_PORT_1 ((gpio_port_t)NRF_P1)
|
|
# endif
|
|
# define GPIO_PORT_0 ((gpio_port_t)NRF_P0)
|
|
#endif
|
|
|
|
static inline gpio_port_t gpio_port(uword_t num)
|
|
{
|
|
(void)num;
|
|
#ifdef GPIO_PORT_1
|
|
if (num == 1) {
|
|
return GPIO_PORT_1;
|
|
}
|
|
#endif
|
|
|
|
return GPIO_PORT_0;
|
|
}
|
|
|
|
static inline uword_t gpio_port_num(gpio_port_t port)
|
|
{
|
|
(void)port;
|
|
#ifdef GPIO_PORT_1
|
|
if (port == GPIO_PORT_1) {
|
|
return 1;
|
|
}
|
|
#endif
|
|
return 0;
|
|
}
|
|
|
|
static inline uword_t gpio_ll_read(gpio_port_t port)
|
|
{
|
|
NRF_GPIO_Type *p = (NRF_GPIO_Type *)port;
|
|
return p->IN;
|
|
}
|
|
|
|
static inline uword_t gpio_ll_read_output(gpio_port_t port)
|
|
{
|
|
NRF_GPIO_Type *p = (NRF_GPIO_Type *)port;
|
|
return p->OUT;
|
|
}
|
|
|
|
static inline void gpio_ll_set(gpio_port_t port, uword_t mask)
|
|
{
|
|
NRF_GPIO_Type *p = (NRF_GPIO_Type *)port;
|
|
p->OUTSET = mask;
|
|
}
|
|
|
|
static inline void gpio_ll_clear(gpio_port_t port, uword_t mask)
|
|
{
|
|
NRF_GPIO_Type *p = (NRF_GPIO_Type *)port;
|
|
p->OUTCLR = mask;
|
|
}
|
|
|
|
static inline void gpio_ll_toggle(gpio_port_t port, uword_t mask)
|
|
{
|
|
NRF_GPIO_Type *p = (NRF_GPIO_Type *)port;
|
|
unsigned state = irq_disable();
|
|
p->OUT ^= mask;
|
|
irq_restore(state);
|
|
}
|
|
|
|
static inline void gpio_ll_write(gpio_port_t port, uword_t value)
|
|
{
|
|
NRF_GPIO_Type *p = (NRF_GPIO_Type *)port;
|
|
p->OUT = value;
|
|
}
|
|
|
|
static inline gpio_port_t gpio_get_port(gpio_t pin)
|
|
{
|
|
#if defined(NRF_P1)
|
|
return gpio_port(pin >> 5);
|
|
#else
|
|
(void)pin;
|
|
return GPIO_PORT_0;
|
|
#endif
|
|
}
|
|
|
|
static inline uint8_t gpio_get_pin_num(gpio_t pin)
|
|
{
|
|
#if defined(NRF_P1)
|
|
return pin & PIN_MASK;
|
|
#else
|
|
return (uint8_t)pin;
|
|
#endif
|
|
}
|
|
|
|
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)
|
|
{
|
|
/* NRF5X_IO_AREA_START is the start of the memory mapped I/O area. Both data
|
|
* and flash are mapped before it. So if it is an I/O address, it
|
|
* cannot be a packed data address and (hopefully) is a GPIO port */
|
|
if (port >= NRF5X_IO_AREA_START) {
|
|
return NULL;
|
|
}
|
|
|
|
return (void *)port;
|
|
}
|
|
|
|
static inline bool is_gpio_port_num_valid(uint_fast8_t num)
|
|
{
|
|
switch (num) {
|
|
default:
|
|
return false;
|
|
case 0:
|
|
#if defined(NRF_P1)
|
|
case 1:
|
|
#endif
|
|
return true;
|
|
}
|
|
}
|
|
|
|
#endif /* DOXYGEN */
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|
|
#endif /* GPIO_LL_ARCH_H */
|
|
/** @} */
|