2016-02-04 15:30:06 +01:00
|
|
|
/*
|
2017-02-07 16:17:41 +01:00
|
|
|
* Copyright (C) 2014-2017 Freie Universität Berlin
|
2016-02-07 13:29:16 +01:00
|
|
|
* 2015 Jan Wagner <mail@jwagner.eu>
|
2018-12-17 10:08:58 +01:00
|
|
|
* 2018 Inria
|
2016-02-07 13:29:16 +01:00
|
|
|
*
|
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 13:29:16 +01:00
|
|
|
* @ingroup cpu_nrf5x_common
|
2017-06-22 15:43:17 +02:00
|
|
|
* @ingroup drivers_periph_uart
|
2016-02-04 15:30:06 +01:00
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @brief Implementation of the peripheral UART interface
|
|
|
|
*
|
2016-02-07 13:29:16 +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>
|
2018-12-17 10:08:58 +01:00
|
|
|
* @author Alexandre Abadie <alexandre.abadie@inria.fr>
|
2016-02-04 15:30:06 +01:00
|
|
|
*
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include "cpu.h"
|
|
|
|
#include "periph/uart.h"
|
2017-04-05 12:10:13 +02:00
|
|
|
#include "periph/gpio.h"
|
2016-02-07 13:29:16 +01:00
|
|
|
|
2017-04-05 12:10:13 +02:00
|
|
|
#ifdef CPU_MODEL_NRF52840XXAA
|
2018-12-17 10:08:58 +01:00
|
|
|
#define PSEL_RXD dev(uart)->PSEL.RXD
|
|
|
|
#define PSEL_TXD dev(uart)->PSEL.TXD
|
|
|
|
#define PSEL_RTS dev(uart)->PSEL.RTS
|
|
|
|
#define PSEL_CTS dev(uart)->PSEL.CTS
|
|
|
|
#define UART_IRQN uart_config[uart].irqn
|
|
|
|
#define UART_PIN_RX uart_config[uart].rx_pin
|
|
|
|
#define UART_PIN_TX uart_config[uart].tx_pin
|
|
|
|
#define UART_PIN_RTS uart_config[uart].rts_pin
|
|
|
|
#define UART_PIN_CTS uart_config[uart].cts_pin
|
|
|
|
#define UART_HWFLOWCTRL (uart_config[uart].rts_pin != GPIO_UNDEF && \
|
|
|
|
uart_config[uart].cts_pin != GPIO_UNDEF)
|
|
|
|
#define ISR_CTX isr_ctx[uart]
|
|
|
|
/**
|
|
|
|
* @brief Allocate memory for the interrupt context
|
|
|
|
*/
|
|
|
|
static uart_isr_ctx_t isr_ctx[UART_NUMOF];
|
2017-04-05 12:10:13 +02:00
|
|
|
#else
|
2018-12-17 10:08:58 +01:00
|
|
|
#define PSEL_RXD dev(uart)->PSELRXD
|
|
|
|
#define PSEL_TXD dev(uart)->PSELTXD
|
|
|
|
#define PSEL_RTS dev(uart)->PSELRTS
|
|
|
|
#define PSEL_CTS dev(uart)->PSELCTS
|
|
|
|
#define UART_0_ISR isr_uart0
|
2018-12-16 12:09:57 +01:00
|
|
|
#ifndef UART_PIN_RTS
|
2018-12-17 10:08:58 +01:00
|
|
|
#define UART_PIN_RTS GPIO_UNDEF
|
2018-12-16 12:09:57 +01:00
|
|
|
#endif
|
|
|
|
#ifndef UART_PIN_CTS
|
2018-12-17 10:08:58 +01:00
|
|
|
#define UART_PIN_CTS GPIO_UNDEF
|
2018-12-16 12:09:57 +01:00
|
|
|
#endif
|
|
|
|
#ifndef UART_HWFLOWCTRL
|
2018-12-17 10:08:58 +01:00
|
|
|
#define UART_HWFLOWCTRL 0
|
2018-12-16 12:09:57 +01:00
|
|
|
#endif
|
2018-12-17 10:08:58 +01:00
|
|
|
#define ISR_CTX isr_ctx
|
2016-02-04 15:30:06 +01:00
|
|
|
/**
|
2016-02-07 13:29:16 +01:00
|
|
|
* @brief Allocate memory for the interrupt context
|
2016-02-04 15:30:06 +01:00
|
|
|
*/
|
2018-12-15 16:06:05 +01:00
|
|
|
static uart_isr_ctx_t isr_ctx;
|
2018-12-17 10:08:58 +01:00
|
|
|
#endif
|
2018-12-15 16:06:05 +01:00
|
|
|
|
|
|
|
#ifdef CPU_MODEL_NRF52840XXAA
|
2018-12-17 10:08:58 +01:00
|
|
|
static inline NRF_UARTE_Type *dev(uart_t uart)
|
|
|
|
{
|
2018-12-15 16:06:05 +01:00
|
|
|
return uart_config[uart].dev;
|
2018-12-17 10:08:58 +01:00
|
|
|
}
|
|
|
|
static uint8_t rx_buf[UART_NUMOF];
|
2018-12-15 16:06:05 +01:00
|
|
|
#else
|
2018-12-17 10:08:58 +01:00
|
|
|
static inline NRF_UART_Type *dev(uart_t uart)
|
|
|
|
{
|
2018-12-15 16:06:05 +01:00
|
|
|
(void)uart;
|
2018-12-17 10:08:58 +01:00
|
|
|
return NRF_UART0;
|
2018-12-15 16:06:05 +01:00
|
|
|
}
|
2018-12-17 10:08:58 +01:00
|
|
|
#endif
|
2016-02-04 15:30:06 +01:00
|
|
|
|
|
|
|
int uart_init(uart_t uart, uint32_t baudrate, uart_rx_cb_t rx_cb, void *arg)
|
|
|
|
{
|
2018-12-15 16:06:05 +01:00
|
|
|
assert(uart < UART_NUMOF);
|
2016-02-04 15:30:06 +01:00
|
|
|
|
|
|
|
/* remember callback addresses and argument */
|
2018-12-17 10:08:58 +01:00
|
|
|
ISR_CTX.rx_cb = rx_cb;
|
|
|
|
ISR_CTX.arg = arg;
|
2016-02-04 15:30:06 +01:00
|
|
|
|
2016-02-07 13:29:16 +01:00
|
|
|
#ifdef CPU_FAM_NRF51
|
|
|
|
/* power on the UART device */
|
2018-12-15 16:06:05 +01:00
|
|
|
dev(uart)->POWER = 1;
|
2016-02-07 13:29:16 +01:00
|
|
|
#endif
|
2017-02-07 16:17:41 +01:00
|
|
|
|
2016-02-04 15:30:06 +01:00
|
|
|
/* reset configuration registers */
|
2018-12-15 16:06:05 +01:00
|
|
|
dev(uart)->CONFIG = 0;
|
2017-02-07 16:17:41 +01:00
|
|
|
|
|
|
|
/* configure RX pin */
|
|
|
|
if (rx_cb) {
|
2017-04-05 12:10:13 +02:00
|
|
|
gpio_init(UART_PIN_RX, GPIO_IN);
|
|
|
|
PSEL_RXD = UART_PIN_RX;
|
2017-02-07 16:17:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* configure TX pin */
|
2017-04-05 12:10:13 +02:00
|
|
|
gpio_init(UART_PIN_TX, GPIO_OUT);
|
|
|
|
PSEL_TXD = UART_PIN_TX;
|
2017-02-07 16:17:41 +01:00
|
|
|
|
2016-02-04 15:30:06 +01:00
|
|
|
/* enable HW-flow control if defined */
|
2018-12-16 12:09:57 +01:00
|
|
|
if (UART_HWFLOWCTRL) {
|
|
|
|
/* set pin mode for RTS and CTS pins */
|
|
|
|
gpio_init(UART_PIN_RTS, GPIO_OUT);
|
|
|
|
gpio_init(UART_PIN_CTS, GPIO_IN);
|
|
|
|
/* configure RTS and CTS pins to use */
|
|
|
|
PSEL_RTS = UART_PIN_RTS;
|
|
|
|
PSEL_CTS = UART_PIN_CTS;
|
|
|
|
dev(uart)->CONFIG |= UART_CONFIG_HWFC_Msk; /* enable HW flow control */
|
|
|
|
} else {
|
|
|
|
PSEL_RTS = 0xffffffff; /* pin disconnected */
|
|
|
|
PSEL_CTS = 0xffffffff; /* pin disconnected */
|
|
|
|
}
|
2016-02-04 15:30:06 +01:00
|
|
|
|
|
|
|
/* select baudrate */
|
|
|
|
switch (baudrate) {
|
|
|
|
case 1200:
|
2018-12-15 16:06:05 +01:00
|
|
|
dev(uart)->BAUDRATE = UART_BAUDRATE_BAUDRATE_Baud1200;
|
2016-02-04 15:30:06 +01:00
|
|
|
break;
|
|
|
|
case 2400:
|
2018-12-15 16:06:05 +01:00
|
|
|
dev(uart)->BAUDRATE = UART_BAUDRATE_BAUDRATE_Baud2400;
|
2016-02-04 15:30:06 +01:00
|
|
|
break;
|
|
|
|
case 4800:
|
2018-12-15 16:06:05 +01:00
|
|
|
dev(uart)->BAUDRATE = UART_BAUDRATE_BAUDRATE_Baud4800;
|
2016-02-04 15:30:06 +01:00
|
|
|
break;
|
|
|
|
case 9600:
|
2018-12-15 16:06:05 +01:00
|
|
|
dev(uart)->BAUDRATE = UART_BAUDRATE_BAUDRATE_Baud9600;
|
2016-02-04 15:30:06 +01:00
|
|
|
break;
|
|
|
|
case 14400:
|
2018-12-15 16:06:05 +01:00
|
|
|
dev(uart)->BAUDRATE = UART_BAUDRATE_BAUDRATE_Baud14400;
|
2016-02-04 15:30:06 +01:00
|
|
|
break;
|
|
|
|
case 19200:
|
2018-12-15 16:06:05 +01:00
|
|
|
dev(uart)->BAUDRATE = UART_BAUDRATE_BAUDRATE_Baud19200;
|
2016-02-04 15:30:06 +01:00
|
|
|
break;
|
|
|
|
case 28800:
|
2018-12-15 16:06:05 +01:00
|
|
|
dev(uart)->BAUDRATE = UART_BAUDRATE_BAUDRATE_Baud28800;
|
2016-02-04 15:30:06 +01:00
|
|
|
break;
|
|
|
|
case 38400:
|
2018-12-15 16:06:05 +01:00
|
|
|
dev(uart)->BAUDRATE = UART_BAUDRATE_BAUDRATE_Baud38400;
|
2016-02-04 15:30:06 +01:00
|
|
|
break;
|
|
|
|
case 57600:
|
2018-12-15 16:06:05 +01:00
|
|
|
dev(uart)->BAUDRATE = UART_BAUDRATE_BAUDRATE_Baud57600;
|
2016-02-04 15:30:06 +01:00
|
|
|
break;
|
|
|
|
case 76800:
|
2018-12-15 16:06:05 +01:00
|
|
|
dev(uart)->BAUDRATE = UART_BAUDRATE_BAUDRATE_Baud76800;
|
2016-02-04 15:30:06 +01:00
|
|
|
break;
|
|
|
|
case 115200:
|
2018-12-15 16:06:05 +01:00
|
|
|
dev(uart)->BAUDRATE = UART_BAUDRATE_BAUDRATE_Baud115200;
|
2016-02-04 15:30:06 +01:00
|
|
|
break;
|
|
|
|
case 230400:
|
2018-12-15 16:06:05 +01:00
|
|
|
dev(uart)->BAUDRATE = UART_BAUDRATE_BAUDRATE_Baud230400;
|
2016-02-04 15:30:06 +01:00
|
|
|
break;
|
|
|
|
case 250000:
|
2018-12-15 16:06:05 +01:00
|
|
|
dev(uart)->BAUDRATE = UART_BAUDRATE_BAUDRATE_Baud250000;
|
2016-02-04 15:30:06 +01:00
|
|
|
break;
|
|
|
|
case 460800:
|
2018-12-15 16:06:05 +01:00
|
|
|
dev(uart)->BAUDRATE = UART_BAUDRATE_BAUDRATE_Baud460800;
|
2016-02-04 15:30:06 +01:00
|
|
|
break;
|
|
|
|
case 921600:
|
2018-12-15 16:06:05 +01:00
|
|
|
dev(uart)->BAUDRATE = UART_BAUDRATE_BAUDRATE_Baud921600;
|
2016-02-04 15:30:06 +01:00
|
|
|
break;
|
|
|
|
default:
|
2016-10-28 10:15:46 +02:00
|
|
|
return UART_NOBAUD;
|
2016-02-04 15:30:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* enable the UART device */
|
2018-12-17 10:08:58 +01:00
|
|
|
#ifdef CPU_MODEL_NRF52840XXAA
|
|
|
|
dev(uart)->ENABLE = UARTE_ENABLE_ENABLE_Enabled;
|
|
|
|
dev(uart)->RXD.MAXCNT = 1;
|
|
|
|
dev(uart)->RXD.PTR = (uint32_t)&rx_buf[uart];
|
|
|
|
#else
|
2018-12-15 16:06:05 +01:00
|
|
|
dev(uart)->ENABLE = UART_ENABLE_ENABLE_Enabled;
|
2018-12-17 10:08:58 +01:00
|
|
|
/* enable TX and RX*/
|
2018-12-15 16:06:05 +01:00
|
|
|
dev(uart)->TASKS_STARTTX = 1;
|
2018-12-17 10:08:58 +01:00
|
|
|
#endif
|
2017-02-07 16:17:41 +01:00
|
|
|
|
|
|
|
if (rx_cb) {
|
2018-12-15 16:06:05 +01:00
|
|
|
dev(uart)->TASKS_STARTRX = 1;
|
2017-02-07 16:17:41 +01:00
|
|
|
/* enable global and receiving interrupt */
|
|
|
|
NVIC_EnableIRQ(UART_IRQN);
|
2018-12-17 10:08:58 +01:00
|
|
|
#ifdef CPU_MODEL_NRF52840XXAA
|
|
|
|
dev(uart)->INTENSET = UARTE_INTENSET_RXDRDY_Msk;
|
|
|
|
#else
|
2018-12-15 16:06:05 +01:00
|
|
|
dev(uart)->INTENSET = UART_INTENSET_RXDRDY_Msk;
|
2018-12-17 10:08:58 +01:00
|
|
|
#endif
|
2017-02-07 16:17:41 +01:00
|
|
|
}
|
|
|
|
|
2016-10-28 10:15:46 +02:00
|
|
|
return UART_OK;
|
2016-02-04 15:30:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void uart_write(uart_t uart, const uint8_t *data, size_t len)
|
|
|
|
{
|
2018-12-15 16:06:05 +01:00
|
|
|
assert(uart < UART_NUMOF);
|
|
|
|
|
2018-12-17 10:08:58 +01:00
|
|
|
#ifdef CPU_MODEL_NRF52840XXAA /* nrf52840 uses EasyDMA to transmit data */
|
|
|
|
/* reset endtx flag */
|
|
|
|
dev(uart)->EVENTS_ENDTX = 0;
|
|
|
|
/* set data to transfer to DMA TX pointer */
|
|
|
|
dev(uart)->TXD.PTR = (uint32_t)data;
|
|
|
|
dev(uart)->TXD.MAXCNT = len;
|
|
|
|
/* start transmission */
|
|
|
|
dev(uart)->TASKS_STARTTX = 1;
|
|
|
|
/* wait for the end of transmission */
|
|
|
|
while (dev(uart)->EVENTS_ENDTX == 0) {}
|
|
|
|
#else
|
2018-12-15 16:06:05 +01:00
|
|
|
for (size_t i = 0; i < len; i++) {
|
|
|
|
/* This section of the function is not thread safe:
|
|
|
|
- another thread may mess up with the uart at the same time.
|
|
|
|
In order to avoid an infinite loop in the interrupted thread,
|
|
|
|
the TXRDY flag must be cleared before writing the data to be
|
|
|
|
sent and not after. This way, the higher priority thread will
|
|
|
|
exit this function with the TXRDY flag set, then the interrupted
|
|
|
|
thread may have not transmitted his data but will still exit the
|
|
|
|
while loop.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* reset ready flag */
|
|
|
|
dev(uart)->EVENTS_TXDRDY = 0;
|
|
|
|
/* write data into transmit register */
|
|
|
|
dev(uart)->TXD = data[i];
|
|
|
|
/* wait for any transmission to be done */
|
|
|
|
while (dev(uart)->EVENTS_TXDRDY == 0) {}
|
2016-02-04 15:30:06 +01:00
|
|
|
}
|
2018-12-17 10:08:58 +01:00
|
|
|
#endif
|
2016-02-04 15:30:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void uart_poweron(uart_t uart)
|
|
|
|
{
|
2018-12-15 16:06:05 +01:00
|
|
|
assert(uart < UART_NUMOF);
|
|
|
|
|
|
|
|
dev(uart)->TASKS_STARTRX = 1;
|
|
|
|
dev(uart)->TASKS_STARTTX = 1;
|
2016-02-04 15:30:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void uart_poweroff(uart_t uart)
|
|
|
|
{
|
2018-12-15 16:06:05 +01:00
|
|
|
assert(uart < UART_NUMOF);
|
|
|
|
|
2018-12-17 10:08:58 +01:00
|
|
|
#ifndef CPU_MODEL_NRF52840XXAA
|
2018-12-15 16:06:05 +01:00
|
|
|
dev(uart)->TASKS_SUSPEND;
|
2018-12-17 10:08:58 +01:00
|
|
|
#else
|
|
|
|
(void)uart;
|
|
|
|
#endif
|
2016-02-04 15:30:06 +01:00
|
|
|
}
|
|
|
|
|
2018-12-15 16:06:05 +01:00
|
|
|
static inline void irq_handler(uart_t uart)
|
2016-02-04 15:30:06 +01:00
|
|
|
{
|
2018-12-15 16:06:05 +01:00
|
|
|
assert(uart < UART_NUMOF);
|
2018-12-17 10:08:58 +01:00
|
|
|
#ifdef CPU_MODEL_NRF52840XXAA /* nrf52840 uses EasyDMA to receive data */
|
|
|
|
if (dev(uart)->EVENTS_RXDRDY == 1) {
|
|
|
|
dev(uart)->EVENTS_RXDRDY = 0;
|
|
|
|
/* RXRDY doesn't mean that received byte is in RAM
|
|
|
|
so wait for ENDRX event */
|
|
|
|
while(dev(uart)->EVENTS_ENDRX == 0) {}
|
|
|
|
dev(uart)->EVENTS_ENDRX = 0;
|
|
|
|
/* Process received byte */
|
|
|
|
ISR_CTX.rx_cb(ISR_CTX.arg, rx_buf[uart]);
|
|
|
|
/* Restart RX task */
|
|
|
|
dev(uart)->TASKS_STARTRX = 1;
|
|
|
|
}
|
|
|
|
#else
|
2018-12-15 16:06:05 +01:00
|
|
|
if (dev(uart)->EVENTS_RXDRDY == 1) {
|
|
|
|
dev(uart)->EVENTS_RXDRDY = 0;
|
|
|
|
uint8_t byte = (uint8_t)(dev(uart)->RXD & 0xff);
|
2018-12-17 10:08:58 +01:00
|
|
|
ISR_CTX.rx_cb(ISR_CTX.arg, byte);
|
2016-02-04 15:30:06 +01:00
|
|
|
}
|
2018-12-17 10:08:58 +01:00
|
|
|
#endif
|
2016-11-30 18:26:05 +01:00
|
|
|
cortexm_isr_end();
|
2016-02-04 15:30:06 +01:00
|
|
|
}
|
2018-12-15 16:06:05 +01:00
|
|
|
|
|
|
|
#ifdef UART_0_ISR
|
|
|
|
void UART_0_ISR(void)
|
|
|
|
{
|
|
|
|
irq_handler(UART_DEV(0));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef UART_1_ISR
|
|
|
|
void UART_1_ISR(void)
|
|
|
|
{
|
|
|
|
irq_handler(UART_DEV(1));
|
|
|
|
}
|
|
|
|
#endif
|