UART Interrupt Service Routine with the XIAO nRF52840 Sense board

Hey all,

For some context, I’m allowing the user to interface with the device over serial. For example, the user may send a (firmware specific) recognized command like ‘print_data’ and the device would respond by sending some data over serial to the user. However, my issue stems from the serial communication tasks not running at the time data is actual transmitted over serial to the device.

My hope is that I can run an ISR to detect when data is sent over serial to then raise a flag and possibly intercept the serial data. I can then have another task set up to check if the flag is true and to have the data shared. This can then process the user’s requests in a more reliable fashion.

That being said, I’m looking for some examples/assistance in creating an interrupt service routine for when data is read over serial in the nRF52840 Sense board. I’ve read elsewhere that there is a function name specifically for UART Peripheral ISR with the nRF52480 processor, but haven’t had any luck in finding it.

Below is some pseudocode of what I’m trying to accomplish:

// Global vars
bool flag = false;
String serialBuffer;


void setup() {
  xTaskCreate(TaskPrintData);
  xTaskCreate(TaskOther);
  xTaskCreate(TaskAnother);
  xTaskCreate(TaskAndOneMore);
  vTaskStartScheduler();
}

void loop() {
// Nothing here in FreeRTOS!
}

// Example implementation
void UART_ISR_Handler(void)  {
  flag = true;
  serialBuffer = Serial.readString(); 
}

// Example data printing task
void TaskPrintData(void *PvParameters)  {
  (void)PvParameters;
  for (;;) {
    if (flag == true) {
      if (serialBuffer == "print_data") {
        Serial.println("flag is true");
      }
    }
    flag = false;
    serialBuffer = "";
  }
}

void TaskOther(void *PvParameters)  {
// code 
}

void TaskAnother(void *PvParameters)  {
// code 
}

void TaskAndOneMore(void *PvParameters)  {
// code 
}

Any advice or assistance is greatly appreciated. Thank you in advance!

If this is an “Arduino” based system, like the Serial.readString(), you will need to look at the documentation they provided for the system.

This is one of the problems with working with a “Hobby” level system. If they provide a FreeRTOS interface, they should provide an interrupt based serial interface to, or it really is just a toy implementation.

@cam
There are UART examples in nrf52840 SDK. For example, libuarte provides asynchronous packets sending and receiving feature with DMA. The libuarte example can be found in nrf52840 SDK, <InstallFolder>\examples\peripheral\libuarte.

Please take a look to see if these information can help you with your design.

Are you trying to have more than one requests in-flight? Can you not simplify your application by having a strict request response semantics?