3

I am looking into using a stream buffer in FreeRTOS to transfer CAN frames from multiple tasks to an ISR, which puts them into a CAN transmit buffer as soon as it's ready. The manual here explains that a stream buffer should only be used by one task/isr and read by one task/isr, and if not, then a critical section is required.

Can a mutex be used in place of a critical section for this scenario? Would it make more sense to use?

9a3eedi
  • 696
  • 2
  • 7
  • 18

1 Answers1

1

First, if you are sending short discrete frames, you may want to consider a message buffer instead of a stream buffer.

Yes you could use a mutex.

If sending from multiple tasks the main thing to consider is what happens when the stream buffer becomes full. If you were using a different FreeRTOS object (other than a message buffer, message buffers being built on stream buffers) then multiple tasks attempting to write to the same instance of an object that was full would all block on their attempt to write to the object, and be automatically unblocked when space in the object became available - the highest priority waiting task would be the first to be unblocked no matter the order in which the tasks entered the blocked state. However, with stream/message buffers you can only have one task blocked attempting to write to a full buffer - and if the buffer was protected by a muted - all other tasks would instead block on the mutex. That could mean that a low priority task was blocked on the stream/message buffer while a higher priority task was blocked on the mutex - a kind of priority inversion.

Richard
  • 3,081
  • 11
  • 9
  • I have considered a message buffer, but the manual says that for fast communication between a task and an ISR, a stream buffer would make more sense. Is there a particular reason why you suggested using a message buffer over a stream buffer? The intention is that the stream buffer never becomes full in normal operation and there is no blocking (wait time of 0). If it did get full, then there is something wrong with the design. – 9a3eedi Dec 24 '19 at 04:58
  • 1
    @9a3eedi, I doubt it recommends stream buffer, as message buffer and stream buffer share their implementation. – Andriy Makukha Feb 11 '21 at 18:15