2015-07-04 09:37:54 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 Rakendra Thapa <rakendrathapa@gmail.com
|
|
|
|
*
|
|
|
|
* 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_lm4f120
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @brief Implementation of the low-level UART driver for the LM4F120
|
|
|
|
*
|
|
|
|
* @author Rakendra Thapa <rakendrathapa@gmail.com>
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include "cpu.h"
|
|
|
|
#include "sched.h"
|
|
|
|
#include "thread.h"
|
|
|
|
#include "periph/uart.h"
|
|
|
|
#include "periph_conf.h"
|
|
|
|
|
|
|
|
/* guard the file in case no UART is defined */
|
|
|
|
#if (UART_0_EN)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Struct holding the configuration data for a UART device
|
|
|
|
*/
|
|
|
|
typedef struct {
|
|
|
|
uart_rx_cb_t rx_cb; /**< receive callback */
|
|
|
|
uart_tx_cb_t tx_cb; /**< transmit callback */
|
|
|
|
void *arg; /**< callback argument */
|
|
|
|
} uart_conf_t;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Unified interrupt handler for all UART devices
|
|
|
|
*
|
|
|
|
* @param uartnum the number of the UART that triggered the ISR
|
|
|
|
* @param uart the UART device that triggered the ISR
|
|
|
|
*/
|
|
|
|
//static inline void irq_handler(uart_t uartnum, USART_TypeDef *uart);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief UART device configurations
|
|
|
|
*/
|
|
|
|
static uart_conf_t config[UART_NUMOF];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The list of UART peripherals.
|
|
|
|
*/
|
|
|
|
static const unsigned long g_ulUARTPeriph[3] =
|
|
|
|
{
|
2015-07-14 18:11:25 +02:00
|
|
|
SYSCTL_PERIPH_UART0,
|
|
|
|
SYSCTL_PERIPH_UART1,
|
|
|
|
SYSCTL_PERIPH_UART2
|
2015-07-04 09:37:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The list of all possible base address of the console UART
|
|
|
|
*/
|
|
|
|
static const unsigned long g_ulUARTBase[3] =
|
|
|
|
{
|
2015-07-14 18:11:25 +02:00
|
|
|
UART0_BASE,
|
|
|
|
UART1_BASE,
|
|
|
|
UART2_BASE
|
2015-07-04 09:37:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// The list of possible interrupts for the console UART.
|
|
|
|
//
|
|
|
|
//*****************************************************************************
|
|
|
|
static const unsigned long g_ulUARTInt[3] =
|
|
|
|
{
|
|
|
|
INT_UART0,
|
2015-07-14 18:11:25 +02:00
|
|
|
INT_UART1,
|
|
|
|
INT_UART2
|
2015-07-04 09:37:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**********************************************************************************/
|
|
|
|
/* Configuring the UART console
|
|
|
|
*/
|
|
|
|
int uart_init(uart_t uart, uint32_t baudrate, uart_rx_cb_t rx_cb, uart_tx_cb_t tx_cb, void *arg)
|
|
|
|
{
|
2015-07-14 18:11:25 +02:00
|
|
|
// Check the arguments
|
|
|
|
ASSERT(uart == 0);
|
|
|
|
// Check to make sure the UART peripheral is present
|
|
|
|
if(!ROM_SysCtlPeripheralPresent(SYSCTL_PERIPH_UART0)){
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int res = uart_init_blocking(uart, baudrate);
|
|
|
|
if(res < 0){
|
|
|
|
return res;
|
|
|
|
}
|
2015-07-04 09:37:54 +02:00
|
|
|
|
|
|
|
/* save callbacks */
|
|
|
|
config[uart].rx_cb = rx_cb;
|
|
|
|
config[uart].tx_cb = tx_cb;
|
|
|
|
config[uart].arg = arg;
|
|
|
|
|
2015-07-14 18:11:25 +02:00
|
|
|
// ulBase = g_ulUARTBase[uart];
|
2015-07-04 09:37:54 +02:00
|
|
|
|
2015-07-14 18:11:25 +02:00
|
|
|
// Configure the relevant UART pins for operations as a UART rather than GPIOs.
|
|
|
|
/* enable recieve interrupt */
|
|
|
|
switch (uart){
|
2015-07-04 09:37:54 +02:00
|
|
|
#if UART_0_EN
|
2015-07-14 18:11:25 +02:00
|
|
|
case UART_0:
|
|
|
|
NVIC_SetPriority(UART_0_IRQ_CHAN, UART_IRQ_PRIO);
|
|
|
|
|
|
|
|
ROM_UARTTxIntModeSet(UART0_BASE, UART_TXINT_MODE_EOT);
|
|
|
|
ROM_UARTFIFOLevelSet(UART0_BASE, UART_FIFO_TX4_8, UART_FIFO_RX4_8); // Set FIFO to 8 Characters
|
|
|
|
ROM_UARTFIFOEnable(UART0_BASE); // Enable FIFOs
|
2015-07-15 11:48:31 +02:00
|
|
|
|
2015-07-14 18:11:25 +02:00
|
|
|
// Enable the UART interrupt
|
2015-07-04 09:37:54 +02:00
|
|
|
NVIC_EnableIRQ(UART_0_IRQ_CHAN);
|
2015-07-14 18:11:25 +02:00
|
|
|
// Enable RX interrupt
|
|
|
|
UART0_IM_R = UART_IM_RXIM | UART_IM_RTIM;
|
|
|
|
break;
|
2015-07-04 09:37:54 +02:00
|
|
|
#endif
|
|
|
|
#if UART_1_EN
|
2015-07-14 18:11:25 +02:00
|
|
|
case UART_1:
|
2015-07-04 09:37:54 +02:00
|
|
|
NVIC_SetPriority(UART_1_IRQ_CHAN, UART_IRQ_PRIO);
|
2015-07-14 18:11:25 +02:00
|
|
|
// Enable the UART interrupt
|
2015-07-04 09:37:54 +02:00
|
|
|
NVIC_EnableIRQ(UART_1_IRQ_CHAN);
|
2015-07-14 18:11:25 +02:00
|
|
|
break;
|
2015-07-04 09:37:54 +02:00
|
|
|
#endif
|
2015-07-14 18:11:25 +02:00
|
|
|
}
|
|
|
|
return 0;
|
2015-07-04 09:37:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int uart_init_blocking(uart_t uart, uint32_t baudrate)
|
|
|
|
{
|
2015-07-14 18:11:25 +02:00
|
|
|
switch(uart){
|
2015-07-04 09:37:54 +02:00
|
|
|
#if UART_0_EN
|
2015-07-14 18:11:25 +02:00
|
|
|
case UART_0:
|
|
|
|
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
|
|
|
|
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
|
|
|
|
ROM_GPIOPinConfigure(GPIO_PA0_U0RX);
|
|
|
|
ROM_GPIOPinConfigure(GPIO_PA1_U0TX);
|
|
|
|
ROM_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
|
2015-07-04 09:37:54 +02:00
|
|
|
|
2015-07-14 18:11:25 +02:00
|
|
|
ROM_UARTDisable(UART0_BASE);
|
|
|
|
ROM_UARTConfigSetExpClk(UART0_BASE,ROM_SysCtlClockGet(), baudrate,
|
|
|
|
(UART_CONFIG_PAR_NONE | UART_CONFIG_STOP_ONE |
|
|
|
|
UART_CONFIG_WLEN_8));
|
2015-07-04 09:37:54 +02:00
|
|
|
|
2015-07-10 04:06:50 +02:00
|
|
|
|
2015-07-14 18:11:25 +02:00
|
|
|
ROM_UARTEnable(UART0_BASE);
|
|
|
|
break;
|
2015-07-04 09:37:54 +02:00
|
|
|
#endif
|
2015-07-14 18:11:25 +02:00
|
|
|
}
|
|
|
|
return 0;
|
2015-07-04 09:37:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void uart_tx_begin(uart_t uart)
|
|
|
|
{
|
2015-07-14 18:11:25 +02:00
|
|
|
uart_write(uart, '\0');
|
|
|
|
UART0_IM_R |= UART_IM_TXIM;
|
2015-07-04 09:37:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int uart_write(uart_t uart, char data)
|
|
|
|
{
|
2015-07-14 18:11:25 +02:00
|
|
|
int ret=ROM_UARTCharPutNonBlocking(UART0_BASE, data);
|
2015-07-10 04:06:50 +02:00
|
|
|
return ret;
|
2015-07-04 09:37:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int uart_read_blocking(uart_t uart, char *data)
|
|
|
|
{
|
2015-07-14 18:11:25 +02:00
|
|
|
*data = (char)ROM_UARTCharGet(UART0_BASE);
|
|
|
|
return 1;
|
2015-07-04 09:37:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int uart_write_blocking(uart_t uart, char data)
|
|
|
|
{
|
2015-07-14 18:11:25 +02:00
|
|
|
ROM_UARTCharPut(UART0_BASE, data);
|
2015-07-04 09:37:54 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void uart_poweron(uart_t uart)
|
|
|
|
{
|
2015-07-14 18:11:25 +02:00
|
|
|
ROM_UARTEnable(UART0_BASE);
|
2015-07-04 09:37:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void uart_poweroff(uart_t uart)
|
|
|
|
{
|
2015-07-14 18:11:25 +02:00
|
|
|
ROM_UARTDisable(UART0_BASE);
|
2015-07-04 09:37:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//*****************************************************************************
|
|
|
|
//
|
|
|
|
// The UART interrupt handler.
|
|
|
|
//
|
|
|
|
//*****************************************************************************
|
|
|
|
|
2015-07-14 00:57:52 +02:00
|
|
|
void isr_usart0(void)
|
2015-07-04 09:37:54 +02:00
|
|
|
{
|
2015-07-14 18:11:25 +02:00
|
|
|
unsigned long ulStatus;
|
|
|
|
char cChar;
|
|
|
|
long lChar;
|
|
|
|
|
|
|
|
// Get the interrupt status
|
|
|
|
ulStatus = ROM_UARTIntStatus(UART0_BASE, true);
|
|
|
|
// Clear the asserted interrupts
|
|
|
|
ROM_UARTIntClear(UART0_BASE, ulStatus);
|
|
|
|
|
|
|
|
// Are we interrupted due to TX done
|
|
|
|
if(ulStatus & UART_INT_TX)
|
|
|
|
{
|
|
|
|
if (config[UART_0].tx_cb(config[UART_0].arg) == 0){
|
|
|
|
UART0_IM_R &= ~UART_IM_TXIM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Are we interrupted due to a recieved character
|
|
|
|
if(ulStatus & (UART_INT_RX | UART_INT_RT))
|
|
|
|
{
|
|
|
|
// Get all the available characters from the UART
|
|
|
|
while(ROM_UARTCharsAvail(UART0_BASE))
|
|
|
|
{
|
|
|
|
// Read a character
|
|
|
|
lChar = ROM_UARTCharGetNonBlocking(UART0_BASE);
|
|
|
|
cChar = (unsigned char)(lChar & 0xFF);
|
|
|
|
config[UART_0].rx_cb(config[UART_0].arg, cChar);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (sched_context_switch_request) {
|
2015-07-04 09:37:54 +02:00
|
|
|
thread_yield();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* (UART_0_EN || UART_1_EN) */
|