Add missing header.
[bertos.git] / mware / fifobuf.h
1 /*!
2  * \file
3  * <!--
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.
7  * -->
8  *
9  * \version $Id$
10  *
11  * \author Bernardo Innocenti <bernie@develer.com>
12  *
13  * \brief General pourpose FIFO buffer implemented with a ring buffer
14  *
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
20  *     back to \c begin.
21  *
22  * \code
23  *
24  *  +-----------------------------------+
25  *  |  empty  |   valid data   |  empty |
26  *  +-----------------------------------+
27  *  ^         ^                ^        ^
28  *  begin    head             tail     end
29  *
30  * \endcode
31  *
32  * The buffer is EMPTY when \c head and \c tail point to the same location:
33  *              \code head == tail \endcode
34  *
35  * The buffer is FULL when \c tail points to the location immediately
36  * after \c head:
37  *              \code tail == head - 1 \endcode
38  *
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
42  */
43
44 /*#*
45  *#* $Log$
46  *#* Revision 1.19  2004/12/08 08:30:12  bernie
47  *#* Add missing header.
48  *#*
49  *#* Revision 1.18  2004/11/16 21:55:12  bernie
50  *#* Workaround for a known fifobuf bug.
51  *#*
52  *#* Revision 1.17  2004/09/14 20:57:00  bernie
53  *#* Use debug.h instead of kdebug.h.
54  *#*
55  *#* Revision 1.16  2004/09/06 21:39:08  bernie
56  *#* Simplify code using ATOMIC().
57  *#*
58  *#* Revision 1.15  2004/08/29 22:05:16  bernie
59  *#* Rename BITS_PER_PTR to CPU_BITS_PER_PTR.
60  *#*
61  *#* Revision 1.14  2004/08/25 14:12:09  rasky
62  *#* Aggiornato il comment block dei log RCS
63  *#*
64  *#* Revision 1.13  2004/08/24 13:16:11  bernie
65  *#* Add type-size definitions for preprocessor.
66  *#*
67  *#* Revision 1.12  2004/08/02 20:20:29  aleph
68  *#* Merge from project_ks
69  *#*
70  *#* Revision 1.11  2004/07/30 14:15:53  rasky
71  *#* Nuovo supporto unificato per detect della CPU
72  *#*
73  *#* Revision 1.10  2004/07/29 22:57:09  bernie
74  *#* Doxygen fix.
75  *#*
76  *#* Revision 1.9  2004/07/20 23:54:27  bernie
77  *#* fifo_flush_locked(): New function;
78  *#* Revamp documentation.
79  *#*
80  *#* Revision 1.8  2004/07/20 23:47:39  bernie
81  *#* Finally remove redundant protos.
82  *#*
83  *#* Revision 1.7  2004/07/20 23:46:29  bernie
84  *#* Finally remove redundant protos.
85  *#*
86  *#* Revision 1.6  2004/06/06 17:18:04  bernie
87  *#* Remove redundant declaration of fifo_isempty_locked().
88  *#*
89  *#* Revision 1.5  2004/06/06 16:50:35  bernie
90  *#* Import fixes for race conditions from project_ks.
91  *#*
92  *#* Revision 1.4  2004/06/06 16:11:17  bernie
93  *#* Protect MetroWerks specific pragmas with #ifdef's
94  *#*/
95
96 #ifndef MWARE_FIFO_H
97 #define MWARE_FIFO_H
98
99 #include <cpu.h>
100 #include <debug.h>
101
102 typedef struct FIFOBuffer
103 {
104         unsigned char * volatile head;
105         unsigned char * volatile tail;
106         unsigned char *begin;
107         unsigned char *end;
108 } FIFOBuffer;
109
110
111 #define ASSERT_VALID_FIFO(fifo) \
112         ATOMIC( \
113                 ASSERT((fifo)->head >= (fifo)->begin); \
114                 ASSERT((fifo)->head <= (fifo)->end); \
115                 ASSERT((fifo)->tail >= (fifo)->begin); \
116                 ASSERT((fifo)->tail <= (fifo)->end); \
117         )
118
119
120 /*!
121  * Check whether the fifo is empty
122  *
123  * \note Calling fifo_isempty() is safe while a concurrent
124  *       execution context is calling fifo_push() or fifo_pop()
125  *       only if the CPU can atomically update a pointer
126  *       (which the AVR and other 8-bit processors can't do).
127  *
128  * \sa fifo_isempty_locked
129  */
130 INLINE bool fifo_isempty(const FIFOBuffer *fb)
131 {
132         //ASSERT_VALID_FIFO(fb);
133         return fb->head == fb->tail;
134 }
135
136
137 /*!
138  * Check whether the fifo is full
139  *
140  * \note Calling fifo_isfull() is safe while a concurrent
141  *       execution context is calling fifo_pop() and the
142  *       CPU can update a pointer atomically.
143  *       It is NOT safe when the other context calls
144  *       fifo_push().
145  *       This limitation is not usually problematic in a
146  *       consumer/producer scenario because the
147  *       fifo_isfull() and fifo_push() are usually called
148  *       in the producer context.
149  */
150 INLINE bool fifo_isfull(const FIFOBuffer *fb)
151 {
152         //ASSERT_VALID_FIFO(fb);
153         return
154                 ((fb->head == fb->begin) && (fb->tail == fb->end))
155                 || (fb->tail == fb->head - 1);
156 }
157
158
159 /*!
160  * Pop a character from the fifo buffer.
161  *
162  * \note Calling \c fifo_push() on a full buffer is undefined.
163  *       The caller must make sure the buffer has at least
164  *       one free slot before calling this function.
165  *
166  * \note It is safe to call fifo_pop() and fifo_push() from
167  *       concurrent contexts, unless the CPU can't update
168  *       a pointer atomically (which the AVR and other 8-bit
169  *       processors can't do).
170  *
171  * \sa fifo_push_locked
172  */
173 INLINE void fifo_push(FIFOBuffer *fb, unsigned char c)
174 {
175 #ifdef __MWERKS__
176 #pragma interrupt called
177 #endif
178         //ASSERT_VALID_FIFO(fb);
179
180         /* Write at tail position */
181         *(fb->tail) = c;
182
183         if (UNLIKELY(fb->tail == fb->end))
184                 /* wrap tail around */
185                 fb->tail = fb->begin;
186         else
187                 /* Move tail forward */
188                 fb->tail++;
189 }
190
191
192 /*!
193  * Pop a character from the fifo buffer.
194  *
195  * \note Calling \c fifo_pop() on an empty buffer is undefined.
196  *       The caller must make sure the buffer contains at least
197  *       one character before calling this function.
198  *
199  * \note It is safe to call fifo_pop() and fifo_push() from
200  *       concurrent contexts.
201  */
202 INLINE unsigned char fifo_pop(FIFOBuffer *fb)
203 {
204 #ifdef __MWERKS__
205 #pragma interrupt called
206 #endif
207         //ASSERT_VALID_FIFO(fb);
208
209         if (UNLIKELY(fb->head == fb->end))
210         {
211                 /* wrap head around */
212                 fb->head = fb->begin;
213                 return *(fb->end);
214         }
215         else
216                 /* move head forward */
217                 return *(fb->head++);
218 }
219
220
221 /*!
222  * Make the fifo empty, discarding all its current contents.
223  */
224 INLINE void fifo_flush(FIFOBuffer *fb)
225 {
226         //ASSERT_VALID_FIFO(fb);
227         fb->head = fb->tail;
228 }
229
230
231 #if CPU_REG_BITS >= CPU_BITS_PER_PTR
232
233         /*
234          * 16/32bit CPUs that can update a pointer with a single write
235          * operation, no need to disable interrupts.
236          */
237         #define fifo_isempty_locked(fb) fifo_isempty((fb))
238         #define fifo_push_locked(fb, c) fifo_push((fb), (c))
239         #define fifo_pop_locked(fb)     fifo_pop((fb))
240         #define fifo_flush_locked(fb)   fifo_flush((fb))
241
242 #else /* CPU_REG_BITS < CPU_BITS_PER_PTR */
243
244         /*!
245          * Similar to fifo_isempty(), but with stronger guarantees for
246          * concurrent access between user and interrupt code.
247          *
248          * \note This is actually only needed for 8-bit processors.
249          *
250          * \sa fifo_isempty()
251          */
252         INLINE bool fifo_isempty_locked(const FIFOBuffer *fb)
253         {
254                 bool result;
255                 ATOMIC(result = fifo_isempty(fb));
256                 return result;
257         }
258
259
260         /*!
261          * Similar to fifo_push(), but with stronger guarantees for
262          * concurrent access between user and interrupt code.
263          *
264          * \note This is actually only needed for 8-bit processors.
265          *
266          * \sa fifo_push()
267          */
268         INLINE void fifo_push_locked(FIFOBuffer *fb, unsigned char c)
269         {
270                 ATOMIC(fifo_push(fb, c));
271         }
272
273         /* Probably not really needed, but hard to prove. */
274         INLINE unsigned char fifo_pop_locked(FIFOBuffer *fb)
275         {
276                 unsigned char c;
277                 ATOMIC(c = fifo_pop(fb));
278                 return c;
279         }
280
281         /*!
282          * Similar to fifo_flush(), but with stronger guarantees for
283          * concurrent access between user and interrupt code.
284          *
285          * \note This is actually only needed for 8-bit processors.
286          *
287          * \sa fifo_flush()
288          */
289         INLINE void fifo_flush_locked(FIFOBuffer *fb)
290         {
291                 ATOMIC(fifo_flush(fb));
292         }
293
294 #endif /* CPU_REG_BITS < BITS_PER_PTR */
295
296
297 /*!
298  * Thread safe version of fifo_isfull()
299  */
300 INLINE bool fifo_isfull_locked(const FIFOBuffer *_fb)
301 {
302         bool result;
303         ATOMIC(result = fifo_isfull(_fb));
304         return result;
305 }
306
307
308 /*!
309  * FIFO Initialization.
310  */
311 INLINE void fifo_init(FIFOBuffer *fb, unsigned char *buf, size_t size)
312 {
313         /* FIFO buffers have a known bug with 1-byte buffers. */
314         ASSERT(size > 1);
315
316         fb->head = fb->tail = fb->begin = buf;
317         fb->end = buf + size - 1;
318 }
319
320
321 #if 0
322
323 /*
324  * UNTESTED: if uncommented, to be moved in fifobuf.c
325  */
326 void fifo_pushblock(FIFOBuffer *fb, unsigned char *block, size_t len)
327 {
328         size_t freelen;
329
330         /* Se c'e' spazio da tail alla fine del buffer */
331         if (fb->tail >= fb->head)
332         {
333                 freelen = fb->end - fb->tail + 1;
334
335                 /* C'e' abbastanza spazio per scrivere tutto il blocco? */
336                 if (freelen < len)
337                 {
338                         /* Scrivi quello che entra fino alla fine del buffer */
339                         memcpy(fb->tail, block, freelen);
340                         block += freelen;
341                         len -= freelen;
342                         fb->tail = fb->begin;
343                 }
344                 else
345                 {
346                         /* Scrivi tutto il blocco */
347                         memcpy(fb->tail, block, len);
348                         fb->tail += len;
349                         return;
350                 }
351         }
352
353         for(;;)
354         {
355                 while (!(freelen = fb->head - fb->tail - 1))
356                         Delay(FIFO_POLLDELAY);
357
358                 /* C'e' abbastanza spazio per scrivere tutto il blocco? */
359                 if (freelen < len)
360                 {
361                         /* Scrivi quello che entra fino alla fine del buffer */
362                         memcpy(fb->tail, block, freelen);
363                         block += freelen;
364                         len -= freelen;
365                         fb->tail += freelen;
366                 }
367                 else
368                 {
369                         /* Scrivi tutto il blocco */
370                         memcpy(fb->tail, block, len);
371                         fb->tail += len;
372                         return;
373                 }
374         }
375 }
376 #endif
377
378 #endif /* MWARE_FIFO_H */
379