Task timing issue when reading data over serial

Hey all,

I’m working with a Seeed Studio XIAO nRF52840 sense. Here’s some brief context. I’m allowing the user to interface with the device over serial by creating tasks that read for certain characters/data and then execute code accordingly when found. For example, a user would send ‘info’ to print some system information or ‘format’ to format the flash. I’m having some timing issues when it comes to reading in these characters/words. Sometimes the functions run and other times they don’t, leading me to believe that when sent, the device might not be on the correct task, and the data is lost partially or in whole.

To further clarify, the device might read in ‘inf’ because the ‘o’ was cut off due to the tasks switching leading to the info printing code not executing.

Does anyone happen to have any ideas on how I can set this up to work properly/reliably?

Thanks in advance!

Do you have the serial input being received in an ISR (like you should) or is some task just spin-waiting on the Data available flag from the serial port (what you shouldn’t do)?

When using an RTOS, most I/O should be done with interrupt driven I/O drivers to allow tasks to run while the I/O is running.

Thank you for your response, it’s very much appreciated. You’re right. I’m sending data over serial and essentially hoping the serial data tasks read the characters/words, rather than handling IO with an ISR. That being said, do you have any examples where an ISR is used so I can see how to implement it into my code?

Thank you!

How to build in interrupt based serial driver will be VERY much dependent on the processor you are using. You will need to look at the processor reference manual for details of how to do that. They may provide a library that allows you to do it and link the ISR into FreeRTOS.

The simplest is to have the ISR that gets automatically called on a character being available put the data into a Queue that you have a task taking the data out of. The Queue will allow the data to be held for a bit of time while other tasks are running.

@cam, you are using an Arduino. From what I understand, the Arduino port treats serial I/O fundamentally different from every other peripheral by tucking UART I/O behind every execution of the loop() function. I would advise to familiarize yourself with those idiosyncracies, possibly inquire at an Arduino forum. In “barebone” FreeRTOS, very different rules apply for UART interaction.