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.9 2004/07/20 23:54:27 bernie
47 * fifo_flush_locked(): New function;
48 * Revamp documentation.
50 * Revision 1.8 2004/07/20 23:47:39 bernie
51 * Finally remove redundant protos.
53 * Revision 1.7 2004/07/20 23:46:29 bernie
54 * Finally remove redundant protos.
56 * Revision 1.6 2004/06/06 17:18:04 bernie
57 * Remove redundant declaration of fifo_isempty_locked().
59 * Revision 1.5 2004/06/06 16:50:35 bernie
60 * Import fixes for race conditions from project_ks.
62 * Revision 1.4 2004/06/06 16:11:17 bernie
63 * Protect MetroWerks specific pragmas with #ifdef's
71 typedef struct FIFOBuffer
73 unsigned char * volatile head;
74 unsigned char * volatile tail;
81 * Check whether the fifo is empty
83 * \note Calling fifo_isempty() is safe while a concurrent
84 * execution context is calling fifo_push() or fifo_pop()
85 * only if the CPU can atomically update a pointer
86 * (which the AVR and other 8-bit processors can't do).
88 * \sa fifo_isempty_locked
90 INLINE bool fifo_isempty(const FIFOBuffer *fb)
92 return fb->head == fb->tail;
97 * Check whether the fifo is full
99 * \note Calling fifo_isfull() is safe while a concurrent
100 * execution context is calling fifo_pop() and the
101 * CPU can update a pointer atomically.
102 * It is NOT safe when the other context calls
104 * This limitation is not usually problematic in a
105 * consumer/producer scenario because the
106 * fifo_isfull() and fifo_push() are usually called
107 * in the producer context.
109 INLINE bool fifo_isfull(const FIFOBuffer *fb)
112 ((fb->head == fb->begin) && (fb->tail == fb->end))
113 || (fb->tail == fb->head - 1);
118 * Pop a character from the fifo buffer.
120 * \note Calling \c fifo_push() on a full buffer is undefined.
121 * The caller must make sure the buffer has at least
122 * one free slot before calling this function.
124 * \note It is safe to call fifo_pop() and fifo_push() from
125 * concurrent contexts, unless the CPU can't update
126 * a pointer atomically (which the AVR and other 8-bit
127 * processors can't do).
129 * \sa fifo_push_locked
131 INLINE void fifo_push(FIFOBuffer *fb, unsigned char c)
134 #pragma interrupt called
136 /* Write at tail position */
139 if (fb->tail == fb->end)
140 /* wrap tail around */
141 fb->tail = fb->begin;
143 /* Move tail forward */
149 * Pop a character from the fifo buffer.
151 * \note Calling \c fifo_pop() on an empty buffer is undefined.
152 * The caller must make sure the buffer contains at least
153 * one character before calling this function.
155 * \note It is safe to call fifo_pop() and fifo_push() from
156 * concurrent contexts.
158 INLINE unsigned char fifo_pop(FIFOBuffer *fb)
161 #pragma interrupt called
163 if (fb->head == fb->end)
165 /* wrap head around */
166 fb->head = fb->begin;
170 /* move head forward */
171 return *(fb->head++);
176 * Make the fifo empty, discarding all its current contents.
178 INLINE void fifo_flush(FIFOBuffer *fb)
184 #if !defined(__AVR__)
186 /* No tricks needed on 16/32bit CPUs */
187 # define fifo_isempty_locked(fb) fifo_isempty((fb))
188 # define fifo_push_locked(fb, c) fifo_push((fb), (c))
189 # define fifo_flush_locked(fb, c) fifo_flush((fb), (c))
194 * Similar to fifo_isempty(), but with stronger guarantees for
195 * concurrent access between user and interrupt code.
196 * This is actually only needed for 8-bit processors.
200 INLINE bool fifo_isempty_locked(const FIFOBuffer *fb)
205 DISABLE_IRQSAVE(flags);
206 result = fifo_isempty(fb);
207 ENABLE_IRQRESTORE(flags);
213 * Similar to fifo_push(), but with stronger guarantees for
214 * concurrent access between user and interrupt code.
216 * \note This is actually only needed for 8-bit processors.
220 INLINE void fifo_push_locked(FIFOBuffer *fb, unsigned char c)
223 DISABLE_IRQSAVE(flags);
225 ENABLE_IRQRESTORE(flags);
229 * Similar to fifo_flush(), but with stronger guarantees for
230 * concurrent access between user and interrupt code.
232 * \note This is actually only needed for 8-bit processors.
236 INLINE void fifo_flush_locked(FIFOBuffer *fb)
239 DISABLE_IRQSAVE(flags);
241 ENABLE_IRQRESTORE(flags);
244 #endif /* !__AVR__ */
248 * Thread safe version of fifo_isfull()
250 INLINE bool fifo_isfull_locked(const FIFOBuffer *_fb)
255 DISABLE_IRQSAVE(_flags);
256 _result = fifo_isfull(_fb);
257 ENABLE_IRQRESTORE(_flags);
264 * FIFO Initialization.
266 INLINE void fifo_init(FIFOBuffer *fb, unsigned char *buf, size_t size)
268 fb->head = fb->tail = fb->begin = buf;
269 fb->end = buf + size - 1;
276 * UNTESTED: if uncommented, to be moved in fifobuf.c
278 void fifo_pushblock(FIFOBuffer *fb, unsigned char *block, size_t len)
282 /* Se c'e' spazio da tail alla fine del buffer */
283 if (fb->tail >= fb->head)
285 freelen = fb->end - fb->tail + 1;
287 /* C'e' abbastanza spazio per scrivere tutto il blocco? */
290 /* Scrivi quello che entra fino alla fine del buffer */
291 memcpy(fb->tail, block, freelen);
294 fb->tail = fb->begin;
298 /* Scrivi tutto il blocco */
299 memcpy(fb->tail, block, len);
307 while (!(freelen = fb->head - fb->tail - 1))
308 Delay(FIFO_POLLDELAY);
310 /* C'e' abbastanza spazio per scrivere tutto il blocco? */
313 /* Scrivi quello che entra fino alla fine del buffer */
314 memcpy(fb->tail, block, freelen);
321 /* Scrivi tutto il blocco */
322 memcpy(fb->tail, block, len);
330 #endif /* MWARE_FIFO_H */