1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
RIOT/cpu/nrf5x_common/periph/gpio.c

157 lines
3.6 KiB
C
Raw Normal View History

2016-02-04 15:30:06 +01:00
/*
* Copyright (C) 2015 Jan Wagner <mail@jwagner.eu>
2016-02-07 12:56:27 +01:00
* 2015-2016 Freie Universität Berlin
2016-02-04 15:30:06 +01:00
*
* 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.
*/
/**
2016-02-07 12:56:27 +01:00
* @ingroup cpu_nrf5x_common
* @ingroup drivers_periph_gpio
2016-02-04 15:30:06 +01:00
* @{
*
* @file
* @brief Low-level GPIO driver implementation
*
2016-02-07 12:56:27 +01:00
* @note This GPIO driver implementation supports only one pin to be
* defined as external interrupt.
2016-02-04 15:30:06 +01:00
*
2016-02-07 12:56:27 +01:00
* @author Christian Kühling <kuehling@zedat.fu-berlin.de>
* @author Timo Ziegler <timo.ziegler@fu-berlin.de>
2016-02-04 15:30:06 +01:00
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
* @author Jan Wagner <mail@jwagner.eu>
*
* @}
*/
#include "cpu.h"
#include "periph/gpio.h"
2016-02-07 12:56:27 +01:00
#include "periph_cpu.h"
2016-02-04 15:30:06 +01:00
#include "periph_conf.h"
#define PORT_BIT (1 << 5)
#define PIN_MASK (0x1f)
#ifdef MODULE_PERIPH_GPIO_IRQ
2016-02-04 15:30:06 +01:00
/**
* @brief Place to store the interrupt context
*/
static gpio_isr_ctx_t exti_chan;
#endif
2016-02-04 15:30:06 +01:00
/**
* @brief Get the port's base address
*/
static inline NRF_GPIO_Type* port(gpio_t pin)
{
#if (CPU_FAM_NRF51)
build: fix unused parameter errors cpu, sam0_common: fix unused parameter in periph/spi cpu, kinetis_common: fix unused parameter in periph/spi cpu, cc2538: fix unused param in periph/i2c cpu, cc2538: fix unused param in periph/spi cpu, sam3: fix unused param in periph/spi cpu, stm32_common: fix unused param in periph/pm cpu, stm32f3: fix unused params in periph/i2c cpu, nrf5x_common: fix unused param in periph/gpio cpu, nrf5x_common: fix unused param in periph/spi cpu, lpc2387: fix unused params in periph/spi cpu, cc2538: fix unused params in radio/netdev cpu, cc2650: fix unused params in periph/uart cpu, lm4f120: fix unused param in periph/spi cpu, lm4f120: fix unused params in periph/timer cpu, lm4f120: fix unused params in periph/uart cpu, stm32_common: fix unused params in periph/dac cpu, stm32l0: fix unused params in periph/i2c cpu, msp430fxyz: fix unused params in periph/uart cpu, mips: fix unused params cpu, cc430: fix unused-params in periph/timer cpu, msp430fxyz: fix unused params in periph/spi drivers, cc2420: fix unused param cpu, mips32r2_common: fix unused params in periph/timer cpu, cc2538: fix unused-param in periph/i2c cpu, mips32r2_common: fix unused-param in periph/timer cpu, msp430fxyz: fix unused params in periph/timer cpu, atmega_common: fix unused params in periph/spi driver, nrfmin: fix unused params cpu, cc2538_rf: fix unused params driver, netdev_ieee802514: fix unused param cpu, mip_pic32m: fix unused params cpu, lpc2387: fix unused params in periph/pwm tests/driver_sdcard_spi: fix unused params cpu, sam3: fix unused param in periph/pwm tests/driver_dynamixel: fix unused params, and style issues cpu, cc430: fix unused param in periph/rtc cpu, atmega_common: fix unused params in periph/i2c
2017-10-31 12:09:11 +01:00
(void) pin;
return NRF_GPIO;
#elif defined(CPU_MODEL_NRF52832XXAA)
build: fix unused parameter errors cpu, sam0_common: fix unused parameter in periph/spi cpu, kinetis_common: fix unused parameter in periph/spi cpu, cc2538: fix unused param in periph/i2c cpu, cc2538: fix unused param in periph/spi cpu, sam3: fix unused param in periph/spi cpu, stm32_common: fix unused param in periph/pm cpu, stm32f3: fix unused params in periph/i2c cpu, nrf5x_common: fix unused param in periph/gpio cpu, nrf5x_common: fix unused param in periph/spi cpu, lpc2387: fix unused params in periph/spi cpu, cc2538: fix unused params in radio/netdev cpu, cc2650: fix unused params in periph/uart cpu, lm4f120: fix unused param in periph/spi cpu, lm4f120: fix unused params in periph/timer cpu, lm4f120: fix unused params in periph/uart cpu, stm32_common: fix unused params in periph/dac cpu, stm32l0: fix unused params in periph/i2c cpu, msp430fxyz: fix unused params in periph/uart cpu, mips: fix unused params cpu, cc430: fix unused-params in periph/timer cpu, msp430fxyz: fix unused params in periph/spi drivers, cc2420: fix unused param cpu, mips32r2_common: fix unused params in periph/timer cpu, cc2538: fix unused-param in periph/i2c cpu, mips32r2_common: fix unused-param in periph/timer cpu, msp430fxyz: fix unused params in periph/timer cpu, atmega_common: fix unused params in periph/spi driver, nrfmin: fix unused params cpu, cc2538_rf: fix unused params driver, netdev_ieee802514: fix unused param cpu, mip_pic32m: fix unused params cpu, lpc2387: fix unused params in periph/pwm tests/driver_sdcard_spi: fix unused params cpu, sam3: fix unused param in periph/pwm tests/driver_dynamixel: fix unused params, and style issues cpu, cc430: fix unused param in periph/rtc cpu, atmega_common: fix unused params in periph/i2c
2017-10-31 12:09:11 +01:00
(void) pin;
return NRF_P0;
#else
return (pin & PORT_BIT) ? NRF_P1 : NRF_P0;
#endif
}
2016-02-20 14:43:41 +01:00
int gpio_init(gpio_t pin, gpio_mode_t mode)
2016-02-04 15:30:06 +01:00
{
2016-02-20 14:43:41 +01:00
switch (mode) {
case GPIO_IN:
case GPIO_IN_PD:
case GPIO_IN_PU:
case GPIO_OUT:
/* configure pin direction, input buffer and pull resistor state */
port(pin)->PIN_CNF[pin] = mode;
2016-02-20 14:43:41 +01:00
break;
default:
return -1;
}
2016-02-04 15:30:06 +01:00
return 0;
}
int gpio_read(gpio_t pin)
{
if (port(pin)->DIR & (1 << pin)) {
return (port(pin)->OUT & (1 << pin)) ? 1 : 0;
}
else {
return (port(pin)->IN & (1 << pin)) ? 1 : 0;
}
}
void gpio_set(gpio_t pin)
{
port(pin)->OUTSET = (1 << pin);
}
void gpio_clear(gpio_t pin)
{
port(pin)->OUTCLR = (1 << pin);
}
void gpio_toggle(gpio_t pin)
{
port(pin)->OUT ^= (1 << pin);
}
void gpio_write(gpio_t pin, int value)
{
if (value) {
port(pin)->OUTSET = (1 << pin);
} else {
port(pin)->OUTCLR = (1 << pin);
}
}
#ifdef MODULE_PERIPH_GPIO_IRQ
2016-02-20 14:43:41 +01:00
int gpio_init_int(gpio_t pin, gpio_mode_t mode, gpio_flank_t flank,
2016-02-04 15:30:06 +01:00
gpio_cb_t cb, void *arg)
{
/* disable external interrupt in case one is active */
NRF_GPIOTE->INTENSET &= ~(GPIOTE_INTENSET_IN0_Msk);
/* save callback */
exti_chan.cb = cb;
exti_chan.arg = arg;
/* configure pin as input */
2016-02-20 14:43:41 +01:00
gpio_init(pin, mode);
2016-02-04 15:30:06 +01:00
/* set interrupt priority and enable global GPIOTE interrupt */
NVIC_EnableIRQ(GPIOTE_IRQn);
/* configure the GPIOTE channel: set even mode, pin and active flank */
NRF_GPIOTE->CONFIG[0] = (GPIOTE_CONFIG_MODE_Event |
(pin << GPIOTE_CONFIG_PSEL_Pos) |
#ifdef CPU_MODEL_NRF52840XXAA
((pin & PORT_BIT) << 8) |
#endif
2016-02-04 15:30:06 +01:00
(flank << GPIOTE_CONFIG_POLARITY_Pos));
/* enable external interrupt */
NRF_GPIOTE->INTENSET |= GPIOTE_INTENSET_IN0_Msk;
return 0;
}
void gpio_irq_enable(gpio_t pin)
{
(void) pin;
NRF_GPIOTE->INTENSET |= GPIOTE_INTENSET_IN0_Msk;
}
void gpio_irq_disable(gpio_t pin)
{
(void) pin;
NRF_GPIOTE->INTENCLR |= GPIOTE_INTENSET_IN0_Msk;
}
void isr_gpiote(void)
{
if (NRF_GPIOTE->EVENTS_IN[0] == 1) {
NRF_GPIOTE->EVENTS_IN[0] = 0;
exti_chan.cb(exti_chan.arg);
}
2016-11-30 18:26:05 +01:00
cortexm_isr_end();
2016-02-04 15:30:06 +01:00
}
#endif