mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
e381317fbf
cpu, nrf5x_common: fix sign-compare in periph/flashpage drivers, periph_common: fix sign-compare in flashpage cpu, sam0_common: fix sign-compare error in periph/gpio cpu, cc2538: fix sign-compare in periph/timer cpu, sam3: fix sign-compare in periph/gpio cpu, stm32_common: fix sign-compare in periph/pwm cpu, stm32_common: fix sign-compare in periph/timer cpu, stm32_common: fix sign-compare in periph/flashpage cpu, nrf5x_common: fix sign-compare in radio/nrfmin cpu, samd21: fix sign-compare in periph/pwm cpu, ezr32wg: fix sign-compare in periph/gpio cpu, ezr32wg: fix sign-compare in periph/timer drivers, ethos: fix sign-compare sys, net: fix sign-compare cpu, atmega_common: fix sign-compare error cpu, msp430fxyz: fix sign-compare in periph/gpio boards, msb-430-common: fix sign-compare in board_init driver, cc2420: fix sign-compared sys/net: fix sign-compare in gnrc_tftp driver, pcd8544: fix sign-compare driver, pn532: fix sign-compare driver, sdcard_spi: fix sign-compare tests: fix sign_compare sys/net, lwmac: fix sign_compare pkg, lwip: fix sign-compare boards, waspmote: make CORECLOCK unsigned long to fix sign_compare error tests, sock_ip: fix sign compare tests, msg_avail: fix sign compare tests, sock_udp: fix sign compare boards: fix sign-compare for calliope and microbit matrix
162 lines
3.5 KiB
C
162 lines
3.5 KiB
C
/*
|
|
* Copyright (C) 2015 Freie Universität Berlin
|
|
*
|
|
* 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_ezr32wg
|
|
* @ingroup drivers_periph_gpio
|
|
* @{
|
|
*
|
|
* @file
|
|
* @brief Low-level GPIO driver implementation
|
|
*
|
|
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
|
*
|
|
* @}
|
|
*/
|
|
|
|
#include "cpu.h"
|
|
#include "periph/gpio.h"
|
|
#include "periph_conf.h"
|
|
|
|
#define ENABLE_DEBUG (0)
|
|
#include "debug.h"
|
|
|
|
/**
|
|
* @brief Number of external interrupt lines
|
|
*/
|
|
#define NUMOF_IRQS (16U)
|
|
/**
|
|
* @brief Hold one interrupt context per interrupt line
|
|
*/
|
|
static gpio_isr_ctx_t isr_ctx[NUMOF_IRQS];
|
|
|
|
static inline int _port_num(gpio_t pin)
|
|
{
|
|
return (pin & 0xf0) >> 4;
|
|
}
|
|
|
|
static inline GPIO_P_TypeDef *_port(gpio_t pin)
|
|
{
|
|
return (GPIO_P_TypeDef *)(&GPIO->P[_port_num(pin)]);
|
|
}
|
|
|
|
static inline int _pin_pos(gpio_t pin)
|
|
{
|
|
return (pin & 0x0f);
|
|
}
|
|
|
|
static inline int _pin_mask(gpio_t pin)
|
|
{
|
|
return (1 << _pin_pos(pin));
|
|
}
|
|
|
|
int gpio_init(gpio_t pin, gpio_mode_t mode)
|
|
{
|
|
GPIO_P_TypeDef *port = _port(pin);
|
|
uint32_t pin_pos = _pin_pos(pin);
|
|
|
|
if (mode == GPIO_IN_PD) {
|
|
return -1;
|
|
}
|
|
|
|
/* enable power for the GPIO module */
|
|
CMU->HFPERCLKEN0 |= CMU_HFPERCLKEN0_GPIO;
|
|
|
|
/* configure the mode */
|
|
port->MODE[pin_pos >> 3] &= ~(0xf << ((pin_pos & 0x7) * 4));
|
|
port->MODE[pin_pos >> 3] |= (mode << ((pin_pos & 0x7) * 4));
|
|
/* if input with pull-up, set the data out register */
|
|
if (mode == GPIO_IN_PU) {
|
|
port->DOUTSET = (1 << pin_pos);
|
|
} else if (mode == GPIO_IN) {
|
|
port->DOUTCLR = (1 << pin_pos);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
int gpio_init_int(gpio_t pin, gpio_mode_t mode, gpio_flank_t flank,
|
|
gpio_cb_t cb, void *arg)
|
|
{
|
|
uint32_t pin_pos = _pin_pos(pin);
|
|
|
|
/* configure as input */
|
|
gpio_init(pin, mode);
|
|
|
|
/* just in case, disable interrupt for this channel */
|
|
GPIO->IEN &= ~(1 << pin_pos);
|
|
/* save callback */
|
|
isr_ctx[pin_pos].cb = cb;
|
|
isr_ctx[pin_pos].arg = arg;
|
|
/* configure interrupt */
|
|
GPIO->EXTIPSEL[pin_pos >> 3] &= (0x7 << ((pin_pos & 0x7) * 4));
|
|
GPIO->EXTIPSEL[pin_pos >> 3] |= (_port_num(pin) << ((pin_pos & 0x7) * 4));
|
|
GPIO->EXTIRISE &= ~(1 << pin_pos);
|
|
GPIO->EXTIRISE |= ((flank & 0x1) << pin_pos);
|
|
GPIO->EXTIFALL &= ~(1 << pin_pos);
|
|
GPIO->EXTIFALL &= (((flank & 0x2) >> 1) << pin_pos);
|
|
/* enable global GPIO IRQ */
|
|
NVIC_EnableIRQ(GPIO_EVEN_IRQn);
|
|
/* enable the interrupt channel */
|
|
GPIO->IEN |= (1 << pin_pos);
|
|
return 0;
|
|
}
|
|
|
|
void gpio_irq_enable(gpio_t pin)
|
|
{
|
|
GPIO->IEN |= _pin_mask(pin);
|
|
}
|
|
|
|
void gpio_irq_disable(gpio_t pin)
|
|
{
|
|
GPIO->IEN &= ~(_pin_mask(pin));
|
|
}
|
|
|
|
int gpio_read(gpio_t pin)
|
|
{
|
|
return _port(pin)->DIN & _pin_mask(pin);
|
|
}
|
|
|
|
void gpio_set(gpio_t pin)
|
|
{
|
|
_port(pin)->DOUTSET = _pin_mask(pin);
|
|
}
|
|
|
|
void gpio_clear(gpio_t pin)
|
|
{
|
|
_port(pin)->DOUTCLR = _pin_mask(pin);
|
|
}
|
|
|
|
void gpio_toggle(gpio_t pin)
|
|
{
|
|
_port(pin)->DOUTTGL = _pin_mask(pin);
|
|
}
|
|
|
|
void gpio_write(gpio_t pin, int value)
|
|
{
|
|
if (value) {
|
|
_port(pin)->DOUTSET = _pin_mask(pin);
|
|
} else {
|
|
_port(pin)->DOUTCLR = _pin_mask(pin);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief External interrupt handler
|
|
*/
|
|
void isr_gpio_even(void)
|
|
{
|
|
for (unsigned i = 0; i < NUMOF_IRQS; i++) {
|
|
if (GPIO->IF & (1 << i)) {
|
|
isr_ctx[i].cb(isr_ctx[i].arg);
|
|
GPIO->IFC = (1 << i);
|
|
}
|
|
}
|
|
cortexm_isr_end();
|
|
}
|