This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

ble_app_uart threading

Hi,

Just a quick question about how the ble_app_uart works.

When sending to the UART from "static void nus_data_handler(ble_nus_evt_t * p_evt)" and also receiving from the UART in "void uart_event_handle(app_uart_evt_t * p_event)", are these running on different threads as they receive data from different event triggers?

If not on different threads then can these functions ever be executed at the same time? More importantly, if I have a global static uint8_t buffer[2048] and add/remove data from from both functions will this cause me issues?

Thanks.

  • Hi,

    First off, there is only one processor in the nRF52, so you can be sure that no sets of code will be executed concurrently. Interrupts are queued
    and executed one after another, and which interrupt is handled first depends on their interrupt priorities.

    The nRF52's UART is asynchronous and equipped with a feature we call EasyDMA, meaning that you can receive and
    transmit UART data concurrently. You have an RX buffer and a TX buffer located in RAM, and data can be read and written to these 
    locations without using the CPU. So in your scenario, what will typically happen is that you receive data via BLE
    and when the data is received the Softdevice will trigger an SVC call (also known as a software interrupt) with
    interrupt priority 4. This is when nus_data_handler() is being processed. While this is happening you might receive some UART data. If the UART reception is completed before you have exited the SVC call, and the interrupt priority of the UART is lower than the SVC routine, a UART RX interrupt will be queued and executed after your CPU has exited the SVC routine. If your UART has higher interrupt priority than the SVC, the SVC routine will be put on hold and the UART interrupt processed immediately. 

    Bottom line, if you get data over UART while executing nus_data_handler(), the data will be processed after your CPU has exited nus_data_handler().

    More importantly, if I have a global static uint8_t buffer[2048] and add/remove data from from both functions will this cause me issues?

    Yes. If you have a receive buffer, you should make sure you only read from it when the UART is not controlling it. The buffers can be changed on-the-fly, so the common way to solve your problem is to switch buffers whenever a transmission is complete and it is time to process the data. 

  • If I give both the same priority can I stop the functions from interrupting each other? I would rather let them be queued serially and execute that way

  • If they both have the same priority level then it is "first come, first served". Here is an informative blog post: A Beginner’s Guide on Interrupt Latency - and Interrupt Latency of the Arm Cortex-M processors.

  • Thanks so much for all the information its much appreciated.

Related