4 * Copyright 2003, 2004 Develer S.r.l. (http://www.develer.com/)
5 * Copyright 2001 Bernardo Innocenti <bernie@develer.com>
6 * This file is part of DevLib - See devlib/README for information.
11 * \author Bernardo Innocenti <bernie@develer.com>
13 * \brief General pourpose FIFO buffer implemented with a ring buffer
15 * \li \c begin points to the first buffer element;
16 * \li \c end points to the last buffer element (unlike the STL convention);
17 * \li \c head points to the element to be extracted next;
18 * \li \c tail points to the location following the last insertion;
19 * \li when any of the pointers advances beyond \c end, it is reset
24 * +-----------------------------------+
25 * | empty | valid data | empty |
26 * +-----------------------------------+
32 * The buffer is EMPTY when \c head and \c tail point to the same location:
33 * \code head == tail \endcode
35 * The buffer is FULL when \c tail points to the location immediately
37 * \code tail == head - 1 \endcode
39 * The buffer is also FULL when \c tail points to the last buffer
40 * location and head points to the first one:
41 * \code head == begin && tail == end \endcode
46 *#* Revision 1.19 2004/12/08 08:30:12 bernie
47 *#* Add missing header.
49 *#* Revision 1.18 2004/11/16 21:55:12 bernie
50 *#* Workaround for a known fifobuf bug.
52 *#* Revision 1.17 2004/09/14 20:57:00 bernie
53 *#* Use debug.h instead of kdebug.h.
55 *#* Revision 1.16 2004/09/06 21:39:08 bernie
56 *#* Simplify code using ATOMIC().
58 *#* Revision 1.15 2004/08/29 22:05:16 bernie
59 *#* Rename BITS_PER_PTR to CPU_BITS_PER_PTR.
61 *#* Revision 1.14 2004/08/25 14:12:09 rasky
62 *#* Aggiornato il comment block dei log RCS
64 *#* Revision 1.13 2004/08/24 13:16:11 bernie
65 *#* Add type-size definitions for preprocessor.
67 *#* Revision 1.12 2004/08/02 20:20:29 aleph
68 *#* Merge from project_ks
70 *#* Revision 1.11 2004/07/30 14:15:53 rasky
71 *#* Nuovo supporto unificato per detect della CPU
73 *#* Revision 1.10 2004/07/29 22:57:09 bernie
76 *#* Revision 1.9 2004/07/20 23:54:27 bernie
77 *#* fifo_flush_locked(): New function;
78 *#* Revamp documentation.
80 *#* Revision 1.8 2004/07/20 23:47:39 bernie
81 *#* Finally remove redundant protos.
83 *#* Revision 1.7 2004/07/20 23:46:29 bernie
84 *#* Finally remove redundant protos.
86 *#* Revision 1.6 2004/06/06 17:18:04 bernie
87 *#* Remove redundant declaration of fifo_isempty_locked().
89 *#* Revision 1.5 2004/06/06 16:50:35 bernie
90 *#* Import fixes for race conditions from project_ks.
92 *#* Revision 1.4 2004/06/06 16:11:17 bernie
93 *#* Protect MetroWerks specific pragmas with #ifdef's
102 typedef struct FIFOBuffer
104 unsigned char * volatile head;
105 unsigned char * volatile tail;
106 unsigned char *begin;
111 #define ASSERT_VALID_FIFO(fifo) \
113 ASSERT((fifo)->head >= (fifo)->begin); \
114 ASSERT((fifo)->head <= (fifo)->end); \
115 ASSERT((fifo)->tail >= (fifo)->begin); \
116 ASSERT((fifo)->tail <= (fifo)->end); \
121 * Check whether the fifo is empty
123 * \note Calling fifo_isempty() is safe while a concurrent
124 * execution context is calling fifo_push() or fifo_pop()
125 * only if the CPU can atomically update a pointer
126 * (which the AVR and other 8-bit processors can't do).
128 * \sa fifo_isempty_locked
130 INLINE bool fifo_isempty(const FIFOBuffer *fb)
132 //ASSERT_VALID_FIFO(fb);
133 return fb->head == fb->tail;
138 * Check whether the fifo is full
140 * \note Calling fifo_isfull() is safe while a concurrent
141 * execution context is calling fifo_pop() and the
142 * CPU can update a pointer atomically.
143 * It is NOT safe when the other context calls
145 * This limitation is not usually problematic in a
146 * consumer/producer scenario because the
147 * fifo_isfull() and fifo_push() are usually called
148 * in the producer context.
150 INLINE bool fifo_isfull(const FIFOBuffer *fb)
152 //ASSERT_VALID_FIFO(fb);
154 ((fb->head == fb->begin) && (fb->tail == fb->end))
155 || (fb->tail == fb->head - 1);
160 * Pop a character from the fifo buffer.
162 * \note Calling \c fifo_push() on a full buffer is undefined.
163 * The caller must make sure the buffer has at least
164 * one free slot before calling this function.
166 * \note It is safe to call fifo_pop() and fifo_push() from
167 * concurrent contexts, unless the CPU can't update
168 * a pointer atomically (which the AVR and other 8-bit
169 * processors can't do).
171 * \sa fifo_push_locked
173 INLINE void fifo_push(FIFOBuffer *fb, unsigned char c)
176 #pragma interrupt called
178 //ASSERT_VALID_FIFO(fb);
180 /* Write at tail position */
183 if (UNLIKELY(fb->tail == fb->end))
184 /* wrap tail around */
185 fb->tail = fb->begin;
187 /* Move tail forward */
193 * Pop a character from the fifo buffer.
195 * \note Calling \c fifo_pop() on an empty buffer is undefined.
196 * The caller must make sure the buffer contains at least
197 * one character before calling this function.
199 * \note It is safe to call fifo_pop() and fifo_push() from
200 * concurrent contexts.
202 INLINE unsigned char fifo_pop(FIFOBuffer *fb)
205 #pragma interrupt called
207 //ASSERT_VALID_FIFO(fb);
209 if (UNLIKELY(fb->head == fb->end))
211 /* wrap head around */
212 fb->head = fb->begin;
216 /* move head forward */
217 return *(fb->head++);
222 * Make the fifo empty, discarding all its current contents.
224 INLINE void fifo_flush(FIFOBuffer *fb)
226 //ASSERT_VALID_FIFO(fb);
231 #if CPU_REG_BITS >= CPU_BITS_PER_PTR
234 * 16/32bit CPUs that can update a pointer with a single write
235 * operation, no need to disable interrupts.
237 #define fifo_isempty_locked(fb) fifo_isempty((fb))
238 #define fifo_push_locked(fb, c) fifo_push((fb), (c))
239 #define fifo_pop_locked(fb) fifo_pop((fb))
240 #define fifo_flush_locked(fb) fifo_flush((fb))
242 #else /* CPU_REG_BITS < CPU_BITS_PER_PTR */
245 * Similar to fifo_isempty(), but with stronger guarantees for
246 * concurrent access between user and interrupt code.
248 * \note This is actually only needed for 8-bit processors.
252 INLINE bool fifo_isempty_locked(const FIFOBuffer *fb)
255 ATOMIC(result = fifo_isempty(fb));
261 * Similar to fifo_push(), but with stronger guarantees for
262 * concurrent access between user and interrupt code.
264 * \note This is actually only needed for 8-bit processors.
268 INLINE void fifo_push_locked(FIFOBuffer *fb, unsigned char c)
270 ATOMIC(fifo_push(fb, c));
273 /* Probably not really needed, but hard to prove. */
274 INLINE unsigned char fifo_pop_locked(FIFOBuffer *fb)
277 ATOMIC(c = fifo_pop(fb));
282 * Similar to fifo_flush(), but with stronger guarantees for
283 * concurrent access between user and interrupt code.
285 * \note This is actually only needed for 8-bit processors.
289 INLINE void fifo_flush_locked(FIFOBuffer *fb)
291 ATOMIC(fifo_flush(fb));
294 #endif /* CPU_REG_BITS < BITS_PER_PTR */
298 * Thread safe version of fifo_isfull()
300 INLINE bool fifo_isfull_locked(const FIFOBuffer *_fb)
303 ATOMIC(result = fifo_isfull(_fb));
309 * FIFO Initialization.
311 INLINE void fifo_init(FIFOBuffer *fb, unsigned char *buf, size_t size)
313 /* FIFO buffers have a known bug with 1-byte buffers. */
316 fb->head = fb->tail = fb->begin = buf;
317 fb->end = buf + size - 1;
324 * UNTESTED: if uncommented, to be moved in fifobuf.c
326 void fifo_pushblock(FIFOBuffer *fb, unsigned char *block, size_t len)
330 /* Se c'e' spazio da tail alla fine del buffer */
331 if (fb->tail >= fb->head)
333 freelen = fb->end - fb->tail + 1;
335 /* C'e' abbastanza spazio per scrivere tutto il blocco? */
338 /* Scrivi quello che entra fino alla fine del buffer */
339 memcpy(fb->tail, block, freelen);
342 fb->tail = fb->begin;
346 /* Scrivi tutto il blocco */
347 memcpy(fb->tail, block, len);
355 while (!(freelen = fb->head - fb->tail - 1))
356 Delay(FIFO_POLLDELAY);
358 /* C'e' abbastanza spazio per scrivere tutto il blocco? */
361 /* Scrivi quello che entra fino alla fine del buffer */
362 memcpy(fb->tail, block, freelen);
369 /* Scrivi tutto il blocco */
370 memcpy(fb->tail, block, len);
378 #endif /* MWARE_FIFO_H */