1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
RIOT/cpu/msp430/periph/uart_usart.c

128 lines
3.0 KiB
C
Raw Normal View History

/*
* 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.
*/
/**
* @ingroup cpu_msp430_x1xx
* @ingroup drivers_periph_uart
* @{
*
* @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)
{
int res = init_base(uart, baudrate);
if (res != UART_OK) {
return res;
}
/* save interrupt context */
ctx_rx_cb = rx_cb;
ctx_isr_arg = arg;
/* reset interrupt flags and enable RX interrupt */
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);
return UART_OK;
}
2015-10-20 16:27:05 +02:00
static int init_base(uart_t uart, uint32_t baudrate)
{
if (uart != 0) {
return UART_NODEV;
}
/* 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;
/* power off and reset device */
uart_poweroff(uart);
dev->CTL = SWRST;
/* configure to 8N1 and using the SMCLK*/
dev->CTL |= CHAR;
dev->TCTL = (TXEPT | UXTCTL_SSEL_SMCLK);
dev->RCTL = 0x00;
/* baudrate configuration */
uint16_t br = (uint16_t)(msp430_submain_clock_freq() / baudrate);
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);
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);
/* enable receiver and transmitter */
uart_poweron(uart);
/* and finally release the software reset bit */
dev->CTL &= ~(SWRST);
return UART_OK;
}
2015-10-20 16:27:05 +02:00
void uart_write(uart_t uart, const uint8_t *data, size_t len)
{
(void)uart;
2015-10-20 16:27:05 +02:00
msp_usart_t *dev = UART_BASE;
2015-10-20 16:27:05 +02:00
for (size_t i = 0; i < len; i++) {
while (!(dev->TCTL & TXEPT)) {}
2015-10-20 16:27:05 +02:00
dev->TXBUF = data[i];
}
}
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;
UART_SFR->ME |= UART_ME_BITS;
}
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;
UART_SFR->ME &= ~(UART_ME_BITS);
}
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);
}
__exit_isr();
}