43ea6f1d6dc6a767de3d4618d51136ca96361338
[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 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.
20  *
21  * \code
22  *
23  *  +-----------------------------------+
24  *  |  vuoto  |   dati validi  |  vuoto |
25  *  +-----------------------------------+
26  *  ^         ^                ^        ^
27  *  begin    head             tail     end
28  *
29  * \endcode
30  *
31  * Il buffer e' VUOTO quando head e tail coincidono:
32  *              \code head == tail \endcode
33  *
34  * Il buffer e' PIENO quando tail si trova immediatamente dietro a head:
35  *              \code tail == head - 1 \endcode
36  *
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
40  */
41
42 /*
43  * $Log$
44  * Revision 1.5  2004/06/06 16:50:35  bernie
45  * Import fixes for race conditions from project_ks.
46  *
47  * Revision 1.4  2004/06/06 16:11:17  bernie
48  * Protect MetroWerks specific pragmas with #ifdef's
49  *
50  * Revision 1.3  2004/06/03 15:04:10  aleph
51  * Merge improvements from project_ks (mainly inlining)
52  *
53  * Revision 1.2  2004/06/03 11:27:09  bernie
54  * Add dual-license information.
55  *
56  * Revision 1.1  2004/05/23 15:43:16  bernie
57  * Import mware modules.
58  *
59  * Revision 1.3  2004/05/22 17:55:58  rasky
60  * \samp non esiste in doxygen
61  *
62  * Revision 1.2  2004/04/27 11:13:29  rasky
63  * Spostate tutte le definizioni CPU-specific da compiler.h nel nuovo file cpu.h
64  *
65  * Revision 1.1  2004/04/21 17:38:25  rasky
66  * New application
67  *
68  * Revision 1.4  2004/03/24 15:37:03  bernie
69  * Remove Copyright messages from Doxygen output
70  *
71  * Revision 1.3  2004/03/18 18:11:07  bernie
72  * Add thread-safe FIFO handling macros
73  *
74  * Revision 1.2  2004/03/01 08:00:36  bernie
75  * Fixes for Doxygen
76  *
77  * Revision 1.1  2003/12/07 04:04:20  bernie
78  * Initial project_ks framework.
79  *
80  * Revision 1.1  2003/11/21 16:36:17  aleph
81  * Rename from fifo to fifobuf to avoid conflict with BSP fifo.h header
82  *
83  * Revision 1.1  2003/11/20 22:17:41  aleph
84  * Add fifo buffer used by serial
85  *
86  */
87
88 #ifndef MWARE_FIFO_H
89 #define MWARE_FIFO_H
90
91 #include "cpu.h"
92
93 typedef struct FIFOBuffer
94 {
95         unsigned char * volatile head;
96         unsigned char * volatile tail;
97         unsigned char *begin;
98         unsigned char *end;
99 } FIFOBuffer;
100
101
102 /* Public function prototypes */
103 INLINE bool fifo_isempty(const FIFOBuffer *fb);
104 INLINE bool fifo_isempty_locked(const FIFOBuffer *fb);
105 INLINE bool fifo_isfull(const FIFOBuffer *fb);
106 INLINE bool fifo_isfull_locked(const FIFOBuffer *fb);
107 #ifdef __MWERKS__
108 #pragma interrupt called
109 #endif
110 INLINE void fifo_push(FIFOBuffer *fb, unsigned char c);
111 #ifdef __MWERKS__
112 #pragma interrupt called
113 #endif
114 INLINE unsigned char fifo_pop(FIFOBuffer *fb);
115 INLINE void fifo_flush(FIFOBuffer *fb);
116 INLINE void fifo_init(FIFOBuffer *fb, unsigned char *buf, size_t size);
117
118
119 /*!
120  * Check whether the fifo is empty
121  *
122  * \note Calling fifo_isempty() is safe while a concurrent
123  *       execution context is calling fifo_push() or fifo_pop()
124  *       only if the CPU can atomically update a pointer
125  *       (which the AVR and other 8-bit processors can't do).
126  *
127  * \sa fifo_isempty_locked
128  */
129 INLINE bool fifo_isempty(const FIFOBuffer *fb)
130 {
131         return fb->head == fb->tail;
132 }
133
134
135 /*!
136  * Check whether the fifo is full
137  *
138  * \note Calling fifo_isfull() is safe while a concurrent
139  *       execution context is calling fifo_pop() and the
140  *       CPU can update a pointer atomically.
141  *       It is NOT safe when the other context calls
142  *       fifo_push().
143  *       This limitation is not usually problematic in a
144  *       consumer/producer scenario because the
145  *       fifo_isfull() and fifo_push() are usually called
146  *       in the producer context.
147  */
148 INLINE bool fifo_isfull(const FIFOBuffer *fb)
149 {
150         return
151                 ((fb->head == fb->begin) && (fb->tail == fb->end))
152                 || (fb->tail == fb->head - 1);
153 }
154
155
156 /*!
157  * Pop a character from the fifo buffer.
158  *
159  * \note Calling \c fifo_push() on a full buffer is undefined.
160  *       The caller must make sure the buffer has at least
161  *       one free slot before calling this function.
162  *
163  * \note It is safe to call fifo_pop() and fifo_push() from
164  *       concurrent contexts, unless the CPU can't update
165  *       a pointer atomically (which the AVR and other 8-bit
166  *       processors can't do).
167  *
168  * \sa fifo_push_locked
169  */
170 INLINE void fifo_push(FIFOBuffer *fb, unsigned char c)
171 {
172         /* Write at tail position */
173         *(fb->tail) = c;
174
175         if (fb->tail == fb->end)
176                 /* wrap tail around */
177                 fb->tail = fb->begin;
178         else
179                 /* Move tail forward */
180                 fb->tail++;
181 }
182
183
184 /*!
185  * Pop a character from the fifo buffer.
186  *
187  * \note Calling \c fifo_pop() on an empty buffer is undefined.
188  *       The caller must make sure the buffer contains at least
189  *       one character before calling this function.
190  *
191  * \note It is safe to call fifo_pop() and fifo_push() from
192  *       concurrent contexts.
193  */
194 INLINE unsigned char fifo_pop(FIFOBuffer *fb)
195 {
196         if (fb->head == fb->end)
197         {
198                 /* wrap head around */
199                 fb->head = fb->begin;
200                 return *(fb->end);
201         }
202         else
203                 /* move head forward */
204                 return *(fb->head++);
205 }
206
207
208 /*!
209  * Make the fifo empty, discarding all its current contents.
210  */
211 INLINE void fifo_flush(FIFOBuffer *fb)
212 {
213         fb->head = fb->tail;
214 }
215
216
217 #if !defined(__AVR__)
218
219         /* No tricks needed on 16/32bit CPUs */
220 #       define fifo_isempty_locked(fb) fifo_isempty((fb))
221 #       define fifo_push_locked(fb, c) fifo_push((fb), (c))
222
223 #else /* !__AVR__ */
224
225         /*!
226          * Similar to fifo_isempty(), but with stronger guarantees for
227          * concurrent access between user and interrupt code.
228          * This is actually only needed for 8-bit processors.
229          *
230          * \sa fifo_isempty()
231          */
232         INLINE bool fifo_isempty_locked(const FIFOBuffer *fb);
233         INLINE bool fifo_isempty_locked(const FIFOBuffer *fb)
234         {
235                 bool result;
236                 cpuflags_t flags;
237
238                 DISABLE_IRQSAVE(flags);
239                 result = fifo_isempty(fb);
240                 ENABLE_IRQRESTORE(flags);
241
242                 return result;
243         }
244
245         /*!
246          * Similar to fifo_push(), but with stronger guarantees for
247          * concurrent access between user and interrupt code.
248          * This is actually only needed for 8-bit processors.
249          *
250          * \sa fifo_push()
251          */
252         INLINE void fifo_push_locked(FIFOBuffer *fb, unsigned char c);
253         INLINE void fifo_push_locked(FIFOBuffer *fb, unsigned char c)
254         {
255                 cpuflags_t flags;
256                 DISABLE_IRQSAVE(flags);
257                 fifo_push(fb, c);
258                 ENABLE_IRQRESTORE(flags);
259         }
260
261 #endif /* !__AVR__ */
262
263
264 /*!
265  * Thread safe version of fifo_isfull()
266  */
267 INLINE bool fifo_isfull_locked(const FIFOBuffer *_fb)
268 {
269         bool _result;
270         cpuflags_t _flags;
271
272         DISABLE_IRQSAVE(_flags);
273         _result = fifo_isfull(_fb);
274         ENABLE_IRQRESTORE(_flags);
275
276         return _result;
277 }
278
279
280
281 /*!
282  * FIFO Initialization.
283  */
284 INLINE void fifo_init(FIFOBuffer *fb, unsigned char *buf, size_t size)
285 {
286         fb->head = fb->tail = fb->begin = buf;
287         fb->end = buf + size - 1;
288 }
289
290
291
292 #if 0
293
294 /*
295  * UNTESTED: if uncommented, to be moved in fifobuf.c
296  */
297 void fifo_pushblock(FIFOBuffer *fb, unsigned char *block, size_t len)
298 {
299         size_t freelen;
300
301         /* Se c'e' spazio da tail alla fine del buffer */
302         if (fb->tail >= fb->head)
303         {
304                 freelen = fb->end - fb->tail + 1;
305
306                 /* C'e' abbastanza spazio per scrivere tutto il blocco? */
307                 if (freelen < len)
308                 {
309                         /* Scrivi quello che entra fino alla fine del buffer */
310                         memcpy(fb->tail, block, freelen);
311                         block += freelen;
312                         len -= freelen;
313                         fb->tail = fb->begin;
314                 }
315                 else
316                 {
317                         /* Scrivi tutto il blocco */
318                         memcpy(fb->tail, block, len);
319                         fb->tail += len;
320                         return;
321                 }
322         }
323
324         for(;;)
325         {
326                 while (!(freelen = fb->head - fb->tail - 1))
327                         Delay(FIFO_POLLDELAY);
328
329                 /* C'e' abbastanza spazio per scrivere tutto il blocco? */
330                 if (freelen < len)
331                 {
332                         /* Scrivi quello che entra fino alla fine del buffer */
333                         memcpy(fb->tail, block, freelen);
334                         block += freelen;
335                         len -= freelen;
336                         fb->tail += freelen;
337                 }
338                 else
339                 {
340                         /* Scrivi tutto il blocco */
341                         memcpy(fb->tail, block, len);
342                         fb->tail += len;
343                         return;
344                 }
345         }
346 }
347 #endif
348
349 #endif /* MWARE_FIFO_H */
350