2023-11-08 03:01 AM
Hello everyone.
This is more of a general RTOS question regarding the approach of task management. I've got everything pretty much working but im certain there is a better solution than mine. I'm using FreeRTOS with the CMSIS v2 wrapper.
Background:
The thing i'm making acts as a data collector, communicating with up to four devices at a time. Reading info from these devices and managing it. The devices can be connected/disconnected during runtime but there can never be more than four connected at the same time.
Tasks:
System task: Handles hardware and the tasks that directly communicate with the devices.
Search for device task: Continuously searches for a device on the communication bus. Flags if a new one is found.
Fetch data from device task: Generic task that gets device specific info from the backend and continuously polls the appropriate device for data. Up to four "copies" of this task are used for the device communication (one task per device).
Approach:
Create four instances of the Fetch data from device task at compile time. The system task checks the NVM (backend) if any device was connected, if not: Suspend relevant Fetch data from device task and resume the Search for device task. Fetch data from device is assigned a device from the system task if a device is/was connected, then it retrieves relevant information from the backend, forms the packets and continuously fetches data from the device without any intervention from other tasks.
Search for device task is suspended by the system task if all four devices are connected and later resumed if less than four devices are connected.
Issue:
I feel that this is quite a "hybrid" RTOS solution and sort of goes against the idea independent task & OS's..
Some of the logic like resume/suspend could be moved into tasks like Fetch data from device or Search for device task or i could use a "os wait forever flag" in Fetch data from device and "flag" when a sensor is detected.
What i think would be a better solution is "create" the Fetch data from device tasks when a device is connected and "terminate" them when the device is removed/permanently disconnected. Then the next question is, what would be responsible for creating the task, would it be the system task or something else. What would be the best way to keep track of the "dynamic" threads, can you create "thread pointers" or similar?
There is a lot of nuance to this and it's been many years since i've done anything with OS's.
How would you guys with more RTOS knowledge approach this?
Any help is appreciated :)