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.18 2004/11/16 21:55:12 bernie
47 *#* Workaround for a known fifobuf bug.
49 *#* Revision 1.17 2004/09/14 20:57:00 bernie
50 *#* Use debug.h instead of kdebug.h.
52 *#* Revision 1.16 2004/09/06 21:39:08 bernie
53 *#* Simplify code using ATOMIC().
55 *#* Revision 1.15 2004/08/29 22:05:16 bernie
56 *#* Rename BITS_PER_PTR to CPU_BITS_PER_PTR.
58 *#* Revision 1.14 2004/08/25 14:12:09 rasky
59 *#* Aggiornato il comment block dei log RCS
61 *#* Revision 1.13 2004/08/24 13:16:11 bernie
62 *#* Add type-size definitions for preprocessor.
64 *#* Revision 1.12 2004/08/02 20:20:29 aleph
65 *#* Merge from project_ks
67 *#* Revision 1.11 2004/07/30 14:15:53 rasky
68 *#* Nuovo supporto unificato per detect della CPU
70 *#* Revision 1.10 2004/07/29 22:57:09 bernie
73 *#* Revision 1.9 2004/07/20 23:54:27 bernie
74 *#* fifo_flush_locked(): New function;
75 *#* Revamp documentation.
77 *#* Revision 1.8 2004/07/20 23:47:39 bernie
78 *#* Finally remove redundant protos.
80 *#* Revision 1.7 2004/07/20 23:46:29 bernie
81 *#* Finally remove redundant protos.
83 *#* Revision 1.6 2004/06/06 17:18:04 bernie
84 *#* Remove redundant declaration of fifo_isempty_locked().
86 *#* Revision 1.5 2004/06/06 16:50:35 bernie
87 *#* Import fixes for race conditions from project_ks.
89 *#* Revision 1.4 2004/06/06 16:11:17 bernie
90 *#* Protect MetroWerks specific pragmas with #ifdef's
98 typedef struct FIFOBuffer
100 unsigned char * volatile head;
101 unsigned char * volatile tail;
102 unsigned char *begin;
107 #define ASSERT_VALID_FIFO(fifo) \
109 ASSERT((fifo)->head >= (fifo)->begin); \
110 ASSERT((fifo)->head <= (fifo)->end); \
111 ASSERT((fifo)->tail >= (fifo)->begin); \
112 ASSERT((fifo)->tail <= (fifo)->end); \
117 * Check whether the fifo is empty
119 * \note Calling fifo_isempty() is safe while a concurrent
120 * execution context is calling fifo_push() or fifo_pop()
121 * only if the CPU can atomically update a pointer
122 * (which the AVR and other 8-bit processors can't do).
124 * \sa fifo_isempty_locked
126 INLINE bool fifo_isempty(const FIFOBuffer *fb)
128 //ASSERT_VALID_FIFO(fb);
129 return fb->head == fb->tail;
134 * Check whether the fifo is full
136 * \note Calling fifo_isfull() is safe while a concurrent
137 * execution context is calling fifo_pop() and the
138 * CPU can update a pointer atomically.
139 * It is NOT safe when the other context calls
141 * This limitation is not usually problematic in a
142 * consumer/producer scenario because the
143 * fifo_isfull() and fifo_push() are usually called
144 * in the producer context.
146 INLINE bool fifo_isfull(const FIFOBuffer *fb)
148 //ASSERT_VALID_FIFO(fb);
150 ((fb->head == fb->begin) && (fb->tail == fb->end))
151 || (fb->tail == fb->head - 1);
156 * Pop a character from the fifo buffer.
158 * \note Calling \c fifo_push() on a full buffer is undefined.
159 * The caller must make sure the buffer has at least
160 * one free slot before calling this function.
162 * \note It is safe to call fifo_pop() and fifo_push() from
163 * concurrent contexts, unless the CPU can't update
164 * a pointer atomically (which the AVR and other 8-bit
165 * processors can't do).
167 * \sa fifo_push_locked
169 INLINE void fifo_push(FIFOBuffer *fb, unsigned char c)
172 #pragma interrupt called
174 //ASSERT_VALID_FIFO(fb);
176 /* Write at tail position */
179 if (UNLIKELY(fb->tail == fb->end))
180 /* wrap tail around */
181 fb->tail = fb->begin;
183 /* Move tail forward */
189 * Pop a character from the fifo buffer.
191 * \note Calling \c fifo_pop() on an empty buffer is undefined.
192 * The caller must make sure the buffer contains at least
193 * one character before calling this function.
195 * \note It is safe to call fifo_pop() and fifo_push() from
196 * concurrent contexts.
198 INLINE unsigned char fifo_pop(FIFOBuffer *fb)
201 #pragma interrupt called
203 //ASSERT_VALID_FIFO(fb);
205 if (UNLIKELY(fb->head == fb->end))
207 /* wrap head around */
208 fb->head = fb->begin;
212 /* move head forward */
213 return *(fb->head++);
218 * Make the fifo empty, discarding all its current contents.
220 INLINE void fifo_flush(FIFOBuffer *fb)
222 //ASSERT_VALID_FIFO(fb);
227 #if CPU_REG_BITS >= CPU_BITS_PER_PTR
230 * 16/32bit CPUs that can update a pointer with a single write
231 * operation, no need to disable interrupts.
233 #define fifo_isempty_locked(fb) fifo_isempty((fb))
234 #define fifo_push_locked(fb, c) fifo_push((fb), (c))
235 #define fifo_pop_locked(fb) fifo_pop((fb))
236 #define fifo_flush_locked(fb) fifo_flush((fb))
238 #else /* CPU_REG_BITS < CPU_BITS_PER_PTR */
241 * Similar to fifo_isempty(), but with stronger guarantees for
242 * concurrent access between user and interrupt code.
244 * \note This is actually only needed for 8-bit processors.
248 INLINE bool fifo_isempty_locked(const FIFOBuffer *fb)
251 ATOMIC(result = fifo_isempty(fb));
257 * Similar to fifo_push(), but with stronger guarantees for
258 * concurrent access between user and interrupt code.
260 * \note This is actually only needed for 8-bit processors.
264 INLINE void fifo_push_locked(FIFOBuffer *fb, unsigned char c)
266 ATOMIC(fifo_push(fb, c));
269 /* Probably not really needed, but hard to prove. */
270 INLINE unsigned char fifo_pop_locked(FIFOBuffer *fb)
273 ATOMIC(c = fifo_pop(fb));
278 * Similar to fifo_flush(), but with stronger guarantees for
279 * concurrent access between user and interrupt code.
281 * \note This is actually only needed for 8-bit processors.
285 INLINE void fifo_flush_locked(FIFOBuffer *fb)
287 ATOMIC(fifo_flush(fb));
290 #endif /* CPU_REG_BITS < BITS_PER_PTR */
294 * Thread safe version of fifo_isfull()
296 INLINE bool fifo_isfull_locked(const FIFOBuffer *_fb)
299 ATOMIC(result = fifo_isfull(_fb));
305 * FIFO Initialization.
307 INLINE void fifo_init(FIFOBuffer *fb, unsigned char *buf, size_t size)
309 /* FIFO buffers have a known bug with 1-byte buffers. */
312 fb->head = fb->tail = fb->begin = buf;
313 fb->end = buf + size - 1;
320 * UNTESTED: if uncommented, to be moved in fifobuf.c
322 void fifo_pushblock(FIFOBuffer *fb, unsigned char *block, size_t len)
326 /* Se c'e' spazio da tail alla fine del buffer */
327 if (fb->tail >= fb->head)
329 freelen = fb->end - fb->tail + 1;
331 /* C'e' abbastanza spazio per scrivere tutto il blocco? */
334 /* Scrivi quello che entra fino alla fine del buffer */
335 memcpy(fb->tail, block, freelen);
338 fb->tail = fb->begin;
342 /* Scrivi tutto il blocco */
343 memcpy(fb->tail, block, len);
351 while (!(freelen = fb->head - fb->tail - 1))
352 Delay(FIFO_POLLDELAY);
354 /* C'e' abbastanza spazio per scrivere tutto il blocco? */
357 /* Scrivi quello che entra fino alla fine del buffer */
358 memcpy(fb->tail, block, freelen);
365 /* Scrivi tutto il blocco */
366 memcpy(fb->tail, block, len);
374 #endif /* MWARE_FIFO_H */