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.12 2004/08/02 20:20:29 aleph
47 * Merge from project_ks
49 * Revision 1.11 2004/07/30 14:15:53 rasky
50 * Nuovo supporto unificato per detect della CPU
52 * Revision 1.10 2004/07/29 22:57:09 bernie
55 * Revision 1.9 2004/07/20 23:54:27 bernie
56 * fifo_flush_locked(): New function;
57 * Revamp documentation.
59 * Revision 1.8 2004/07/20 23:47:39 bernie
60 * Finally remove redundant protos.
62 * Revision 1.7 2004/07/20 23:46:29 bernie
63 * Finally remove redundant protos.
65 * Revision 1.6 2004/06/06 17:18:04 bernie
66 * Remove redundant declaration of fifo_isempty_locked().
68 * Revision 1.5 2004/06/06 16:50:35 bernie
69 * Import fixes for race conditions from project_ks.
71 * Revision 1.4 2004/06/06 16:11:17 bernie
72 * Protect MetroWerks specific pragmas with #ifdef's
80 typedef struct FIFOBuffer
82 unsigned char * volatile head;
83 unsigned char * volatile tail;
90 * Check whether the fifo is empty
92 * \note Calling fifo_isempty() is safe while a concurrent
93 * execution context is calling fifo_push() or fifo_pop()
94 * only if the CPU can atomically update a pointer
95 * (which the AVR and other 8-bit processors can't do).
97 * \sa fifo_isempty_locked
99 INLINE bool fifo_isempty(const FIFOBuffer *fb)
101 return fb->head == fb->tail;
106 * Check whether the fifo is full
108 * \note Calling fifo_isfull() is safe while a concurrent
109 * execution context is calling fifo_pop() and the
110 * CPU can update a pointer atomically.
111 * It is NOT safe when the other context calls
113 * This limitation is not usually problematic in a
114 * consumer/producer scenario because the
115 * fifo_isfull() and fifo_push() are usually called
116 * in the producer context.
118 INLINE bool fifo_isfull(const FIFOBuffer *fb)
121 ((fb->head == fb->begin) && (fb->tail == fb->end))
122 || (fb->tail == fb->head - 1);
127 * Pop a character from the fifo buffer.
129 * \note Calling \c fifo_push() on a full buffer is undefined.
130 * The caller must make sure the buffer has at least
131 * one free slot before calling this function.
133 * \note It is safe to call fifo_pop() and fifo_push() from
134 * concurrent contexts, unless the CPU can't update
135 * a pointer atomically (which the AVR and other 8-bit
136 * processors can't do).
138 * \sa fifo_push_locked
140 INLINE void fifo_push(FIFOBuffer *fb, unsigned char c)
143 #pragma interrupt called
145 /* Write at tail position */
148 if (fb->tail == fb->end)
149 /* wrap tail around */
150 fb->tail = fb->begin;
152 /* Move tail forward */
158 * Pop a character from the fifo buffer.
160 * \note Calling \c fifo_pop() on an empty buffer is undefined.
161 * The caller must make sure the buffer contains at least
162 * one character before calling this function.
164 * \note It is safe to call fifo_pop() and fifo_push() from
165 * concurrent contexts.
167 INLINE unsigned char fifo_pop(FIFOBuffer *fb)
170 #pragma interrupt called
172 if (fb->head == fb->end)
174 /* wrap head around */
175 fb->head = fb->begin;
179 /* move head forward */
180 return *(fb->head++);
185 * Make the fifo empty, discarding all its current contents.
187 INLINE void fifo_flush(FIFOBuffer *fb)
195 /* No tricks needed on 16/32bit CPUs */
196 # define fifo_isempty_locked(fb) fifo_isempty((fb))
197 # define fifo_push_locked(fb, c) fifo_push((fb), (c))
198 # define fifo_flush_locked(fb) fifo_flush((fb))
203 * Similar to fifo_isempty(), but with stronger guarantees for
204 * concurrent access between user and interrupt code.
206 * \note This is actually only needed for 8-bit processors.
210 INLINE bool fifo_isempty_locked(const FIFOBuffer *fb)
215 DISABLE_IRQSAVE(flags);
216 result = fifo_isempty(fb);
217 ENABLE_IRQRESTORE(flags);
223 * Similar to fifo_push(), but with stronger guarantees for
224 * concurrent access between user and interrupt code.
226 * \note This is actually only needed for 8-bit processors.
230 INLINE void fifo_push_locked(FIFOBuffer *fb, unsigned char c)
233 DISABLE_IRQSAVE(flags);
235 ENABLE_IRQRESTORE(flags);
239 * Similar to fifo_flush(), but with stronger guarantees for
240 * concurrent access between user and interrupt code.
242 * \note This is actually only needed for 8-bit processors.
246 INLINE void fifo_flush_locked(FIFOBuffer *fb)
249 DISABLE_IRQSAVE(flags);
251 ENABLE_IRQRESTORE(flags);
254 #endif /* !CPU_AVR */
258 * Thread safe version of fifo_isfull()
260 INLINE bool fifo_isfull_locked(const FIFOBuffer *_fb)
265 DISABLE_IRQSAVE(_flags);
266 _result = fifo_isfull(_fb);
267 ENABLE_IRQRESTORE(_flags);
274 * FIFO Initialization.
276 INLINE void fifo_init(FIFOBuffer *fb, unsigned char *buf, size_t size)
278 fb->head = fb->tail = fb->begin = buf;
279 fb->end = buf + size - 1;
286 * UNTESTED: if uncommented, to be moved in fifobuf.c
288 void fifo_pushblock(FIFOBuffer *fb, unsigned char *block, size_t len)
292 /* Se c'e' spazio da tail alla fine del buffer */
293 if (fb->tail >= fb->head)
295 freelen = fb->end - fb->tail + 1;
297 /* C'e' abbastanza spazio per scrivere tutto il blocco? */
300 /* Scrivi quello che entra fino alla fine del buffer */
301 memcpy(fb->tail, block, freelen);
304 fb->tail = fb->begin;
308 /* Scrivi tutto il blocco */
309 memcpy(fb->tail, block, len);
317 while (!(freelen = fb->head - fb->tail - 1))
318 Delay(FIFO_POLLDELAY);
320 /* C'e' abbastanza spazio per scrivere tutto il blocco? */
323 /* Scrivi quello che entra fino alla fine del buffer */
324 memcpy(fb->tail, block, freelen);
331 /* Scrivi tutto il blocco */
332 memcpy(fb->tail, block, len);
340 #endif /* MWARE_FIFO_H */