This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

LP-EM-CC2745R10-Q1: BLE Application Design with TI CC2745R10-Q1 chipset.

Part Number: LP-EM-CC2745R10-Q1
Other Parts Discussed in Thread: CC2745R10-Q1

Tool/software:

Hi,

I am designing a BLE Application with TI CC2745R10-Q1 chipset. 

Below are my queries :

1. I want to call TI APIs from different tasks other than the BLEApp_util task. Is it possible to call

Example: BLEAppUtil_advStart() API I want to call from the UART task

2. If it's not possible to call TI APIs from different tasks then I need to enqueue to the same queue where the TI callbacks are getting enquired 

API : BLEAppUtil_enqueueMsg();--> BLEAppUtil_theardEntity.queueHandle.

To call BLEAppUtil from different tasks BLEAppUtil_invokeFunction() API is used which interns add to the same queue with different EVT -BLEAPPUTIL_EVT_CALL_IN_BLEAPPUTIL_CONTEXT.

If I do this design I will end up in race condition where when we have multiple data to send to TI and at the same time if we receive a call back from TI. 

Suggest to me how to handle this condition.

PFA for the issue that I mentioned above.

  • Hello,

    If you are in a different context and want to call BLEAppUtil APIs, then you must invoke the function call using BLEAppUtil_invokeFunction.

    You can find the header file in <project root>/common/BLEAppUtil/inc/bleapputil_api.h. 

    I suggest using this API, since that will be easier then trying to enqueue messages in the BLE App queue (however, I'd like to note that this is still possible).

    Best,

    Nima Behmanesh

  • Hi,

    Thank you for the quick response. Please find below snippet for the race condition . UART data and TI call backs uses the same queue to send data to util task they are chances of race condition . And I cannot take mutex/semphore lock on Queue because the same queue is used by callback functions and I don't have control over stack callbacks . UART data I can control since its our application

     

  • Hi Nima,

    Could you please help me with how to handle synchronization with above-mentioned mechanism .

    And we will handling 5 connections at at time .And there is one more drawback

    Drawback : 1. Assume on one BLE link we receive data because of which callback is triggered and we have to enque data

                       2.Same time I got some data from UART where I have call some API to TI stack example . Disconnect for different link 

                       3.If I take lock the queue and try to add data then there are chances of missing stack call back data

    Because we are blocked from starting implementation.

    Regards,

    Ramya Gumaste

  • Hello,

    I’m a little confused on the ask. UART and the stack operations should not interfere with one another. 

    Drawback : 1. Assume on one BLE link we receive data because of which callback is triggered and we have to enque data

    Enqueue UART data? What data is being enqueued? The stack has the highest priority.

    2.Same time I got some data from UART where I have call some API to TI stack example . Disconnect for different link 

    To make sure I’m understanding this correctly, a message is sent over UART to disconnect a BLE connection? This is possible, and it’s done in the example. The display uses UART and you can initiate a disconnection through the display. 

    3.If I take lock the queue and try to add data then there are chances of missing stack call back data

    Why would you lock the queue? The stack will always have priority since its thread has the highest priority, there’s no way around this really. If you were to lower the priority you will miss messages, at the cost of getting that UART data.

    I’m not sure if I’m understanding the question correctly, but the BLE stack has the highest priority and it’s not recommended to change this or have tasks at the same priority, there are many timing requirements in the protocol. 

    Additionally, the BLE thread is created using FreeRTOS, so you could create a separate thread entirely. Why not create a new thread that handles the UART tasks?

    Best,

    Nima Behmanesh

  • Thanks Nima for the Update .

    Our Requirement for the Application : 

    1. BLE App receives some requests from the HMI through UART (like Connect,Disconnect) , based on user input .

    Based on the received request BLE App call TI API. Which has to go through the BLE util task by enquing in BLEAppUtil_theardEntity.queueHandle , through BLEUtil_invokeFuntion(). 

    2.The Received call backs from TI has to be sent to HMI. In the given example the received data also getting enqued in the same queue BLEAppUtil_theardEntity.queueHandle which will be processed in BLE Util Task .

    Query : My query is how do I handle two simultaneous data's. 


    Regards,

    Ramya 

  • Hi Nima,

    please help me with the query.

    Regards,

    Ramya Gumaste

  • Ramya,

    I think we need to take a step back here. 

    1. You can either have a separate UART thread that subscribes to ICall messages. 

    2. You can create a custom event within the BLEAppUtil queue, such that when a callback occurs, you store the UART data in whatever desired data structure, and the post event that will handle that data. 

    Example: User sends a disconnect message, the thread receives this message that triggers a callback. This callback will enqueue a disconnect event, where the disconnect API is called.

    These are single core devices, so there is no such thing as sending/receiving at the same time. So you will need to figure out a way to synchronize the threads that you create.

    Best,

    Nima Behmanesh

  • Hi Nima,

    Below is my understanding, please confirm if it is wrong

    1. TI APIs must be called with BLE Util task

    2. The TI callback data shall be enqueued on BLEAppUtil_enqueueMsg() based on standard events defined by TI or custom events 

    3. In case custom events that has to be added to BLEAppUtil_enqueueMsg() failed due to the resource being busy by TI callbacks then proper synchronization methods should be used 

    PFA attached image notes. 

    If BLEAppUtil_queue() is busy then in the TI callbacks instead of enqueuing the data BLEAppUtil_enqueueMsg() wait on the resource to become free.

    Example:

    void BLEAppUtil_advCB(uint32_t event, GapAdv_data_t *pBuf, uint32_t *arg)

    {

    if (BLEAppUtil_enqueueMsg(BLEAPPUTIL_EVT_ADV_CB_EVENT, pData) != SUCCESS)
    {

    //Free data

    }else   //Else case where queue is busy by another task

    {

    Approach 1:Wait until queue resources available freely then enqueue data

    Approach 2 : Enqueue data locally and free callback pData

    }

    Suggest which approach is better. 

    Please correct me if my understanding is wrong 

  • Hello,

    1. TI APIs must be called with BLE Util task

    Sort of. TI BLE APIs must be called within the context of the BLE Util task. However, you can get around this by using the BLEAppUtil_invokeFunction API. Calling BLEAppUtil_invokeFunction will switch contexts for you and call the API, and you can call BLEAppUtil_invokeFunction outside of the BLE Util task.

    2. The TI callback data shall be enqueued on BLEAppUtil_enqueueMsg() based on standard events defined by TI or custom events 

    Which callback data? If it's UART, then no. UART, SPI, other drivers, do not need to be called within the BLE App Util task, only the BLE APIs need to be called within the Util task. That's why it's recommended to create your own task. You can always take the data you get within the BLE App util task, move it somewhere else (like a buffer) that can be handled by another task.

    PFA attached image notes. 

    If BLEAppUtil_queue() is busy then in the TI callbacks instead of enqueuing the data BLEAppUtil_enqueueMsg() wait on the resource to become free.

    Example:

    I have never seen this issue occur in practice. Most of the time, developers will create their own queues and leave the BLE app util queue alone. Otherwise, you will need to use synchronization methods.

    Hope that helps.

    Best,

    Nima Behmanesh


  • Thanks Nima,It helps.

    one more Clarification: In the Picture mentioned above. TI Stack callbacks mean (TI events like (example BLEAppUtil_pairStateCB()) which are enqueued in BLEAppUtil_enqueueMsg(). 

    when I call the BLEAppUtil_invokeFunction () function from other tasks which internally calls the same queue. 

    if (BLEAppUtil_enqueueMsg(BLEAPPUTIL_EVT_CALL_IN_BLEAPPUTIL_CONTEXT, pDataMsg) != SUCCESS)

    How do I handle synchronization for this queue? 

  • Hi Ramya,

    I don't believe you need to. BLEAppUtil_enqueueMsg eventually calls mq_send, which depending on the context (within an ISR or not) the proper FreeRTOS queue function is used. I recommend looking at the implementation of mq_send to see the error codes. For instance, if the queue is full, then EAGAIN is returned, and it's up to the developer on how to handle this. Additionally, mq_send, has a timeout in the case that the queue is busy.

    Though, for the most part, you shouldn't have to worry about this. Have you run any tests that indicate a race condition would occur? I'm skeptical since we have applications that use up to 32 simultaneous connections, and data is rarely lost.

    Best,

    Nima Behmanesh