Hi Miguel,
Thank you for your response.
Our aim to keep getting data bytes via USART along as they are coming as we do not know ahead the length of incoming messages.
Here is an extract of my code:
#define BT_USART USART0
#define USART_CLK_SRC kCLOCK_Flexcomm0
#define USART_CLK_FREQ CLOCK_GetFlexCommClkFreq(0U)
#define USART0_IRQHandler FLEXCOMM0_IRQHandler
#define RX_BUFFER_SIZE 64
static uint8_t g_rxBuffer[RX_BUFFER_SIZE];
static volatile size_t receivedBytes=0;
uint8_t receivedDataSize=0;
//Interrupt handing routine
void USART0_IRQHandler(void)
{
uint8_t data;
uint8_t receivedBytes;
uint8_t rxIndex=0;
receivedBytes=(BT_USART->FIFOSTAT>>16)&0x1F;
/* If new data arrived. */
if (kUSART_RxFifoNotEmptyFlag & USART_GetStatusFlags(BT_USART))
{
for(uint8_t i=0; i<receivedBytes; ++i)
{
data = USART_ReadByte(BT_USART);
g_rxBuffer[rxIndex] = data;
rxIndex++;
rxIndex %= RX_BUFFER_SIZE;
}
size_t bytesSent = xStreamBufferSendFromISR( BTStreamBuffer,
( void * ) g_rxBuffer,
receivedBytes,
NULL );
}
else if (kUSART_RxError & USART_GetStatusFlags(USART0))
{
printf("Received ERROR\n");
/* Clear rx error state. */
BT_USART->FIFOSTAT |= USART_FIFOSTAT_RXERR_MASK;
/* clear rxFIFO */
BT_USART->FIFOCFG |= USART_FIFOCFG_EMPTYRX_MASK;
}
SDK_ISR_EXIT_BARRIER;
}
void BT_USART_Init(StreamBufferHandle_t xStreamBuffer)
{
PRINTF("BT_USART_Init entry\n");
/* attach main clock divide to FLEXCOMM0 */
CLOCK_AttachClk(kFRO12M_to_FLEXCOMM0);
BTStreamBuffer = xStreamBuffer;
if(xTaskCreate(BT_USART_Task, "BT_USART_Task", configMINIMAL_STACK_SIZE + 100, NULL, supervisor_task_PRIORITY, &g_BTUSARTTaskHandle) !=pdPASS)
{
PRINTF("BT_USART_Task creation failed!.\r\n");
while (1)
;
}
}
static void BT_USART_Task(void *pvParameters)
{
uint32_t i;
PRINTF("BT_USART_Task entry\n");
/*
* config.baudRate_Bps = 115200U;
* config.parityMode = kUSART_ParityDisabled;
* config.stopBitCount = kUSART_OneStopBit;
* config.loopback = false;
* config.enableTxFifo = false;
* config.enableRxFifo = false;
*/
usart_config_t config;
status_t status;
USART_GetDefaultConfig(&config);
config.baudRate_Bps = 115200U;
config.enableTx = true;
config.enableRx = true;
size_t bytesSent=0;
const TickType_t x100ms = pdMS_TO_TICKS( 100 );
status = USART_Init(BT_USART, &config, USART_CLK_FREQ);
if (kStatus_Success == status){
PRINTF("USART0 init success\n");
}
else{
PRINTF("USART0 init failed\n");
}
/* Enable RX interrupt. */
USART_EnableInterrupts(USART0, kUSART_RxLevelInterruptEnable | kUSART_RxErrorInterruptEnable);
EnableIRQ(FLEXCOMM0_IRQn);
while (1)
{
/* Delay some time, simulate the app is processing other things */
i = 0x10U;
while (i--)
{
__NOP();
}
}
vTaskSuspend(NULL);
}
The idea of this program: when USART0 interrupt occurred, check how many bytes were received, copy all received bytes from USART0 to local array and then send it to another task using FreeRTOS StreamBuffer which is basically copying data to another buffer.
Another task (called appInput) will receive the data and print them out to the console for verification.
Sending 18 bytes from my PC to the USART0 on LPC55S16EVK development board, I am getting the following results:
"appInput got 1 bytes
Received ERROR
appInput got 16 bytes"
First interrupt happens when 1 byte of data received and it is sent to the printing task ("appInput got 1 bytes"). Then another interrupt occurred and this time kUSART_RxError status was indicated, hence print output of "Received ERROR", followed by final 3rd interrupt where 16bytes of data received. In total 17 bytes received instead of 18.
I would appreciate if you could point me out into a right direction in order to resolve this issue and allow consistent USART operation.
Thank you.