kopia lustrzana https://github.com/SP8EBC/ParaTNC
111 wiersze
3.1 KiB
C
111 wiersze
3.1 KiB
C
#ifndef FIFO_H_
|
|
#define FIFO_H_
|
|
|
|
|
|
typedef struct FIFOBuffer
|
|
{
|
|
|
|
uint8_t * volatile head;
|
|
uint8_t * volatile tail;
|
|
uint8_t * begin;
|
|
uint8_t * end;
|
|
|
|
} FIFOBuffer;
|
|
|
|
|
|
/*********************************************************************************************************************/
|
|
static bool fifo_isempty(const FIFOBuffer *fb) {
|
|
/*********************************************************************************************************************/
|
|
|
|
return fb->head == fb->tail;
|
|
|
|
}
|
|
|
|
|
|
/*********************************************************************************************************************/
|
|
static bool fifo_isfull(const FIFOBuffer *fb) {
|
|
/*********************************************************************************************************************/
|
|
|
|
return ((fb->head == fb->begin) && (fb->tail == fb->end)) || (fb->tail == fb->head - 1);
|
|
|
|
}
|
|
|
|
|
|
/*********************************************************************************************************************/
|
|
static void fifo_push(FIFOBuffer *fb, uint8_t c) {
|
|
/*********************************************************************************************************************/
|
|
|
|
/* Write at tail position */
|
|
*(fb->tail) = c;
|
|
|
|
if (fb->tail == fb->end)
|
|
{
|
|
/* wrap tail around */
|
|
fb->tail = fb->begin;
|
|
}
|
|
else
|
|
{
|
|
/* Move tail forward */
|
|
fb->tail++;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
/*********************************************************************************************************************/
|
|
static uint8_t fifo_pop(FIFOBuffer *fb) {
|
|
/*********************************************************************************************************************/
|
|
|
|
if (fb->head == fb->end)
|
|
{
|
|
/* wrap head around */
|
|
fb->head = fb->begin;
|
|
return *(fb->end);
|
|
}
|
|
else
|
|
{
|
|
/* move head forward */
|
|
return *(fb->head++);
|
|
}
|
|
|
|
}
|
|
|
|
|
|
/*********************************************************************************************************************/
|
|
static void fifo_flush(FIFOBuffer *fb) {
|
|
/*********************************************************************************************************************/
|
|
|
|
fb->head = fb->tail;
|
|
|
|
}
|
|
|
|
|
|
/*********************************************************************************************************************/
|
|
static void fifo_init(FIFOBuffer *fb, uint8_t *buf, uint16_t size) {
|
|
/*********************************************************************************************************************/
|
|
|
|
fb->head = fb->tail = fb->begin = buf;
|
|
fb->end = buf + size - 1;
|
|
|
|
}
|
|
|
|
|
|
/*********************************************************************************************************************/
|
|
static int16_t fifo_getc(FIFOBuffer *fb) {
|
|
/*********************************************************************************************************************/
|
|
|
|
if (!fifo_isempty(fb)) return fifo_pop(fb);
|
|
else return -1;
|
|
|
|
}
|
|
|
|
/*********************************************************************************************************************/
|
|
static void fifo_putc(uint8_t c, FIFOBuffer *fb) {
|
|
/*********************************************************************************************************************/
|
|
|
|
if (!fifo_isfull(fb)) fifo_push(fb, c);
|
|
|
|
}
|
|
|
|
#endif /* FIFO_H_ */
|