Continuous execution of a Task

I’m developing an application that intends to do the following:

  • configure one LaunchXL-CC3235S (a TI Launchpad for WiFi based application development) as an Access Point and another one as Station.
  • transmit data between AP and STA (full duplex, bidirectional) on continuous basis.

for these two requirements, I’ve developed attached applications. With the same, I am facing following issue:

  • My socket_task in the program has random execution. Sometimes is sends 577 messages, sometimes 5444 sometimes 389. (I’ve tried three iterations) This shall be continuous. As I’ve written two counters, it should run 50000 x 50000 times without stopping or switching. I’m not able to understand why does the program stops current execution and switches back to main_thread task?

AP_28Feb2023_main_FreeRTOS.c (2.5 KB)
AP_28Feb2023_platform.c (21.1 KB)
STA_28Feb2023_main_FreeRTOS.c (2.5 KB)
STA_28Feb2023_platform.c (16.5 KB)

Regards,
H C Trivedi

Your server client code does not look correct at the first glace. One of the device needs to act as server which listens for the incoming connections. The usual sequence of socket calls for that would be -

  • socket
  • bind
  • listen
  • accept
  • send/recv [After a connection from a clinet is accepted]

Once the server device is up and running, the client can initiate a connection by doing the following socket calls -

  • socket
  • connect
  • send/recv

Though these TCP server client examples do not use simple link APIs, you can still use them as reference -

hello @aggarg ,
Thanks for the prompt response. I referred to the link you provided. It gave me some clarity, however, I’m still struggling resolving my issue and understanding the code that you shared.
While I compared my code with your recommendation, I could find the following (pl. note that I’m writing code for UDP send and receive application):

  1. I created a socket (return value indicated socket created successfully)
  2. asked it to bind to any address (return value indicated socket bound successfully)
  3. Connect to the Address specified.
  4. start sending data. Also listen to the data being received on the same socket (as my STA was also transmitting).

I also referred to listen command. As i could make out, it is for TCP kind of connections (is this understanding correct?).

Also, I tried one more thing. I created a queue to push the data and then pulled the same inside socket task. At this time also, the task execution stopped abruptly. It returned to the main thread while(1) statement. I tried using various hooks like MallocFailedHook, StackOverflowHook, IdleHook etc to debug the issue. But this also did not help. While normal execution was going on, it abruptly returned to the mainThread while(1) statement and stayed there only. Normally task function shall never return, but mine did!!

Please let me know if you need any further details from my end!

BR,
H C Trivedi

If you are writing UDP, please refer to this example - https://github.com/FreeRTOS/FreeRTOS/blob/main/FreeRTOS-Plus/Demo/FreeRTOS_Plus_TCP_Minimal_Windows_Simulator/DemoTasks/SimpleUDPClientAndServer.c

You need to use equivalent of sendto and recvfrom APIs.

Does this mean that you have a program just using FreeRTOS task and queues and NO network functionality? If yes, can you share that? If no, please remove the network code and see if the task sill seems to exit.

Hello @aggarg ,
I’ll surely refer to the example link you shared.
Please find the codes attached herewith.
AP_2Mar2023_platform.c (22.6 KB)
AP_2Mar2023_main_FreeRTOS.c (2.8 KB)

Regards,
H C Trivedi

What should I do with this code? I still see a lot of SimpleLink code. If you saying that your task is unexpectedly returning while just using a queue, can you create a sample without any SimpleLink code and share that?

Many thanks for the inputs @aggarg. I think your suggestions and example code brought much clarity. I learnt from it and tried to bring logic to my code. I still have some queries left.

  • Regarding my queue point: I implemented simple FreeRTOS tasks. One used to push to the queue and another one used to pull from the queue. This ran infinitely without any error or unexpected performance. So the issue was with some of my simple link tasks for socket communication.
  • update 1 from the example code: I could implement UDP send task on one microcontroller and UDP receive task on another microcontroller that ran for more than messages. So that issue is resolved.
  • query 1 From the example code: while server task (receive task) has higher priority as compared to the client task (sender task). There is no vTaskDelay implemented at the end of UDP server task. Then how does task switching take place between server task and client task?
  • query 2 from the example code: in continuation with my first query, as I implemented server receive) task and client (sender) task, sequence of task execution is a bit random. Have a look at the image below. Transmit and Receive messages are completely random. By any chance do you have any idea what may be the probable cause and how do I make it more streamlined in which sequence be like:
    received…
    transmitted…
    received…
    transmitted…
    received…
    transmitted…
    received…
    transmitted…

Thanks,
H C Trivedi

Also, in my text, server task = prvSimpleServerTask from the reference code and client task = prvSimpleClientTask.

Because FreeRTOS_recvfrom is a blocking call - https://github.com/FreeRTOS/FreeRTOS/blob/main/FreeRTOS-Plus/Demo/FreeRTOS_Plus_TCP_Minimal_Windows_Simulator/DemoTasks/SimpleUDPClientAndServer.c#L192

I do not think it will be random - you need to realize that the higher priority task will run as soon as it is ready to run. In your case, if server is running at high priority, it will start running as soon as the client sends a message - which means that if the print statement is after the send call, it will execute only after the server tasks blocks again in FreeRTOS_recvfrom.

Thanks.

Okay! Understood. I checked with Simplelink Libraries. sl_recv (equivalent to FreeRTOS_recvfrom) also works in blocking mode. However the issue is as follows:

  • Once AP or STA starts transmitting, the other counterpart starts receiving.
  • if enough amount of delay is not introduced after each transmission, then it ends up in a scenario wherein one will transmit continuously and another one will receive continuously. Since the rate of data exchange is so high, receiving task is always in running stage. It will never enter into blocked state and hence, another task will never execute.
  • As a workaround, i introduced delay after each transmission. This resulted into continuous transmission and reception. However, since the delay is still unoptimised, there are some instances, where transmission from one device takes place in chunk of multiple messages.

Do you have any idea as to how to regulate this? Is me introducing delay intentionally to give it some time for transmission is simply okay or anything else can be done?

Thanks
H C Trivedi

You can try to use other synchronization primitives like semaphore to synchronize tasks such that they execute one by one. The question is why would you want to do that. In a real application where server and client run on different machines, you will not able to control order.