From 6035e1a2c26d239ef7138454e0dac4fe0bd361ca Mon Sep 17 00:00:00 2001 From: "jacob.eva" Date: Wed, 4 Sep 2024 17:35:07 +0100 Subject: [PATCH] Move FIFOBuffer to src dir --- Utilities.h | 166 +----------------------------------------- src/misc/FIFOBuffer.c | 95 ++++++++++++++++++++++++ src/misc/FIFOBuffer.h | 59 +++++++++++++++ 3 files changed, 155 insertions(+), 165 deletions(-) create mode 100644 src/misc/FIFOBuffer.c create mode 100644 src/misc/FIFOBuffer.h diff --git a/Utilities.h b/Utilities.h index 95def2f..1afe6ce 100644 --- a/Utilities.h +++ b/Utilities.h @@ -1516,168 +1516,4 @@ void unlock_rom() { eeprom_erase(); } -typedef struct FIFOBuffer -{ - unsigned char *begin; - unsigned char *end; - unsigned char * volatile head; - unsigned char * volatile tail; -} FIFOBuffer; - -inline bool fifo_isempty(const FIFOBuffer *f) { - return f->head == f->tail; -} - -inline bool fifo_isfull(const FIFOBuffer *f) { - return ((f->head == f->begin) && (f->tail == f->end)) || (f->tail == f->head - 1); -} - -inline void fifo_push(FIFOBuffer *f, unsigned char c) { - *(f->tail) = c; - - if (f->tail == f->end) { - f->tail = f->begin; - } else { - f->tail++; - } -} - -inline unsigned char fifo_pop(FIFOBuffer *f) { - if(f->head == f->end) { - f->head = f->begin; - return *(f->end); - } else { - return *(f->head++); - } -} - -inline void fifo_flush(FIFOBuffer *f) { - f->head = f->tail; -} - -#if MCU_VARIANT != MCU_ESP32 && MCU_VARIANT != MCU_NRF52 - static inline bool fifo_isempty_locked(const FIFOBuffer *f) { - bool result; - ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { - result = fifo_isempty(f); - } - return result; - } - - static inline bool fifo_isfull_locked(const FIFOBuffer *f) { - bool result; - ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { - result = fifo_isfull(f); - } - return result; - } - - static inline void fifo_push_locked(FIFOBuffer *f, unsigned char c) { - ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { - fifo_push(f, c); - } - } -#endif - -/* -static inline unsigned char fifo_pop_locked(FIFOBuffer *f) { - unsigned char c; - ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { - c = fifo_pop(f); - } - return c; -} -*/ - -inline void fifo_init(FIFOBuffer *f, unsigned char *buffer, size_t size) { - f->head = f->tail = f->begin = buffer; - f->end = buffer + size; -} - -inline size_t fifo_len(FIFOBuffer *f) { - return f->end - f->begin; -} - -typedef struct FIFOBuffer16 -{ - uint16_t *begin; - uint16_t *end; - uint16_t * volatile head; - uint16_t * volatile tail; -} FIFOBuffer16; - -inline bool fifo16_isempty(const FIFOBuffer16 *f) { - return f->head == f->tail; -} - -inline bool fifo16_isfull(const FIFOBuffer16 *f) { - return ((f->head == f->begin) && (f->tail == f->end)) || (f->tail == f->head - 1); -} - -inline void fifo16_push(FIFOBuffer16 *f, uint16_t c) { - *(f->tail) = c; - - if (f->tail == f->end) { - f->tail = f->begin; - } else { - f->tail++; - } -} - -inline uint16_t fifo16_pop(FIFOBuffer16 *f) { - if(f->head == f->end) { - f->head = f->begin; - return *(f->end); - } else { - return *(f->head++); - } -} - -inline void fifo16_flush(FIFOBuffer16 *f) { - f->head = f->tail; -} - -#if MCU_VARIANT != MCU_ESP32 && MCU_VARIANT != MCU_NRF52 - static inline bool fifo16_isempty_locked(const FIFOBuffer16 *f) { - bool result; - ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { - result = fifo16_isempty(f); - } - - return result; - } -#endif - -/* -static inline bool fifo16_isfull_locked(const FIFOBuffer16 *f) { - bool result; - ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { - result = fifo16_isfull(f); - } - return result; -} - - -static inline void fifo16_push_locked(FIFOBuffer16 *f, uint16_t c) { - ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { - fifo16_push(f, c); - } -} - -static inline size_t fifo16_pop_locked(FIFOBuffer16 *f) { - size_t c; - ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { - c = fifo16_pop(f); - } - return c; -} -*/ - -inline void fifo16_init(FIFOBuffer16 *f, uint16_t *buffer, uint16_t size) { - f->head = f->tail = f->begin = buffer; - f->end = buffer + size; -} - -inline uint16_t fifo16_len(FIFOBuffer16 *f) { - return (f->end - f->begin); -} +#include "src/misc/FIFOBuffer.h" diff --git a/src/misc/FIFOBuffer.c b/src/misc/FIFOBuffer.c new file mode 100644 index 0000000..86fa0ca --- /dev/null +++ b/src/misc/FIFOBuffer.c @@ -0,0 +1,95 @@ +#include +#include +#include +#include "FIFOBuffer.h" + +#ifdef __cplusplus + extern "C" { +#endif + + bool fifo_isempty(const FIFOBuffer *f) { + return f->head == f->tail; +} + + bool fifo_isfull(const FIFOBuffer *f) { + return ((f->head == f->begin) && (f->tail == f->end)) || (f->tail == f->head - 1); +} + + void fifo_push(FIFOBuffer *f, unsigned char c) { + *(f->tail) = c; + + if (f->tail == f->end) { + f->tail = f->begin; + } else { + f->tail++; + } +} + + unsigned char fifo_pop(FIFOBuffer *f) { + if(f->head == f->end) { + f->head = f->begin; + return *(f->end); + } else { + return *(f->head++); + } +} + + void fifo_flush(FIFOBuffer *f) { + f->head = f->tail; +} + + + void fifo_init(FIFOBuffer *f, unsigned char *buffer, size_t size) { + f->head = f->tail = f->begin = buffer; + f->end = buffer + size; +} + +// todo, fix this so it actually displays the amount of data in the fifo +// buffer, not just the size allocated for the buffer + size_t fifo_len(FIFOBuffer *f) { + return f->end - f->begin; +} + + bool fifo16_isempty(const FIFOBuffer16 *f) { + return f->head == f->tail; +} + + bool fifo16_isfull(const FIFOBuffer16 *f) { + return ((f->head == f->begin) && (f->tail == f->end)) || (f->tail == f->head - 1); +} + + void fifo16_push(FIFOBuffer16 *f, uint16_t c) { + *(f->tail) = c; + + if (f->tail == f->end) { + f->tail = f->begin; + } else { + f->tail++; + } +} + + uint16_t fifo16_pop(FIFOBuffer16 *f) { + if(f->head == f->end) { + f->head = f->begin; + return *(f->end); + } else { + return *(f->head++); + } +} + + void fifo16_flush(FIFOBuffer16 *f) { + f->head = f->tail; +} + + void fifo16_init(FIFOBuffer16 *f, uint16_t *buffer, uint16_t size) { + f->head = f->tail = f->begin = buffer; + f->end = buffer + size; +} + + uint16_t fifo16_len(FIFOBuffer16 *f) { + return (f->end - f->begin); +} + +#ifdef __cplusplus +} +#endif diff --git a/src/misc/FIFOBuffer.h b/src/misc/FIFOBuffer.h new file mode 100644 index 0000000..bdc6d00 --- /dev/null +++ b/src/misc/FIFOBuffer.h @@ -0,0 +1,59 @@ +#ifndef FIFOBUFFER_H + +#define FIFOBUFFER_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* An 8 bit FIFO buffer implementation */ +typedef struct FIFOBuffer +{ + unsigned char *begin; + unsigned char *end; + unsigned char * volatile head; + unsigned char * volatile tail; +} FIFOBuffer; + +bool fifo_isempty(const FIFOBuffer *f); + +bool fifo_isfull(const FIFOBuffer *f); + +void fifo_push(FIFOBuffer *f, unsigned char c); + +unsigned char fifo_pop(FIFOBuffer *f); + +void fifo_flush(FIFOBuffer *f); + +void fifo_init(FIFOBuffer *f, unsigned char *buffer, size_t size); + +size_t fifo_len(FIFOBuffer *f); + +/* A 16-bit implementation of the same FIFO buffer. */ +typedef struct FIFOBuffer16 +{ + uint16_t *begin; + uint16_t *end; + uint16_t * volatile head; + uint16_t * volatile tail; +} FIFOBuffer16; + +bool fifo16_isempty(const FIFOBuffer16 *f); + +bool fifo16_isfull(const FIFOBuffer16 *f); + +void fifo16_push(FIFOBuffer16 *f, uint16_t c); + +uint16_t fifo16_pop(FIFOBuffer16 *f); + +void fifo16_flush(FIFOBuffer16 *f); + +void fifo16_init(FIFOBuffer16 *f, uint16_t *buffer, uint16_t size); + +uint16_t fifo16_len(FIFOBuffer16 *f); + +#ifdef __cplusplus +} +#endif + +#endif