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.15 2004/08/29 22:05:16 bernie
47 *#* Rename BITS_PER_PTR to CPU_BITS_PER_PTR.
49 *#* Revision 1.14 2004/08/25 14:12:09 rasky
50 *#* Aggiornato il comment block dei log RCS
52 *#* Revision 1.13 2004/08/24 13:16:11 bernie
53 *#* Add type-size definitions for preprocessor.
55 *#* Revision 1.12 2004/08/02 20:20:29 aleph
56 *#* Merge from project_ks
58 *#* Revision 1.11 2004/07/30 14:15:53 rasky
59 *#* Nuovo supporto unificato per detect della CPU
61 *#* Revision 1.10 2004/07/29 22:57:09 bernie
64 *#* Revision 1.9 2004/07/20 23:54:27 bernie
65 *#* fifo_flush_locked(): New function;
66 *#* Revamp documentation.
68 *#* Revision 1.8 2004/07/20 23:47:39 bernie
69 *#* Finally remove redundant protos.
71 *#* Revision 1.7 2004/07/20 23:46:29 bernie
72 *#* Finally remove redundant protos.
74 *#* Revision 1.6 2004/06/06 17:18:04 bernie
75 *#* Remove redundant declaration of fifo_isempty_locked().
77 *#* Revision 1.5 2004/06/06 16:50:35 bernie
78 *#* Import fixes for race conditions from project_ks.
80 *#* Revision 1.4 2004/06/06 16:11:17 bernie
81 *#* Protect MetroWerks specific pragmas with #ifdef's
89 typedef struct FIFOBuffer
91 unsigned char * volatile head;
92 unsigned char * volatile tail;
99 * Check whether the fifo is empty
101 * \note Calling fifo_isempty() is safe while a concurrent
102 * execution context is calling fifo_push() or fifo_pop()
103 * only if the CPU can atomically update a pointer
104 * (which the AVR and other 8-bit processors can't do).
106 * \sa fifo_isempty_locked
108 INLINE bool fifo_isempty(const FIFOBuffer *fb)
110 return fb->head == fb->tail;
115 * Check whether the fifo is full
117 * \note Calling fifo_isfull() is safe while a concurrent
118 * execution context is calling fifo_pop() and the
119 * CPU can update a pointer atomically.
120 * It is NOT safe when the other context calls
122 * This limitation is not usually problematic in a
123 * consumer/producer scenario because the
124 * fifo_isfull() and fifo_push() are usually called
125 * in the producer context.
127 INLINE bool fifo_isfull(const FIFOBuffer *fb)
130 ((fb->head == fb->begin) && (fb->tail == fb->end))
131 || (fb->tail == fb->head - 1);
136 * Pop a character from the fifo buffer.
138 * \note Calling \c fifo_push() on a full buffer is undefined.
139 * The caller must make sure the buffer has at least
140 * one free slot before calling this function.
142 * \note It is safe to call fifo_pop() and fifo_push() from
143 * concurrent contexts, unless the CPU can't update
144 * a pointer atomically (which the AVR and other 8-bit
145 * processors can't do).
147 * \sa fifo_push_locked
149 INLINE void fifo_push(FIFOBuffer *fb, unsigned char c)
152 #pragma interrupt called
154 /* Write at tail position */
157 if (fb->tail == fb->end)
158 /* wrap tail around */
159 fb->tail = fb->begin;
161 /* Move tail forward */
167 * Pop a character from the fifo buffer.
169 * \note Calling \c fifo_pop() on an empty buffer is undefined.
170 * The caller must make sure the buffer contains at least
171 * one character before calling this function.
173 * \note It is safe to call fifo_pop() and fifo_push() from
174 * concurrent contexts.
176 INLINE unsigned char fifo_pop(FIFOBuffer *fb)
179 #pragma interrupt called
181 if (fb->head == fb->end)
183 /* wrap head around */
184 fb->head = fb->begin;
188 /* move head forward */
189 return *(fb->head++);
194 * Make the fifo empty, discarding all its current contents.
196 INLINE void fifo_flush(FIFOBuffer *fb)
202 #if CPU_REG_BITS >= CPU_BITS_PER_PTR
205 * 16/32bit CPUs that can update a pointer with a single write
206 * operation, no need to disable interrupts.
208 #define fifo_isempty_locked(fb) fifo_isempty((fb))
209 #define fifo_push_locked(fb, c) fifo_push((fb), (c))
210 #define fifo_flush_locked(fb) fifo_flush((fb))
212 #else /* CPU_REG_BITS < CPU_BITS_PER_PTR */
215 * Similar to fifo_isempty(), but with stronger guarantees for
216 * concurrent access between user and interrupt code.
218 * \note This is actually only needed for 8-bit processors.
222 INLINE bool fifo_isempty_locked(const FIFOBuffer *fb)
227 DISABLE_IRQSAVE(flags);
228 result = fifo_isempty(fb);
229 ENABLE_IRQRESTORE(flags);
236 * Similar to fifo_push(), but with stronger guarantees for
237 * concurrent access between user and interrupt code.
239 * \note This is actually only needed for 8-bit processors.
243 INLINE void fifo_push_locked(FIFOBuffer *fb, unsigned char c)
246 DISABLE_IRQSAVE(flags);
248 ENABLE_IRQRESTORE(flags);
253 * Similar to fifo_flush(), but with stronger guarantees for
254 * concurrent access between user and interrupt code.
256 * \note This is actually only needed for 8-bit processors.
260 INLINE void fifo_flush_locked(FIFOBuffer *fb)
263 DISABLE_IRQSAVE(flags);
265 ENABLE_IRQRESTORE(flags);
268 #endif /* CPU_REG_BITS < BITS_PER_PTR */
272 * Thread safe version of fifo_isfull()
274 INLINE bool fifo_isfull_locked(const FIFOBuffer *_fb)
279 DISABLE_IRQSAVE(_flags);
280 _result = fifo_isfull(_fb);
281 ENABLE_IRQRESTORE(_flags);
288 * FIFO Initialization.
290 INLINE void fifo_init(FIFOBuffer *fb, unsigned char *buf, size_t size)
292 fb->head = fb->tail = fb->begin = buf;
293 fb->end = buf + size - 1;
300 * UNTESTED: if uncommented, to be moved in fifobuf.c
302 void fifo_pushblock(FIFOBuffer *fb, unsigned char *block, size_t len)
306 /* Se c'e' spazio da tail alla fine del buffer */
307 if (fb->tail >= fb->head)
309 freelen = fb->end - fb->tail + 1;
311 /* C'e' abbastanza spazio per scrivere tutto il blocco? */
314 /* Scrivi quello che entra fino alla fine del buffer */
315 memcpy(fb->tail, block, freelen);
318 fb->tail = fb->begin;
322 /* Scrivi tutto il blocco */
323 memcpy(fb->tail, block, len);
331 while (!(freelen = fb->head - fb->tail - 1))
332 Delay(FIFO_POLLDELAY);
334 /* C'e' abbastanza spazio per scrivere tutto il blocco? */
337 /* Scrivi quello che entra fino alla fine del buffer */
338 memcpy(fb->tail, block, freelen);
345 /* Scrivi tutto il blocco */
346 memcpy(fb->tail, block, len);
354 #endif /* MWARE_FIFO_H */