console: use buffer and interrupts instead of busy loops for UART debug output

This commit is contained in:
Kévin Redon
2018-06-25 15:55:33 +02:00
parent 2bac56494f
commit 67e181fb15
2 changed files with 52 additions and 13 deletions

View File

@@ -65,10 +65,14 @@
/** UART0 */ /** UART0 */
/** Console baudrate always using 115200. */ /** Console baudrate always using 115200. */
#define CONSOLE_BAUDRATE 115200 #define CONSOLE_BAUDRATE 115200
/** Usart Hw interface used by the console (UART0). */ /** UART peripheral used by the console (UART0). */
#define CONSOLE_USART UART0 #define CONSOLE_UART UART0
/** Usart Hw ID used by the console (UART0). */ /** UART peripheral ID used by the console (UART0). */
#define CONSOLE_ID ID_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) */ /** Pins description corresponding to Rxd,Txd, (UART pins) */
#define CONSOLE_PINS {PINS_UART} #define CONSOLE_PINS {PINS_UART}

View File

@@ -43,6 +43,8 @@
#include <stdio.h> #include <stdio.h>
#include <stdint.h> #include <stdint.h>
#include "ringbuffer.h"
/*---------------------------------------------------------------------------- /*----------------------------------------------------------------------------
* Definitions * Definitions
*----------------------------------------------------------------------------*/ *----------------------------------------------------------------------------*/
@@ -52,7 +54,9 @@
*----------------------------------------------------------------------------*/ *----------------------------------------------------------------------------*/
/** Is Console Initialized. */ /** 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. * \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) extern void UART_Configure( uint32_t baudrate, uint32_t masterClock)
{ {
const Pin pPins[] = CONSOLE_PINS; const Pin pPins[] = CONSOLE_PINS;
Uart *pUart = CONSOLE_USART; Uart *pUart = CONSOLE_UART;
/* Configure PIO */ /* Configure PIO */
PIO_Configure(pPins, PIO_LISTSIZE(pPins)); PIO_Configure(pPins, PIO_LISTSIZE(pPins));
@@ -85,12 +89,34 @@ extern void UART_Configure( uint32_t baudrate, uint32_t masterClock)
/* Disable PDC channel */ /* Disable PDC channel */
pUart->UART_PTCR = UART_PTCR_RXTDIS | UART_PTCR_TXTDIS; 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 */ /* Enable receiver and transmitter */
pUart->UART_CR = UART_CR_RXEN | UART_CR_TXEN; pUart->UART_CR = UART_CR_RXEN | UART_CR_TXEN;
/* Remember the configuration is complete */
_ucIsConsoleInitialized=1 ; _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. * \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 ) extern void UART_PutChar( uint8_t c )
{ {
Uart *pUart=CONSOLE_USART ; Uart *pUart = CONSOLE_UART ;
/* Initialize console is not already done */
if ( !_ucIsConsoleInitialized ) if ( !_ucIsConsoleInitialized )
{ {
UART_Configure(CONSOLE_BAUDRATE, BOARD_MCK); UART_Configure(CONSOLE_BAUDRATE, BOARD_MCK);
} }
/* Wait for the transmitter to be ready */ /* Wait until there is space in the buffer */
while ( (pUart->UART_SR & UART_SR_TXEMPTY) == 0 ) ; while (rbuf_is_full(&uart_tx_buffer)) {
if (pUart->UART_SR & UART_SR_TXEMPTY) {
/* Send character */ pUart->UART_IER = UART_IER_TXRDY;
pUart->UART_THR=c ; 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 ) extern uint32_t UART_GetChar( void )
{ {
Uart *pUart=CONSOLE_USART ; Uart *pUart = CONSOLE_UART ;
if ( !_ucIsConsoleInitialized ) if ( !_ucIsConsoleInitialized )
{ {
@@ -142,7 +177,7 @@ extern uint32_t UART_GetChar( void )
*/ */
extern uint32_t UART_IsRxReady( void ) extern uint32_t UART_IsRxReady( void )
{ {
Uart *pUart=CONSOLE_USART ; Uart *pUart = CONSOLE_UART;
if ( !_ucIsConsoleInitialized ) if ( !_ucIsConsoleInitialized )
{ {