fifofast: A “Zero-Burden” FIFO Circular Buffer Implementation Tailored for Entry-Level MCUs

Imagine your MCU is frantically sending and receiving serial data, ADC results, and various real-time tasks, but is being bogged down by an inefficient FIFO buffer causing stuttering and interruptions… Don’t panic, today I introduce you to a powerful open-source tool – fifofast, which specializes in eliminating stuttering and can make your data flow smoothly!

What is fifofast? In simple terms, fifofast is a “zero-burden” FIFO circular buffer implementation specifically designed for entry-level MCUs (like AVR8). It is written in C, compiled with GCC 5.4.0, has minimal code, high efficiency, and its functionality is not inferior to those bulky libraries. Its core goal is singular: to manage data in a “first-in, first-out” manner using the least CPU cycles and SRAM usage.

What pain points does it solve?– Are you still using <span>if (idx >= N) idx = 0;</span> for index handling? Too slow!– Do you want to store data of any type and length? Existing FIFO implementations are either rigid or memory-hungry…– Are you worried about performance when writing buffers in interrupt service routines (ISRs)?– Want to receive strings via serial but end up with “broken packets” and have to copy data and fiddle with GCC attributes for ages?

fifofast takes care of all that for you, as detailed below:

Main Features

Feature Description
Generic Data Types Supports any <span>typedef</span>, structures, and even unions with alignment attributes
Static Memory Allocation No dynamic allocation at all, with only 3 bytes of management overhead
Inline Support Read and write in ISRs without function jumps, achieving high speed
Circular Buffer Optimization Capacity must be 2ⁿ, utilizing bitwise AND (<span>&</span>) for fast wrapping, eliminating branch checks
Debug-Friendly Compatible with Atmel Studio 7 debuggers, with detailed comments in the source code
_fff_peek / _fff_rebase Allows arbitrary read/write and relocation within the buffer, enabling continuous processing of serial strings

Limitations

Limitation Description
Capacity must be 2ⁿ If you fill 20 elements, it will automatically round up to 32
Pointer FIFO Elements Maximum 255 bytes, exceeding that requires DIY
Program Space Macro expansion or inline will increase Flash usage, slightly larger than a typical function-based FIFO

Code Example: One Line to Declare Buffer Without further ado, here’s a minimal compilable example to get you started in under a minute:

#include "fifofast.h"

// Declare and initialize a FIFO of depth 16, storing int8_t in a .c file
_fff_declare(int8_t, fifo_int8,16);
_fff_init(fifo_int8);

int main(void) {
    volatile int8_t tmp;
    _fff_write(fifo_int8,-42);// Write
    tmp = _fff_read(fifo_int8);// Read back -42
    while(1);
}

Isn’t it clean?<span>_fff_declare(type, id, depth)</span>: Declare a structure variable<span>_fff_init(id)</span>: One-time initialization<span>_fff_write(id, value)</span> / <span>_fff_read(id)</span>: Read and write like an array– Advanced usage:<span>_fff_peek(id, idx)</span> allows you to “peek” at internal elements,<span>_fff_rebase(id)</span> performs a memory rearrangement for serial strings

Summary of Pros and ConsPros

  1. 1. Microsecond-level performance – bitwise operations replace branches, allowing for rapid read/write in ISRs
  2. 2. Extremely low memory usage – management requires only three to four bytes
  3. 3. Type-agnostic – can store not only basic types but also structures and unions
  4. 4. Debug-friendly – Atmel Studio has comprehensive comments, and you can check the stack if bugs occur

Cons

  1. 1. Capacity can only be 2ⁿ, you can’t just fill it with 20 and expect it to work
  2. 2. Slightly larger program space – more use of macro expansion and inline will increase Flash usage a bit
  3. 3. Accessing from both ISR and normal code requires you to wrap it in an atomic section, be cautious of non-atomic interruptions

Conclusion Overall, fifofast is a tool that you can just use: it occupies little memory, executes quickly, and supports various functionalities. For MCU engineers working on serial drivers, ADC buffering, and real-time multitasking, this tool can save you from all the brain cells spent on optimizing circular queues, allowing you to focus on business logic. Want to give it a try? You’ll surely love it.

Project Address: https://github.com/nqtronix/fifofast

Leave a Comment