Fixed missing semicolon in the ATAVRUSBRF01 LED board driver code, changed LED board...
[pub/USBasp.git] / LUFA / Drivers / USB / LowLevel / Pipe.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, and distribute this software
13 and its documentation for any purpose and without fee is hereby
14 granted, provided that the above copyright notice appear in all
15 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 #include "../HighLevel/USBMode.h"
32
33 #if defined(USB_CAN_BE_HOST)
34
35 #define INCLUDE_FROM_PIPE_C
36 #include "Pipe.h"
37
38 uint8_t USB_ControlPipeSize = PIPE_CONTROLPIPE_DEFAULT_SIZE;
39
40 bool Pipe_ConfigurePipe(const uint8_t Number, const uint8_t Type, const uint8_t Token, const uint8_t EndpointNumber,
41 const uint16_t Size, const uint8_t Banks)
42 {
43 Pipe_SelectPipe(Number);
44 Pipe_EnablePipe();
45
46 UPCFG1X = 0;
47
48 UPCFG0X = ((Type << EPTYPE0) | Token | (EndpointNumber << PEPNUM0));
49 UPCFG1X = ((1 << ALLOC) | Banks | Pipe_BytesToEPSizeMask(Size));
50
51 return Pipe_IsConfigured();
52 }
53
54 void Pipe_ClearPipes(void)
55 {
56 UPINT = 0;
57
58 for (uint8_t PNum = 0; PNum < PIPE_TOTAL_PIPES; PNum++)
59 {
60 Pipe_ResetPipe(PNum);
61 Pipe_SelectPipe(PNum);
62 UPIENX = 0;
63 UPINTX = 0;
64 Pipe_ClearError();
65 Pipe_ClearErrorFlags();
66 Pipe_DeallocateMemory();
67 Pipe_DisablePipe();
68 }
69 }
70
71 uint8_t Pipe_WaitUntilReady(void)
72 {
73 uint16_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
74
75 USB_INT_Clear(USB_INT_HSOFI);
76
77 for (;;)
78 {
79 if (Pipe_GetPipeToken() == PIPE_TOKEN_IN)
80 {
81 if (Pipe_IsINReceived())
82 return PIPE_READYWAIT_NoError;
83 }
84 else
85 {
86 if (Pipe_IsOUTReady())
87 return PIPE_READYWAIT_NoError;
88 }
89
90 if (Pipe_IsStalled())
91 return PIPE_READYWAIT_PipeStalled;
92 else if (!(USB_IsConnected))
93 return PIPE_READYWAIT_DeviceDisconnected;
94
95 if (USB_INT_HasOccurred(USB_INT_HSOFI))
96 {
97 USB_INT_Clear(USB_INT_HSOFI);
98
99 if (!(TimeoutMSRem--))
100 return PIPE_READYWAIT_Timeout;
101 }
102 }
103 }
104
105 uint8_t Pipe_Write_Stream_LE(const void* Data, uint16_t Length
106 #if !defined(NO_STREAM_CALLBACKS)
107 , uint8_t (* const Callback)(void)
108 #endif
109 )
110 {
111 uint8_t* DataStream = (uint8_t*)Data;
112 uint8_t ErrorCode;
113
114 if ((ErrorCode = Pipe_WaitUntilReady()))
115 return ErrorCode;
116
117 while (Length)
118 {
119 if (!(Pipe_IsReadWriteAllowed()))
120 {
121 Pipe_ClearOUT();
122
123 #if !defined(NO_STREAM_CALLBACKS)
124 if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort))
125 return PIPE_RWSTREAM_CallbackAborted;
126 #endif
127
128 if ((ErrorCode = Pipe_WaitUntilReady()))
129 return ErrorCode;
130 }
131 else
132 {
133 Pipe_Write_Byte(*(DataStream++));
134 Length--;
135 }
136 }
137
138 return PIPE_RWSTREAM_NoError;
139 }
140
141 uint8_t Pipe_Write_Stream_BE(const void* Data, uint16_t Length
142 #if !defined(NO_STREAM_CALLBACKS)
143 , uint8_t (* const Callback)(void)
144 #endif
145 )
146 {
147 uint8_t* DataStream = (uint8_t*)(Data + Length - 1);
148 uint8_t ErrorCode;
149
150 if ((ErrorCode = Pipe_WaitUntilReady()))
151 return ErrorCode;
152
153 while (Length)
154 {
155 if (!(Pipe_IsReadWriteAllowed()))
156 {
157 Pipe_ClearOUT();
158
159 #if !defined(NO_STREAM_CALLBACKS)
160 if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort))
161 return PIPE_RWSTREAM_CallbackAborted;
162 #endif
163
164 if ((ErrorCode = Pipe_WaitUntilReady()))
165 return ErrorCode;
166 }
167 else
168 {
169 Pipe_Write_Byte(*(DataStream--));
170 Length--;
171 }
172 }
173
174 return PIPE_RWSTREAM_NoError;
175 }
176
177 uint8_t Pipe_Discard_Stream(uint16_t Length
178 #if !defined(NO_STREAM_CALLBACKS)
179 , uint8_t (* const Callback)(void)
180 #endif
181 )
182 {
183 uint8_t ErrorCode;
184
185 if ((ErrorCode = Pipe_WaitUntilReady()))
186 return ErrorCode;
187
188 while (Length)
189 {
190 if (!(Pipe_IsReadWriteAllowed()))
191 {
192 Pipe_ClearIN();
193
194 #if !defined(NO_STREAM_CALLBACKS)
195 if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort))
196 return PIPE_RWSTREAM_CallbackAborted;
197 #endif
198
199 if ((ErrorCode = Pipe_WaitUntilReady()))
200 return ErrorCode;
201 }
202 else
203 {
204 Pipe_Discard_Byte();
205 Length--;
206 }
207 }
208
209 return PIPE_RWSTREAM_NoError;
210 }
211
212 uint8_t Pipe_Read_Stream_LE(void* Buffer, uint16_t Length
213 #if !defined(NO_STREAM_CALLBACKS)
214 , uint8_t (* const Callback)(void)
215 #endif
216 )
217 {
218 uint8_t* DataStream = (uint8_t*)Buffer;
219 uint8_t ErrorCode;
220
221 if ((ErrorCode = Pipe_WaitUntilReady()))
222 return ErrorCode;
223
224 while (Length)
225 {
226 if (!(Pipe_IsReadWriteAllowed()))
227 {
228 Pipe_ClearIN();
229
230 #if !defined(NO_STREAM_CALLBACKS)
231 if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort))
232 return PIPE_RWSTREAM_CallbackAborted;
233 #endif
234
235 if ((ErrorCode = Pipe_WaitUntilReady()))
236 return ErrorCode;
237 }
238 else
239 {
240 *(DataStream++) = Pipe_Read_Byte();
241 Length--;
242 }
243 }
244
245 return PIPE_RWSTREAM_NoError;
246 }
247
248 uint8_t Pipe_Read_Stream_BE(void* Buffer, uint16_t Length
249 #if !defined(NO_STREAM_CALLBACKS)
250 , uint8_t (* const Callback)(void)
251 #endif
252 )
253 {
254 uint8_t* DataStream = (uint8_t*)(Buffer + Length - 1);
255 uint8_t ErrorCode;
256
257 if ((ErrorCode = Pipe_WaitUntilReady()))
258 return ErrorCode;
259
260 while (Length)
261 {
262 if (!(Pipe_IsReadWriteAllowed()))
263 {
264 Pipe_ClearIN();
265
266 #if !defined(NO_STREAM_CALLBACKS)
267 if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort))
268 return PIPE_RWSTREAM_CallbackAborted;
269 #endif
270
271 if ((ErrorCode = Pipe_WaitUntilReady()))
272 return ErrorCode;
273 }
274 else
275 {
276 *(DataStream--) = Pipe_Read_Byte();
277 Length--;
278 }
279 }
280
281 return PIPE_RWSTREAM_NoError;
282 }
283
284 #endif