annotate network/Lib/RingBuff.h @ 110:4eb5a746d7af avr-http

Import avrusbmodem code minus the USB bits. Not built yet.
author Matt Johnston <matt@ucc.asn.au>
date Sat, 15 Sep 2012 21:49:05 +0800
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
110
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 /*
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 LUFA Library
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 Copyright (C) Dean Camera, 2010.
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 dean [at] fourwalledcubicle [dot] com
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 www.fourwalledcubicle.com
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7 */
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 /*
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12 Permission to use, copy, modify, distribute, and sell this
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 software and its documentation for any purpose is hereby granted
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 without fee, provided that the above copyright notice appear in
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 all copies and that both that the copyright notice and this
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 permission notice and warranty disclaimer appear in supporting
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17 documentation, and that the name of the author not be used in
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18 advertising or publicity pertaining to distribution of the
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 software without specific, written prior permission.
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 The author disclaim all warranties with regard to this
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 software, including all implied warranties of merchantability
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 and fitness. In no event shall the author be liable for any
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 special, indirect or consequential damages or any damages
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 whatsoever resulting from loss of use, data or profits, whether
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 in an action of contract, negligence or other tortious action,
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 arising out of or in connection with the use or performance of
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 this software.
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 */
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 /* Buffer Configuration: */
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 /* Buffer length - select static size of created ring buffers: */
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 #define BUFF_STATICSIZE 128 // Set to the static ring buffer size for all ring buffers (place size after define)
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 /* Volatile mode - uncomment to make buffers volatile, for use in ISRs, etc: */
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 // #define BUFF_VOLATILE // Uncomment to cause all ring buffers to become volatile (and atomic if multi-byte) in access
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 /* Drop mode - select behaviour when Buffer_StoreElement called on a full buffer: */
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39 #define BUFF_DROPOLD // Uncomment to cause full ring buffers to drop the oldest character to make space when full
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40 // #define BUFF_DROPNEW // Uncomment to cause full ring buffers to drop the new character when full
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 // #define BUFF_NODROPCHECK // Uncomment to ignore full ring buffer checks - checking left to user!
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 /* Underflow behaviour - select behaviour when Buffer_GetElement is called with an empty ring buffer: */
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 //#define BUFF_EMPTYRETURNSZERO // Uncomment to return 0 when an empty ring buffer is read
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 #define BUFF_NOEMPTYCHECK // Uncomment to disable checking of empty ring buffers - checking left to user!
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 /* Buffer storage type - set the datatype for the stored data */
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 #define BUFF_DATATYPE uint8_t // Change to the data type that is going to be stored into the buffer
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50 /* Peek routine - uncomment to include the peek routine (fetches next byte without removing it from the buffer */
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 //#define BUFF_USEPEEK
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53 #ifndef _RINGBUFF_H_
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 #define _RINGBUFF_H_
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 /* Includes: */
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57 #include <avr/io.h>
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 #include <avr/interrupt.h>
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 #include <util/atomic.h>
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60 #include <limits.h>
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62 #include <LUFA/Common/Common.h>
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 /* Defines and checks: */
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65 #if defined(BUFF_STATICSIZE)
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
66 #define BUFF_LENGTH BUFF_STATICSIZE
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
67 #else
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
68 #error No buffer length specified!
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
69 #endif
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
70
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71 #if !(defined(BUFF_DROPOLD) || defined(BUFF_DROPNEW) || defined(BUFF_NODROPCHECK))
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72 #error No buffer drop mode specified.
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73 #endif
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75 #if !defined(BUFF_DATATYPE)
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
76 #error Ringbuffer storage data type not specified.
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
77 #endif
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
78
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
79 #if defined(BUFF_VOLATILE)
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
80 #define BUFF_MODE volatile
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
81 #define BUFF_ATOMIC_BLOCK ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82 #else
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
83 #define BUFF_MODE
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
84 #define BUFF_ATOMIC_BLOCK
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
85 #endif
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
86
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
87 #if (BUFF_STATICSIZE > LONG_MAX)
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
88 #define RingBuff_Elements_t uint64_t
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
89 #elif (BUFF_STATICSIZE > INT_MAX)
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
90 #define RingBuff_Elements_t uint32_t
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
91 #elif (BUFF_STATICSIZE > CHAR_MAX)
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
92 #define RingBuff_Elements_t uint16_t
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
93 #else
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
94 #define RingBuff_Elements_t uint8_t
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
95 #endif
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
96
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
97 /* Type Defines: */
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
98 typedef BUFF_DATATYPE RingBuff_Data_t;
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
99
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
100 typedef BUFF_MODE struct
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
101 {
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
102 RingBuff_Data_t Buffer[BUFF_LENGTH];
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
103 RingBuff_Data_t* InPtr;
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
104 RingBuff_Data_t* OutPtr;
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
105 RingBuff_Elements_t Elements;
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
106 } RingBuff_t;
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
107
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
108 /* Function Prototypes: */
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
109 void Buffer_Initialize(RingBuff_t* const Buff);
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
110 void Buffer_StoreElement(RingBuff_t* const Buffer,
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
111 RingBuff_Data_t Data);
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
112 RingBuff_Data_t Buffer_GetElement(RingBuff_t* const Buffer);
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
113 #if defined(BUFF_USEPEEK)
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
114 RingBuff_Data_t Buffer_PeekElement(const RingBuff_t* const Buffer);
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
115 #endif
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
116
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
117 #endif