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...


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.

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.