2015-08-27 14:18:21 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
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>
|
|
|
|
*
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "cpu.h"
|
|
|
|
#include "periph_cpu.h"
|
|
|
|
#include "periph_conf.h"
|
|
|
|
#include "periph/uart.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Keep track of the interrupt context
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
static uart_rx_cb_t ctx_rx_cb;
|
|
|
|
static void *ctx_isr_arg;
|
|
|
|
/** @} */
|
|
|
|
|
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)
|
2015-08-27 14:18:21 +02:00
|
|
|
{
|
2016-10-28 10:15:46 +02:00
|
|
|
int res = init_base(uart, baudrate);
|
|
|
|
if (res != UART_OK) {
|
|
|
|
return res;
|
2015-08-27 14:18:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* save interrupt context */
|
|
|
|
ctx_rx_cb = rx_cb;
|
|
|
|
ctx_isr_arg = arg;
|
|
|
|
/* reset interrupt flags and enable RX interrupt */
|
2023-06-14 16:18:15 +02:00
|
|
|
UART_SFR->IE &= ~(UART_IE_TX_BIT);
|
|
|
|
UART_SFR->IFG &= ~(UART_IE_RX_BIT);
|
|
|
|
UART_SFR->IFG |= (UART_IE_TX_BIT);
|
|
|
|
UART_SFR->IE |= (UART_IE_RX_BIT);
|
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
|
|
|
static int init_base(uart_t uart, uint32_t baudrate)
|
2015-08-27 14:18:21 +02:00
|
|
|
{
|
|
|
|
if (uart != 0) {
|
2016-10-28 10:15:46 +02:00
|
|
|
return UART_NODEV;
|
2015-08-27 14:18:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* get the default UART for now -> TODO: enable for multiple devices */
|
2015-10-20 16:27:05 +02:00
|
|
|
msp_usart_t *dev = UART_BASE;
|
2015-08-27 14:18:21 +02:00
|
|
|
|
|
|
|
/* power off and reset device */
|
|
|
|
uart_poweroff(uart);
|
2023-06-14 16:18:15 +02:00
|
|
|
dev->CTL = SWRST;
|
2015-08-27 14:18:21 +02:00
|
|
|
/* configure to 8N1 and using the SMCLK*/
|
2023-06-14 16:18:15 +02:00
|
|
|
dev->CTL |= CHAR;
|
|
|
|
dev->TCTL = (TXEPT | UXTCTL_SSEL_SMCLK);
|
2015-08-27 14:18:21 +02:00
|
|
|
dev->RCTL = 0x00;
|
|
|
|
/* baudrate configuration */
|
2023-06-13 15:56:24 +02:00
|
|
|
uint16_t br = (uint16_t)(msp430_submain_clock_freq() / baudrate);
|
2015-08-27 14:18:21 +02:00
|
|
|
dev->BR0 = (uint8_t)br;
|
|
|
|
dev->BR1 = (uint8_t)(br >> 8);
|
|
|
|
/* TODO: calculate value for modulation register */
|
|
|
|
dev->MCTL = 0;
|
|
|
|
/* configure pins -> TODO: move into GPIO driver (once implemented) */
|
|
|
|
UART_PORT->SEL |= (UART_RX_PIN | UART_TX_PIN);
|
2023-06-14 16:18:15 +02:00
|
|
|
msp_port_t *port = &UART_PORT->base;
|
|
|
|
port->OD |= UART_RX_PIN;
|
|
|
|
port->OD &= ~(UART_TX_PIN);
|
|
|
|
port->DIR |= UART_TX_PIN;
|
|
|
|
port->DIR &= ~(UART_RX_PIN);
|
2015-08-27 14:18:21 +02:00
|
|
|
/* enable receiver and transmitter */
|
|
|
|
uart_poweron(uart);
|
|
|
|
/* and finally release the software reset bit */
|
2023-06-14 16:18:15 +02:00
|
|
|
dev->CTL &= ~(SWRST);
|
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
|
|
|
{
|
|
|
|
(void)uart;
|
2015-10-20 16:27:05 +02:00
|
|
|
msp_usart_t *dev = UART_BASE;
|
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];
|
|
|
|
}
|
2015-08-27 14:18:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void uart_poweron(uart_t uart)
|
|
|
|
{
|
build: fix unused parameter errors
cpu, sam0_common: fix unused parameter in periph/spi
cpu, kinetis_common: fix unused parameter in periph/spi
cpu, cc2538: fix unused param in periph/i2c
cpu, cc2538: fix unused param in periph/spi
cpu, sam3: fix unused param in periph/spi
cpu, stm32_common: fix unused param in periph/pm
cpu, stm32f3: fix unused params in periph/i2c
cpu, nrf5x_common: fix unused param in periph/gpio
cpu, nrf5x_common: fix unused param in periph/spi
cpu, lpc2387: fix unused params in periph/spi
cpu, cc2538: fix unused params in radio/netdev
cpu, cc2650: fix unused params in periph/uart
cpu, lm4f120: fix unused param in periph/spi
cpu, lm4f120: fix unused params in periph/timer
cpu, lm4f120: fix unused params in periph/uart
cpu, stm32_common: fix unused params in periph/dac
cpu, stm32l0: fix unused params in periph/i2c
cpu, msp430fxyz: fix unused params in periph/uart
cpu, mips: fix unused params
cpu, cc430: fix unused-params in periph/timer
cpu, msp430fxyz: fix unused params in periph/spi
drivers, cc2420: fix unused param
cpu, mips32r2_common: fix unused params in periph/timer
cpu, cc2538: fix unused-param in periph/i2c
cpu, mips32r2_common: fix unused-param in periph/timer
cpu, msp430fxyz: fix unused params in periph/timer
cpu, atmega_common: fix unused params in periph/spi
driver, nrfmin: fix unused params
cpu, cc2538_rf: fix unused params
driver, netdev_ieee802514: fix unused param
cpu, mip_pic32m: fix unused params
cpu, lpc2387: fix unused params in periph/pwm
tests/driver_sdcard_spi: fix unused params
cpu, sam3: fix unused param in periph/pwm
tests/driver_dynamixel: fix unused params, and style issues
cpu, cc430: fix unused param in periph/rtc
cpu, atmega_common: fix unused params in periph/i2c
2017-10-31 12:09:11 +01:00
|
|
|
(void)uart;
|
2023-06-14 16:18:15 +02:00
|
|
|
UART_SFR->ME |= UART_ME_BITS;
|
2015-08-27 14:18:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void uart_poweroff(uart_t uart)
|
|
|
|
{
|
build: fix unused parameter errors
cpu, sam0_common: fix unused parameter in periph/spi
cpu, kinetis_common: fix unused parameter in periph/spi
cpu, cc2538: fix unused param in periph/i2c
cpu, cc2538: fix unused param in periph/spi
cpu, sam3: fix unused param in periph/spi
cpu, stm32_common: fix unused param in periph/pm
cpu, stm32f3: fix unused params in periph/i2c
cpu, nrf5x_common: fix unused param in periph/gpio
cpu, nrf5x_common: fix unused param in periph/spi
cpu, lpc2387: fix unused params in periph/spi
cpu, cc2538: fix unused params in radio/netdev
cpu, cc2650: fix unused params in periph/uart
cpu, lm4f120: fix unused param in periph/spi
cpu, lm4f120: fix unused params in periph/timer
cpu, lm4f120: fix unused params in periph/uart
cpu, stm32_common: fix unused params in periph/dac
cpu, stm32l0: fix unused params in periph/i2c
cpu, msp430fxyz: fix unused params in periph/uart
cpu, mips: fix unused params
cpu, cc430: fix unused-params in periph/timer
cpu, msp430fxyz: fix unused params in periph/spi
drivers, cc2420: fix unused param
cpu, mips32r2_common: fix unused params in periph/timer
cpu, cc2538: fix unused-param in periph/i2c
cpu, mips32r2_common: fix unused-param in periph/timer
cpu, msp430fxyz: fix unused params in periph/timer
cpu, atmega_common: fix unused params in periph/spi
driver, nrfmin: fix unused params
cpu, cc2538_rf: fix unused params
driver, netdev_ieee802514: fix unused param
cpu, mip_pic32m: fix unused params
cpu, lpc2387: fix unused params in periph/pwm
tests/driver_sdcard_spi: fix unused params
cpu, sam3: fix unused param in periph/pwm
tests/driver_dynamixel: fix unused params, and style issues
cpu, cc430: fix unused param in periph/rtc
cpu, atmega_common: fix unused params in periph/i2c
2017-10-31 12:09:11 +01:00
|
|
|
(void)uart;
|
2023-06-14 16:18:15 +02:00
|
|
|
UART_SFR->ME &= ~(UART_ME_BITS);
|
2015-08-27 14:18:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ISR(UART_RX_ISR, isr_uart_0_rx)
|
|
|
|
{
|
|
|
|
__enter_isr();
|
|
|
|
|
2015-09-22 03:49:26 +02:00
|
|
|
/* read character (resets interrupt flag) */
|
2015-10-20 16:27:05 +02:00
|
|
|
char c = UART_BASE->RXBUF;
|
2015-09-22 03:49:26 +02:00
|
|
|
|
|
|
|
/* only call callback if there was no receive error */
|
2015-10-20 16:27:05 +02:00
|
|
|
if(! (UART_BASE->RCTL & RXERR)) {
|
2015-09-22 03:49:26 +02:00
|
|
|
ctx_rx_cb(ctx_isr_arg, c);
|
2015-08-27 14:18:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
__exit_isr();
|
|
|
|
}
|