diff --git a/firmware/libboard/common/include/board_common.h b/firmware/libboard/common/include/board_common.h index f2314ced..27f74097 100644 --- a/firmware/libboard/common/include/board_common.h +++ b/firmware/libboard/common/include/board_common.h @@ -65,10 +65,14 @@ /** UART0 */ /** Console baudrate always using 115200. */ #define CONSOLE_BAUDRATE 115200 -/** Usart Hw interface used by the console (UART0). */ -#define CONSOLE_USART UART0 -/** Usart Hw ID used by the console (UART0). */ +/** UART peripheral used by the console (UART0). */ +#define CONSOLE_UART UART0 +/** UART peripheral ID used by the console (UART0). */ #define CONSOLE_ID ID_UART0 +/** UART ISR used by the console (UART0). */ +#define CONSOLE_ISR UART0_IrqHandler +/** UART IRQ used by the console (UART0). */ +#define CONSOLE_IRQ UART0_IRQn /** Pins description corresponding to Rxd,Txd, (UART pins) */ #define CONSOLE_PINS {PINS_UART} diff --git a/firmware/libboard/common/source/uart_console.c b/firmware/libboard/common/source/uart_console.c index 2fda8ba6..e52cd514 100644 --- a/firmware/libboard/common/source/uart_console.c +++ b/firmware/libboard/common/source/uart_console.c @@ -43,6 +43,8 @@ #include #include +#include "ringbuffer.h" + /*---------------------------------------------------------------------------- * Definitions *----------------------------------------------------------------------------*/ @@ -52,7 +54,9 @@ *----------------------------------------------------------------------------*/ /** Is Console Initialized. */ -static uint8_t _ucIsConsoleInitialized=0 ; +static uint8_t _ucIsConsoleInitialized=0; +/** Ring buffer to queue data to be sent */ +static ringbuf uart_tx_buffer; /** * \brief Configures an USART peripheral with the specified parameters. @@ -63,7 +67,7 @@ static uint8_t _ucIsConsoleInitialized=0 ; extern void UART_Configure( uint32_t baudrate, uint32_t masterClock) { const Pin pPins[] = CONSOLE_PINS; - Uart *pUart = CONSOLE_USART; + Uart *pUart = CONSOLE_UART; /* Configure PIO */ PIO_Configure(pPins, PIO_LISTSIZE(pPins)); @@ -85,12 +89,34 @@ extern void UART_Configure( uint32_t baudrate, uint32_t masterClock) /* Disable PDC channel */ pUart->UART_PTCR = UART_PTCR_RXTDIS | UART_PTCR_TXTDIS; + /* Reset transmit ring buffer */ + rbuf_reset(&uart_tx_buffer); + + /* Enable TX interrupts */ + pUart->UART_IER = UART_IER_TXRDY; + NVIC_EnableIRQ(CONSOLE_IRQ); + /* Enable receiver and transmitter */ pUart->UART_CR = UART_CR_RXEN | UART_CR_TXEN; + /* Remember the configuration is complete */ _ucIsConsoleInitialized=1 ; } +/** Interrupt Service routine to transmit queued data */ +void CONSOLE_ISR(void) +{ + Uart *uart = CONSOLE_UART; + if (uart->UART_SR & UART_SR_TXRDY) { + if (!rbuf_is_empty(&uart_tx_buffer)) { + //uart->UART_IER = UART_IER_TXRDY; + uart->UART_THR = rbuf_read(&uart_tx_buffer); + } else { + uart->UART_IDR = UART_IER_TXRDY; + } + } +} + /** * \brief Outputs a character on the UART line. * @@ -99,19 +125,28 @@ extern void UART_Configure( uint32_t baudrate, uint32_t masterClock) */ extern void UART_PutChar( uint8_t c ) { - Uart *pUart=CONSOLE_USART ; + Uart *pUart = CONSOLE_UART ; + /* Initialize console is not already done */ if ( !_ucIsConsoleInitialized ) { UART_Configure(CONSOLE_BAUDRATE, BOARD_MCK); } - /* Wait for the transmitter to be ready */ - while ( (pUart->UART_SR & UART_SR_TXEMPTY) == 0 ) ; - - /* Send character */ - pUart->UART_THR=c ; + /* Wait until there is space in the buffer */ + while (rbuf_is_full(&uart_tx_buffer)) { + if (pUart->UART_SR & UART_SR_TXEMPTY) { + pUart->UART_IER = UART_IER_TXRDY; + CONSOLE_ISR(); + } + } + /* Put character into buffer */ + rbuf_write(&uart_tx_buffer, c); + if (pUart->UART_SR & UART_SR_TXEMPTY) { + pUart->UART_IER = UART_IER_TXRDY; + CONSOLE_ISR(); + } } /** @@ -122,7 +157,7 @@ extern void UART_PutChar( uint8_t c ) */ extern uint32_t UART_GetChar( void ) { - Uart *pUart=CONSOLE_USART ; + Uart *pUart = CONSOLE_UART ; if ( !_ucIsConsoleInitialized ) { @@ -142,7 +177,7 @@ extern uint32_t UART_GetChar( void ) */ extern uint32_t UART_IsRxReady( void ) { - Uart *pUart=CONSOLE_USART ; + Uart *pUart = CONSOLE_UART; if ( !_ucIsConsoleInitialized ) {