Firmware Buffering & Ring Buffers Questions
Low-level data movement and buffering in constrained systems: circular/ring buffers, producer-consumer queues, lock-free single-producer/single-consumer designs, and handling overflow and wraparound. Covers buffering strategies for streaming data between ISRs, DMA, and application code without dynamic allocation.
In C, implement a compact ring buffer API suitable for an embedded RTOS where producers may run in ISR context and consumers run in task context. The ring buffer stores uint8_t values. Provide the struct definition and implementations for:
- bool rb_push_from_isr(ringbuf_t *rb, uint8_t b);
- bool rb_pop_blocking(ringbuf_t *rb, uint8_t *b, TickType_t timeout);
Constraints: producer runs in ISR and must not call blocking RTOS APIs; consumer may block; avoid dynamic allocation; target single-core Cortex-M; assume 32-bit atomic read/write for indices. Also briefly explain why your approach is safe.
Implement a lockless single-producer (ISR) / single-consumer (main) circular buffer in C for passing variable-length messages. Constraints: single-core 32-bit MCU (32-bit reads/writes are atomic), no mutexes or atomics, minimal RAM overhead, and handle wrap-around. Provide code for enqueue_from_isr and dequeue_in_thread and explain memory ordering concerns.
Write a C implementation of a fixed-size circular byte buffer with power-of-two capacity. Requirements: target ARM Cortex-M, single-producer single-consumer (SPSC) semantics where the producer runs in an ISR and the consumer in the main loop. Operations must be non-blocking and interrupt-safe without disabling interrupts. Provide functions: void rb_init(uint8_t *buf, size_t size), bool rb_push(uint8_t b), bool rb_pop(uint8_t *out). Document any memory-ordering or 'volatile' usage assumptions you make.
Implement an ISR-safe logging mechanism that replaces printf calls in interrupts. Requirements: minimal overhead in ISR, no heap use, store textual or binary events to a fixed-size RAM circular buffer, provide a non-blocking API for ISR to append events, and a consumer in main context to flush to UART/flash. Provide C code for append() and consumer() functions and discuss crash/power-loss considerations.
Design an atomic, zero-copy, high-throughput API for an application that streams sensor samples from an ISR to a processing thread using circular DMA buffers. Detail how you would coordinate buffer ownership, detect overruns, expose completed packet lengths, and allow the processor to process one buffer while DMA continues filling another.
Unlock Full Question Bank
Get access to all 11 Firmware Buffering & Ring Buffers interview questions and detailed answers.
Sign in to ContinueJoin thousands of developers preparing for their dream job.