2016-01-23 14:38:51 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 Leon George
|
|
|
|
*
|
|
|
|
* 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_cc26x0
|
2017-06-22 15:43:17 +02:00
|
|
|
* @ingroup drivers_periph_uart
|
2016-01-23 14:38:51 +01:00
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @brief Low-level UART driver implementation
|
|
|
|
*
|
|
|
|
* @author Leon M. George <leon@georgemail.eu>
|
|
|
|
*
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "cpu.h"
|
|
|
|
#include "periph/uart.h"
|
2019-05-29 01:10:38 +02:00
|
|
|
#include "periph_conf.h"
|
2016-01-23 14:38:51 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Bit mask for the fractional part of the baudrate
|
|
|
|
*/
|
|
|
|
#define FRAC_BITS (6U)
|
|
|
|
#define FRAC_MASK (0x3f)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get the enable mask depending on enabled HW flow control
|
|
|
|
*/
|
|
|
|
#if UART_HW_FLOW_CONTROL
|
|
|
|
#define ENABLE_MASK (UART_CTSEN | UART_CTL_RTSEN | \
|
|
|
|
UART_CTL_RXE | UART_CTL_TXE | UART_CTL_UARTEN)
|
|
|
|
#else
|
|
|
|
#define ENABLE_MASK (UART_CTL_RXE | UART_CTL_TXE | UART_CTL_UARTEN)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief allocate memory to store callback functions
|
|
|
|
*/
|
|
|
|
static uart_isr_ctx_t ctx[UART_NUMOF];
|
|
|
|
|
|
|
|
|
|
|
|
int uart_init(uart_t uart, uint32_t baudrate, uart_rx_cb_t rx_cb, void *arg)
|
|
|
|
{
|
|
|
|
/* make sure the uart device is valid */
|
|
|
|
if (uart != 0) {
|
2016-10-28 10:15:46 +02:00
|
|
|
return UART_NODEV;
|
2016-01-23 14:38:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* enable clocks: serial power domain and UART */
|
|
|
|
PRCM->PDCTL0SERIAL = 1;
|
|
|
|
while (!(PRCM->PDSTAT0 & PDSTAT0_SERIAL_ON)) ;
|
|
|
|
uart_poweron(uart);
|
|
|
|
|
|
|
|
/* disable and reset the UART */
|
|
|
|
UART->CTL = 0;
|
|
|
|
|
|
|
|
/* save context */
|
|
|
|
ctx[0].rx_cb = rx_cb;
|
|
|
|
ctx[0].arg = arg;
|
|
|
|
|
|
|
|
/* configure pins */
|
|
|
|
IOC->CFG[UART_TX_PIN] = IOCFG_PORTID_UART0_TX;
|
|
|
|
IOC->CFG[UART_RX_PIN] = (IOCFG_PORTID_UART0_RX | IOCFG_INPUT_ENABLE);
|
|
|
|
#if UART_HW_FLOW_CONTROL
|
|
|
|
IOC->CFG[UART_RTS_PIN] = IOCFG_PORTID_UART0_RTS;
|
|
|
|
IOC->CFG[UART_CTS_PIN] = (IOCFG_PORTID_UART0_CTS | IOCFG_INPUT_ENABLE);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* calculate baud-rate */
|
|
|
|
uint32_t tmp = (CLOCK_CORECLOCK * 4);
|
|
|
|
tmp += (baudrate / 2);
|
|
|
|
tmp /= baudrate;
|
|
|
|
UART->IBRD = (tmp >> FRAC_BITS);
|
|
|
|
UART->FBRD = (tmp & FRAC_MASK);
|
|
|
|
|
|
|
|
/* configure line to 8N1 mode, LRCH must be written after IBRD and FBRD! */
|
|
|
|
UART->LCRH = UART_LCRH_WLEN_8;
|
|
|
|
|
|
|
|
/* enable the RX interrupt */
|
|
|
|
UART->IMSC = UART_IMSC_RXIM;
|
|
|
|
NVIC_EnableIRQ(UART0_IRQN);
|
|
|
|
|
|
|
|
/* start the UART */
|
|
|
|
UART->CTL = ENABLE_MASK;
|
|
|
|
|
2016-10-28 10:15:46 +02:00
|
|
|
return UART_OK;
|
2016-01-23 14:38:51 +01:00
|
|
|
}
|
|
|
|
|
2019-05-29 01:10:38 +02:00
|
|
|
|
|
|
|
#ifdef MODULE_PERIPH_UART_MODECFG
|
|
|
|
int uart_mode(uart_t uart, uart_data_bits_t data_bits, uart_parity_t parity,
|
|
|
|
uart_stop_bits_t stop_bits)
|
|
|
|
{
|
|
|
|
assert(data_bits == UART_DATA_BITS_5 ||
|
|
|
|
data_bits == UART_DATA_BITS_6 ||
|
|
|
|
data_bits == UART_DATA_BITS_7 ||
|
|
|
|
data_bits == UART_DATA_BITS_8);
|
|
|
|
|
|
|
|
assert(parity == UART_PARITY_NONE ||
|
|
|
|
parity == UART_PARITY_EVEN ||
|
|
|
|
parity == UART_PARITY_ODD ||
|
|
|
|
parity == UART_PARITY_MARK ||
|
|
|
|
parity == UART_PARITY_SPACE);
|
|
|
|
|
|
|
|
assert(stop_bits == UART_STOP_BITS_1 ||
|
|
|
|
stop_bits == UART_STOP_BITS_2);
|
|
|
|
|
|
|
|
/* make sure the uart device is valid */
|
|
|
|
if (uart != 0) {
|
|
|
|
return UART_NODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* cc26x0 does not support mark or space parity */
|
|
|
|
if (parity == UART_PARITY_MARK || parity == UART_PARITY_SPACE) {
|
|
|
|
return UART_NOMODE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Disable UART and clear old settings */
|
|
|
|
UART->CTL = 0;
|
|
|
|
UART->LCRH = 0;
|
|
|
|
|
|
|
|
/* Apply setting and enable UART */
|
|
|
|
UART->LCRH = data_bits | parity | stop_bits;
|
|
|
|
UART->CTL = ENABLE_MASK;
|
|
|
|
|
|
|
|
return UART_OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-01-23 14:38:51 +01:00
|
|
|
void uart_write(uart_t uart, const uint8_t *data, size_t len)
|
|
|
|
{
|
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;
|
|
|
|
|
2016-01-23 14:38:51 +01:00
|
|
|
for (size_t i = 0; i < len; i++) {
|
|
|
|
while (UART->FR & UART_FR_TXFF) {}
|
|
|
|
UART->DR = 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;
|
|
|
|
|
2016-01-23 14:38:51 +01:00
|
|
|
PRCM->UARTCLKGR = 1;
|
|
|
|
PRCM->CLKLOADCTL = CLKLOADCTL_LOAD;
|
|
|
|
while (!(PRCM->CLKLOADCTL & CLKLOADCTL_LOADDONE)) {}
|
|
|
|
|
|
|
|
UART->CTL = ENABLE_MASK;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2016-01-23 14:38:51 +01:00
|
|
|
UART->CTL = 0;
|
|
|
|
|
|
|
|
PRCM->UARTCLKGR = 0;
|
|
|
|
PRCM->CLKLOADCTL = CLKLOADCTL_LOAD;
|
|
|
|
while (!(PRCM->CLKLOADCTL & CLKLOADCTL_LOADDONE)) {}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void isr_uart(void)
|
|
|
|
{
|
|
|
|
/* remember pending interrupts */
|
|
|
|
uint32_t mis = UART->MIS;
|
|
|
|
/* clear them */
|
|
|
|
UART->ICR = mis;
|
|
|
|
|
|
|
|
/* read received byte and pass it to the RX callback */
|
|
|
|
if (mis & UART_MIS_RXMIS) {
|
|
|
|
ctx[0].rx_cb(ctx[0].arg, (uint8_t)UART->DR);
|
|
|
|
}
|
|
|
|
|
2016-11-30 18:26:05 +01:00
|
|
|
cortexm_isr_end();
|
2016-01-23 14:38:51 +01:00
|
|
|
}
|