Change AVRISP-MKII and XPLAINBridge descriptors to indicate that the device is bus...
[pub/USBasp.git] / Projects / USBtoSerial / Lib / LightweightRingBuff.h
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
20
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
28 this software.
29 */
30
31 /** \file
32 *
33 * Ultra lightweight ring buffer, for fast insertion/deletion.
34 */
35
36 #ifndef _ULW_RING_BUFF_H_
37 #define _ULW_RING_BUFF_H_
38
39 /* Includes: */
40 #include <util/atomic.h>
41
42 #include <stdint.h>
43 #include <stdbool.h>
44
45 /* Defines: */
46 /** Size of each ring buffer, in data elements - must be between 1 and 255. */
47 #define BUFFER_SIZE 255
48
49 /** Type of data to store into the buffer. */
50 #define RingBuff_Data_t uint8_t
51
52 /* Type Defines: */
53 /** Type define for a new ring buffer object. Buffers should be initialized via a call to
54 * \ref RingBuffer_InitBuffer() before use.
55 */
56 typedef struct
57 {
58 RingBuff_Data_t Buffer[BUFFER_SIZE]; /**< Internal ring buffer data, referenced by the buffer pointers. */
59 RingBuff_Data_t* In; /**< Current storage location in the circular buffer */
60 RingBuff_Data_t* Out; /**< Current retrieval location in the circular buffer */
61 uint8_t Count; /**< Total number of bytes stored in the circular buffer */
62 } RingBuff_t;
63
64 /* Inline Functions: */
65 /** Initializes a ring buffer ready for use. Buffers must be initialized via this function
66 * before any operations are called upon them. Already initialized buffers may be reset
67 * by re-initializing them using this function.
68 *
69 * \param[out] Buffer Pointer to a ring buffer structure to initialize
70 */
71 static inline void RingBuffer_InitBuffer(RingBuff_t* const Buffer)
72 {
73 Buffer->In = Buffer->Buffer;
74 Buffer->Out = Buffer->Buffer;
75 Buffer->Count = 0;
76 }
77
78 /** Atomically inserts an element into the ring buffer.
79 *
80 * \param[in,out] Buffer Pointer to a ring buffer structure to insert into
81 * \param[in] Data Data element to insert into the buffer
82 */
83 static inline void RingBuffer_AtomicInsert(RingBuff_t* const Buffer, RingBuff_Data_t Data)
84 {
85 ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
86 {
87 *Buffer->In = Data;
88
89 if (++Buffer->In == &Buffer->Buffer[BUFFER_SIZE])
90 Buffer->In = Buffer->Buffer;
91
92 Buffer->Count++;
93 }
94 }
95
96 /** Atomically retrieves an element from the ring buffer.
97 *
98 * \param[in,out] Buffer Pointer to a ring buffer structure to retrieve from
99 *
100 * \return Next data element stored in the buffer
101 */
102 static inline RingBuff_Data_t RingBuffer_AtomicRemove(RingBuff_t* const Buffer)
103 {
104 RingBuff_Data_t Data;
105
106 ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
107 {
108 Data = *Buffer->Out;
109
110 if (++Buffer->Out == &Buffer->Buffer[BUFFER_SIZE])
111 Buffer->Out = Buffer->Buffer;
112
113 Buffer->Count--;
114 }
115
116 return Data;
117 }
118
119 /** Inserts an element into the ring buffer.
120 *
121 * \param[in,out] Buffer Pointer to a ring buffer structure to insert into
122 * \param[in] Data Data element to insert into the buffer
123 */
124 static inline void RingBuffer_Insert(RingBuff_t* const Buffer, RingBuff_Data_t Data)
125 {
126 *Buffer->In = Data;
127
128 if (++Buffer->In == &Buffer->Buffer[BUFFER_SIZE])
129 Buffer->In = Buffer->Buffer;
130
131 Buffer->Count++;
132 }
133
134 /** Retrieves an element from the ring buffer.
135 *
136 * \param[in,out] Buffer Pointer to a ring buffer structure to retrieve from
137 *
138 * \return Next data element stored in the buffer
139 */
140 static inline RingBuff_Data_t RingBuffer_Remove(RingBuff_t* const Buffer)
141 {
142 RingBuff_Data_t Data = *Buffer->Out;
143
144 if (++Buffer->Out == &Buffer->Buffer[BUFFER_SIZE])
145 Buffer->Out = Buffer->Buffer;
146
147 Buffer->Count--;
148
149 return Data;
150 }
151
152 #endif
153