Best aproach to share/switch a UART?

Hi there, right now I'm working with an application that allows me to collect data via UART from several sensors (two), and I also I have a SIM module that allows me to communicate via UART.
But I'm facing the problem that I'm just using a single UART to achieve all of these tasks.
But in some cases, I can't use the UART when I need it at a specific moment because it has been acquired for another task.
Then, what could be the best approach to sharing or switching a single UART beyond more a timeout?
Whatever advice you leave, it'll be received.
Thanks.
Parents
  • Let me write down my thoughts. A simple solution is to introduce a mutex for UART access. This allows only one task to control UART at any given time, ensuring smoother task coordination without blocking or interrupting each other’s communication.

     

    Start by setting up a FreeRTOS mutex around the UART. Whenever a task needs to access UART, it locks the mutex, completes its operation, then releases it. This ensures only one task is using UART at any time, which keeps things from clashing.

    If one task has tighter timing needs, consider adjusting its priority or giving it a shorter wait on the mutex. This way, urgent UART access isn’t held up unnecessarily.

    Adding a timeout to the mutex acquisition is also a good idea. If a task can’t get access to UART within the set time, you can log the issue or handle it some other way to avoid blocking everything else.

    If there are frequent UART requests from multiple tasks, you might also think about using a queue. With a queue, tasks can queue up their UART requests, and a single handler can take these one by one. This setup simplifies retries and error handling, making the overall code a bit easier to manage.

Reply
  • Let me write down my thoughts. A simple solution is to introduce a mutex for UART access. This allows only one task to control UART at any given time, ensuring smoother task coordination without blocking or interrupting each other’s communication.

     

    Start by setting up a FreeRTOS mutex around the UART. Whenever a task needs to access UART, it locks the mutex, completes its operation, then releases it. This ensures only one task is using UART at any time, which keeps things from clashing.

    If one task has tighter timing needs, consider adjusting its priority or giving it a shorter wait on the mutex. This way, urgent UART access isn’t held up unnecessarily.

    Adding a timeout to the mutex acquisition is also a good idea. If a task can’t get access to UART within the set time, you can log the issue or handle it some other way to avoid blocking everything else.

    If there are frequent UART requests from multiple tasks, you might also think about using a queue. With a queue, tasks can queue up their UART requests, and a single handler can take these one by one. This setup simplifies retries and error handling, making the overall code a bit easier to manage.

Children
No Data
Related