Tool/software: Code Composer Studio
Hello I'm trying to use UART with queue.
I think it doesn't have defect in logic but it works strange in the way.
For example, I added the content like "STATE 13 0 MODE 1 CALSTATE 0" and the result shows "STATEE 13 0 MODE 1 CALSTATE 0" or "STATE 33 0 MODE 1 CALSTATE 0"
I tried to debug and I can't understand the situation I encountered.
To do debugging, first I made a array variable and I stored the data MCU sent. And I confirm that queue's data is fine and the data come out from queue isn't fine.
But if I use break point every time, the data come out from queue suddenly works fine.
So, I think there's something wrong but I can't understand what's going on.
I attach the code I'm using
-----------------------------------------------------------------------------------------------------------------------------------
<<<<<<<<serialFunc.h>>>>>>>
#define BUFFERSIZE 256
class buffer
{
private:
volatile uint8_t data[BUFFERSIZE];
volatile uint32_t length;
volatile uint32_t size;
volatile uint32_t index;
volatile uint32_t lineNum;
volatile uint32_t readIndex;
volatile uint32_t writeIndex;
public :
void init();
void addToEnd(uint8_t data);
uint32_t byteAvailable();
uint32_t lineAvailable();
uint8_t getFromFront();
uint8_t peek();
void addArrayToEnd(const char* pui8Buffer);
inline bool full();
inline bool empty();
};
<<<<<<<<buffer.cpp>>>>>>>>
void buffer::init()
{
length = 0;
size = BUFFERSIZE;
index = 0;
lineNum = 0;
readIndex = 0;
writeIndex = 0;
for(int i=0; i<BUFFERSIZE; i++)
data[i] = 0;
}
void buffer::addToEnd(uint8_t cdata)
{
IntMasterDisable();
if(!full())
{
data[writeIndex] = cdata;
writeIndex = (writeIndex+1)%size;
if(cdata == '\n')
lineNum++;
}
IntMasterEnable();
}
void buffer::addArrayToEnd(const char* pui8Buffer)
{
IntMasterDisable();
while(*pui8Buffer)
{
if(full())
break;
if(*pui8Buffer == '\n')
lineNum++;
data[writeIndex] = *pui8Buffer;
pui8Buffer++;
writeIndex = (writeIndex+1)%size;
}
IntMasterEnable();
}
uint32_t buffer::byteAvailable()
{
return (writeIndex+size-readIndex)%size;
}
uint32_t buffer::lineAvailable()
{
return lineNum;
}
uint8_t buffer::getFromFront()
{
IntMasterDisable();
static char returnValue;
returnValue = 0;
if(!empty())
{
returnValue = data[readIndex];
readIndex = (readIndex+1)%size;
}
if(returnValue == '\n')
lineNum--;
IntMasterEnable();
return returnValue;
}
uint8_t buffer::peek()
{
IntMasterDisable();
static uint8_t returnValue;
if(empty())
returnValue = 0;
else
returnValue = data[readIndex];
IntMasterEnable();
return returnValue;
}
inline bool buffer::empty()
{
if(readIndex == writeIndex)
return true;
else
return false;
}
inline bool buffer::full()
{
if(((writeIndex+1)%size) == readIndex)
return true;
else
return false;
}
<<<<<<<UART.cpp>>>>>>>>
void UARTIntHandler(void)
{
IntMasterDisable();
static uint32_t ui32Status;
//
// Get the interrupt status.
//
ui32Status = UARTIntStatus(UART0_BASE, true);
//
// Clear the asserted interrupts.
//
UARTIntClear(UART0_BASE, ui32Status);
//
// Loop while there are characters in the receive FIFO.
//
if(ui32Status & UART_INT_TX)
Serial.transmit();
if(UARTCharsAvail(UART0_BASE))
{
Serial.receive();
}
IntMasterEnable();
}
void serial::init()
{
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
//
// Enable pin PA0 for UART0 U0RX
//
GPIOPinConfigure(GPIO_PA0_U0RX);
GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0);
//
// Enable pin PA1 for UART0 U0TX
//
GPIOPinConfigure(GPIO_PA1_U0TX);
GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_1);
UARTConfigSetExpClk(UART0_BASE, SysCtlClockGet(), 921600, (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE | UART_CONFIG_PAR_NONE));
//
// Enable the UART interrupt.
//
UARTFIFOLevelSet(UART0_BASE, UART_FIFO_TX2_8, UART_FIFO_RX4_8);
IntEnable(INT_UART0);
UARTIntEnable(UART0_BASE, UART_INT_RX | UART_INT_RT | UART_INT_TX);
IntRegister(INT_UART0, UARTIntHandler);
//IntPrioritySet(INT_UART0, 0xE0);
txBuffer.init();
rxBuffer.init();
timeout = 1000;
}
void serial::transmit()
{
bool doTransmit = true;
while (UARTSpaceAvail(UART0_BASE) && doTransmit)
{
if(!txBuffer.empty())
{
uint8_t cdata = txBuffer.peek();
txBuffer.getFromFront();
if(cdata)
UARTCharPutNonBlocking(UART0_BASE, cdata);
}
else
{
doTransmit = false;
}
}
}