2014-06-11 14:59:24 +02: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_stm32f1
|
|
|
|
* @{
|
|
|
|
*
|
2014-07-03 10:36:46 +02:00
|
|
|
* @file uart.c
|
2014-06-11 14:59:24 +02:00
|
|
|
* @brief Low-level UART driver implementation
|
|
|
|
*
|
|
|
|
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
|
|
|
* @author Thomas Eichinger <thomas.eichinger@fu-berlin.de>
|
|
|
|
*
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
#include "cpu.h"
|
|
|
|
#include "board.h"
|
|
|
|
#include "periph_conf.h"
|
|
|
|
#include "periph/uart.h"
|
|
|
|
|
2014-07-03 10:36:46 +02:00
|
|
|
#include "sched.h"
|
|
|
|
#include "thread.h"
|
|
|
|
|
2014-07-24 20:08:32 +02:00
|
|
|
#ifdef MODULE_UART0
|
|
|
|
#include "board_uart0.h"
|
|
|
|
#endif
|
|
|
|
|
2014-06-11 14:59:24 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Each UART device has to store two callbacks.
|
|
|
|
*/
|
|
|
|
typedef struct {
|
2014-07-24 20:08:58 +02:00
|
|
|
uart_rx_cb_t rx_cb;
|
|
|
|
uart_tx_cb_t tx_cb;
|
|
|
|
void *arg;
|
2014-06-11 14:59:24 +02:00
|
|
|
} uart_conf_t;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Unified interrupt handler for all UART devices
|
|
|
|
*
|
|
|
|
* @param uartnum the number of the UART that triggered the ISR
|
|
|
|
* @param uart the UART device that triggered the ISR
|
|
|
|
*/
|
|
|
|
static inline void irq_handler(uart_t uartnum, USART_TypeDef *uart);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Allocate memory to store the callback functions.
|
|
|
|
*/
|
|
|
|
static uart_conf_t config[UART_NUMOF];
|
|
|
|
|
|
|
|
|
2014-07-24 20:08:58 +02:00
|
|
|
int uart_init(uart_t uart, uint32_t baudrate, uart_rx_cb_t rx_cb, uart_tx_cb_t tx_cb, void *arg)
|
2014-06-11 14:59:24 +02:00
|
|
|
{
|
|
|
|
int res;
|
|
|
|
|
|
|
|
/* initialize UART in blocking mode first */
|
|
|
|
res = uart_init_blocking(uart, baudrate);
|
|
|
|
if (res < 0) {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* enable global interrupt and configure the interrupts priority */
|
|
|
|
switch (uart) {
|
|
|
|
#if UART_0_EN
|
|
|
|
case UART_0:
|
|
|
|
NVIC_SetPriority(UART_0_IRQ, UART_IRQ_PRIO);
|
|
|
|
NVIC_EnableIRQ(UART_0_IRQ);
|
|
|
|
UART_0_DEV->CR1 |= USART_CR1_RXNEIE;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if UART_1_EN
|
|
|
|
case UART_1:
|
|
|
|
NVIC_SetPriority(UART_1_IRQ, UART_IRQ_PRIO);
|
|
|
|
NVIC_EnableIRQ(UART_1_IRQ);
|
|
|
|
UART_1_DEV->CR1 |= USART_CR1_RXNEIE;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* register callbacks */
|
|
|
|
config[uart].rx_cb = rx_cb;
|
|
|
|
config[uart].tx_cb = tx_cb;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int uart_init_blocking(uart_t uart, uint32_t baudrate)
|
|
|
|
{
|
|
|
|
USART_TypeDef *dev;
|
|
|
|
GPIO_TypeDef *port;
|
2014-07-03 10:36:46 +02:00
|
|
|
uint32_t rx_pin, tx_pin, bus_freq;
|
2014-06-11 14:59:24 +02:00
|
|
|
float divider;
|
|
|
|
uint16_t mantissa;
|
|
|
|
uint8_t fraction;
|
|
|
|
|
|
|
|
/* enable UART and port clocks and select devices */
|
|
|
|
switch (uart) {
|
|
|
|
#if UART_0_EN
|
|
|
|
case UART_0:
|
|
|
|
dev = UART_0_DEV;
|
|
|
|
port = UART_0_PORT;
|
|
|
|
rx_pin = UART_0_RX_PIN;
|
|
|
|
tx_pin = UART_0_TX_PIN;
|
2014-07-03 10:36:46 +02:00
|
|
|
bus_freq = UART_0_BUS_FREQ;
|
2014-06-11 14:59:24 +02:00
|
|
|
/* enable clocks */
|
|
|
|
UART_0_CLKEN();
|
|
|
|
UART_0_PORT_CLKEN();
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if UART_1_EN
|
|
|
|
case UART_1:
|
|
|
|
dev = UART_1_DEV;
|
|
|
|
port = UART_1_PORT;
|
|
|
|
tx_pin = UART_1_TX_PIN;
|
|
|
|
rx_pin = UART_1_RX_PIN;
|
2014-07-03 10:36:46 +02:00
|
|
|
bus_freq = UART_1_BUS_FREQ;
|
2014-06-11 14:59:24 +02:00
|
|
|
/* enable clocks */
|
|
|
|
UART_1_CLKEN();
|
|
|
|
UART_1_PORT_CLKEN();
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
/* Configure USART Tx as alternate function push-pull and 50MHz*/
|
|
|
|
if (tx_pin < 8) {
|
|
|
|
port->CRL &= ~(0xf << (tx_pin * 4));
|
|
|
|
port->CRL |= (0xB << (tx_pin * 4));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
port->CRH &= ~(0xf << ((tx_pin-8) * 4));
|
|
|
|
port->CRH |= (0xB << ((tx_pin-8) * 4));
|
|
|
|
}
|
|
|
|
/* Configure USART Rx as floating input */
|
|
|
|
if (rx_pin < 8) {
|
|
|
|
port->CRL &= ~(0xf << (rx_pin * 4));
|
|
|
|
port->CRL |= (0x4 << (rx_pin * 4));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
port->CRH &= ~(0xf << ((rx_pin-8) * 4));
|
|
|
|
port->CRH |= (0x4 << ((rx_pin-8) * 4));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* configure UART to mode 8N1 with given baudrate */
|
2014-07-03 10:36:46 +02:00
|
|
|
divider = ((float)bus_freq) / (16 * baudrate);
|
2014-06-11 14:59:24 +02:00
|
|
|
mantissa = (uint16_t)floorf(divider);
|
|
|
|
fraction = (uint8_t)floorf((divider - mantissa) * 16);
|
|
|
|
dev->BRR = 0;
|
|
|
|
dev->BRR |= ((mantissa & 0x0fff) << 4) | (0x0f & fraction);
|
|
|
|
|
|
|
|
/* enable receive and transmit mode */
|
|
|
|
dev->CR1 |= USART_CR1_UE | USART_CR1_TE | USART_CR1_RE;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void uart_tx_begin(uart_t uart)
|
|
|
|
{
|
|
|
|
switch (uart) {
|
|
|
|
#if UART_0_EN
|
|
|
|
case UART_0:
|
|
|
|
UART_0_DEV->CR1 |= USART_CR1_TXEIE;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if UART_1_EN
|
|
|
|
case UART_1:
|
|
|
|
UART_1_DEV->CR1 |= USART_CR1_TXEIE;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void uart_tx_end(uart_t uart)
|
|
|
|
{
|
|
|
|
switch (uart) {
|
|
|
|
#if UART_0_EN
|
|
|
|
case UART_0:
|
|
|
|
UART_0_DEV->CR1 &= ~USART_CR1_TXEIE;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if UART_1_EN
|
|
|
|
case UART_1:
|
|
|
|
UART_1_DEV->CR1 &= ~USART_CR1_TXEIE;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int uart_write(uart_t uart, char data)
|
|
|
|
{
|
|
|
|
USART_TypeDef *dev;
|
|
|
|
|
|
|
|
switch (uart) {
|
|
|
|
#if UART_0_EN
|
|
|
|
case UART_0:
|
|
|
|
dev = UART_0_DEV;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if UART_1_EN
|
|
|
|
case UART_1:
|
|
|
|
dev = UART_1_DEV;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dev->SR & USART_SR_TXE) {
|
|
|
|
dev->DR = (uint8_t)data;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int uart_read_blocking(uart_t uart, char *data)
|
|
|
|
{
|
|
|
|
USART_TypeDef *dev;
|
|
|
|
|
|
|
|
switch (uart) {
|
|
|
|
#if UART_0_EN
|
|
|
|
case UART_0:
|
|
|
|
dev = UART_0_DEV;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if UART_1_EN
|
|
|
|
case UART_1:
|
|
|
|
dev = UART_1_DEV;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!(dev->SR & USART_SR_RXNE));
|
|
|
|
*data = (char)dev->DR;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int uart_write_blocking(uart_t uart, char data)
|
|
|
|
{
|
|
|
|
USART_TypeDef *dev;
|
|
|
|
|
|
|
|
switch (uart) {
|
|
|
|
#if UART_0_EN
|
|
|
|
case UART_0:
|
|
|
|
dev = UART_0_DEV;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if UART_1_EN
|
|
|
|
case UART_1:
|
|
|
|
dev = UART_1_DEV;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!(dev->SR & USART_SR_TXE));
|
|
|
|
dev->DR = (uint8_t)data;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if UART_0_EN
|
|
|
|
__attribute__((naked)) void UART_0_ISR(void)
|
|
|
|
{
|
|
|
|
ISR_ENTER();
|
|
|
|
irq_handler(UART_0, UART_0_DEV);
|
|
|
|
ISR_EXIT();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if UART_1_EN
|
|
|
|
__attribute__((naked)) void UART_1_ISR(void)
|
|
|
|
{
|
|
|
|
ISR_ENTER();
|
|
|
|
irq_handler(UART_1, UART_1_DEV);
|
|
|
|
ISR_EXIT();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static inline void irq_handler(uint8_t uartnum, USART_TypeDef *dev)
|
|
|
|
{
|
|
|
|
if (dev->SR & USART_SR_RXNE) {
|
|
|
|
char data = (char)dev->DR;
|
2014-07-24 20:08:32 +02:00
|
|
|
#ifdef MODULE_UART0
|
|
|
|
if (uart0_handler_pid) {
|
|
|
|
uart0_handle_incoming(data);
|
|
|
|
|
|
|
|
uart0_notify_thread();
|
|
|
|
}
|
|
|
|
#else
|
2014-08-19 13:41:53 +02:00
|
|
|
config[uartnum].rx_cb(config[uartnum].arg, data);
|
2014-07-24 20:08:32 +02:00
|
|
|
#endif
|
2014-06-11 14:59:24 +02:00
|
|
|
}
|
|
|
|
else if (dev->SR & USART_SR_ORE) {
|
|
|
|
/* ORE is cleared by reading SR and DR sequentially */
|
|
|
|
dev->DR;
|
|
|
|
}
|
|
|
|
else if (dev->SR & USART_SR_TXE) {
|
2014-07-24 20:08:32 +02:00
|
|
|
#ifdef MODULE_UART0
|
|
|
|
dev->SR &= ~(USART_SR_TXE);
|
|
|
|
#else
|
2014-08-19 13:41:53 +02:00
|
|
|
config[uartnum].tx_cb(config[uartnum].arg);
|
2014-07-24 20:08:32 +02:00
|
|
|
#endif
|
2014-06-11 14:59:24 +02:00
|
|
|
}
|
2014-07-03 10:36:46 +02:00
|
|
|
|
|
|
|
if (sched_context_switch_request) {
|
|
|
|
thread_yield();
|
|
|
|
}
|
2014-06-11 14:59:24 +02:00
|
|
|
}
|