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 punta al primo elemento del buffer,
16 * \li \c end punta all'ultimo elemento,
17 * \li \c head punta al prossimo elemento che verra' estratto,
18 * \li \c tail punta alla posizione successiva all'ultimo elemento inserito.
19 * \li quando uno dei due puntatori raggiunge @c end, viene resettato a @c begin.
23 * +-----------------------------------+
24 * | vuoto | dati validi | vuoto |
25 * +-----------------------------------+
31 * Il buffer e' VUOTO quando head e tail coincidono:
32 * \code head == tail \endcode
34 * Il buffer e' PIENO quando tail si trova immediatamente dietro a head:
35 * \code tail == head - 1 \endcode
37 * Il buffer e' PIENO anche quando tail e' posizionato
38 * sull'ultima posizione del buffer e head sulla prima:
39 * \code head == begin && tail == end \endcode
44 * Revision 1.8 2004/07/20 23:47:39 bernie
45 * Finally remove redundant protos.
47 * Revision 1.7 2004/07/20 23:46:29 bernie
48 * Finally remove redundant protos.
50 * Revision 1.6 2004/06/06 17:18:04 bernie
51 * Remove redundant declaration of fifo_isempty_locked().
53 * Revision 1.5 2004/06/06 16:50:35 bernie
54 * Import fixes for race conditions from project_ks.
56 * Revision 1.4 2004/06/06 16:11:17 bernie
57 * Protect MetroWerks specific pragmas with #ifdef's
59 * Revision 1.3 2004/06/03 15:04:10 aleph
60 * Merge improvements from project_ks (mainly inlining)
62 * Revision 1.2 2004/06/03 11:27:09 bernie
63 * Add dual-license information.
65 * Revision 1.1 2004/05/23 15:43:16 bernie
66 * Import mware modules.
68 * Revision 1.3 2004/05/22 17:55:58 rasky
69 * \samp non esiste in doxygen
71 * Revision 1.2 2004/04/27 11:13:29 rasky
72 * Spostate tutte le definizioni CPU-specific da compiler.h nel nuovo file cpu.h
74 * Revision 1.1 2004/04/21 17:38:25 rasky
77 * Revision 1.4 2004/03/24 15:37:03 bernie
78 * Remove Copyright messages from Doxygen output
80 * Revision 1.3 2004/03/18 18:11:07 bernie
81 * Add thread-safe FIFO handling macros
83 * Revision 1.2 2004/03/01 08:00:36 bernie
86 * Revision 1.1 2003/12/07 04:04:20 bernie
87 * Initial project_ks framework.
89 * Revision 1.1 2003/11/21 16:36:17 aleph
90 * Rename from fifo to fifobuf to avoid conflict with BSP fifo.h header
92 * Revision 1.1 2003/11/20 22:17:41 aleph
93 * Add fifo buffer used by serial
102 typedef struct FIFOBuffer
104 unsigned char * volatile head;
105 unsigned char * volatile tail;
106 unsigned char *begin;
112 * Check whether the fifo is empty
114 * \note Calling fifo_isempty() is safe while a concurrent
115 * execution context is calling fifo_push() or fifo_pop()
116 * only if the CPU can atomically update a pointer
117 * (which the AVR and other 8-bit processors can't do).
119 * \sa fifo_isempty_locked
121 INLINE bool fifo_isempty(const FIFOBuffer *fb)
123 return fb->head == fb->tail;
128 * Check whether the fifo is full
130 * \note Calling fifo_isfull() is safe while a concurrent
131 * execution context is calling fifo_pop() and the
132 * CPU can update a pointer atomically.
133 * It is NOT safe when the other context calls
135 * This limitation is not usually problematic in a
136 * consumer/producer scenario because the
137 * fifo_isfull() and fifo_push() are usually called
138 * in the producer context.
140 INLINE bool fifo_isfull(const FIFOBuffer *fb)
143 ((fb->head == fb->begin) && (fb->tail == fb->end))
144 || (fb->tail == fb->head - 1);
149 * Pop a character from the fifo buffer.
151 * \note Calling \c fifo_push() on a full buffer is undefined.
152 * The caller must make sure the buffer has at least
153 * one free slot before calling this function.
155 * \note It is safe to call fifo_pop() and fifo_push() from
156 * concurrent contexts, unless the CPU can't update
157 * a pointer atomically (which the AVR and other 8-bit
158 * processors can't do).
160 * \sa fifo_push_locked
162 INLINE void fifo_push(FIFOBuffer *fb, unsigned char c)
165 #pragma interrupt called
167 /* Write at tail position */
170 if (fb->tail == fb->end)
171 /* wrap tail around */
172 fb->tail = fb->begin;
174 /* Move tail forward */
180 * Pop a character from the fifo buffer.
182 * \note Calling \c fifo_pop() on an empty buffer is undefined.
183 * The caller must make sure the buffer contains at least
184 * one character before calling this function.
186 * \note It is safe to call fifo_pop() and fifo_push() from
187 * concurrent contexts.
189 INLINE unsigned char fifo_pop(FIFOBuffer *fb)
192 #pragma interrupt called
194 if (fb->head == fb->end)
196 /* wrap head around */
197 fb->head = fb->begin;
201 /* move head forward */
202 return *(fb->head++);
207 * Make the fifo empty, discarding all its current contents.
209 INLINE void fifo_flush(FIFOBuffer *fb)
215 #if !defined(__AVR__)
217 /* No tricks needed on 16/32bit CPUs */
218 # define fifo_isempty_locked(fb) fifo_isempty((fb))
219 # define fifo_push_locked(fb, c) fifo_push((fb), (c))
224 * Similar to fifo_isempty(), but with stronger guarantees for
225 * concurrent access between user and interrupt code.
226 * This is actually only needed for 8-bit processors.
230 INLINE bool fifo_isempty_locked(const FIFOBuffer *fb)
235 DISABLE_IRQSAVE(flags);
236 result = fifo_isempty(fb);
237 ENABLE_IRQRESTORE(flags);
243 * Similar to fifo_push(), but with stronger guarantees for
244 * concurrent access between user and interrupt code.
245 * This is actually only needed for 8-bit processors.
249 INLINE void fifo_push_locked(FIFOBuffer *fb, unsigned char c)
252 DISABLE_IRQSAVE(flags);
254 ENABLE_IRQRESTORE(flags);
257 #endif /* !__AVR__ */
261 * Thread safe version of fifo_isfull()
263 INLINE bool fifo_isfull_locked(const FIFOBuffer *_fb)
268 DISABLE_IRQSAVE(_flags);
269 _result = fifo_isfull(_fb);
270 ENABLE_IRQRESTORE(_flags);
278 * FIFO Initialization.
280 INLINE void fifo_init(FIFOBuffer *fb, unsigned char *buf, size_t size)
282 fb->head = fb->tail = fb->begin = buf;
283 fb->end = buf + size - 1;
291 * UNTESTED: if uncommented, to be moved in fifobuf.c
293 void fifo_pushblock(FIFOBuffer *fb, unsigned char *block, size_t len)
297 /* Se c'e' spazio da tail alla fine del buffer */
298 if (fb->tail >= fb->head)
300 freelen = fb->end - fb->tail + 1;
302 /* C'e' abbastanza spazio per scrivere tutto il blocco? */
305 /* Scrivi quello che entra fino alla fine del buffer */
306 memcpy(fb->tail, block, freelen);
309 fb->tail = fb->begin;
313 /* Scrivi tutto il blocco */
314 memcpy(fb->tail, block, len);
322 while (!(freelen = fb->head - fb->tail - 1))
323 Delay(FIFO_POLLDELAY);
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);
336 /* Scrivi tutto il blocco */
337 memcpy(fb->tail, block, len);
345 #endif /* MWARE_FIFO_H */