UART Data Getting DMA'd In Wrong Order After First Packet
I have a device that receives six bytes via the UART. There is a known SOF and EOF byte. The first time I get the data, all is well. But subsequent receptions are "corrupted": either the EOF byte is first, or sometimes I get two SOF bytes in a row (and no EOF byte at all).
The DMA is set up for normal (non-circular) operation. Does anyone see why this would be happening? I only call the RX transfer after the callback has been run, so data still being transferred shouldn't be an issue.
int main(void)
{
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DMA_Init();
MX_USART2_UART_Init();
MX_LPUART1_UART_Init();
/* Infinite loop */
while (1)
{
switch(state)
{
case STATE_READY_TO_RECEIVE:
Result = HAL_UART_Receive_DMA (&huart2, RX_Data, 6);
// check result for errors
break;
case STATE_READY_TO_SEND:
Result = HAL_UART_Transmit_DMA(&hlpuart1, ACK_Data, 3);
// check result for errors
state = STATE_SENDING;
break;
case STATE_RECEIVING:
case STATE_SENDING:
// Nothing to do, waiting for the hardware
break;
default:
// handle bad state error
break;
}
}
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if(RX_Data[0] == 0xA1)
{
state = STATE_READY_TO_SEND;
}
else
{
memset(RX_Data, 0, 6);
}
//Process data
}
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
if (state == STATE_SENDING)
{
state = STATE_READY_TO_RECEIVE;
}
else
{
// error - state machine broken
}
}