2014-04-17 19:36:12 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 Freie Universität Berlin
|
|
|
|
*
|
2014-07-31 19:45:27 +02: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.
|
2014-04-17 19:36:12 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ingroup driver_periph
|
|
|
|
* @{
|
2014-05-14 10:46:15 +02:00
|
|
|
*
|
2015-05-22 07:34:41 +02:00
|
|
|
* @file
|
2014-04-17 19:36:12 +02:00
|
|
|
* @brief Low-level UART driver implementation
|
|
|
|
*
|
|
|
|
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
2014-05-14 10:46:15 +02:00
|
|
|
*
|
2014-04-17 19:36:12 +02:00
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "board.h"
|
|
|
|
#include "cpu.h"
|
2014-10-17 00:34:37 +02:00
|
|
|
#include "sched.h"
|
|
|
|
#include "thread.h"
|
2014-04-17 19:36:12 +02:00
|
|
|
#include "periph/uart.h"
|
|
|
|
#include "periph_conf.h"
|
|
|
|
|
2014-10-17 00:34:37 +02:00
|
|
|
#define ENABLE_DEBUG (0)
|
|
|
|
#include "debug.h"
|
|
|
|
|
2014-04-17 19:36:12 +02:00
|
|
|
/**
|
|
|
|
* @brief Allocate memory to store the callback functions.
|
|
|
|
*/
|
2015-10-20 13:19:09 +02:00
|
|
|
static uart_isr_ctx_t uart_config[UART_NUMOF];
|
2014-04-17 19:36:12 +02:00
|
|
|
|
2015-10-20 13:19:09 +02:00
|
|
|
static int init_base(uart_t uart, uint32_t baudrate);
|
2014-04-17 19:36:12 +02:00
|
|
|
|
2015-10-20 13:19:09 +02:00
|
|
|
int uart_init(uart_t uart, uint32_t baudrate, uart_rx_cb_t rx_cb, void *arg)
|
2014-04-17 19:36:12 +02:00
|
|
|
{
|
|
|
|
/* initialize basic functionality */
|
2015-10-20 13:19:09 +02:00
|
|
|
int res = init_base(uart, baudrate);
|
2014-04-17 19:36:12 +02:00
|
|
|
if (res != 0) {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* register callbacks */
|
2014-07-28 14:49:04 +02:00
|
|
|
uart_config[uart].rx_cb = rx_cb;
|
|
|
|
uart_config[uart].arg = arg;
|
2014-04-17 19:36:12 +02:00
|
|
|
|
|
|
|
/* configure interrupts and enable RX interrupt */
|
|
|
|
switch (uart) {
|
2014-10-17 00:34:37 +02:00
|
|
|
#if UART_0_EN
|
2014-04-17 19:36:12 +02:00
|
|
|
case UART_0:
|
2014-10-17 00:34:37 +02:00
|
|
|
NVIC_EnableIRQ(UART_0_IRQ);
|
2014-04-17 19:36:12 +02:00
|
|
|
UART_0_DEV->UART_IER = UART_IER_RXRDY;
|
2014-10-17 00:34:37 +02:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if UART_1_EN
|
|
|
|
case UART_1:
|
|
|
|
NVIC_EnableIRQ(UART_1_IRQ);
|
|
|
|
UART_1_DEV->US_IER = US_IER_RXRDY;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if UART_2_EN
|
|
|
|
case UART_2:
|
|
|
|
NVIC_EnableIRQ(UART_2_IRQ);
|
|
|
|
UART_2_DEV->US_IER = US_IER_RXRDY;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if UART_3_EN
|
|
|
|
case UART_3:
|
|
|
|
NVIC_EnableIRQ(UART_3_IRQ);
|
|
|
|
UART_3_DEV->US_IER = US_IER_RXRDY;
|
|
|
|
break;
|
|
|
|
#endif
|
2014-04-17 19:36:12 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-20 13:19:09 +02:00
|
|
|
static int init_base(uart_t uart, uint32_t baudrate)
|
2014-04-17 19:36:12 +02:00
|
|
|
{
|
|
|
|
switch (uart) {
|
|
|
|
#if UART_0_EN
|
|
|
|
case UART_0:
|
2014-10-17 00:34:37 +02:00
|
|
|
/* enable uart clock */
|
|
|
|
UART_0_CLKEN();
|
|
|
|
|
2014-04-17 19:36:12 +02:00
|
|
|
/* configure PINS */
|
|
|
|
UART_0_PORT->PIO_PDR = UART_0_PINS;
|
2014-10-17 00:34:37 +02:00
|
|
|
UART_0_PORT->PIO_ABSR &= ~UART_0_PINS; /* periph function A */
|
2014-04-17 19:36:12 +02:00
|
|
|
|
|
|
|
/* set clock divider */
|
2015-06-12 14:41:36 +02:00
|
|
|
UART_0_DEV->UART_BRGR = (F_CPU / (16 * baudrate));
|
2014-04-17 19:36:12 +02:00
|
|
|
/* set to normal mode without parity */
|
|
|
|
UART_0_DEV->UART_MR = UART_MR_PAR_NO | UART_MR_CHMODE_NORMAL;
|
|
|
|
/* enable receiver and transmitter and reset status bits */
|
|
|
|
UART_0_DEV->UART_CR = UART_CR_RXEN | UART_CR_TXEN | UART_CR_RSTSTA;
|
|
|
|
break;
|
2014-10-17 00:34:37 +02:00
|
|
|
#endif
|
|
|
|
#if UART_1_EN
|
|
|
|
case UART_1:
|
|
|
|
/* enable uart clock */
|
|
|
|
UART_1_CLKEN();
|
|
|
|
|
|
|
|
/* configure PINS */
|
|
|
|
UART_1_PORT->PIO_PDR = UART_1_PINS;
|
|
|
|
UART_1_PORT->PIO_ABSR &= ~UART_1_PINS; /* periph function A */
|
|
|
|
|
|
|
|
/* set clock divider */
|
2015-06-12 14:41:36 +02:00
|
|
|
UART_1_DEV->US_BRGR = (F_CPU / (16 * baudrate));
|
2014-10-17 00:34:37 +02:00
|
|
|
/* set to normal mode without parity */
|
|
|
|
UART_1_DEV->US_MR = US_MR_CHRL_8_BIT | US_MR_PAR_NO;
|
|
|
|
/* enable receiver and transmitter and reset status bits */
|
|
|
|
UART_1_DEV->US_CR = US_CR_RXEN | US_CR_TXEN | US_CR_RSTSTA;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if UART_2_EN
|
|
|
|
case UART_2:
|
|
|
|
/* enable uart clock */
|
|
|
|
UART_2_CLKEN();
|
|
|
|
|
|
|
|
/* configure PINS */
|
|
|
|
UART_2_PORT->PIO_PDR = UART_2_PINS;
|
|
|
|
UART_2_PORT->PIO_ABSR &= ~UART_2_PINS; /* periph function A */
|
|
|
|
|
|
|
|
/* set clock divider */
|
2015-06-12 14:41:36 +02:00
|
|
|
UART_2_DEV->US_BRGR = (F_CPU / (16 * baudrate));
|
2014-10-17 00:34:37 +02:00
|
|
|
/* set to normal mode without parity */
|
|
|
|
UART_2_DEV->US_MR = US_MR_CHRL_8_BIT | US_MR_PAR_NO;
|
|
|
|
/* enable receiver and transmitter and reset status bits */
|
|
|
|
UART_2_DEV->US_CR = US_CR_RXEN | US_CR_TXEN | US_CR_RSTSTA;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if UART_3_EN
|
|
|
|
case UART_3:
|
|
|
|
/* enable uart clock */
|
|
|
|
UART_3_CLKEN();
|
|
|
|
|
|
|
|
/* configure PINS */
|
|
|
|
UART_3_PORT->PIO_PDR = UART_3_PINS;
|
|
|
|
UART_3_PORT->PIO_ABSR |= UART_3_PINS; /* periph function B */
|
|
|
|
|
|
|
|
/* set clock divider */
|
2015-06-12 14:41:36 +02:00
|
|
|
UART_3_DEV->US_BRGR = (F_CPU / (16 * baudrate));
|
2014-10-17 00:34:37 +02:00
|
|
|
/* set to normal mode without parity */
|
|
|
|
UART_3_DEV->US_MR = US_MR_CHRL_8_BIT | US_MR_PAR_NO;
|
|
|
|
/* enable receiver and transmitter and reset status bits */
|
|
|
|
UART_3_DEV->US_CR = US_CR_RXEN | US_CR_TXEN | US_CR_RSTSTA;
|
|
|
|
break;
|
2014-04-17 19:36:12 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-20 13:19:09 +02:00
|
|
|
void uart_write(uart_t uart, const uint8_t *data, size_t len)
|
2014-04-17 19:36:12 +02:00
|
|
|
{
|
2015-10-20 13:19:09 +02:00
|
|
|
Uart *dev;
|
2014-04-17 19:36:12 +02:00
|
|
|
|
|
|
|
switch (uart) {
|
2014-10-17 00:34:37 +02:00
|
|
|
#if UART_0_EN
|
2014-04-17 19:36:12 +02:00
|
|
|
case UART_0:
|
2015-10-20 13:19:09 +02:00
|
|
|
dev = (Uart *)UART_0_DEV;
|
2014-04-17 19:36:12 +02:00
|
|
|
break;
|
2014-10-17 00:34:37 +02:00
|
|
|
#endif
|
|
|
|
#if UART_1_EN
|
|
|
|
case UART_1:
|
2015-10-20 13:19:09 +02:00
|
|
|
dev = (Uart *)UART_1_DEV;
|
2014-10-17 00:34:37 +02:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if UART_2_EN
|
|
|
|
case UART_2:
|
2015-10-20 13:19:09 +02:00
|
|
|
dev = (Uart *)UART_2_DEV;
|
2014-10-17 00:34:37 +02:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if UART_3_EN
|
|
|
|
case UART_3:
|
2015-10-20 13:19:09 +02:00
|
|
|
dev = (Uart *)UART_3_DEV;
|
2014-10-17 00:34:37 +02:00
|
|
|
break;
|
|
|
|
#endif
|
2015-10-20 13:19:09 +02:00
|
|
|
default:
|
|
|
|
return;
|
2014-04-17 19:36:12 +02:00
|
|
|
}
|
2014-10-17 00:34:37 +02:00
|
|
|
|
2015-10-20 13:19:09 +02:00
|
|
|
for (size_t i = 0; i < len; i++) {
|
|
|
|
while (!(dev->UART_SR & UART_SR_TXRDY));
|
|
|
|
dev->UART_THR = data[i];
|
2014-04-17 19:36:12 +02:00
|
|
|
}
|
2014-07-28 14:49:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void uart_poweron(uart_t uart)
|
|
|
|
{
|
2014-10-17 00:34:37 +02:00
|
|
|
switch (uart) {
|
|
|
|
#if UART_0_EN
|
|
|
|
case UART_0:
|
|
|
|
UART_0_CLKEN();
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if UART_1_EN
|
|
|
|
case UART_1:
|
|
|
|
UART_1_CLKEN();
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if UART_2_EN
|
|
|
|
case UART_2:
|
|
|
|
UART_2_CLKEN();
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if UART_3_EN
|
|
|
|
case UART_3:
|
|
|
|
UART_3_CLKEN();
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
}
|
2014-07-28 14:49:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void uart_poweroff(uart_t uart)
|
|
|
|
{
|
2014-10-17 00:34:37 +02:00
|
|
|
switch (uart) {
|
|
|
|
#if UART_0_EN
|
|
|
|
case UART_0:
|
|
|
|
UART_0_CLKDIS();
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if UART_1_EN
|
|
|
|
case UART_1:
|
|
|
|
UART_1_CLKDIS();
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if UART_2_EN
|
|
|
|
case UART_2:
|
|
|
|
UART_2_CLKDIS();
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if UART_3_EN
|
|
|
|
case UART_3:
|
|
|
|
UART_3_CLKDIS();
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if UART_0_EN
|
|
|
|
void UART_0_ISR(void)
|
|
|
|
{
|
|
|
|
if (UART_0_DEV->UART_SR & UART_SR_RXRDY) {
|
|
|
|
char data = (char)UART_0_DEV->UART_RHR;
|
|
|
|
uart_config[UART_0].rx_cb(uart_config[UART_0].arg, data);
|
|
|
|
}
|
|
|
|
if (sched_context_switch_request) {
|
|
|
|
thread_yield();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2014-07-28 14:49:04 +02:00
|
|
|
|
2014-10-17 00:34:37 +02:00
|
|
|
#if UART_1_EN
|
|
|
|
void UART_1_ISR(void)
|
|
|
|
{
|
|
|
|
if (UART_1_DEV->US_CSR & US_CSR_RXRDY) {
|
|
|
|
char data = (char)UART_1_DEV->US_RHR;
|
|
|
|
uart_config[UART_1].rx_cb(uart_config[UART_1].arg, data);
|
|
|
|
}
|
|
|
|
if (sched_context_switch_request) {
|
|
|
|
thread_yield();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if UART_2_EN
|
|
|
|
void UART_2_ISR(void)
|
|
|
|
{
|
|
|
|
if (UART_2_DEV->US_CSR & US_CSR_RXRDY) {
|
|
|
|
char data = (char)UART_2_DEV->US_RHR;
|
|
|
|
uart_config[UART_2].rx_cb(uart_config[UART_2].arg, data);
|
|
|
|
}
|
|
|
|
if (sched_context_switch_request) {
|
|
|
|
thread_yield();
|
|
|
|
}
|
2014-07-28 14:49:04 +02:00
|
|
|
}
|
2014-10-17 00:34:37 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if UART_3_EN
|
|
|
|
void UART_3_ISR(void)
|
|
|
|
{
|
|
|
|
if (UART_3_DEV->US_CSR & US_CSR_RXRDY) {
|
|
|
|
char data = (char)UART_3_DEV->US_RHR;
|
|
|
|
uart_config[UART_3].rx_cb(uart_config[UART_3].arg, data);
|
|
|
|
}
|
|
|
|
if (sched_context_switch_request) {
|
|
|
|
thread_yield();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|