2015-08-27 14:18:21 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 Freie Universität Berlin
|
2023-07-18 15:05:49 +02:00
|
|
|
* 2023 Otto-von-Guericke-Universität Magdeburg
|
2015-08-27 14:18:21 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2023-06-13 15:56:24 +02:00
|
|
|
* @ingroup cpu_msp430_x1xx
|
2017-06-22 15:43:17 +02:00
|
|
|
* @ingroup drivers_periph_uart
|
2015-08-27 14:18:21 +02:00
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @brief Low-level UART driver implementation
|
|
|
|
*
|
|
|
|
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
2023-07-18 15:05:49 +02:00
|
|
|
* @author Marian Buschsieweke <marian.buschsieweke@posteo.net>
|
2015-08-27 14:18:21 +02:00
|
|
|
*
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "cpu.h"
|
|
|
|
#include "periph_cpu.h"
|
|
|
|
#include "periph_conf.h"
|
|
|
|
#include "periph/uart.h"
|
2023-07-18 15:05:49 +02:00
|
|
|
#include "periph/gpio.h"
|
|
|
|
#include "compiler_hints.h"
|
2015-08-27 14:18:21 +02:00
|
|
|
|
2023-07-18 15:05:49 +02:00
|
|
|
static uart_isr_ctx_t ctx[UART_NUMOF];
|
|
|
|
static msp430_usart_conf_t confs[UART_NUMOF];
|
2015-08-27 14:18:21 +02:00
|
|
|
|
2023-07-18 15:05:49 +02:00
|
|
|
static void init(uart_t uart)
|
2015-08-27 14:18:21 +02:00
|
|
|
{
|
2023-07-18 15:05:49 +02:00
|
|
|
const msp430_usart_uart_params_t *params = uart_config[uart].uart;
|
|
|
|
msp430_usart_t *dev = params->usart_params.dev;
|
|
|
|
uint8_t enable_mask = params->rxtx_enable_mask;
|
|
|
|
|
|
|
|
/* most of the time UART is used to both send and receive */
|
|
|
|
if (unlikely(!ctx[uart].rx_cb)) {
|
|
|
|
enable_mask = params->tx_enable_mask;
|
2015-08-27 14:18:21 +02:00
|
|
|
}
|
|
|
|
|
2023-07-18 15:05:49 +02:00
|
|
|
/* acquire USART and put in in UART mode */
|
|
|
|
msp430_usart_acquire(¶ms->usart_params, &confs[uart], enable_mask);
|
|
|
|
|
|
|
|
/* configure pins */
|
|
|
|
gpio_set(params->txd);
|
|
|
|
gpio_init(params->txd, GPIO_OUT);
|
|
|
|
gpio_periph_mode(params->txd, true);
|
|
|
|
|
|
|
|
gpio_init(params->rxd, GPIO_IN);
|
|
|
|
gpio_periph_mode(params->rxd, true);
|
|
|
|
|
|
|
|
/* now that everything is configured, release the software reset bit */
|
|
|
|
dev->CTL &= ~(SWRST);
|
|
|
|
|
|
|
|
/* finally, enable the RX IRQ (won't work prior to releasing the software
|
|
|
|
* reset bit, as this is cleared under reset) */
|
|
|
|
if (likely(ctx[uart].rx_cb)) {
|
|
|
|
msp430_usart_enable_rx_irq(¶ms->usart_params);
|
|
|
|
}
|
2015-08-27 14:18:21 +02:00
|
|
|
}
|
|
|
|
|
2023-07-18 15:05:49 +02:00
|
|
|
int uart_init(uart_t uart, uint32_t symbolrate, uart_rx_cb_t rx_cb, void *arg)
|
2015-08-27 14:18:21 +02:00
|
|
|
{
|
2023-07-18 15:05:49 +02:00
|
|
|
assume((unsigned)uart < UART_NUMOF);
|
|
|
|
|
|
|
|
/* save interrupt context */
|
|
|
|
ctx[uart].rx_cb = rx_cb;
|
|
|
|
ctx[uart].arg = arg;
|
|
|
|
|
|
|
|
/* prepare and save UART config */
|
|
|
|
confs[uart].ctl = CHAR;
|
|
|
|
confs[uart].prescaler = msp430_usart_prescale(symbolrate, USART_MIN_BR_UART);
|
|
|
|
|
|
|
|
|
|
|
|
if (rx_cb) {
|
|
|
|
init(uart);
|
2015-08-27 14:18:21 +02:00
|
|
|
}
|
|
|
|
|
2016-10-28 10:15:46 +02:00
|
|
|
return UART_OK;
|
2015-08-27 14:18:21 +02:00
|
|
|
}
|
|
|
|
|
2015-10-20 16:27:05 +02:00
|
|
|
void uart_write(uart_t uart, const uint8_t *data, size_t len)
|
2015-08-27 14:18:21 +02:00
|
|
|
{
|
2023-07-18 15:05:49 +02:00
|
|
|
assume((unsigned)uart < UART_NUMOF);
|
|
|
|
const msp430_usart_uart_params_t *params = uart_config[uart].uart;
|
|
|
|
msp430_usart_t *dev = params->usart_params.dev;
|
|
|
|
|
|
|
|
/* If UART is in TX-only, the USART is released when done sending.
|
|
|
|
* This is not only helpful for power saving, but also to share the USART.
|
|
|
|
*/
|
|
|
|
if (!unlikely(ctx[uart].rx_cb)) {
|
|
|
|
init(uart);
|
|
|
|
}
|
2015-08-27 14:18:21 +02:00
|
|
|
|
2015-10-20 16:27:05 +02:00
|
|
|
for (size_t i = 0; i < len; i++) {
|
2023-06-14 16:18:15 +02:00
|
|
|
while (!(dev->TCTL & TXEPT)) {}
|
2015-10-20 16:27:05 +02:00
|
|
|
dev->TXBUF = data[i];
|
|
|
|
}
|
2023-07-18 15:05:49 +02:00
|
|
|
|
|
|
|
if (!unlikely(ctx[uart].rx_cb)) {
|
|
|
|
msp430_usart_release(¶ms->usart_params);
|
|
|
|
}
|
2015-08-27 14:18:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void uart_poweron(uart_t uart)
|
|
|
|
{
|
2023-07-18 15:05:49 +02:00
|
|
|
assume((unsigned)uart < UART_NUMOF);
|
|
|
|
/* In TX-only mode, the USART is only powered on when transmitting */
|
|
|
|
if (likely(ctx[uart].rx_cb)) {
|
|
|
|
init(uart);
|
|
|
|
}
|
2015-08-27 14:18:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void uart_poweroff(uart_t uart)
|
|
|
|
{
|
2023-07-18 15:05:49 +02:00
|
|
|
assume((unsigned)uart < UART_NUMOF);
|
|
|
|
const msp430_usart_uart_params_t *params = uart_config[uart].uart;
|
|
|
|
/* In TX-only mode, the USART is only powered on when transmitting */
|
|
|
|
if (likely(ctx[uart].rx_cb)) {
|
|
|
|
msp430_usart_release(¶ms->usart_params);
|
|
|
|
}
|
2015-08-27 14:18:21 +02:00
|
|
|
}
|
|
|
|
|
2023-07-18 15:05:49 +02:00
|
|
|
static void uart_rx_isr(uart_t uart)
|
2015-08-27 14:18:21 +02:00
|
|
|
{
|
2023-07-18 15:05:49 +02:00
|
|
|
assume((unsigned)uart < UART_NUMOF);
|
|
|
|
const msp430_usart_uart_params_t *params = uart_config[uart].uart;
|
|
|
|
msp430_usart_t *dev = params->usart_params.dev;
|
2015-08-27 14:18:21 +02:00
|
|
|
|
2015-09-22 03:49:26 +02:00
|
|
|
/* read character (resets interrupt flag) */
|
2023-07-18 15:05:49 +02:00
|
|
|
char c = dev->RXBUF;
|
2015-09-22 03:49:26 +02:00
|
|
|
|
|
|
|
/* only call callback if there was no receive error */
|
2023-07-18 15:05:49 +02:00
|
|
|
if (!(dev->RCTL & RXERR)) {
|
|
|
|
ctx[uart].rx_cb(ctx[uart].arg, c);
|
2015-08-27 14:18:21 +02:00
|
|
|
}
|
2023-07-18 15:05:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef UART0_RX_ISR
|
|
|
|
ISR(UART0_RX_ISR, isr_uart_0_rx)
|
|
|
|
{
|
|
|
|
__enter_isr();
|
|
|
|
uart_rx_isr(UART_DEV(0));
|
|
|
|
__exit_isr();
|
|
|
|
}
|
|
|
|
#endif
|
2015-08-27 14:18:21 +02:00
|
|
|
|
2023-07-18 15:05:49 +02:00
|
|
|
#ifdef UART1_RX_ISR
|
|
|
|
ISR(UART1_RX_ISR, isr_uart_0_rx)
|
|
|
|
{
|
|
|
|
__enter_isr();
|
|
|
|
uart_rx_isr(UART_DEV(1));
|
2015-08-27 14:18:21 +02:00
|
|
|
__exit_isr();
|
|
|
|
}
|
2023-07-18 15:05:49 +02:00
|
|
|
#endif
|