工具/软件:Code Composer Studio
大家好、我正在尝试将 UART 与队列结合使用。
我觉得逻辑方面没有缺陷、但在这方面工作起来很奇怪。
例如、我添加了"状态13 0模式1 CALSTATE 0"等内容、结果显示为"STATEE 13 0模式1 CALSTATE 0"或"状态33 0模式1 CALSTATE 0"
我尝试调试、无法理解我遇到的情况。
为了进行调试、首先我设置了一个数组变量、并存储了 MCU 发送的数据。 我确认队列的数据正常、队列中的数据不正常。
但是、如果我每次都使用断点、则从队列中输出的数据会突然工作正常。
所以、我认为有问题、但我无法理解发生了什么。
随附我正在使用的代码
--------------------------------------------------------------------
<<<<<<<< >>>
#define buffersize 256
类缓冲器
{
专用:
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;
公开:
void init();
void addToEnd (uint8_t data);
uint32_t byteAvailable();
uint32_t lineAvailable();
uint8_t getFromFront();
uint8_t peek();
void addArrayToEnd (const char* pui8Buffer);
内联 bool full();
内联 bool empty();
};
<<<<<<<< >>>
空缓冲区:init()
{
长度= 0;
size = buffersize;
索引= 0;
lineNum = 0;
readIndex = 0;
writeIndex = 0;
for (int i=i;<BUFFERSIZE; i++) 0
DATA[i]= 0;
}
空缓冲区:addToEnd (uint8_t cData)
{
IntMasterDisable();
if (!full())(如果(!full()))
{
Data[writeIndex]= cData;
writeIndex =(writeIndex+1)%size;
if (cData ='\n')
lineNum++;
}
IntMasterEnable();
}
空缓冲区::addArrayToEnd (const char* pui8Buffer)
{
IntMasterDisable();
while (* pui8缓冲区)
{
if (full ())
中断;
if (*pui8Buffer='\n')
lineNum++;
data[writeIndex]=* pui8Buffer;
pui8Buffer++;
writeIndex =(writeIndex+1)%size;
}
IntMasterEnable();
}
uint32_t 缓冲区:byteAvailable()
{
返回(writeIndex+size-readIndex)%size;
}
uint32_t 缓冲区:lineAvailable()
{
返回 lineNum;
}
uint8_t 缓冲区::getFromFront()
{
IntMasterDisable();
静态字符返回值;
返回值= 0;
if (!empty())(如果!empty())
{
返回值= data[readIndex];
readIndex =(readIndex+1)%size;
}
if (返回值='\n')
lineNum--;
IntMasterEnable();
返回返回值;
}
uint8_t 缓冲区::peek()
{
IntMasterDisable();
静态 uint8_t 返回值;
if (empty())
返回值= 0;
其他
返回值= data[readIndex];
IntMasterEnable();
返回返回值;
}
内联 bool 缓冲区:empty()
{
if (readIndex == writeIndex)
返回 true;
其他
返回 false;
}
内联 bool 缓冲区:full()
{
if (((writeIndex+1)%size)== readIndex)
返回 true;
其他
返回 false;
}
<<<<<< >>>
空 UARTIntHandler (空)
{
IntMasterDisable();
静态 uint32_t ui32Status;
//
//获取中断状态。
//
ui32Status = UARTIntStatus (UART0_BASE、TRUE);
//
//清除已发出的中断。
//
UARTIntClear (UART0_BASE、ui32Status);
//
//在接收 FIFO 中有字符时循环。
//
IF (ui32Status 和 UART_INT_TX)
serial.transmit ();
if (UARTCharsAvail (UART0_BASE))
{
serial.receive();
}
IntMasterEnable();
}
空序列:init()
{
SysCtlPeripheralEnable (SYSCTL_Periph_UART0);
SysCtlPeripheralEnable (SYSCTL_Periph_GPIOA);
//
// UART0 U0RX 的使能引脚 PA0
//
GPIOPinConfigure (GPIO_PA0_U0RX);
GPIOPinTypeUART (GPIO_Porta_base、GPIO_PIN_0);
//
// UART0 U0TX 的使能引脚 PA1
//
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));
//
//启用 UART 中断。
//
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();
超时= 1000;
}
空串行: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);
}
其他
{
doTransmit = false;
}
}
}