Changed all Device mode LowLevel demos and Device Class drivers so that the control...
[pub/USBasp.git] / LUFA / Drivers / USB / LowLevel / Pipe.c
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 #define __INCLUDE_FROM_USB_DRIVER
32 #include "../HighLevel/USBMode.h"
33
34 #if defined(USB_CAN_BE_HOST)
35
36 #define __INCLUDE_FROM_PIPE_C
37 #include "Pipe.h"
38
39 uint8_t USB_ControlPipeSize = PIPE_CONTROLPIPE_DEFAULT_SIZE;
40
41 bool Pipe_ConfigurePipe(const uint8_t Number,
42 const uint8_t Type,
43 const uint8_t Token,
44 const uint8_t EndpointNumber,
45 const uint16_t Size,
46 const uint8_t Banks)
47 {
48 uint8_t UPCFG0XTemp[PIPE_TOTAL_PIPES];
49 uint8_t UPCFG1XTemp[PIPE_TOTAL_PIPES];
50
51 for (uint8_t PNum = 0; PNum < PIPE_TOTAL_PIPES; PNum++)
52 {
53 Pipe_SelectPipe(PNum);
54 UPCFG0XTemp[PNum] = UPCFG0X;
55 UPCFG1XTemp[PNum] = UPCFG1X;
56 }
57
58 UPCFG0XTemp[Number] = ((Type << EPTYPE0) | Token | ((EndpointNumber & PIPE_EPNUM_MASK) << PEPNUM0));
59 UPCFG1XTemp[Number] = ((1 << ALLOC) | Banks | Pipe_BytesToEPSizeMask(Size));
60
61 for (uint8_t PNum = 0; PNum < PIPE_TOTAL_PIPES; PNum++)
62 {
63 Pipe_SelectPipe(PNum);
64 UPIENX = 0;
65 UPINTX = 0;
66 UPCFG1X = 0;
67 Pipe_DisablePipe();
68 }
69
70 for (uint8_t PNum = 0; PNum < PIPE_TOTAL_PIPES; PNum++)
71 {
72 if (!(UPCFG1XTemp[PNum] & (1 << ALLOC)))
73 continue;
74
75 Pipe_SelectPipe(PNum);
76 Pipe_EnablePipe();
77
78 UPCFG0X = UPCFG0XTemp[PNum];
79 UPCFG1X = UPCFG1XTemp[PNum];
80
81 if (!(Pipe_IsConfigured()))
82 return false;
83 }
84
85 Pipe_SelectPipe(Number);
86 Pipe_SetInfiniteINRequests();
87
88 return true;
89 }
90
91 void Pipe_ClearPipes(void)
92 {
93 UPINT = 0;
94
95 for (uint8_t PNum = 0; PNum < PIPE_TOTAL_PIPES; PNum++)
96 {
97 Pipe_SelectPipe(PNum);
98 UPIENX = 0;
99 UPINTX = 0;
100 UPCFG1X = 0;
101 Pipe_DisablePipe();
102 }
103 }
104
105 bool Pipe_IsEndpointBound(const uint8_t EndpointAddress)
106 {
107 uint8_t PrevPipeNumber = Pipe_GetCurrentPipe();
108
109 for (uint8_t PNum = 0; PNum < PIPE_TOTAL_PIPES; PNum++)
110 {
111 Pipe_SelectPipe(PNum);
112
113 if (!(Pipe_IsConfigured()))
114 continue;
115
116 uint8_t PipeToken = Pipe_GetPipeToken();
117 bool PipeTokenCorrect = true;
118
119 if (PipeToken != PIPE_TOKEN_SETUP)
120 PipeTokenCorrect = (PipeToken == ((EndpointAddress & PIPE_EPDIR_MASK) ? PIPE_TOKEN_IN : PIPE_TOKEN_OUT));
121
122 if (PipeTokenCorrect && (Pipe_BoundEndpointNumber() == (EndpointAddress & PIPE_EPNUM_MASK)))
123 return true;
124 }
125
126 Pipe_SelectPipe(PrevPipeNumber);
127 return false;
128 }
129
130 uint8_t Pipe_WaitUntilReady(void)
131 {
132 #if (USB_STREAM_TIMEOUT_MS < 0xFF)
133 uint8_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
134 #else
135 uint16_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
136 #endif
137
138 for (;;)
139 {
140 if (Pipe_GetPipeToken() == PIPE_TOKEN_IN)
141 {
142 if (Pipe_IsINReceived())
143 return PIPE_READYWAIT_NoError;
144 }
145 else
146 {
147 if (Pipe_IsOUTReady())
148 return PIPE_READYWAIT_NoError;
149 }
150
151 if (Pipe_IsStalled())
152 return PIPE_READYWAIT_PipeStalled;
153 else if (USB_HostState == HOST_STATE_Unattached)
154 return PIPE_READYWAIT_DeviceDisconnected;
155
156 if (USB_INT_HasOccurred(USB_INT_HSOFI))
157 {
158 USB_INT_Clear(USB_INT_HSOFI);
159
160 if (!(TimeoutMSRem--))
161 return PIPE_READYWAIT_Timeout;
162 }
163 }
164 }
165
166 uint8_t Pipe_Discard_Stream(uint16_t Length
167 #if !defined(NO_STREAM_CALLBACKS)
168 , StreamCallbackPtr_t Callback
169 #endif
170 )
171 {
172 uint8_t ErrorCode;
173
174 Pipe_SetPipeToken(PIPE_TOKEN_IN);
175
176 if ((ErrorCode = Pipe_WaitUntilReady()))
177 return ErrorCode;
178
179 #if defined(FAST_STREAM_TRANSFERS)
180 uint8_t BytesRemToAlignment = (Pipe_BytesInPipe() & 0x07);
181
182 if (Length >= 8)
183 {
184 Length -= BytesRemToAlignment;
185
186 switch (BytesRemToAlignment)
187 {
188 default:
189 do
190 {
191 if (!(Pipe_IsReadWriteAllowed()))
192 {
193 Pipe_ClearIN();
194
195 #if !defined(NO_STREAM_CALLBACKS)
196 if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort))
197 return PIPE_RWSTREAM_CallbackAborted;
198 #endif
199
200 if ((ErrorCode = Pipe_WaitUntilReady()))
201 return ErrorCode;
202 }
203
204 Length -= 8;
205
206 Pipe_Discard_Byte();
207 case 7: Pipe_Discard_Byte();
208 case 6: Pipe_Discard_Byte();
209 case 5: Pipe_Discard_Byte();
210 case 4: Pipe_Discard_Byte();
211 case 3: Pipe_Discard_Byte();
212 case 2: Pipe_Discard_Byte();
213 case 1: Pipe_Discard_Byte();
214 } while (Length >= 8);
215 }
216 }
217 #endif
218
219 while (Length)
220 {
221 if (!(Pipe_IsReadWriteAllowed()))
222 {
223 Pipe_ClearIN();
224
225 #if !defined(NO_STREAM_CALLBACKS)
226 if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort))
227 return PIPE_RWSTREAM_CallbackAborted;
228 #endif
229
230 if ((ErrorCode = Pipe_WaitUntilReady()))
231 return ErrorCode;
232 }
233 else
234 {
235 Pipe_Discard_Byte();
236 Length--;
237 }
238 }
239
240 return PIPE_RWSTREAM_NoError;
241 }
242
243 /* The following abuses the C preprocessor in order to copy-past common code with slight alterations,
244 * so that the code needs to be written once. It is a crude form of templating to reduce code maintenance. */
245
246 #define TEMPLATE_FUNC_NAME Pipe_Write_Stream_LE
247 #define TEMPLATE_BUFFER_TYPE const void*
248 #define TEMPLATE_TOKEN PIPE_TOKEN_OUT
249 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearOUT()
250 #define TEMPLATE_BUFFER_OFFSET(Length) 0
251 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Pipe_Write_Byte(*((uint8_t*)BufferPtr++))
252 #include "Template/Template_Pipe_RW.c"
253
254 #define TEMPLATE_FUNC_NAME Pipe_Write_PStream_LE
255 #define TEMPLATE_BUFFER_TYPE const void*
256 #define TEMPLATE_TOKEN PIPE_TOKEN_OUT
257 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearOUT()
258 #define TEMPLATE_BUFFER_OFFSET(Length) 0
259 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Pipe_Write_Byte(pgm_read_byte((uint8_t*)BufferPtr++))
260 #include "Template/Template_Pipe_RW.c"
261
262 #define TEMPLATE_FUNC_NAME Pipe_Write_EStream_LE
263 #define TEMPLATE_BUFFER_TYPE const void*
264 #define TEMPLATE_TOKEN PIPE_TOKEN_OUT
265 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearOUT()
266 #define TEMPLATE_BUFFER_OFFSET(Length) 0
267 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Pipe_Write_Byte(eeprom_read_byte((uint8_t*)BufferPtr++))
268 #include "Template/Template_Pipe_RW.c"
269
270 #define TEMPLATE_FUNC_NAME Pipe_Write_Stream_BE
271 #define TEMPLATE_BUFFER_TYPE const void*
272 #define TEMPLATE_TOKEN PIPE_TOKEN_OUT
273 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearOUT()
274 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
275 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Pipe_Write_Byte(*((uint8_t*)BufferPtr--))
276 #include "Template/Template_Pipe_RW.c"
277
278 #define TEMPLATE_FUNC_NAME Pipe_Write_PStream_BE
279 #define TEMPLATE_BUFFER_TYPE const void*
280 #define TEMPLATE_TOKEN PIPE_TOKEN_OUT
281 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearOUT()
282 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
283 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Pipe_Write_Byte(pgm_read_byte((uint8_t*)BufferPtr--))
284 #include "Template/Template_Pipe_RW.c"
285
286 #define TEMPLATE_FUNC_NAME Pipe_Write_EStream_BE
287 #define TEMPLATE_BUFFER_TYPE const void*
288 #define TEMPLATE_TOKEN PIPE_TOKEN_OUT
289 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearOUT()
290 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
291 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Pipe_Write_Byte(eeprom_read_byte((uint8_t*)BufferPtr--))
292 #include "Template/Template_Pipe_RW.c"
293
294 #define TEMPLATE_FUNC_NAME Pipe_Read_Stream_LE
295 #define TEMPLATE_BUFFER_TYPE void*
296 #define TEMPLATE_TOKEN PIPE_TOKEN_IN
297 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearIN()
298 #define TEMPLATE_BUFFER_OFFSET(Length) 0
299 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) *((uint8_t*)BufferPtr++) = Pipe_Read_Byte()
300 #include "Template/Template_Pipe_RW.c"
301
302 #define TEMPLATE_FUNC_NAME Pipe_Read_EStream_LE
303 #define TEMPLATE_BUFFER_TYPE void*
304 #define TEMPLATE_TOKEN PIPE_TOKEN_IN
305 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearIN()
306 #define TEMPLATE_BUFFER_OFFSET(Length) 0
307 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) eeprom_update_byte((uint8_t*)BufferPtr++, Pipe_Read_Byte())
308 #include "Template/Template_Pipe_RW.c"
309
310 #define TEMPLATE_FUNC_NAME Pipe_Read_Stream_BE
311 #define TEMPLATE_BUFFER_TYPE void*
312 #define TEMPLATE_TOKEN PIPE_TOKEN_IN
313 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearIN()
314 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
315 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) *((uint8_t*)BufferPtr--) = Pipe_Read_Byte()
316 #include "Template/Template_Pipe_RW.c"
317
318 #define TEMPLATE_FUNC_NAME Pipe_Read_EStream_BE
319 #define TEMPLATE_BUFFER_TYPE void*
320 #define TEMPLATE_TOKEN PIPE_TOKEN_IN
321 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearIN()
322 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
323 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) eeprom_update_byte((uint8_t*)BufferPtr--, Pipe_Read_Byte())
324 #include "Template/Template_Pipe_RW.c"
325
326 #endif