2013-01-12 22:44:42 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2012, Texas Instruments Incorporated - http://www.ti.com/
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* 3. Neither the name of the copyright holder nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
|
|
|
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* \addtogroup cc2538-uart
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* \file
|
|
|
|
* Implementation of the cc2538 UART driver
|
|
|
|
*/
|
|
|
|
#include "contiki.h"
|
|
|
|
#include "sys/energest.h"
|
|
|
|
#include "dev/sys-ctrl.h"
|
|
|
|
#include "dev/ioc.h"
|
|
|
|
#include "dev/gpio.h"
|
|
|
|
#include "dev/uart.h"
|
2013-11-15 16:24:26 +00:00
|
|
|
#include "lpm.h"
|
2013-01-12 22:44:42 +00:00
|
|
|
#include "reg.h"
|
|
|
|
|
2013-11-15 16:24:26 +00:00
|
|
|
#include <stdbool.h>
|
2013-01-12 22:44:42 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
static int (* input_handler)(unsigned char c);
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2013-11-25 14:00:41 +00:00
|
|
|
#define UART_RX_PORT_BASE GPIO_PORT_TO_BASE(UART_RX_PORT)
|
|
|
|
#define UART_RX_PIN_MASK GPIO_PIN_MASK(UART_RX_PIN)
|
|
|
|
|
|
|
|
#define UART_TX_PORT_BASE GPIO_PORT_TO_BASE(UART_TX_PORT)
|
|
|
|
#define UART_TX_PIN_MASK GPIO_PIN_MASK(UART_TX_PIN)
|
|
|
|
|
|
|
|
#define UART_CTS_PORT_BASE GPIO_PORT_TO_BASE(UART_CTS_PORT)
|
|
|
|
#define UART_CTS_PIN_MASK GPIO_PIN_MASK(UART_CTS_PIN)
|
|
|
|
|
|
|
|
#define UART_RTS_PORT_BASE GPIO_PORT_TO_BASE(UART_RTS_PORT)
|
|
|
|
#define UART_RTS_PIN_MASK GPIO_PIN_MASK(UART_RTS_PIN)
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2013-05-15 11:59:49 +00:00
|
|
|
/*
|
|
|
|
* Once we know what UART we're on, configure correct values to be written to
|
|
|
|
* the correct registers
|
|
|
|
*/
|
|
|
|
#if UART_BASE==UART_1_BASE
|
|
|
|
/* Running, in sleep, in deep sleep, enable the clock for the correct UART */
|
|
|
|
#define SYS_CTRL_RCGCUART_UART SYS_CTRL_RCGCUART_UART1
|
|
|
|
#define SYS_CTRL_SCGCUART_UART SYS_CTRL_SCGCUART_UART1
|
|
|
|
#define SYS_CTRL_DCGCUART_UART SYS_CTRL_DCGCUART_UART1
|
|
|
|
|
|
|
|
#define NVIC_INT_UART NVIC_INT_UART1
|
|
|
|
#define IOC_PXX_SEL_UART_TXD IOC_PXX_SEL_UART1_TXD
|
|
|
|
#define IOC_UARTRXD_UART IOC_UARTRXD_UART1
|
|
|
|
#else /* Defaults for UART0 */
|
|
|
|
#define SYS_CTRL_RCGCUART_UART SYS_CTRL_RCGCUART_UART0
|
|
|
|
#define SYS_CTRL_SCGCUART_UART SYS_CTRL_SCGCUART_UART0
|
|
|
|
#define SYS_CTRL_DCGCUART_UART SYS_CTRL_DCGCUART_UART0
|
|
|
|
|
|
|
|
#define NVIC_INT_UART NVIC_INT_UART0
|
|
|
|
|
|
|
|
#define IOC_PXX_SEL_UART_TXD IOC_PXX_SEL_UART0_TXD
|
|
|
|
#define IOC_UARTRXD_UART IOC_UARTRXD_UART0
|
|
|
|
#endif
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2013-01-12 22:44:42 +00:00
|
|
|
static void
|
|
|
|
reset(void)
|
|
|
|
{
|
|
|
|
uint32_t lchr;
|
|
|
|
|
|
|
|
/* Make sure the UART is disabled before trying to configure it */
|
2014-03-14 20:06:24 +00:00
|
|
|
REG(UART_BASE | UART_CTL) = UART_CTL_VALUE;
|
2013-01-12 22:44:42 +00:00
|
|
|
|
|
|
|
/* Clear error status */
|
2013-05-15 11:59:49 +00:00
|
|
|
REG(UART_BASE | UART_ECR) = 0xFF;
|
2013-01-12 22:44:42 +00:00
|
|
|
|
|
|
|
/* Store LCHR configuration */
|
2013-05-15 11:59:49 +00:00
|
|
|
lchr = REG(UART_BASE | UART_LCRH);
|
2013-01-12 22:44:42 +00:00
|
|
|
|
|
|
|
/* Flush FIFOs by clearing LCHR.FEN */
|
2013-05-15 11:59:49 +00:00
|
|
|
REG(UART_BASE | UART_LCRH) = 0;
|
2013-01-12 22:44:42 +00:00
|
|
|
|
|
|
|
/* Restore LCHR configuration */
|
2013-05-15 11:59:49 +00:00
|
|
|
REG(UART_BASE | UART_LCRH) = lchr;
|
2013-01-12 22:44:42 +00:00
|
|
|
|
|
|
|
/* UART Enable */
|
2013-05-15 11:59:49 +00:00
|
|
|
REG(UART_BASE | UART_CTL) |= UART_CTL_UARTEN;
|
2013-01-12 22:44:42 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2013-11-15 16:24:26 +00:00
|
|
|
static bool
|
|
|
|
permit_pm1(void)
|
|
|
|
{
|
|
|
|
/* Note: UART_FR.TXFE reads 0 if the UART clock is gated. */
|
|
|
|
return (REG(SYS_CTRL_RCGCUART) & SYS_CTRL_RCGCUART_UART) == 0 ||
|
|
|
|
(REG(UART_BASE | UART_FR) & UART_FR_TXFE) != 0;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2013-01-12 22:44:42 +00:00
|
|
|
void
|
|
|
|
uart_init(void)
|
|
|
|
{
|
2013-11-15 16:24:26 +00:00
|
|
|
lpm_register_peripheral(permit_pm1);
|
|
|
|
|
2013-01-12 22:44:42 +00:00
|
|
|
/* Enable clock for the UART while Running, in Sleep and Deep Sleep */
|
2013-05-15 11:59:49 +00:00
|
|
|
REG(SYS_CTRL_RCGCUART) |= SYS_CTRL_RCGCUART_UART;
|
|
|
|
REG(SYS_CTRL_SCGCUART) |= SYS_CTRL_SCGCUART_UART;
|
|
|
|
REG(SYS_CTRL_DCGCUART) |= SYS_CTRL_DCGCUART_UART;
|
2013-01-12 22:44:42 +00:00
|
|
|
|
|
|
|
/* Run on SYS_DIV */
|
2013-05-15 11:59:49 +00:00
|
|
|
REG(UART_BASE | UART_CC) = 0;
|
2013-01-12 22:44:42 +00:00
|
|
|
|
2013-05-15 11:59:49 +00:00
|
|
|
/*
|
|
|
|
* Select the UARTx RX pin by writing to the IOC_UARTRXD_UARTn register
|
|
|
|
*
|
|
|
|
* The value to be written will be on of the IOC_INPUT_SEL_Pxn defines from
|
|
|
|
* ioc.h. The value can also be calculated as:
|
|
|
|
*
|
|
|
|
* (port << 3) + pin
|
|
|
|
*/
|
|
|
|
REG(IOC_UARTRXD_UART) = (UART_RX_PORT << 3) + UART_RX_PIN;
|
2013-01-12 22:44:42 +00:00
|
|
|
|
2013-05-15 11:59:49 +00:00
|
|
|
/*
|
|
|
|
* Pad Control for the TX pin:
|
|
|
|
* - Set function to UART0 TX
|
|
|
|
* - Output Enable
|
|
|
|
*/
|
|
|
|
ioc_set_sel(UART_TX_PORT, UART_TX_PIN, IOC_PXX_SEL_UART_TXD);
|
|
|
|
ioc_set_over(UART_TX_PORT, UART_TX_PIN, IOC_OVERRIDE_OE);
|
2013-01-12 22:44:42 +00:00
|
|
|
|
2013-09-11 13:23:56 +00:00
|
|
|
/* Set RX and TX pins to peripheral mode */
|
2013-11-25 14:00:41 +00:00
|
|
|
GPIO_PERIPHERAL_CONTROL(UART_TX_PORT_BASE, UART_TX_PIN_MASK);
|
|
|
|
GPIO_PERIPHERAL_CONTROL(UART_RX_PORT_BASE, UART_RX_PIN_MASK);
|
2013-01-12 22:44:42 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* UART Interrupt Masks:
|
|
|
|
* Acknowledge RX and RX Timeout
|
|
|
|
* Acknowledge Framing, Overrun and Break Errors
|
|
|
|
*/
|
2013-05-15 11:59:49 +00:00
|
|
|
REG(UART_BASE | UART_IM) = UART_IM_RXIM | UART_IM_RTIM;
|
|
|
|
REG(UART_BASE | UART_IM) |= UART_IM_OEIM | UART_IM_BEIM | UART_IM_FEIM;
|
2013-01-12 22:44:42 +00:00
|
|
|
|
2013-05-15 11:59:49 +00:00
|
|
|
REG(UART_BASE | UART_IFLS) =
|
2013-01-12 22:44:42 +00:00
|
|
|
UART_IFLS_RXIFLSEL_1_8 | UART_IFLS_TXIFLSEL_1_2;
|
|
|
|
|
|
|
|
/* Make sure the UART is disabled before trying to configure it */
|
2014-03-14 20:06:24 +00:00
|
|
|
REG(UART_BASE | UART_CTL) = UART_CTL_VALUE;
|
2013-01-12 22:44:42 +00:00
|
|
|
|
|
|
|
/* Baud Rate Generation */
|
2014-03-14 20:06:24 +00:00
|
|
|
uart_set_baudrate(UART_CONF_BAUD_RATE);
|
2013-01-12 22:44:42 +00:00
|
|
|
|
|
|
|
/* UART Control: 8N1 with FIFOs */
|
2013-05-15 11:59:49 +00:00
|
|
|
REG(UART_BASE | UART_LCRH) = UART_LCRH_WLEN_8 | UART_LCRH_FEN;
|
2013-01-12 22:44:42 +00:00
|
|
|
|
|
|
|
/* UART Enable */
|
2013-05-15 11:59:49 +00:00
|
|
|
REG(UART_BASE | UART_CTL) |= UART_CTL_UARTEN;
|
2013-01-12 22:44:42 +00:00
|
|
|
|
|
|
|
/* Enable UART0 Interrupts */
|
2013-05-15 11:59:49 +00:00
|
|
|
nvic_interrupt_enable(NVIC_INT_UART);
|
2013-01-12 22:44:42 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
uart_set_input(int (* input)(unsigned char c))
|
|
|
|
{
|
|
|
|
input_handler = input;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
uart_write_byte(uint8_t b)
|
|
|
|
{
|
|
|
|
/* Block if the TX FIFO is full */
|
2013-05-15 11:59:49 +00:00
|
|
|
while(REG(UART_BASE | UART_FR) & UART_FR_TXFF);
|
2013-01-12 22:44:42 +00:00
|
|
|
|
2013-05-15 11:59:49 +00:00
|
|
|
REG(UART_BASE | UART_DR) = b;
|
2013-01-12 22:44:42 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
uart_isr(void)
|
|
|
|
{
|
|
|
|
uint16_t mis;
|
|
|
|
|
|
|
|
ENERGEST_ON(ENERGEST_TYPE_IRQ);
|
|
|
|
|
|
|
|
/* Store the current MIS and clear all flags early, except the RTM flag.
|
|
|
|
* This will clear itself when we read out the entire FIFO contents */
|
2013-05-15 11:59:49 +00:00
|
|
|
mis = REG(UART_BASE | UART_MIS) & 0x0000FFFF;
|
2013-01-12 22:44:42 +00:00
|
|
|
|
2013-05-15 11:59:49 +00:00
|
|
|
REG(UART_BASE | UART_ICR) = 0x0000FFBF;
|
2013-01-12 22:44:42 +00:00
|
|
|
|
|
|
|
if(mis & (UART_MIS_RXMIS | UART_MIS_RTMIS)) {
|
2013-05-15 11:59:49 +00:00
|
|
|
while(!(REG(UART_BASE | UART_FR) & UART_FR_RXFE)) {
|
2013-01-12 22:44:42 +00:00
|
|
|
if(input_handler != NULL) {
|
2013-05-15 11:59:49 +00:00
|
|
|
input_handler((unsigned char)(REG(UART_BASE | UART_DR) & 0xFF));
|
2013-01-12 22:44:42 +00:00
|
|
|
} else {
|
|
|
|
/* To prevent an Overrun Error, we need to flush the FIFO even if we
|
|
|
|
* don't have an input_handler. Use mis as a data trash can */
|
2013-05-15 11:59:49 +00:00
|
|
|
mis = REG(UART_BASE | UART_DR);
|
2013-01-12 22:44:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if(mis & (UART_MIS_OEMIS | UART_MIS_BEMIS | UART_MIS_FEMIS)) {
|
|
|
|
/* ISR triggered due to some error condition */
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
ENERGEST_OFF(ENERGEST_TYPE_IRQ);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @} */
|