root/trunk/RingBuffAdv/RingBuff.h

Revision 16, 3.1 kB (checked in by charlie, 3 years ago)

Added serial communications (transmit only).

Line 
1 /*
2                             RING BUFFER LIBRARY
3                             By Dean Camera, 2007
4 */
5
6 // Configuration:
7
8    /* Buffer length - select static size of created ringbuffers: */
9          #define BUFF_STATICSIZE 64       // Set to the static ringbuffer size for all ringbuffers (place size after define)
10
11    /* Volatile mode - uncomment to make buffers volatile, for use in ISRs, etc: */
12          #define BUFF_VOLATILE            // Uncomment to cause all ring buffers to become volatile (and atomic if multi-byte) in access
13
14    /* Drop mode - select behaviour when Buffer_StoreElement called on a full buffer: */
15      #define BUFF_DROPOLD             // Uncomment to cause full ring buffers to drop the oldest character to make space when full
16      // #define BUFF_DROPNEW          // Uncomment to cause full ring buffers to drop the new character when full
17      // #define BUFF_NODROPCHECK      // Uncomment to ignore full ring buffer checks - checking left to user!
18
19    /* Underflow behaviour - select behaviour when Buffer_GetElement is called with an empty ringbuffer: */
20       #define BUFF_EMPTYRETURNSZERO   // Uncomment to return 0 when an empty ringbuffer is read
21      //#define BUFF_NOEMPTYCHECK      // Uncomment to disable checking of empty ringbuffers - checking left to user!
22         
23    /* Buffer storage type - set the datatype for the stored data */
24      #define BUFF_DATATYPE uint8_t    // Change to the data type that is going to be stored into the buffer
25         
26    /* Peek routine - uncomment to include the peek routine (fetches next byte without removing it from the buffer */
27      //#define BUFF_USEPEEK
28                 
29 #ifndef RINGBUFF_H
30 #define RINGBUFF_H
31
32         // Includes:
33         #include <avr/io.h>
34         #include <avr/interrupt.h>
35         #include <util/atomic.h>
36         #include <limits.h>
37
38         // Defines and Checks:
39 #if defined(BUFF_STATICSIZE)
40         #define BUFF_LENGTH BUFF_STATICSIZE
41 #else
42
43         #error No buffer length specified!
44 #endif
45
46
47 #if !(defined(BUFF_DROPOLD) || defined(BUFF_DROPNEW) || defined(BUFF_NODROPCHECK))
48         #error No buffer drop mode specified.
49 #endif
50
51
52 #if !defined(BUFF_DATATYPE)
53         #error Ringbuffer storage data type not specified.
54 #endif
55
56
57 #if defined(BUFF_VOLATILE)
58         #define BUFF_MODE volatile
59         #define BUFF_ATOMIC_BLOCK ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
60 #else
61
62         #define BUFF_MODE
63         #define BUFF_ATOMIC_BLOCK
64 #endif
65
66
67 #if (BUFF_STATICSIZE   > LONG_MAX)
68         #define RingBuff_Elements_t uint64_t
69 #elif (BUFF_STATICSIZE > INT_MAX)
70         #define RingBuff_Elements_t uint32_t
71 #elif (BUFF_STATICSIZE > CHAR_MAX)
72         #define RingBuff_Elements_t uint16_t
73 #else
74
75         #define RingBuff_Elements_t uint8_t
76 #endif
77
78        
79         // Type Defines:
80         typedef BUFF_DATATYPE RingBuff_Data_t;
81
82         typedef BUFF_MODE struct
83         {
84                 RingBuff_Data_t      Buffer[BUFF_LENGTH];
85                 RingBuff_Data_t*     InPtr;
86                 RingBuff_Data_t*     OutPtr;
87                 RingBuff_Elements_t  Elements;
88         } RingBuff_t;
89                
90         // Prototypes:
91         void            Buffer_Initialize(RingBuff_t* Buff);
92         void            Buffer_StoreElement(RingBuff_t* Buffer, RingBuff_Data_t Data);
93         RingBuff_Data_t Buffer_GetElement(RingBuff_t* Buffer);
94 #if defined(BUFF_USEPEEK)
95         RingBuff_Data_t Buffer_PeekElement(RingBuff_t* Buffer);
96 #endif
97
98        
99 #endif
100
Note: See TracBrowser for help on using the browser.