Fix makefiles -- the auto-addition of -D switches to each LUFA compile time option...
[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 & PIPE_EPNUM_MASK) << 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 #if (USB_STREAM_TIMEOUT_MS < 0xFF)
74 uint8_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
75 #else
76 uint16_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
77 #endif
78
79 for (;;)
80 {
81 if (Pipe_GetPipeToken() == PIPE_TOKEN_IN)
82 {
83 if (Pipe_IsINReceived())
84 return PIPE_READYWAIT_NoError;
85 }
86 else
87 {
88 if (Pipe_IsOUTReady())
89 return PIPE_READYWAIT_NoError;
90 }
91
92 if (Pipe_IsStalled())
93 return PIPE_READYWAIT_PipeStalled;
94 else if (!(USB_IsConnected))
95 return PIPE_READYWAIT_DeviceDisconnected;
96
97 if (USB_INT_HasOccurred(USB_INT_HSOFI))
98 {
99 USB_INT_Clear(USB_INT_HSOFI);
100
101 if (!(TimeoutMSRem--))
102 return PIPE_READYWAIT_Timeout;
103 }
104 }
105 }
106
107 uint8_t Pipe_Write_Stream_LE(const void* Data, uint16_t Length
108 #if !defined(NO_STREAM_CALLBACKS)
109 , StreamCallbackPtr_t Callback
110 #endif
111 )
112 {
113 uint8_t* DataStream = (uint8_t*)Data;
114 uint8_t ErrorCode;
115
116 if ((ErrorCode = Pipe_WaitUntilReady()))
117 return ErrorCode;
118
119 while (Length)
120 {
121 if (!(Pipe_IsReadWriteAllowed()))
122 {
123 Pipe_ClearOUT();
124
125 #if !defined(NO_STREAM_CALLBACKS)
126 if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort))
127 return PIPE_RWSTREAM_CallbackAborted;
128 #endif
129
130 if ((ErrorCode = Pipe_WaitUntilReady()))
131 return ErrorCode;
132 }
133 else
134 {
135 Pipe_Write_Byte(*(DataStream++));
136 Length--;
137 }
138 }
139
140 return PIPE_RWSTREAM_NoError;
141 }
142
143 uint8_t Pipe_Write_Stream_BE(const void* Data, uint16_t Length
144 #if !defined(NO_STREAM_CALLBACKS)
145 , StreamCallbackPtr_t Callback
146 #endif
147 )
148 {
149 uint8_t* DataStream = (uint8_t*)(Data + Length - 1);
150 uint8_t ErrorCode;
151
152 if ((ErrorCode = Pipe_WaitUntilReady()))
153 return ErrorCode;
154
155 while (Length)
156 {
157 if (!(Pipe_IsReadWriteAllowed()))
158 {
159 Pipe_ClearOUT();
160
161 #if !defined(NO_STREAM_CALLBACKS)
162 if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort))
163 return PIPE_RWSTREAM_CallbackAborted;
164 #endif
165
166 if ((ErrorCode = Pipe_WaitUntilReady()))
167 return ErrorCode;
168 }
169 else
170 {
171 Pipe_Write_Byte(*(DataStream--));
172 Length--;
173 }
174 }
175
176 return PIPE_RWSTREAM_NoError;
177 }
178
179 uint8_t Pipe_Discard_Stream(uint16_t Length
180 #if !defined(NO_STREAM_CALLBACKS)
181 , StreamCallbackPtr_t Callback
182 #endif
183 )
184 {
185 uint8_t ErrorCode;
186
187 if ((ErrorCode = Pipe_WaitUntilReady()))
188 return ErrorCode;
189
190 while (Length)
191 {
192 if (!(Pipe_IsReadWriteAllowed()))
193 {
194 Pipe_ClearIN();
195
196 #if !defined(NO_STREAM_CALLBACKS)
197 if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort))
198 return PIPE_RWSTREAM_CallbackAborted;
199 #endif
200
201 if ((ErrorCode = Pipe_WaitUntilReady()))
202 return ErrorCode;
203 }
204 else
205 {
206 Pipe_Discard_Byte();
207 Length--;
208 }
209 }
210
211 return PIPE_RWSTREAM_NoError;
212 }
213
214 uint8_t Pipe_Read_Stream_LE(void* Buffer, uint16_t Length
215 #if !defined(NO_STREAM_CALLBACKS)
216 , StreamCallbackPtr_t Callback
217 #endif
218 )
219 {
220 uint8_t* DataStream = (uint8_t*)Buffer;
221 uint8_t ErrorCode;
222
223 if ((ErrorCode = Pipe_WaitUntilReady()))
224 return ErrorCode;
225
226 while (Length)
227 {
228 if (!(Pipe_IsReadWriteAllowed()))
229 {
230 Pipe_ClearIN();
231
232 #if !defined(NO_STREAM_CALLBACKS)
233 if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort))
234 return PIPE_RWSTREAM_CallbackAborted;
235 #endif
236
237 if ((ErrorCode = Pipe_WaitUntilReady()))
238 return ErrorCode;
239 }
240 else
241 {
242 *(DataStream++) = Pipe_Read_Byte();
243 Length--;
244 }
245 }
246
247 return PIPE_RWSTREAM_NoError;
248 }
249
250 uint8_t Pipe_Read_Stream_BE(void* Buffer, uint16_t Length
251 #if !defined(NO_STREAM_CALLBACKS)
252 , StreamCallbackPtr_t Callback
253 #endif
254 )
255 {
256 uint8_t* DataStream = (uint8_t*)(Buffer + Length - 1);
257 uint8_t ErrorCode;
258
259 if ((ErrorCode = Pipe_WaitUntilReady()))
260 return ErrorCode;
261
262 while (Length)
263 {
264 if (!(Pipe_IsReadWriteAllowed()))
265 {
266 Pipe_ClearIN();
267
268 #if !defined(NO_STREAM_CALLBACKS)
269 if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort))
270 return PIPE_RWSTREAM_CallbackAborted;
271 #endif
272
273 if ((ErrorCode = Pipe_WaitUntilReady()))
274 return ErrorCode;
275 }
276 else
277 {
278 *(DataStream--) = Pipe_Read_Byte();
279 Length--;
280 }
281 }
282
283 return PIPE_RWSTREAM_NoError;
284 }
285
286 #endif