summaryrefslogtreecommitdiff
path: root/src/common/tusb_fifo.h
diff options
context:
space:
mode:
authorReinhard Panhuber <[email protected]>2020-09-14 18:24:08 +0200
committerReinhard Panhuber <[email protected]>2020-09-14 18:24:08 +0200
commit349c0f640e7d4532cc96fd28dea2dd11a5d43bde (patch)
treea1f2ec660ea7b44832468ced28553ee38dbcdbef /src/common/tusb_fifo.h
parent62a8e39c9b2f122594bf15f31874483f6344f76a (diff)
Implementation done, yet to be tested.
Diffstat (limited to 'src/common/tusb_fifo.h')
-rw-r--r--src/common/tusb_fifo.h71
1 files changed, 32 insertions, 39 deletions
diff --git a/src/common/tusb_fifo.h b/src/common/tusb_fifo.h
index fb0c896f3..56491c37b 100644
--- a/src/common/tusb_fifo.h
+++ b/src/common/tusb_fifo.h
@@ -52,14 +52,16 @@
*/
typedef struct
{
- uint8_t* buffer ; ///< buffer pointer
- uint16_t depth ; ///< max items
- uint16_t item_size ; ///< size of each item
- bool overwritable ;
+ uint8_t* buffer ; ///< buffer pointer
+ uint16_t depth ; ///< max items
+ uint16_t item_size ; ///< size of each item
+ bool overwritable ;
- volatile uint16_t count ; ///< number of items in queue
- volatile uint16_t wr_idx ; ///< write pointer
- volatile uint16_t rd_idx ; ///< read pointer
+ uint16_t non_used_index_space ; ///< required for non-power-of-two buffer length
+ uint16_t max_pointer_idx ; ///< maximum absolute pointer index
+
+ volatile uint16_t wr_idx ; ///< write pointer
+ volatile uint16_t rd_idx ; ///< read pointer
#if CFG_FIFO_MUTEX
tu_fifo_mutex_t mutex;
@@ -67,13 +69,15 @@ typedef struct
} tu_fifo_t;
-#define TU_FIFO_DEF(_name, _depth, _type, _overwritable) \
- uint8_t _name##_buf[_depth*sizeof(_type)]; \
- tu_fifo_t _name = { \
- .buffer = _name##_buf, \
- .depth = _depth, \
- .item_size = sizeof(_type), \
- .overwritable = _overwritable, \
+#define TU_FIFO_DEF(_name, _depth, _type, _overwritable) \
+ uint8_t _name##_buf[_depth*sizeof(_type)]; \
+ tu_fifo_t _name = { \
+ .buffer = _name##_buf, \
+ .depth = _depth, \
+ .item_size = sizeof(_type), \
+ .overwritable = _overwritable, \
+ .non_used_index_space = (2^16-1) % _depth, \
+ .max_pointer_idx = (2^16-1) - ((2^16-1) % _depth), \
}
bool tu_fifo_clear(tu_fifo_t *f);
@@ -86,39 +90,28 @@ static inline void tu_fifo_config_mutex(tu_fifo_t *f, tu_fifo_mutex_t mutex_hdl)
}
#endif
-bool tu_fifo_write (tu_fifo_t* f, void const * p_data);
-uint16_t tu_fifo_write_n (tu_fifo_t* f, void const * p_data, uint16_t count);
+bool tu_fifo_write (tu_fifo_t* f, void const * p_data);
+uint16_t tu_fifo_write_n (tu_fifo_t* f, void const * p_data, uint16_t count);
+
+bool tu_fifo_read (tu_fifo_t* f, void * p_buffer);
+uint16_t tu_fifo_read_n (tu_fifo_t* f, void * p_buffer, uint16_t count);
-bool tu_fifo_read (tu_fifo_t* f, void * p_buffer);
-uint16_t tu_fifo_read_n (tu_fifo_t* f, void * p_buffer, uint16_t count);
+bool tu_fifo_peek_at (tu_fifo_t* f, uint16_t pos, void * p_buffer);
+uint16_t tu_fifo_peek_at_n (tu_fifo_t* f, uint16_t pos, void * p_buffer, uint16_t n);
-bool tu_fifo_peek_at (tu_fifo_t* f, uint16_t pos, void * p_buffer);
+uint16_t tu_fifo_count (tu_fifo_t* f);
+void tu_fifo_correct_read_pointer (tu_fifo_t* f);
+bool tu_fifo_empty (tu_fifo_t* f);
+bool tu_fifo_full (tu_fifo_t* f);
+uint16_t tu_fifo_remaining (tu_fifo_t* f);
+bool tu_fifo_overflow (tu_fifo_t* f);
+void tu_fifo_correct_read_pointer (tu_fifo_t* f);
static inline bool tu_fifo_peek(tu_fifo_t* f, void * p_buffer)
{
return tu_fifo_peek_at(f, 0, p_buffer);
}
-static inline bool tu_fifo_empty(tu_fifo_t* f)
-{
- return (f->count == 0);
-}
-
-static inline bool tu_fifo_full(tu_fifo_t* f)
-{
- return (f->count == f->depth);
-}
-
-static inline uint16_t tu_fifo_count(tu_fifo_t* f)
-{
- return f->count;
-}
-
-static inline uint16_t tu_fifo_remaining(tu_fifo_t* f)
-{
- return f->depth - f->count;
-}
-
static inline uint16_t tu_fifo_depth(tu_fifo_t* f)
{
return f->depth;