Ring buffer impl
Implemented ring buffer with inline push and pop functions Updated copyrights on main.c and ring_buffer files
This commit is contained in:
28
STM32/CM7/Core/Src/ring_buffer.c
Normal file
28
STM32/CM7/Core/Src/ring_buffer.c
Normal file
@@ -0,0 +1,28 @@
|
||||
// Copyright (C) 2026 Hector van der Aa <hector@h3cx.dev>
|
||||
// Copyright (C) 2026 Association Exergie <association.exergie@gmail.com>
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#include <assert.h>
|
||||
#include <ring_buffer.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
inline void ringBufferPush(ring_buffer_t *rb, uint32_t value) {
|
||||
rb->buffer[rb->w_head] = value;
|
||||
rb->w_head++;
|
||||
if (rb->w_head == rb->r_head) {
|
||||
// if write head catches up to read head, crash program as consumer task
|
||||
// should keep up with write head
|
||||
assert(false);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
inline int ringBufferPop(ring_buffer_t *rb, uint32_t *output) {
|
||||
if (rb->w_head == rb->r_head) {
|
||||
// if read head is at write head, buffer is empty so nothing to pop
|
||||
return 1;
|
||||
}
|
||||
*output = rb->buffer[rb->r_head];
|
||||
rb->r_head++;
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user