Finally remove redundant protos.
[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.8  2004/07/20 23:47:39  bernie
45  * Finally remove redundant protos.
46  *
47  * Revision 1.7  2004/07/20 23:46:29  bernie
48  * Finally remove redundant protos.
49  *
50  * Revision 1.6  2004/06/06 17:18:04  bernie
51  * Remove redundant declaration of fifo_isempty_locked().
52  *
53  * Revision 1.5  2004/06/06 16:50:35  bernie
54  * Import fixes for race conditions from project_ks.
55  *
56  * Revision 1.4  2004/06/06 16:11:17  bernie
57  * Protect MetroWerks specific pragmas with #ifdef's
58  *
59  * Revision 1.3  2004/06/03 15:04:10  aleph
60  * Merge improvements from project_ks (mainly inlining)
61  *
62  * Revision 1.2  2004/06/03 11:27:09  bernie
63  * Add dual-license information.
64  *
65  * Revision 1.1  2004/05/23 15:43:16  bernie
66  * Import mware modules.
67  *
68  * Revision 1.3  2004/05/22 17:55:58  rasky
69  * \samp non esiste in doxygen
70  *
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
73  *
74  * Revision 1.1  2004/04/21 17:38:25  rasky
75  * New application
76  *
77  * Revision 1.4  2004/03/24 15:37:03  bernie
78  * Remove Copyright messages from Doxygen output
79  *
80  * Revision 1.3  2004/03/18 18:11:07  bernie
81  * Add thread-safe FIFO handling macros
82  *
83  * Revision 1.2  2004/03/01 08:00:36  bernie
84  * Fixes for Doxygen
85  *
86  * Revision 1.1  2003/12/07 04:04:20  bernie
87  * Initial project_ks framework.
88  *
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
91  *
92  * Revision 1.1  2003/11/20 22:17:41  aleph
93  * Add fifo buffer used by serial
94  *
95  */
96
97 #ifndef MWARE_FIFO_H
98 #define MWARE_FIFO_H
99
100 #include "cpu.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 /*!
112  * Check whether the fifo is empty
113  *
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).
118  *
119  * \sa fifo_isempty_locked
120  */
121 INLINE bool fifo_isempty(const FIFOBuffer *fb)
122 {
123         return fb->head == fb->tail;
124 }
125
126
127 /*!
128  * Check whether the fifo is full
129  *
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
134  *       fifo_push().
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.
139  */
140 INLINE bool fifo_isfull(const FIFOBuffer *fb)
141 {
142         return
143                 ((fb->head == fb->begin) && (fb->tail == fb->end))
144                 || (fb->tail == fb->head - 1);
145 }
146
147
148 /*!
149  * Pop a character from the fifo buffer.
150  *
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.
154  *
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).
159  *
160  * \sa fifo_push_locked
161  */
162 INLINE void fifo_push(FIFOBuffer *fb, unsigned char c)
163 {
164 #ifdef __MWERKS__
165 #pragma interrupt called
166 #endif
167         /* Write at tail position */
168         *(fb->tail) = c;
169
170         if (fb->tail == fb->end)
171                 /* wrap tail around */
172                 fb->tail = fb->begin;
173         else
174                 /* Move tail forward */
175                 fb->tail++;
176 }
177
178
179 /*!
180  * Pop a character from the fifo buffer.
181  *
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.
185  *
186  * \note It is safe to call fifo_pop() and fifo_push() from
187  *       concurrent contexts.
188  */
189 INLINE unsigned char fifo_pop(FIFOBuffer *fb)
190 {
191 #ifdef __MWERKS__
192 #pragma interrupt called
193 #endif
194         if (fb->head == fb->end)
195         {
196                 /* wrap head around */
197                 fb->head = fb->begin;
198                 return *(fb->end);
199         }
200         else
201                 /* move head forward */
202                 return *(fb->head++);
203 }
204
205
206 /*!
207  * Make the fifo empty, discarding all its current contents.
208  */
209 INLINE void fifo_flush(FIFOBuffer *fb)
210 {
211         fb->head = fb->tail;
212 }
213
214
215 #if !defined(__AVR__)
216
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))
220
221 #else /* !__AVR__ */
222
223         /*!
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.
227          *
228          * \sa fifo_isempty()
229          */
230         INLINE bool fifo_isempty_locked(const FIFOBuffer *fb)
231         {
232                 bool result;
233                 cpuflags_t flags;
234
235                 DISABLE_IRQSAVE(flags);
236                 result = fifo_isempty(fb);
237                 ENABLE_IRQRESTORE(flags);
238
239                 return result;
240         }
241
242         /*!
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.
246          *
247          * \sa fifo_push()
248          */
249         INLINE void fifo_push_locked(FIFOBuffer *fb, unsigned char c)
250         {
251                 cpuflags_t flags;
252                 DISABLE_IRQSAVE(flags);
253                 fifo_push(fb, c);
254                 ENABLE_IRQRESTORE(flags);
255         }
256
257 #endif /* !__AVR__ */
258
259
260 /*!
261  * Thread safe version of fifo_isfull()
262  */
263 INLINE bool fifo_isfull_locked(const FIFOBuffer *_fb)
264 {
265         bool _result;
266         cpuflags_t _flags;
267
268         DISABLE_IRQSAVE(_flags);
269         _result = fifo_isfull(_fb);
270         ENABLE_IRQRESTORE(_flags);
271
272         return _result;
273 }
274
275
276
277 /*!
278  * FIFO Initialization.
279  */
280 INLINE void fifo_init(FIFOBuffer *fb, unsigned char *buf, size_t size)
281 {
282         fb->head = fb->tail = fb->begin = buf;
283         fb->end = buf + size - 1;
284 }
285
286
287
288 #if 0
289
290 /*
291  * UNTESTED: if uncommented, to be moved in fifobuf.c
292  */
293 void fifo_pushblock(FIFOBuffer *fb, unsigned char *block, size_t len)
294 {
295         size_t freelen;
296
297         /* Se c'e' spazio da tail alla fine del buffer */
298         if (fb->tail >= fb->head)
299         {
300                 freelen = fb->end - fb->tail + 1;
301
302                 /* C'e' abbastanza spazio per scrivere tutto il blocco? */
303                 if (freelen < len)
304                 {
305                         /* Scrivi quello che entra fino alla fine del buffer */
306                         memcpy(fb->tail, block, freelen);
307                         block += freelen;
308                         len -= freelen;
309                         fb->tail = fb->begin;
310                 }
311                 else
312                 {
313                         /* Scrivi tutto il blocco */
314                         memcpy(fb->tail, block, len);
315                         fb->tail += len;
316                         return;
317                 }
318         }
319
320         for(;;)
321         {
322                 while (!(freelen = fb->head - fb->tail - 1))
323                         Delay(FIFO_POLLDELAY);
324
325                 /* C'e' abbastanza spazio per scrivere tutto il blocco? */
326                 if (freelen < len)
327                 {
328                         /* Scrivi quello che entra fino alla fine del buffer */
329                         memcpy(fb->tail, block, freelen);
330                         block += freelen;
331                         len -= freelen;
332                         fb->tail += freelen;
333                 }
334                 else
335                 {
336                         /* Scrivi tutto il blocco */
337                         memcpy(fb->tail, block, len);
338                         fb->tail += len;
339                         return;
340                 }
341         }
342 }
343 #endif
344
345 #endif /* MWARE_FIFO_H */
346