Static deque for CANFD ISR buffer

Regarding the S32K driver layer for libuavcan, the current implementation uses an almost naive use of the deque STL for limiting the size of the RX queue as shown briefly next:

if (RX_ISRframeCount <= Frame_Capactiy )

/* Insert the frame into the queue */ 

And in the read function it will be inverted as shown:

/* Check if the ISR buffer isn't empty */
/* Get the front element of the queue buffer */
out_frames[1] = FS32K_InterfaceManager::frame_ISRbuffer.front();

/* Pop the front element of the queue buffer */

/* Decrease frame count */

¿How can the deque STL be used while keeping a static deterministic ISR buffer?

Thank you in advance.

Hi Abraham. I am no @scottdixon, but if I were impersonating him I would have probably said:

std::deque<CAN::Frame, libuavcan::platform::memory::PoolAllocator<...>>

Replace the ellipsis with the appropriate parameters as needed. The second template parameter libuavcan::platform::memory::PoolAllocator comes from:

I assume here that the pool allocator is interrupt-safe. If you are uncomfortable with such assumptions, write your own queue instead, or just re-use this one:

(implementation:; you will need to ensure synchronized access).

I don’t think you need RX_ISRframeCount; use std::deque<>::size() instead or whatever queue class you’re using. Redundant state is bad.

I strongly recommend following a consistent naming style as described in the Zubax C++ Conventions.

The block allocator is ISR-safe (it uses an optimistic/lockless implementation to find free blocks) as long as you are not compiling with -fno-threadsafe-statics (which we are in our current example so we should change that).

In short, Pavel’s channeling of myself is correct.

Thank you, the PoolAllocator fits as a plug & play custom allocator for the STL deque, the line would be as follows

/* Intermediate RX buffer for ISR reception with static memory pool */
static std::deque<FrameType, platform::PoolAllocator<S32K_FRAME_CAPACITY, sizeof(FrameType)> > frame_ISRbuffer;

This constant it’s a private member of S32K_InterfaceManager

/* Frame capacity for the intermediate ISR buffer */
constexpr static std::size_t S32K_FRAME_CAPACITY  = 500;

And substituted the CAN::Frame with the “using” directive from the class template parameter at interfaces.hpp which solves to CAN::Frame< CAN::TypeFD::MaxFrameSizeBytes>

    * The media-specific frame type exchanged across this interface.
    using FrameType = FrameT;

The changes can be seen in