LwRB is a generic, platform independent FIFO (First In, First Out) ring buffer written in C11, optimized for embedded systems. It uses no dynamic memory, is lock-free for a single writer and a single reader (for example an interrupt and the main loop), and gives direct, zero-copy access to its memory, which makes it a natural fit for UART, USB and DMA data streams.
📖 Documentation · Changelog · Releases
- Written in C (C11), compatible with
stdint.hdata types - Platform independent, no architecture specific code
- No dynamic memory allocation, data is stored in a user-provided static array
- Uses optimized memory copy instead of loops to read/write data from/to memory
- Thread and interrupt safe when used as a pipe with a single write and a single read context
- Uses C11 atomic operations by default to keep reads and writes race-free, even on architectures whose native word size is smaller than
size_t(AVR for instance); can be disabled withLWRB_DISABLE_ATOMIC - Suitable for DMA transfers from and to memory with zero-copy overhead between buffer and application memory
- Supports data peek, skip for read and advance for write
- Byte-sequence search within buffered data
- Optional all-or-nothing read and write operations to avoid partial transfers
- Optional per-buffer custom user argument storage
- Event notifications on read, write and reset
- Optimized single byte read/write API
- User friendly MIT license
#include <stdio.h>
#include "lwrb/lwrb.h"
/* Buffer instance and its raw memory */
lwrb_t buff;
uint8_t buff_data[8];
int
main(void) {
uint8_t data[8];
size_t len;
/* Initialize buffer */
lwrb_init(&buff, buff_data, sizeof(buff_data));
/* Write 4 bytes of data */
lwrb_write(&buff, "0123", 4);
printf("Bytes in buffer: %d\r\n", (int)lwrb_get_full(&buff)); /* Prints "4" */
/* Read data back, returns number of bytes read */
len = lwrb_read(&buff, data, sizeof(data));
printf("Number of bytes read: %d\r\n", (int)len); /* Prints "4" */
return 0;
}Note: A buffer of size
Nholds at mostN - 1bytes. One byte is always kept free to tell a full buffer apart from an empty one.
Instead of copying data out of the buffer, hand the buffer memory directly to the DMA and release it once the transfer completes:
volatile size_t len;
void
send_data(void) {
if (len > 0) {
return; /* DMA transfer already in progress */
}
/* Get the longest contiguous block available for reading */
len = lwrb_get_linear_block_read_length(&buff);
if (len > 0) {
start_dma_transfer(lwrb_get_linear_block_read_address(&buff), len);
}
}
/* Called when the DMA transfer finishes */
void
DMA_Interrupt_handler(void) {
lwrb_skip(&buff, len); /* Mark transmitted data as read */
len = 0;
send_data(); /* Start the next transfer, if any */
}More examples are in docs/examples_src and in the documentation.
The library is lock-free for exactly one write context and one read context (a context is one thread or one interrupt).
Write functions (lwrb_write, lwrb_advance, ...) only modify the write pointer, and read functions (lwrb_read, lwrb_skip, ...) only modify the read pointer,
so a producer and a consumer can run in parallel without locks.
Multiple writers or multiple readers, as well as lwrb_init and lwrb_reset, require application-level locking.
See Thread safety in the documentation for details.
add_subdirectory(path/to/lwrb/lwrb)
target_link_libraries(${PROJECT_NAME} PRIVATE lwrb)Link lwrb_ex instead to also get the extended functions (lwrb_overwrite, lwrb_move).
Optional compiler options and definitions can be set with LWRB_COMPILE_OPTIONS and LWRB_COMPILE_DEFINITIONS, see lwrb/library.cmake.
- Copy the
lwrbfolder to your project - Add
lwrb/src/includeto the include path - Add the source files from
lwrb/src/lwrb/to the build
LwRB is available in the PlatformIO Registry. Add it to your platformio.ini:
lib_deps =
majerle/LwRBFresh contributions are always welcome. Simple instructions to proceed:
- Fork Github repository
- Follow C style & coding rules and use
clang-formatto format the code - Create a pull request to
developbranch with new features or bug fixes
Alternatively you may:
- Report a bug
- Ask for a feature request
LwRB is released under the MIT license.