What is fifofast? fifofast is a FIFO (First In First Out) buffer library specifically designed for 8-bit and even lower-end MCUs (AVR8, SAM series). It is written in C and occupies very little SRAM, with only a few bytes of management overhead, yet it can store various data types (integers, structures, and even custom types) in a circular buffer. In simple terms, it allows you to read and write data in ISRs and the main loop without blocking the CPU, while keeping the code clean.
What problems does it solve?
| Problem | fifofast’s Solution |
| High CPU Usage | Uses a circular buffer of size 2ⁿ, with write and read pointers quickly wrapping around using bitwise operations, eliminating branch checks. |
| Insufficient SRAM | All data is stored in a statically allocated array defined by the user, with the management structure occupying only 3 Bytes. |
| Type Limitations | Achieves “generic” functionality through macros, allowing any <span>typedef</span> type to be directly declared as FIFO. |
| ISR and Main Loop Conflicts | All operations are <span>inline</span>, resulting in almost no function call overhead in interrupt contexts. |
| Difficult Debugging | Compatible with the built-in debugger of Atmel Studio 7, allowing direct visibility of FIFO contents in the variable window. |
Installation & Getting Started
- 1. Prepare Tools: Download and install Atmel Studio 7.0 (free). If you are using IDEs like VS Code or PlatformIO, you can configure it yourself, but the author has only tested it on Atmel Studio.
- 2. Obtain the Source Code:
<span>git clone https://github.com/nqtronix/fifofast.git</span>or download the ZIP directly. - 3. Include in Your Project: Add
<span>fifofast.h</span>and<span>fifofast.c</span>to your project. - 4. Declare a FIFO (demonstrating with 16
<span>int8_t</span>):
_fff_declare(int8_t, fifo_int8, 16); // Declare
_fff_init(fifo_int8); // Initialize (call only once in one .c file)
- 5. Read/Write Example:
_fff_write(fifo_int8, -42); // Write
int8_t v = _fff_read(fifo_int8); // Read, v == -42
With just a few lines of code, compile and upload to the MCU, and you can safely push sampled values into the FIFO in the interrupt, and pop them out in the main loop.
Core Features Overview
- • Generic Data:
<span>_fff_declare(uint16_t, fifo_u16, 32)</span>, any type can be used directly. - • Static Memory: No need for
<span>malloc</span>, all memory is determined at compile time. - • Inline Macros: Almost no function call overhead in ISRs.
- • Minimal RAM Overhead: Only 3 Bytes for management information.
- • Debug-Friendly: Atmel Studio variable watch window directly displays FIFO contents.
- • Pointer-Compatible:
<span>_fff_declare_p</span>allows you to pass FIFO as a pointer, suitable for complex structures. - • FIFO Array:
<span>_fff_declare_a</span>supports creating multiple buffers of the same depth at once.
Pros and Cons at a Glance
| Advantages | Disadvantages |
| Extremely low CPU cycle consumption (especially in ISRs) | Can only use buffers of size 2ⁿ, actual capacity will round up |
| Fully static allocation, suitable for resource-constrained MCUs | Each macro expansion generates code, Flash usage is slightly higher than ordinary function implementations |
| Supports any data type (including structures) | Using pointer-based FIFO requires manual type casting, which can be cumbersome |
| Rich debugging/viewing methods | Documentation is mainly in source code comments, external manuals are relatively brief |
Usage Tips
- • Select Capacity as 2ⁿ: For example, if you want 20 data items, set it to 32 directly to avoid unnecessary rounding code.
- • Synchronize ISR and Main Loop: If the same FIFO is accessed by both ISR and normal code, remember to add
<span>ATOMIC_BLOCK(ATOMIC_RESTORESTATE){ … }</span>in the normal code segment to prevent race conditions. - • _fff_rebase(): When you put a character stream (like commands received via serial) into the FIFO, and need to move the data to contiguous memory for functions like
<span>strcpy</span>,<span>printf</span>, use this macro. - • Debugging Tips: In Atmel Studio’s “Watch” window, directly write
<span>_fff_peek(fifo_int8, 0)</span>to see the real-time value of the first element.
Conclusion fifofast is not a fancy library, but it finely slices the “old cucumber” of FIFO. It balances CPU cycles, SRAM usage, and code readability perfectly, making it especially suitable for resource-constrained 8-bit MCU projects. If you are working on serial protocols, ADC buffering, or any embedded project that requires real-time data handling, this library will ensure your ISR won’t get stuck, and your code won’t be a mess of manual circular buffers.
Project Address: https://github.com/nqtronix/fifofast