blob: 18db289a106c4dd3e1dedbd4fcc606601773d2c8 [file] [log] [blame]
Austin Schuh41baf202022-01-01 14:33:40 -08001/*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2019 Ha Thach (tinyusb.org)
5 * Copyright (c) 2020 Reinhard Panhuber - rework to unmasked pointers
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 *
25 * This file is part of the TinyUSB stack.
26 */
27
28#ifndef _TUSB_FIFO_H_
29#define _TUSB_FIFO_H_
30
31#ifdef __cplusplus
32extern "C" {
33#endif
34
35// Due to the use of unmasked pointers, this FIFO does not suffer from loosing
36// one item slice. Furthermore, write and read operations are completely
37// decoupled as write and read functions do not modify a common state. Henceforth,
38// writing or reading from the FIFO within an ISR is safe as long as no other
39// process (thread or ISR) interferes.
40// Also, this FIFO is ready to be used in combination with a DMA as the write and
41// read pointers can be updated from within a DMA ISR. Overflows are detectable
42// within a certain number (see tu_fifo_overflow()).
43
44#include "common/tusb_common.h"
45
46// mutex is only needed for RTOS
47// for OS None, we don't get preempted
48#define CFG_FIFO_MUTEX (CFG_TUSB_OS != OPT_OS_NONE)
49
50#if CFG_FIFO_MUTEX
51#include "osal/osal.h"
52#define tu_fifo_mutex_t osal_mutex_t
53#endif
54
55typedef struct
56{
57 uint8_t* buffer ; ///< buffer pointer
58 uint16_t depth ; ///< max items
59 uint16_t item_size ; ///< size of each item
60 bool overwritable ;
61
62 uint16_t non_used_index_space ; ///< required for non-power-of-two buffer length
63 uint16_t max_pointer_idx ; ///< maximum absolute pointer index
64
65 volatile uint16_t wr_idx ; ///< write pointer
66 volatile uint16_t rd_idx ; ///< read pointer
67
68#if CFG_FIFO_MUTEX
69 tu_fifo_mutex_t mutex_wr;
70 tu_fifo_mutex_t mutex_rd;
71#endif
72
73} tu_fifo_t;
74
75typedef struct
76{
77 uint16_t len_lin ; ///< linear length in item size
78 uint16_t len_wrap ; ///< wrapped length in item size
79 void * ptr_lin ; ///< linear part start pointer
80 void * ptr_wrap ; ///< wrapped part start pointer
81} tu_fifo_buffer_info_t;
82
83#define TU_FIFO_INIT(_buffer, _depth, _type, _overwritable) \
84{ \
85 .buffer = _buffer, \
86 .depth = _depth, \
87 .item_size = sizeof(_type), \
88 .overwritable = _overwritable, \
89 .non_used_index_space = UINT16_MAX - (2*(_depth)-1), \
90 .max_pointer_idx = 2*(_depth)-1, \
91}
92
93#define TU_FIFO_DEF(_name, _depth, _type, _overwritable) \
94 uint8_t _name##_buf[_depth*sizeof(_type)]; \
95 tu_fifo_t _name = TU_FIFO_INIT(_name##_buf, _depth, _type, _overwritable)
96
97
98bool tu_fifo_set_overwritable(tu_fifo_t *f, bool overwritable);
99bool tu_fifo_clear(tu_fifo_t *f);
100bool tu_fifo_config(tu_fifo_t *f, void* buffer, uint16_t depth, uint16_t item_size, bool overwritable);
101
102#if CFG_FIFO_MUTEX
103TU_ATTR_ALWAYS_INLINE static inline
104void tu_fifo_config_mutex(tu_fifo_t *f, tu_fifo_mutex_t write_mutex_hdl, tu_fifo_mutex_t read_mutex_hdl)
105{
106 f->mutex_wr = write_mutex_hdl;
107 f->mutex_rd = read_mutex_hdl;
108}
109#endif
110
111bool tu_fifo_write (tu_fifo_t* f, void const * p_data);
112uint16_t tu_fifo_write_n (tu_fifo_t* f, void const * p_data, uint16_t n);
113uint16_t tu_fifo_write_n_const_addr_full_words (tu_fifo_t* f, const void * data, uint16_t n);
114
115bool tu_fifo_read (tu_fifo_t* f, void * p_buffer);
116uint16_t tu_fifo_read_n (tu_fifo_t* f, void * p_buffer, uint16_t n);
117uint16_t tu_fifo_read_n_const_addr_full_words (tu_fifo_t* f, void * buffer, uint16_t n);
118
119bool tu_fifo_peek (tu_fifo_t* f, void * p_buffer);
120uint16_t tu_fifo_peek_n (tu_fifo_t* f, void * p_buffer, uint16_t n);
121
122uint16_t tu_fifo_count (tu_fifo_t* f);
123uint16_t tu_fifo_remaining (tu_fifo_t* f);
124bool tu_fifo_empty (tu_fifo_t* f);
125bool tu_fifo_full (tu_fifo_t* f);
126bool tu_fifo_overflowed (tu_fifo_t* f);
127void tu_fifo_correct_read_pointer (tu_fifo_t* f);
128
129TU_ATTR_ALWAYS_INLINE static inline
130uint16_t tu_fifo_depth(tu_fifo_t* f)
131{
132 return f->depth;
133}
134
135// Pointer modifications intended to be used in combinations with DMAs.
136// USE WITH CARE - NO SAFTY CHECKS CONDUCTED HERE! NOT MUTEX PROTECTED!
137void tu_fifo_advance_write_pointer(tu_fifo_t *f, uint16_t n);
138void tu_fifo_advance_read_pointer (tu_fifo_t *f, uint16_t n);
139
140// If you want to read/write from/to the FIFO by use of a DMA, you may need to conduct two copies
141// to handle a possible wrapping part. These functions deliver a pointer to start
142// reading/writing from/to and a valid linear length along which no wrap occurs.
143void tu_fifo_get_read_info (tu_fifo_t *f, tu_fifo_buffer_info_t *info);
144void tu_fifo_get_write_info(tu_fifo_t *f, tu_fifo_buffer_info_t *info);
145
146
147#ifdef __cplusplus
148}
149#endif
150
151#endif /* _TUSB_FIFO_H_ */