annotate network/Lib/RingBuff.c @ 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 #include "RingBuff.h"
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 void Buffer_Initialize(RingBuff_t* const Buffer)
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 BUFF_ATOMIC_BLOCK
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 {
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 Buffer->InPtr = (RingBuff_Data_t*)&Buffer->Buffer;
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 Buffer->OutPtr = (RingBuff_Data_t*)&Buffer->Buffer;
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39 Buffer->Elements = 0;
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40 }
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 }
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 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
44 RingBuff_Data_t Data)
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 {
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 BUFF_ATOMIC_BLOCK
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 {
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 #if defined(BUFF_DROPOLD)
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 if (Buffer->Elements == BUFF_LENGTH)
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50 {
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 Buffer->OutPtr++;
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 if (Buffer->OutPtr == &Buffer->Buffer[BUFF_LENGTH])
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 Buffer->OutPtr = (RingBuff_Data_t*)&Buffer->Buffer;
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 else
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57 {
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 Buffer->Elements++;
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 }
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60 #elif defined(BUFF_DROPNEW)
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61 if (Buffer->Elements == BUFF_LENGTH)
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62 return;
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 Buffer->Elements++;
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65 #elif defined(BUFF_NODROPCHECK)
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
66 Buffer->Elements++;
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
67 #endif
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
68
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
69 *(Buffer->InPtr) = Data;
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
70 Buffer->InPtr++;
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72 if (Buffer->InPtr == &Buffer->Buffer[BUFF_LENGTH])
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73 Buffer->InPtr = (RingBuff_Data_t*)&Buffer->Buffer;
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 }
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
76
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
77 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
78 {
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
79 RingBuff_Data_t BuffData;
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
80
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
81 BUFF_ATOMIC_BLOCK
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82 {
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
83 #if defined(BUFF_EMPTYRETURNSZERO)
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
84 if (!(Buffer->Elements))
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
85 return 0;
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
86 #elif !defined(BUFF_NOEMPTYCHECK)
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
87 #error No empty buffer check behavior specified.
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
88 #endif
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
89
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
90 BuffData = *(Buffer->OutPtr);
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
91
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
92 Buffer->OutPtr++;
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
93 Buffer->Elements--;
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
94
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
95 if (Buffer->OutPtr == &Buffer->Buffer[BUFF_LENGTH])
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
96 Buffer->OutPtr = (RingBuff_Data_t*)&Buffer->Buffer;
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
97 }
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
98
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
99 return BuffData;
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
100 }
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 #if defined(BUFF_USEPEEK)
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
103 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
104 {
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
105 RingBuff_Data_t BuffData;
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
106
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
107 BUFF_ATOMIC_BLOCK
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
108 {
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
109 #if defined(BUFF_EMPTYRETURNSZERO)
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
110 if (!(Buffer->Elements))
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
111 return 0;
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
112 #elif !defined(BUFF_NOEMPTYCHECK)
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
113 #error No empty buffer check behavior specified.
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
114 #endif
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
115
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
116 BuffData = *(Buffer->OutPtr);
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
117 }
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
118
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
119 return BuffData;
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
120 }
4eb5a746d7af Import avrusbmodem code minus the USB bits. Not built yet.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
121 #endif