tinyusb
fifo.h
Go to the documentation of this file.
1 /**************************************************************************/
37 /**************************************************************************/
38 
43 #ifndef _TUSB_FIFO_H_
44 #define _TUSB_FIFO_H_
45 
46 #include "common/common.h"
47 
48 #ifdef __cplusplus
49  extern "C" {
50 #endif
51 
55 typedef struct
56 {
57  uint8_t* const buffer ;
58  uint16_t const depth ;
59  uint16_t const item_size ;
60  volatile uint16_t count ;
61  volatile uint16_t wr_idx ;
62  volatile uint16_t rd_idx ;
63  bool overwritable ;
64  // IRQn_Type irq;
65 } fifo_t;
66 
67 #define FIFO_DEF(name, ff_depth, type, is_overwritable) /*, irq_mutex)*/ \
68  uint8_t name##_buffer[ff_depth*sizeof(type)];\
69  fifo_t name = {\
70  .buffer = name##_buffer,\
71  .depth = ff_depth,\
72  .item_size = sizeof(type),\
73  .overwritable = is_overwritable,\
74  /*.irq = irq_mutex*/\
75  }
76 
77 bool fifo_write(fifo_t* f, void const * p_data);
78 bool fifo_read(fifo_t* f, void * p_buffer);
79 void fifo_clear(fifo_t *f);
80 
81 static inline bool fifo_is_empty(fifo_t* f) ATTR_PURE ATTR_ALWAYS_INLINE;
82 static inline bool fifo_is_empty(fifo_t* f)
83 {
84  return (f->count == 0);
85 }
86 
87 static inline bool fifo_is_full(fifo_t* f) ATTR_PURE ATTR_ALWAYS_INLINE;
88 static inline bool fifo_is_full(fifo_t* f)
89 {
90  return (f->count == f->depth);
91 }
92 
93 static inline uint16_t fifo_get_length(fifo_t* f) ATTR_PURE ATTR_ALWAYS_INLINE;
94 static inline uint16_t fifo_get_length(fifo_t* f)
95 {
96  return f->count;
97 }
98 
99 #ifdef __cplusplus
100  }
101 #endif
102 
103 #endif /* _TUSB_FIFO_H_ */
volatile uint16_t rd_idx
read pointer
Definition: fifo.h:62
uint16_t const depth
max items
Definition: fifo.h:58
uint16_t const item_size
size of each item
Definition: fifo.h:59
void fifo_clear(fifo_t *f)
Clear the fifo read and write pointers and set length to zero.
Definition: fifo.c:138
#define ATTR_PURE
Many functions have no effects except the return value and their return value depends only on the par...
Definition: compiler_gcc.h:96
bool fifo_write(fifo_t *f, void const *p_data)
Write one byte into the RX buffer.
Definition: fifo.c:102
bool fifo_read(fifo_t *f, void *p_buffer)
Read one byte out of the RX buffer.
Definition: fifo.c:65
volatile uint16_t wr_idx
write pointer
Definition: fifo.h:61
uint8_t *const buffer
buffer pointer
Definition: fifo.h:57
Simple Circular FIFO.
Definition: fifo.h:55
#define ATTR_ALWAYS_INLINE
Generally, functions are not inlined unless optimization is specified. For functions declared inline...
Definition: compiler_gcc.h:89
volatile uint16_t count
number of items in queue
Definition: fifo.h:60