3 Copyright (C) Dean Camera, 2009.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
21 The author disclaim all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
31 /* Buffer Configuration: */
32 /* Buffer length - select static size of created ring buffers: */
33 #define BUFF_STATICSIZE 128 // Set to the static ring buffer size for all ring buffers (place size after define)
35 /* Volatile mode - uncomment to make buffers volatile, for use in ISRs, etc: */
36 #define BUFF_VOLATILE // Uncomment to cause all ring buffers to become volatile (and atomic if multi-byte) in access
38 /* Drop mode - select behaviour when Buffer_StoreElement called on a full buffer: */
39 #define BUFF_DROPOLD // Uncomment to cause full ring buffers to drop the oldest character to make space when full
40 // #define BUFF_DROPNEW // Uncomment to cause full ring buffers to drop the new character when full
41 // #define BUFF_NODROPCHECK // Uncomment to ignore full ring buffer checks - checking left to user!
43 /* Underflow behaviour - select behaviour when Buffer_GetElement is called with an empty ring buffer: */
44 //#define BUFF_EMPTYRETURNSZERO // Uncomment to return 0 when an empty ring buffer is read
45 #define BUFF_NOEMPTYCHECK // Uncomment to disable checking of empty ring buffers - checking left to user!
47 /* Buffer storage type - set the datatype for the stored data */
48 #define BUFF_DATATYPE uint8_t // Change to the data type that is going to be stored into the buffer
50 /* Peek routine - uncomment to include the peek routine (fetches next byte without removing it from the buffer */
51 //#define BUFF_USEPEEK
58 #include <avr/interrupt.h>
59 #include <util/atomic.h>
62 #include <LUFA/Common/Common.h>
64 /* Defines and checks: */
65 #if defined(BUFF_STATICSIZE)
66 #define BUFF_LENGTH BUFF_STATICSIZE
68 #error No buffer length specified!
71 #if !(defined(BUFF_DROPOLD) || defined(BUFF_DROPNEW) || defined(BUFF_NODROPCHECK))
72 #error No buffer drop mode specified.
75 #if !defined(BUFF_DATATYPE)
76 #error Ringbuffer storage data type not specified.
79 #if defined(BUFF_VOLATILE)
80 #define BUFF_MODE volatile
81 #define BUFF_ATOMIC_BLOCK ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
84 #define BUFF_ATOMIC_BLOCK
87 #if (BUFF_STATICSIZE > LONG_MAX)
88 #define RingBuff_Elements_t uint64_t
89 #elif (BUFF_STATICSIZE > INT_MAX)
90 #define RingBuff_Elements_t uint32_t
91 #elif (BUFF_STATICSIZE > CHAR_MAX)
92 #define RingBuff_Elements_t uint16_t
94 #define RingBuff_Elements_t uint8_t
98 typedef BUFF_DATATYPE RingBuff_Data_t
;
100 typedef BUFF_MODE
struct
102 RingBuff_Data_t Buffer
[BUFF_LENGTH
];
103 RingBuff_Data_t
* InPtr
;
104 RingBuff_Data_t
* OutPtr
;
105 RingBuff_Elements_t Elements
;
108 /* Function Prototypes: */
109 void Buffer_Initialize(RingBuff_t
* const Buff
);
110 void Buffer_StoreElement(RingBuff_t
* const Buffer
, RingBuff_Data_t Data
);
111 RingBuff_Data_t
Buffer_GetElement(RingBuff_t
* const Buffer
);
112 #if defined(BUFF_USEPEEK)
113 RingBuff_Data_t
Buffer_PeekElement(const RingBuff_t
* const Buffer
);