2014-11-19 16:31:33 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 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_lpc1768
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @brief Implementation of the low-level UART driver for the LPC1768
|
|
|
|
*
|
|
|
|
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include "cpu.h"
|
2014-12-08 19:47:25 +01:00
|
|
|
#include "sched.h"
|
|
|
|
#include "thread.h"
|
2014-11-19 16:31:33 +01:00
|
|
|
#include "periph/uart.h"
|
|
|
|
#include "periph_conf.h"
|
|
|
|
|
2014-12-08 19:47:25 +01:00
|
|
|
/**
|
|
|
|
* @brief UART device configurations
|
|
|
|
*/
|
2015-10-20 16:27:05 +02:00
|
|
|
static uart_isr_ctx_t config[UART_NUMOF];
|
2014-12-08 19:47:25 +01:00
|
|
|
|
2015-10-20 16:27:05 +02:00
|
|
|
static int init_base(uart_t uart, uint32_t baudrate);
|
|
|
|
|
|
|
|
int uart_init(uart_t uart, uint32_t baudrate, uart_rx_cb_t rx_cb, void *arg)
|
2014-11-19 16:31:33 +01:00
|
|
|
{
|
2015-10-20 16:27:05 +02:00
|
|
|
int res = init_base(uart, baudrate);
|
2014-12-16 15:30:57 +01:00
|
|
|
if (res < 0) {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* save callbacks */
|
|
|
|
config[uart].rx_cb = rx_cb;
|
|
|
|
config[uart].arg = arg;
|
|
|
|
|
|
|
|
switch (uart) {
|
|
|
|
#if UART_0_EN
|
|
|
|
case UART_0:
|
2014-12-08 19:47:25 +01:00
|
|
|
/* configure and enable global device interrupts */
|
|
|
|
NVIC_SetPriority(UART_0_IRQ, UART_IRQ_PRIO);
|
|
|
|
NVIC_EnableIRQ(UART_0_IRQ);
|
|
|
|
/* enable RX interrupt */
|
|
|
|
UART_0_DEV->IER |= (1 << 0);
|
2014-12-16 15:30:57 +01:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if UART_1_EN
|
|
|
|
case UART_1:
|
|
|
|
/* configure and enable global device interrupts */
|
|
|
|
NVIC_SetPriority(UART_1_IRQ, UART_IRQ_PRIO);
|
|
|
|
NVIC_EnableIRQ(UART_1_IRQ);
|
|
|
|
/* enable RX interrupt */
|
|
|
|
UART_1_DEV->IER |= (1 << 0);
|
|
|
|
break;
|
|
|
|
#endif
|
2014-12-08 19:47:25 +01:00
|
|
|
}
|
2014-12-16 15:30:57 +01:00
|
|
|
|
|
|
|
return 0;
|
2014-11-19 16:31:33 +01:00
|
|
|
}
|
|
|
|
|
2015-10-20 16:27:05 +02:00
|
|
|
static int init_base(uart_t uart, uint32_t baudrate)
|
2014-11-19 16:31:33 +01:00
|
|
|
{
|
2014-12-16 15:30:57 +01:00
|
|
|
switch (uart) {
|
|
|
|
#if UART_0_EN
|
|
|
|
case UART_0:
|
|
|
|
/* this implementation only supports 115200 baud */
|
|
|
|
if (baudrate != 115200) {
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
2014-12-18 16:20:14 +01:00
|
|
|
/* power on UART device and select peripheral clock */
|
2014-12-16 15:30:57 +01:00
|
|
|
UART_0_CLKEN();
|
2014-12-18 16:20:14 +01:00
|
|
|
UART_0_CLKSEL();
|
2014-12-16 15:30:57 +01:00
|
|
|
/* set mode to 8N1 and enable access to divisor latch */
|
|
|
|
UART_0_DEV->LCR = ((0x3 << 0) | (1 << 7));
|
|
|
|
/* set baud rate registers (fixed for now) */
|
|
|
|
UART_0_DEV->DLM = 0;
|
|
|
|
UART_0_DEV->DLL = 13;
|
|
|
|
/* enable FIFOs */
|
|
|
|
UART_0_DEV->FCR = 1;
|
|
|
|
/* select and configure the pin for RX */
|
|
|
|
UART_0_RX_PINSEL &= ~(0x3 << (UART_0_RX_PIN * 2));
|
|
|
|
UART_0_RX_PINSEL |= (UART_0_AF << (UART_0_RX_PIN * 2));
|
|
|
|
UART_0_RX_PINMODE &= ~(0x3 << (UART_0_RX_PIN * 2));
|
|
|
|
UART_0_RX_PINMODE |= (0x2 << (UART_0_RX_PIN * 2));
|
|
|
|
/* select and configure the pin for TX */
|
|
|
|
UART_0_TX_PINSEL &= ~(0x3 << (UART_0_TX_PIN * 2));
|
|
|
|
UART_0_TX_PINSEL |= (UART_0_AF << (UART_0_TX_PIN * 2));
|
|
|
|
UART_0_TX_PINMODE &= ~(0x3 << (UART_0_TX_PIN * 2));
|
|
|
|
UART_0_TX_PINMODE |= (0x2 << (UART_0_TX_PIN * 2));
|
|
|
|
/* disable access to divisor latch */
|
|
|
|
UART_0_DEV->LCR &= ~(1 << 7);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if UART_1_EN
|
|
|
|
case UART_1:
|
|
|
|
/* this implementation only supports 115200 baud */
|
|
|
|
if (baudrate != 115200) {
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
2014-12-18 16:20:14 +01:00
|
|
|
/* power on UART device and select peripheral clock */
|
2014-12-16 15:30:57 +01:00
|
|
|
UART_1_CLKEN();
|
2014-12-18 16:20:14 +01:00
|
|
|
UART_1_CLKSEL();
|
2014-12-16 15:30:57 +01:00
|
|
|
/* set mode to 8N1 and enable access to divisor latch */
|
|
|
|
UART_1_DEV->LCR = ((0x3 << 0) | (1 << 7));
|
|
|
|
/* set baud rate registers (fixed for now) */
|
|
|
|
UART_1_DEV->DLM = 0;
|
|
|
|
UART_1_DEV->DLL = 13;
|
|
|
|
/* enable FIFOs */
|
|
|
|
UART_1_DEV->FCR = 1;
|
|
|
|
/* select and configure the pin for RX */
|
|
|
|
UART_1_RX_PINSEL &= ~(0x3 << (UART_1_RX_PIN * 2));
|
|
|
|
UART_1_RX_PINSEL |= (UART_1_AF << (UART_1_RX_PIN * 2));
|
|
|
|
UART_1_RX_PINMODE &= ~(0x3 << (UART_1_RX_PIN * 2));
|
|
|
|
UART_1_RX_PINMODE |= (0x2 << (UART_1_RX_PIN * 2));
|
|
|
|
/* select and configure the pin for TX */
|
|
|
|
UART_1_TX_PINSEL &= ~(0x3 << (UART_1_TX_PIN * 2));
|
|
|
|
UART_1_TX_PINSEL |= (UART_1_AF << (UART_1_TX_PIN * 2));
|
|
|
|
UART_1_TX_PINMODE &= ~(0x3 << (UART_1_TX_PIN * 2));
|
|
|
|
UART_1_TX_PINMODE |= (0x2 << (UART_1_TX_PIN * 2));
|
|
|
|
/* disable access to divisor latch */
|
|
|
|
UART_1_DEV->LCR &= ~(1 << 7);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
return -1;
|
2014-12-08 19:47:25 +01:00
|
|
|
}
|
2014-12-16 15:30:57 +01:00
|
|
|
|
|
|
|
return 0;
|
2014-11-19 16:31:33 +01:00
|
|
|
}
|
|
|
|
|
2015-10-20 16:27:05 +02:00
|
|
|
void uart_write(uart_t uart, const uint8_t *data, size_t len)
|
2014-11-19 16:31:33 +01:00
|
|
|
{
|
2015-10-20 16:27:05 +02:00
|
|
|
LPC_UART_TypeDef *dev;
|
2014-11-19 16:31:33 +01:00
|
|
|
|
2014-12-16 15:30:57 +01:00
|
|
|
switch (uart) {
|
|
|
|
#if UART_0_EN
|
|
|
|
case UART_0:
|
2015-10-20 16:27:05 +02:00
|
|
|
dev = (LPC_UART_TypeDef *)UART_0_DEV;
|
2014-12-16 15:30:57 +01:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if UART_1_EN
|
|
|
|
case UART_1:
|
2015-10-20 16:27:05 +02:00
|
|
|
dev = (LPC_UART_TypeDef *)UART_1_DEV;
|
2014-12-16 15:30:57 +01:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
2015-10-20 16:27:05 +02:00
|
|
|
return;
|
2014-12-08 19:47:25 +01:00
|
|
|
}
|
2014-12-16 15:30:57 +01:00
|
|
|
|
2015-10-20 16:27:05 +02:00
|
|
|
for (size_t i = 0; i < len; i++) {
|
|
|
|
while (!(dev->LSR & (1 << 5))); /* wait for THRE bit to be set */
|
|
|
|
dev->THR = data[i];
|
2014-12-08 19:47:25 +01:00
|
|
|
}
|
2014-11-19 16:31:33 +01:00
|
|
|
}
|
|
|
|
|
2014-12-08 19:47:25 +01:00
|
|
|
void uart_poweron(uart_t uart)
|
|
|
|
{
|
2014-12-16 15:30:57 +01: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
|
2014-12-08 19:47:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void uart_poweroff(uart_t uart)
|
|
|
|
{
|
2014-12-16 15:30:57 +01: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
|
2014-12-08 19:47:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if UART_0_EN
|
|
|
|
void UART_0_ISR(void)
|
|
|
|
{
|
|
|
|
if (UART_0_DEV->LSR & (1 << 0)) { /* is RDR flag set? */
|
|
|
|
char data = (char)UART_0_DEV->RBR;
|
|
|
|
config[UART_0].rx_cb(config[UART_0].arg, data);
|
|
|
|
}
|
2014-12-16 15:30:57 +01:00
|
|
|
if (sched_context_switch_request) {
|
|
|
|
thread_yield();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if UART_1_EN
|
|
|
|
void UART_1_ISR(void)
|
|
|
|
{
|
|
|
|
if (UART_1_DEV->LSR & (1 << 0)) { /* is RDR flag set? */
|
|
|
|
char data = (char)UART_1_DEV->RBR;
|
|
|
|
config[UART_1].rx_cb(config[UART_1].arg, data);
|
|
|
|
}
|
2014-12-08 19:47:25 +01:00
|
|
|
if (sched_context_switch_request) {
|
|
|
|
thread_yield();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|