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.11 2004/07/30 14:15:53 rasky
47 * Nuovo supporto unificato per detect della CPU
49 * Revision 1.10 2004/07/29 22:57:09 bernie
52 * Revision 1.9 2004/07/20 23:54:27 bernie
53 * fifo_flush_locked(): New function;
54 * Revamp documentation.
56 * Revision 1.8 2004/07/20 23:47:39 bernie
57 * Finally remove redundant protos.
59 * Revision 1.7 2004/07/20 23:46:29 bernie
60 * Finally remove redundant protos.
62 * Revision 1.6 2004/06/06 17:18:04 bernie
63 * Remove redundant declaration of fifo_isempty_locked().
65 * Revision 1.5 2004/06/06 16:50:35 bernie
66 * Import fixes for race conditions from project_ks.
68 * Revision 1.4 2004/06/06 16:11:17 bernie
69 * Protect MetroWerks specific pragmas with #ifdef's
77 typedef struct FIFOBuffer
79 unsigned char * volatile head;
80 unsigned char * volatile tail;
87 * Check whether the fifo is empty
89 * \note Calling fifo_isempty() is safe while a concurrent
90 * execution context is calling fifo_push() or fifo_pop()
91 * only if the CPU can atomically update a pointer
92 * (which the AVR and other 8-bit processors can't do).
94 * \sa fifo_isempty_locked
96 INLINE bool fifo_isempty(const FIFOBuffer *fb)
98 return fb->head == fb->tail;
103 * Check whether the fifo is full
105 * \note Calling fifo_isfull() is safe while a concurrent
106 * execution context is calling fifo_pop() and the
107 * CPU can update a pointer atomically.
108 * It is NOT safe when the other context calls
110 * This limitation is not usually problematic in a
111 * consumer/producer scenario because the
112 * fifo_isfull() and fifo_push() are usually called
113 * in the producer context.
115 INLINE bool fifo_isfull(const FIFOBuffer *fb)
118 ((fb->head == fb->begin) && (fb->tail == fb->end))
119 || (fb->tail == fb->head - 1);
124 * Pop a character from the fifo buffer.
126 * \note Calling \c fifo_push() on a full buffer is undefined.
127 * The caller must make sure the buffer has at least
128 * one free slot before calling this function.
130 * \note It is safe to call fifo_pop() and fifo_push() from
131 * concurrent contexts, unless the CPU can't update
132 * a pointer atomically (which the AVR and other 8-bit
133 * processors can't do).
135 * \sa fifo_push_locked
137 INLINE void fifo_push(FIFOBuffer *fb, unsigned char c)
140 #pragma interrupt called
142 /* Write at tail position */
145 if (fb->tail == fb->end)
146 /* wrap tail around */
147 fb->tail = fb->begin;
149 /* Move tail forward */
155 * Pop a character from the fifo buffer.
157 * \note Calling \c fifo_pop() on an empty buffer is undefined.
158 * The caller must make sure the buffer contains at least
159 * one character before calling this function.
161 * \note It is safe to call fifo_pop() and fifo_push() from
162 * concurrent contexts.
164 INLINE unsigned char fifo_pop(FIFOBuffer *fb)
167 #pragma interrupt called
169 if (fb->head == fb->end)
171 /* wrap head around */
172 fb->head = fb->begin;
176 /* move head forward */
177 return *(fb->head++);
182 * Make the fifo empty, discarding all its current contents.
184 INLINE void fifo_flush(FIFOBuffer *fb)
192 /* No tricks needed on 16/32bit CPUs */
193 # define fifo_isempty_locked(fb) fifo_isempty((fb))
194 # define fifo_push_locked(fb, c) fifo_push((fb), (c))
195 # define fifo_flush_locked(fb) fifo_flush((fb))
200 * Similar to fifo_isempty(), but with stronger guarantees for
201 * concurrent access between user and interrupt code.
203 * \note This is actually only needed for 8-bit processors.
207 INLINE bool fifo_isempty_locked(const FIFOBuffer *fb)
212 DISABLE_IRQSAVE(flags);
213 result = fifo_isempty(fb);
214 ENABLE_IRQRESTORE(flags);
220 * Similar to fifo_push(), but with stronger guarantees for
221 * concurrent access between user and interrupt code.
223 * \note This is actually only needed for 8-bit processors.
227 INLINE void fifo_push_locked(FIFOBuffer *fb, unsigned char c)
230 DISABLE_IRQSAVE(flags);
232 ENABLE_IRQRESTORE(flags);
236 * Similar to fifo_flush(), 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_flush_locked(FIFOBuffer *fb)
246 DISABLE_IRQSAVE(flags);
248 ENABLE_IRQRESTORE(flags);
251 #endif /* !__AVR__ */
255 * Thread safe version of fifo_isfull()
257 INLINE bool fifo_isfull_locked(const FIFOBuffer *_fb)
262 DISABLE_IRQSAVE(_flags);
263 _result = fifo_isfull(_fb);
264 ENABLE_IRQRESTORE(_flags);
271 * FIFO Initialization.
273 INLINE void fifo_init(FIFOBuffer *fb, unsigned char *buf, size_t size)
275 fb->head = fb->tail = fb->begin = buf;
276 fb->end = buf + size - 1;
283 * UNTESTED: if uncommented, to be moved in fifobuf.c
285 void fifo_pushblock(FIFOBuffer *fb, unsigned char *block, size_t len)
289 /* Se c'e' spazio da tail alla fine del buffer */
290 if (fb->tail >= fb->head)
292 freelen = fb->end - fb->tail + 1;
294 /* C'e' abbastanza spazio per scrivere tutto il blocco? */
297 /* Scrivi quello che entra fino alla fine del buffer */
298 memcpy(fb->tail, block, freelen);
301 fb->tail = fb->begin;
305 /* Scrivi tutto il blocco */
306 memcpy(fb->tail, block, len);
314 while (!(freelen = fb->head - fb->tail - 1))
315 Delay(FIFO_POLLDELAY);
317 /* C'e' abbastanza spazio per scrivere tutto il blocco? */
320 /* Scrivi quello che entra fino alla fine del buffer */
321 memcpy(fb->tail, block, freelen);
328 /* Scrivi tutto il blocco */
329 memcpy(fb->tail, block, len);
337 #endif /* MWARE_FIFO_H */