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.20 2005/04/11 19:10:28 bernie
47 *#* Include top-level headers from cfg/ subdir.
49 *#* Revision 1.19 2004/12/08 08:30:12 bernie
50 *#* Add missing header.
52 *#* Revision 1.18 2004/11/16 21:55:12 bernie
53 *#* Workaround for a known fifobuf bug.
55 *#* Revision 1.17 2004/09/14 20:57:00 bernie
56 *#* Use debug.h instead of kdebug.h.
58 *#* Revision 1.16 2004/09/06 21:39:08 bernie
59 *#* Simplify code using ATOMIC().
61 *#* Revision 1.15 2004/08/29 22:05:16 bernie
62 *#* Rename BITS_PER_PTR to CPU_BITS_PER_PTR.
64 *#* Revision 1.14 2004/08/25 14:12:09 rasky
65 *#* Aggiornato il comment block dei log RCS
67 *#* Revision 1.13 2004/08/24 13:16:11 bernie
68 *#* Add type-size definitions for preprocessor.
70 *#* Revision 1.12 2004/08/02 20:20:29 aleph
71 *#* Merge from project_ks
73 *#* Revision 1.11 2004/07/30 14:15:53 rasky
74 *#* Nuovo supporto unificato per detect della CPU
76 *#* Revision 1.10 2004/07/29 22:57:09 bernie
79 *#* Revision 1.9 2004/07/20 23:54:27 bernie
80 *#* fifo_flush_locked(): New function;
81 *#* Revamp documentation.
83 *#* Revision 1.8 2004/07/20 23:47:39 bernie
84 *#* Finally remove redundant protos.
86 *#* Revision 1.7 2004/07/20 23:46:29 bernie
87 *#* Finally remove redundant protos.
89 *#* Revision 1.6 2004/06/06 17:18:04 bernie
90 *#* Remove redundant declaration of fifo_isempty_locked().
92 *#* Revision 1.5 2004/06/06 16:50:35 bernie
93 *#* Import fixes for race conditions from project_ks.
95 *#* Revision 1.4 2004/06/06 16:11:17 bernie
96 *#* Protect MetroWerks specific pragmas with #ifdef's
103 #include <cfg/debug.h>
105 typedef struct FIFOBuffer
107 unsigned char * volatile head;
108 unsigned char * volatile tail;
109 unsigned char *begin;
114 #define ASSERT_VALID_FIFO(fifo) \
116 ASSERT((fifo)->head >= (fifo)->begin); \
117 ASSERT((fifo)->head <= (fifo)->end); \
118 ASSERT((fifo)->tail >= (fifo)->begin); \
119 ASSERT((fifo)->tail <= (fifo)->end); \
124 * Check whether the fifo is empty
126 * \note Calling fifo_isempty() is safe while a concurrent
127 * execution context is calling fifo_push() or fifo_pop()
128 * only if the CPU can atomically update a pointer
129 * (which the AVR and other 8-bit processors can't do).
131 * \sa fifo_isempty_locked
133 INLINE bool fifo_isempty(const FIFOBuffer *fb)
135 //ASSERT_VALID_FIFO(fb);
136 return fb->head == fb->tail;
141 * Check whether the fifo is full
143 * \note Calling fifo_isfull() is safe while a concurrent
144 * execution context is calling fifo_pop() and the
145 * CPU can update a pointer atomically.
146 * It is NOT safe when the other context calls
148 * This limitation is not usually problematic in a
149 * consumer/producer scenario because the
150 * fifo_isfull() and fifo_push() are usually called
151 * in the producer context.
153 INLINE bool fifo_isfull(const FIFOBuffer *fb)
155 //ASSERT_VALID_FIFO(fb);
157 ((fb->head == fb->begin) && (fb->tail == fb->end))
158 || (fb->tail == fb->head - 1);
163 * Pop a character from the fifo buffer.
165 * \note Calling \c fifo_push() on a full buffer is undefined.
166 * The caller must make sure the buffer has at least
167 * one free slot before calling this function.
169 * \note It is safe to call fifo_pop() and fifo_push() from
170 * concurrent contexts, unless the CPU can't update
171 * a pointer atomically (which the AVR and other 8-bit
172 * processors can't do).
174 * \sa fifo_push_locked
176 INLINE void fifo_push(FIFOBuffer *fb, unsigned char c)
179 #pragma interrupt called
181 //ASSERT_VALID_FIFO(fb);
183 /* Write at tail position */
186 if (UNLIKELY(fb->tail == fb->end))
187 /* wrap tail around */
188 fb->tail = fb->begin;
190 /* Move tail forward */
196 * Pop a character from the fifo buffer.
198 * \note Calling \c fifo_pop() on an empty buffer is undefined.
199 * The caller must make sure the buffer contains at least
200 * one character before calling this function.
202 * \note It is safe to call fifo_pop() and fifo_push() from
203 * concurrent contexts.
205 INLINE unsigned char fifo_pop(FIFOBuffer *fb)
208 #pragma interrupt called
210 //ASSERT_VALID_FIFO(fb);
212 if (UNLIKELY(fb->head == fb->end))
214 /* wrap head around */
215 fb->head = fb->begin;
219 /* move head forward */
220 return *(fb->head++);
225 * Make the fifo empty, discarding all its current contents.
227 INLINE void fifo_flush(FIFOBuffer *fb)
229 //ASSERT_VALID_FIFO(fb);
234 #if CPU_REG_BITS >= CPU_BITS_PER_PTR
237 * 16/32bit CPUs that can update a pointer with a single write
238 * operation, no need to disable interrupts.
240 #define fifo_isempty_locked(fb) fifo_isempty((fb))
241 #define fifo_push_locked(fb, c) fifo_push((fb), (c))
242 #define fifo_pop_locked(fb) fifo_pop((fb))
243 #define fifo_flush_locked(fb) fifo_flush((fb))
245 #else /* CPU_REG_BITS < CPU_BITS_PER_PTR */
248 * Similar to fifo_isempty(), but with stronger guarantees for
249 * concurrent access between user and interrupt code.
251 * \note This is actually only needed for 8-bit processors.
255 INLINE bool fifo_isempty_locked(const FIFOBuffer *fb)
258 ATOMIC(result = fifo_isempty(fb));
264 * Similar to fifo_push(), but with stronger guarantees for
265 * concurrent access between user and interrupt code.
267 * \note This is actually only needed for 8-bit processors.
271 INLINE void fifo_push_locked(FIFOBuffer *fb, unsigned char c)
273 ATOMIC(fifo_push(fb, c));
276 /* Probably not really needed, but hard to prove. */
277 INLINE unsigned char fifo_pop_locked(FIFOBuffer *fb)
280 ATOMIC(c = fifo_pop(fb));
285 * Similar to fifo_flush(), but with stronger guarantees for
286 * concurrent access between user and interrupt code.
288 * \note This is actually only needed for 8-bit processors.
292 INLINE void fifo_flush_locked(FIFOBuffer *fb)
294 ATOMIC(fifo_flush(fb));
297 #endif /* CPU_REG_BITS < BITS_PER_PTR */
301 * Thread safe version of fifo_isfull()
303 INLINE bool fifo_isfull_locked(const FIFOBuffer *_fb)
306 ATOMIC(result = fifo_isfull(_fb));
312 * FIFO Initialization.
314 INLINE void fifo_init(FIFOBuffer *fb, unsigned char *buf, size_t size)
316 /* FIFO buffers have a known bug with 1-byte buffers. */
319 fb->head = fb->tail = fb->begin = buf;
320 fb->end = buf + size - 1;
327 * UNTESTED: if uncommented, to be moved in fifobuf.c
329 void fifo_pushblock(FIFOBuffer *fb, unsigned char *block, size_t len)
333 /* Se c'e' spazio da tail alla fine del buffer */
334 if (fb->tail >= fb->head)
336 freelen = fb->end - fb->tail + 1;
338 /* C'e' abbastanza spazio per scrivere tutto il blocco? */
341 /* Scrivi quello che entra fino alla fine del buffer */
342 memcpy(fb->tail, block, freelen);
345 fb->tail = fb->begin;
349 /* Scrivi tutto il blocco */
350 memcpy(fb->tail, block, len);
358 while (!(freelen = fb->head - fb->tail - 1))
359 Delay(FIFO_POLLDELAY);
361 /* C'e' abbastanza spazio per scrivere tutto il blocco? */
364 /* Scrivi quello che entra fino alla fine del buffer */
365 memcpy(fb->tail, block, freelen);
372 /* Scrivi tutto il blocco */
373 memcpy(fb->tail, block, len);
381 #endif /* MWARE_FIFO_H */