1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
RIOT/cpu/gd32v/periph/gpio_ll_irq.c
Marian Buschsieweke 36e8526046
drivers/periph_gpio_ll: change API to access GPIO ports
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
2024-08-02 09:55:24 +02:00

193 lines
5.0 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_irq
* @{
*
* @file
* @brief IRQ implementation of the GPIO Low-Level API for GD32V
*
* @author Gunar Schorcht <gunar@schorcht.net>
* @}
*/
#include <errno.h>
#include "cpu.h"
#include "bitarithm.h"
#include "periph/gpio_ll_irq.h"
#define ENABLE_DEBUG 0
#include "debug.h"
/**
* @brief Number of available external interrupt lines
*/
#define GPIO_ISR_CHAN_NUMOF (16U)
#define GPIO_ISR_CHAN_MASK (0xFFFF)
struct _gpio_isr_ctx {
gpio_ll_cb_t cb;
void *arg;
};
/**
* @brief Allocate memory for one callback and argument per EXTI channel
*/
static struct _gpio_isr_ctx _exti_ctx[GPIO_ISR_CHAN_NUMOF];
/* keep the state of level triggered interrupt activation using 32-bit values
* since the registers require 32-bit access */
static uint32_t _h_level_triggered = 0;
static uint32_t _l_level_triggered = 0;
void gpio_ll_irq_mask(gpio_port_t port, uint8_t pin)
{
(void)port;
EXTI->INTEN &= ~(1 << pin);
}
void gpio_ll_irq_unmask_and_clear(gpio_port_t port, uint8_t pin)
{
(void)port;
EXTI->PD = (1 << pin);
EXTI->INTEN |= (1 << pin);
}
static inline unsigned _irq_num(unsigned pin)
{
if (pin < 5) {
return EXTI0_IRQn + pin;
}
if (pin < 10) {
return EXTI5_9_IRQn;
}
return EXTI10_15_IRQn;
}
/* Forward declaration of ISR */
static void _gpio_isr(unsigned irqn);
int gpio_ll_irq(gpio_port_t port, uint8_t pin, gpio_irq_trig_t trig, gpio_ll_cb_t cb, void *arg)
{
unsigned irq_state = irq_disable();
int port_num = gpio_port_num(port);
/* set callback */
_exti_ctx[pin].cb = cb;
_exti_ctx[pin].arg = arg;
/* enable alternate function clock for the GPIO module */
periph_clk_en(APB2, RCU_APB2EN_AFEN_Msk);
/* configure the EXTI channel */
volatile uint32_t *afio_exti_ss = &AFIO->EXTISS0 + (pin >> 2);
*afio_exti_ss &= ~(0xfUL << ((pin & 0x03) * 4));
*afio_exti_ss |= (uint32_t)port_num << ((pin & 0x03) * 4);
uint32_t pin_mask = 1UL << pin;
_h_level_triggered &= ~pin_mask;
_l_level_triggered &= ~pin_mask;
EXTI->RTEN &= ~pin_mask;
EXTI->FTEN &= ~pin_mask;
/* configure the active flank, level interrupts are emulated */
switch (trig) {
case GPIO_TRIGGER_EDGE_RISING:
EXTI->RTEN |= pin_mask;
break;
case GPIO_TRIGGER_EDGE_FALLING:
EXTI->FTEN |= pin_mask;
break;
case GPIO_TRIGGER_EDGE_BOTH:
EXTI->RTEN |= pin_mask;
EXTI->FTEN |= pin_mask;
break;
case GPIO_TRIGGER_LEVEL_HIGH:
EXTI->RTEN |= pin_mask;
_h_level_triggered |= pin_mask;
break;
case GPIO_TRIGGER_LEVEL_LOW:
EXTI->FTEN |= pin_mask;
_l_level_triggered |= pin_mask;
break;
}
/* clear pending interrupts */
EXTI->PD = pin_mask;
/* enable global pin interrupt */
unsigned irqn = _irq_num(pin);
clic_set_handler(irqn, _gpio_isr);
clic_enable_interrupt(irqn, CPU_DEFAULT_IRQ_PRIO);
/* unmask the pins interrupt channel */
EXTI->INTEN |= pin_mask;
/* emulate a level interrupt if the pin already has the level */
uint32_t level = gpio_ll_read(port) & pin_mask;
if ((trig == GPIO_TRIGGER_LEVEL_HIGH) && level) {
EXTI->SWIEV |= pin_mask;
}
else if ((trig == GPIO_TRIGGER_LEVEL_LOW) && !level) {
EXTI->SWIEV |= pin_mask;
}
irq_restore(irq_state);
return 0;
}
static void _gpio_isr(unsigned irqn)
{
(void)irqn;
/* read pending interrupts */
uint32_t pending = (EXTI->PD & GPIO_ISR_CHAN_MASK);
/* clear pending interrupts */
EXTI->PD = pending;
/* generate soft interrupts for lines which have their interrupt enabled */
pending &= EXTI->INTEN;
/* iterate over all set bits */
uint8_t pin = 0;
while (pending) {
/* get next pin with interrupt */
pending = bitarithm_test_and_clear(pending, &pin);
/* call registered callball function */
_exti_ctx[pin].cb(_exti_ctx[pin].arg);
/* emulate level triggered IRQs by asserting the IRQ again in software */
uint32_t pin_mask = 1UL << pin;
if ((_h_level_triggered & pin_mask) || (_l_level_triggered & pin_mask)) {
/* determine the port of triggered interrupt */
volatile uint32_t *afio_exti_ss = &AFIO->EXTISS0 + (pin >> 2);
gpio_port_t port = gpio_port(((*afio_exti_ss >> ((pin & 0x03) * 4)) & 0xfUL));
/* trigger software interrupt if the pin has the according level */
uint32_t level = gpio_ll_read(port) & pin_mask;
if ((_h_level_triggered & pin_mask) && level) {
EXTI->SWIEV |= pin_mask;
}
else if ((_l_level_triggered & pin_mask) && !level) {
EXTI->SWIEV |= pin_mask;
}
}
}
}