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.17 2004/09/14 20:57:00 bernie
47 *#* Use debug.h instead of kdebug.h.
49 *#* Revision 1.16 2004/09/06 21:39:08 bernie
50 *#* Simplify code using ATOMIC().
52 *#* Revision 1.15 2004/08/29 22:05:16 bernie
53 *#* Rename BITS_PER_PTR to CPU_BITS_PER_PTR.
55 *#* Revision 1.14 2004/08/25 14:12:09 rasky
56 *#* Aggiornato il comment block dei log RCS
58 *#* Revision 1.13 2004/08/24 13:16:11 bernie
59 *#* Add type-size definitions for preprocessor.
61 *#* Revision 1.12 2004/08/02 20:20:29 aleph
62 *#* Merge from project_ks
64 *#* Revision 1.11 2004/07/30 14:15:53 rasky
65 *#* Nuovo supporto unificato per detect della CPU
67 *#* Revision 1.10 2004/07/29 22:57:09 bernie
70 *#* Revision 1.9 2004/07/20 23:54:27 bernie
71 *#* fifo_flush_locked(): New function;
72 *#* Revamp documentation.
74 *#* Revision 1.8 2004/07/20 23:47:39 bernie
75 *#* Finally remove redundant protos.
77 *#* Revision 1.7 2004/07/20 23:46:29 bernie
78 *#* Finally remove redundant protos.
80 *#* Revision 1.6 2004/06/06 17:18:04 bernie
81 *#* Remove redundant declaration of fifo_isempty_locked().
83 *#* Revision 1.5 2004/06/06 16:50:35 bernie
84 *#* Import fixes for race conditions from project_ks.
86 *#* Revision 1.4 2004/06/06 16:11:17 bernie
87 *#* Protect MetroWerks specific pragmas with #ifdef's
95 typedef struct FIFOBuffer
97 unsigned char * volatile head;
98 unsigned char * volatile tail;
104 #define ASSERT_VALID_FIFO(fifo) \
106 ASSERT((fifo)->head >= (fifo)->begin); \
107 ASSERT((fifo)->head <= (fifo)->end); \
108 ASSERT((fifo)->tail >= (fifo)->begin); \
109 ASSERT((fifo)->tail <= (fifo)->end); \
114 * Check whether the fifo is empty
116 * \note Calling fifo_isempty() is safe while a concurrent
117 * execution context is calling fifo_push() or fifo_pop()
118 * only if the CPU can atomically update a pointer
119 * (which the AVR and other 8-bit processors can't do).
121 * \sa fifo_isempty_locked
123 INLINE bool fifo_isempty(const FIFOBuffer *fb)
125 //ASSERT_VALID_FIFO(fb);
126 return fb->head == fb->tail;
131 * Check whether the fifo is full
133 * \note Calling fifo_isfull() is safe while a concurrent
134 * execution context is calling fifo_pop() and the
135 * CPU can update a pointer atomically.
136 * It is NOT safe when the other context calls
138 * This limitation is not usually problematic in a
139 * consumer/producer scenario because the
140 * fifo_isfull() and fifo_push() are usually called
141 * in the producer context.
143 INLINE bool fifo_isfull(const FIFOBuffer *fb)
145 //ASSERT_VALID_FIFO(fb);
147 ((fb->head == fb->begin) && (fb->tail == fb->end))
148 || (fb->tail == fb->head - 1);
153 * Pop a character from the fifo buffer.
155 * \note Calling \c fifo_push() on a full buffer is undefined.
156 * The caller must make sure the buffer has at least
157 * one free slot before calling this function.
159 * \note It is safe to call fifo_pop() and fifo_push() from
160 * concurrent contexts, unless the CPU can't update
161 * a pointer atomically (which the AVR and other 8-bit
162 * processors can't do).
164 * \sa fifo_push_locked
166 INLINE void fifo_push(FIFOBuffer *fb, unsigned char c)
169 #pragma interrupt called
171 //ASSERT_VALID_FIFO(fb);
173 /* Write at tail position */
176 if (UNLIKELY(fb->tail == fb->end))
177 /* wrap tail around */
178 fb->tail = fb->begin;
180 /* Move tail forward */
186 * Pop a character from the fifo buffer.
188 * \note Calling \c fifo_pop() on an empty buffer is undefined.
189 * The caller must make sure the buffer contains at least
190 * one character before calling this function.
192 * \note It is safe to call fifo_pop() and fifo_push() from
193 * concurrent contexts.
195 INLINE unsigned char fifo_pop(FIFOBuffer *fb)
198 #pragma interrupt called
200 //ASSERT_VALID_FIFO(fb);
202 if (UNLIKELY(fb->head == fb->end))
204 /* wrap head around */
205 fb->head = fb->begin;
209 /* move head forward */
210 return *(fb->head++);
215 * Make the fifo empty, discarding all its current contents.
217 INLINE void fifo_flush(FIFOBuffer *fb)
219 //ASSERT_VALID_FIFO(fb);
224 #if CPU_REG_BITS >= CPU_BITS_PER_PTR
227 * 16/32bit CPUs that can update a pointer with a single write
228 * operation, no need to disable interrupts.
230 #define fifo_isempty_locked(fb) fifo_isempty((fb))
231 #define fifo_push_locked(fb, c) fifo_push((fb), (c))
232 #define fifo_pop_locked(fb) fifo_pop((fb))
233 #define fifo_flush_locked(fb) fifo_flush((fb))
235 #else /* CPU_REG_BITS < CPU_BITS_PER_PTR */
238 * Similar to fifo_isempty(), but with stronger guarantees for
239 * concurrent access between user and interrupt code.
241 * \note This is actually only needed for 8-bit processors.
245 INLINE bool fifo_isempty_locked(const FIFOBuffer *fb)
248 ATOMIC(result = fifo_isempty(fb));
254 * Similar to fifo_push(), but with stronger guarantees for
255 * concurrent access between user and interrupt code.
257 * \note This is actually only needed for 8-bit processors.
261 INLINE void fifo_push_locked(FIFOBuffer *fb, unsigned char c)
263 ATOMIC(fifo_push(fb, c));
266 /* Probably not really needed, but hard to prove. */
267 INLINE unsigned char fifo_pop_locked(FIFOBuffer *fb)
270 ATOMIC(c = fifo_pop(fb));
275 * Similar to fifo_flush(), but with stronger guarantees for
276 * concurrent access between user and interrupt code.
278 * \note This is actually only needed for 8-bit processors.
282 INLINE void fifo_flush_locked(FIFOBuffer *fb)
284 ATOMIC(fifo_flush(fb));
287 #endif /* CPU_REG_BITS < BITS_PER_PTR */
291 * Thread safe version of fifo_isfull()
293 INLINE bool fifo_isfull_locked(const FIFOBuffer *_fb)
296 ATOMIC(result = fifo_isfull(_fb));
302 * FIFO Initialization.
304 INLINE void fifo_init(FIFOBuffer *fb, unsigned char *buf, size_t size)
306 fb->head = fb->tail = fb->begin = buf;
307 fb->end = buf + size - 1;
314 * UNTESTED: if uncommented, to be moved in fifobuf.c
316 void fifo_pushblock(FIFOBuffer *fb, unsigned char *block, size_t len)
320 /* Se c'e' spazio da tail alla fine del buffer */
321 if (fb->tail >= fb->head)
323 freelen = fb->end - fb->tail + 1;
325 /* C'e' abbastanza spazio per scrivere tutto il blocco? */
328 /* Scrivi quello che entra fino alla fine del buffer */
329 memcpy(fb->tail, block, freelen);
332 fb->tail = fb->begin;
336 /* Scrivi tutto il blocco */
337 memcpy(fb->tail, block, len);
345 while (!(freelen = fb->head - fb->tail - 1))
346 Delay(FIFO_POLLDELAY);
348 /* C'e' abbastanza spazio per scrivere tutto il blocco? */
351 /* Scrivi quello che entra fino alla fine del buffer */
352 memcpy(fb->tail, block, freelen);
359 /* Scrivi tutto il blocco */
360 memcpy(fb->tail, block, len);
368 #endif /* MWARE_FIFO_H */