I am developing a CAN-BUS reader (logger) with an ESP32 microcontroller.

This sample uses RTOS and several task to send and receive CAN-BUS messages. https://github.com/espressif/esp-idf/tree/master/examples/peripherals/can/can_self_test

In my application I mostly only want to receive CAN-BUS messages. There will be possibly hundreds of messages to receive within a second. And I want to send maybe every 500ms one single message and this is not time critical.

I wonder if I should follow the example above and have two different tasks for sending and receiving or if I should use just one task and send in that task from time to time that single message. I plan to do that by comparing the current milliseconds with a value which is saved when the last message was sent. If that is more than 500ms ago then send a new message.

I am used to programming for Windows and I have very little experience with the ESP32. The above is my question for my current problem but I think this is a general question when it makes sense to separate tasks and when not.

I searched for an existing answer and I guess there is one but I didn't find it...


I do not know ESP32 but I assume it will buffer some messages for you to collect in your own time. If that buffer overruns because you are not collecting in time you will get an error status code when reading the next message so you will know you missed some messages.

If this is indeed how it works, this would allow you to briefly step out and send a message. However, if you have threads available to you and the reading and writing is independent of each other it may still be more convenient and cleaner to use separate threads. Your logic will be easier.

I would first check if this is indeed the way it works for that controller. How many messages will it save for you? Then you can calculate from the bitrate (coursely) how much time you have for sending. It may not be critical at all but it is good to know.

| improve this answer | |

There is no right answer to this question; it depends on your requirements.

If using a single task achieves the required functionality, then that is one solution.

Conversely, if using multiple tasks also achieves the required functionality, and the benefits (e.g. maybe it runs faster) outweigh any costs (e.g. it requires more code space and RAM), then that is another solution.

Note that in FreeRTOS, task notifications can be used to allow a single task to be triggered by multiple simultaneous events while allowing the task to distinguish between them, something that is not possible using a semaphore. That provides one solution for combining multiple tasks into one.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.