Hi all,
I am using a tiva C controller, TM4C123GH6PM, for my IMU application. I want to print my debug message through UART. My code is below. Function description:
ConfigUART: Configure UART
UARTIntHandler: Interrupt handler. In this function, I check for UART message receiving and transmitting interrupt. If there is a transmitting interrupt, it means that the previous byte is transmitted, I will check if there is any byte in my transmitting buffer, UARTTXBuffer. If yes, transmit that byte, if no, annouce the upper layer that transmitting is done.
UARTSend: Put bytes in to buffer and send the first byte using UARTCharPutNonBlocking to create an interrupt.
UART.c
inline void ConfigUART(uint32_t sysClk, uint32_t baudrate)
{
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
GPIOPinConfigure(GPIO_PA0_U0RX);
GPIOPinConfigure(GPIO_PA1_U0TX);
GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
// UARTConfigSetExpClk(UART0_BASE, sysClk, baudrate,
// (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE |
// UART_CONFIG_PAR_NONE));
UARTStdioConfig(0, baudrate, sysClk);
UARTIntRegister(UART0_BASE, &UARTIntHandler);
IntEnable(INT_UART0);
UARTIntEnable(UART0_BASE, UART_INT_RX | UART_INT_RT | UART_INT_TX);
UARTTxIntModeSet(UART0_BASE, UART_TXINT_MODE_EOT);
IntMasterEnable(); //Enable processor interrupts
UARTClearEvtQueue();
}
/** Description:
* @param :
* @return:
*/
static void UARTIntHandler()
{
uint32_t IntStatus;
IntStatus = UARTIntStatus(UART0_BASE, true);
UARTIntClear(UART0_BASE, IntStatus);
// Check for UART transmitting interrupt
if (IntStatus & UART_INT_TX)
{
if (txFootCounter < MAX_TX_BUFFER)
{
if (txHeadCounter != txFootCounter)
{
UARTCharPutNonBlocking(UART0_BASE, UARTTXBuffer[txFootCounter++]);
}
else
{
UARTPutEvtIntoQueue(UART_TX_DONE_EVENT);
}
}
else
{
if (0 != txHeadCounter)
{
UARTCharPutNonBlocking(UART0_BASE, UARTTXBuffer[0]);
txFootCounter = 1;
}
else
{
UARTPutEvtIntoQueue(UART_TX_DONE_EVENT);
}
}
}
// Check for UART receiving interrupt
while (UARTCharsAvail(UART0_BASE))
{
isNewData = true;
if (rxHeadCounter + 1 < MAX_RX_BUFFER)
{
if ((rxHeadCounter + 1) != rxFootCounter)
{
UARTRXBuffer[rxHeadCounter++] = UARTCharGet(UART0_BASE);
rxSize++;
}
}
else
{
if (0 != rxFootCounter)
{
UARTRXBuffer[rxHeadCounter] = UARTCharGet(UART0_BASE);
rxSize++;
rxHeadCounter = 0;
}
}
}
}
bool UARTSend(uint8_t *pui8Buffer, uint32_t ui32Count)
{
//
// Loop while there are more characters to send.
//
while (ui32Count--)
{
if (txHeadCounter + 1 < MAX_TX_BUFFER)
{
if ((txHeadCounter + 1) != txFootCounter)
{
if(*pui8Buffer == '\n')
UARTTXBuffer[txHeadCounter++] = '\r';
UARTTXBuffer[txHeadCounter++] = *pui8Buffer++;
}
else
return false;
}
else
{
if (0 != txFootCounter)
{
if(*pui8Buffer == '\n')
UARTTXBuffer[txHeadCounter++] = '\r';
UARTTXBuffer[txHeadCounter] = *pui8Buffer++;
txHeadCounter = 0;
}
else
return false;
}
}
UARTCharPutNonBlocking(UART0_BASE, UARTTXBuffer[txFootCounter]);
txFootCounter++;
return true;
}
I have another c file for definition of log functions:
Log.c
void PrintDebug(const char *string, ...)
{
volatile char txBuf[100];
DEBUG_HEADER();
va_list arg;
va_start(arg, string);
int len = vsprintf((char *) txBuf, string, arg);
va_end(arg);
UARTSend((uint8_t *) txBuf, len);
COLOR_ENDL();
}
My question is that when I print message through UART, I get the flowing issue. My baudrate in my terminal and MCU is match.
I have another question. In my PrintDebug function, I have use a static array txBuf[100]. I use it with function vsprintf to create a string to send. But that will lead to memory problem. How can I avoid that?
