avr-fw-modules/core/include/util/fifo.h

98 lines
1.4 KiB
C

#pragma once
#include <sys/errno.h>
#include <stdint.h>
#include <string.h>
typedef struct {
uint8_t set,
get;
uint8_t b[32];
} fifo32_t;
typedef struct {
uint8_t set,
get;
uint8_t b[64];
} fifo64_t;
/**
* @brief 32byte FIFOs
* @param fifo
* @return
*/
static inline int f32_reset(fifo32_t *fifo){
memset( fifo, 0x00, sizeof(fifo32_t) );
return ESUCCESS;
};
static inline int f32_read(fifo32_t *fifo){
int r;
if (fifo->get == fifo->set){
return -ENOFILE;
};
r = fifo->b[fifo->get];
fifo->get ++;
fifo->get &= 0x1F;
return r;
};
static inline int f32_write(fifo32_t *fifo,int ch){
if ( ((fifo->set + 1) & 0x1F) == fifo->get ){
return -ENOFILE;
};
fifo->b[fifo->set] = (uint8_t)(ch & 0xFF);
fifo->set++;
fifo->set &= 0x1F;
return ESUCCESS;
};
/**
* @brief 64 Byte FIFOs
* @param fifo
* @return
*/
static inline int f64_reset(fifo64_t *fifo){
memset( fifo, 0x00, sizeof(fifo64_t) );
return ESUCCESS;
};
static inline int f64_read(fifo64_t *fifo){
int r;
if (fifo->get == fifo->set){
return -ENOFILE;
};
r = fifo->b[fifo->get];
fifo->get ++;
fifo->get &= 0x3F;
return r;
};
static inline int f64_write(fifo64_t *fifo,int ch){
if ( ((fifo->set + 1) & 0x3F) == fifo->get ){
return -ENOFILE;
};
fifo->b[fifo->set] = (uint8_t)(ch & 0xFF);
fifo->set++;
fifo->set &= 0x3F;
return ESUCCESS;
};