3 Copyright (C) Dean Camera, 2009.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
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.
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
31 #include "../HighLevel/USBMode.h"
33 #if defined(USB_CAN_BE_HOST)
35 #define INCLUDE_FROM_PIPE_C
38 uint8_t USB_ControlPipeSize
= PIPE_CONTROLPIPE_DEFAULT_SIZE
;
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
)
43 Pipe_SelectPipe(Number
);
48 UPCFG0X
= ((Type
<< EPTYPE0
) | Token
| ((EndpointNumber
& PIPE_EPNUM_MASK
) << PEPNUM0
));
49 UPCFG1X
= ((1 << ALLOC
) | Banks
| Pipe_BytesToEPSizeMask(Size
));
51 Pipe_SetInfiniteINRequests();
53 return Pipe_IsConfigured();
56 void Pipe_ClearPipes(void)
60 for (uint8_t PNum
= 0; PNum
< PIPE_TOTAL_PIPES
; PNum
++)
63 Pipe_SelectPipe(PNum
);
67 Pipe_ClearErrorFlags();
68 Pipe_DeallocateMemory();
73 uint8_t Pipe_WaitUntilReady(void)
75 #if (USB_STREAM_TIMEOUT_MS < 0xFF)
76 uint8_t TimeoutMSRem
= USB_STREAM_TIMEOUT_MS
;
78 uint16_t TimeoutMSRem
= USB_STREAM_TIMEOUT_MS
;
83 if (Pipe_GetPipeToken() == PIPE_TOKEN_IN
)
85 if (Pipe_IsINReceived())
86 return PIPE_READYWAIT_NoError
;
90 if (Pipe_IsOUTReady())
91 return PIPE_READYWAIT_NoError
;
95 return PIPE_READYWAIT_PipeStalled
;
96 else if (!(USB_IsConnected
))
97 return PIPE_READYWAIT_DeviceDisconnected
;
99 if (USB_INT_HasOccurred(USB_INT_HSOFI
))
101 USB_INT_Clear(USB_INT_HSOFI
);
103 if (!(TimeoutMSRem
--))
104 return PIPE_READYWAIT_Timeout
;
109 uint8_t Pipe_Write_Stream_LE(const void* Data
, uint16_t Length
110 #if !defined(NO_STREAM_CALLBACKS)
111 , StreamCallbackPtr_t Callback
115 uint8_t* DataStream
= (uint8_t*)Data
;
118 Pipe_SetToken(PIPE_TOKEN_OUT
);
120 if ((ErrorCode
= Pipe_WaitUntilReady()))
123 #if defined(FAST_STREAM_TRANSFERS)
124 uint8_t BytesRemToAlignment
= (Pipe_BytesInPipe() & 0x07);
128 Length
-= BytesRemToAlignment
;
130 switch (BytesRemToAlignment
)
135 if (!(Pipe_IsReadWriteAllowed()))
139 #if !defined(NO_STREAM_CALLBACKS)
140 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
141 return PIPE_RWSTREAM_CallbackAborted
;
144 if ((ErrorCode
= Pipe_WaitUntilReady()))
150 Pipe_Write_Byte(*(DataStream
++));
151 case 7: Pipe_Write_Byte(*(DataStream
++));
152 case 6: Pipe_Write_Byte(*(DataStream
++));
153 case 5: Pipe_Write_Byte(*(DataStream
++));
154 case 4: Pipe_Write_Byte(*(DataStream
++));
155 case 3: Pipe_Write_Byte(*(DataStream
++));
156 case 2: Pipe_Write_Byte(*(DataStream
++));
157 case 1: Pipe_Write_Byte(*(DataStream
++));
158 } while (Length
>= 8);
165 if (!(Pipe_IsReadWriteAllowed()))
169 #if !defined(NO_STREAM_CALLBACKS)
170 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
171 return PIPE_RWSTREAM_CallbackAborted
;
174 if ((ErrorCode
= Pipe_WaitUntilReady()))
179 Pipe_Write_Byte(*(DataStream
++));
184 return PIPE_RWSTREAM_NoError
;
187 uint8_t Pipe_Write_Stream_BE(const void* Data
, uint16_t Length
188 #if !defined(NO_STREAM_CALLBACKS)
189 , StreamCallbackPtr_t Callback
193 uint8_t* DataStream
= (uint8_t*)(Data
+ Length
- 1);
196 Pipe_SetToken(PIPE_TOKEN_OUT
);
198 if ((ErrorCode
= Pipe_WaitUntilReady()))
201 #if defined(FAST_STREAM_TRANSFERS)
202 uint8_t BytesRemToAlignment
= (Pipe_BytesInPipe() & 0x07);
206 Length
-= BytesRemToAlignment
;
208 switch (BytesRemToAlignment
)
213 if (!(Pipe_IsReadWriteAllowed()))
217 #if !defined(NO_STREAM_CALLBACKS)
218 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
219 return PIPE_RWSTREAM_CallbackAborted
;
222 if ((ErrorCode
= Pipe_WaitUntilReady()))
228 Pipe_Write_Byte(*(DataStream
--));
229 case 7: Pipe_Write_Byte(*(DataStream
--));
230 case 6: Pipe_Write_Byte(*(DataStream
--));
231 case 5: Pipe_Write_Byte(*(DataStream
--));
232 case 4: Pipe_Write_Byte(*(DataStream
--));
233 case 3: Pipe_Write_Byte(*(DataStream
--));
234 case 2: Pipe_Write_Byte(*(DataStream
--));
235 case 1: Pipe_Write_Byte(*(DataStream
--));
236 } while (Length
>= 8);
243 if (!(Pipe_IsReadWriteAllowed()))
247 #if !defined(NO_STREAM_CALLBACKS)
248 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
249 return PIPE_RWSTREAM_CallbackAborted
;
252 if ((ErrorCode
= Pipe_WaitUntilReady()))
257 Pipe_Write_Byte(*(DataStream
--));
262 return PIPE_RWSTREAM_NoError
;
265 uint8_t Pipe_Discard_Stream(uint16_t Length
266 #if !defined(NO_STREAM_CALLBACKS)
267 , StreamCallbackPtr_t Callback
273 Pipe_SetToken(PIPE_TOKEN_IN
);
275 if ((ErrorCode
= Pipe_WaitUntilReady()))
278 #if defined(FAST_STREAM_TRANSFERS)
279 uint8_t BytesRemToAlignment
= (Pipe_BytesInPipe() & 0x07);
283 Length
-= BytesRemToAlignment
;
285 switch (BytesRemToAlignment
)
290 if (!(Pipe_IsReadWriteAllowed()))
294 #if !defined(NO_STREAM_CALLBACKS)
295 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
296 return PIPE_RWSTREAM_CallbackAborted
;
299 if ((ErrorCode
= Pipe_WaitUntilReady()))
306 case 7: Pipe_Discard_Byte();
307 case 6: Pipe_Discard_Byte();
308 case 5: Pipe_Discard_Byte();
309 case 4: Pipe_Discard_Byte();
310 case 3: Pipe_Discard_Byte();
311 case 2: Pipe_Discard_Byte();
312 case 1: Pipe_Discard_Byte();
313 } while (Length
>= 8);
320 if (!(Pipe_IsReadWriteAllowed()))
324 #if !defined(NO_STREAM_CALLBACKS)
325 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
326 return PIPE_RWSTREAM_CallbackAborted
;
329 if ((ErrorCode
= Pipe_WaitUntilReady()))
339 return PIPE_RWSTREAM_NoError
;
342 uint8_t Pipe_Read_Stream_LE(void* Buffer
, uint16_t Length
343 #if !defined(NO_STREAM_CALLBACKS)
344 , StreamCallbackPtr_t Callback
348 uint8_t* DataStream
= (uint8_t*)Buffer
;
351 Pipe_SetToken(PIPE_TOKEN_IN
);
353 if ((ErrorCode
= Pipe_WaitUntilReady()))
356 #if defined(FAST_STREAM_TRANSFERS)
357 uint8_t BytesRemToAlignment
= (Pipe_BytesInPipe() & 0x07);
361 Length
-= BytesRemToAlignment
;
363 switch (BytesRemToAlignment
)
368 if (!(Pipe_IsReadWriteAllowed()))
372 #if !defined(NO_STREAM_CALLBACKS)
373 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
374 return PIPE_RWSTREAM_CallbackAborted
;
377 if ((ErrorCode
= Pipe_WaitUntilReady()))
383 *(DataStream
++) = Pipe_Read_Byte();
384 case 7: *(DataStream
++) = Pipe_Read_Byte();
385 case 6: *(DataStream
++) = Pipe_Read_Byte();
386 case 5: *(DataStream
++) = Pipe_Read_Byte();
387 case 4: *(DataStream
++) = Pipe_Read_Byte();
388 case 3: *(DataStream
++) = Pipe_Read_Byte();
389 case 2: *(DataStream
++) = Pipe_Read_Byte();
390 case 1: *(DataStream
++) = Pipe_Read_Byte();
391 } while (Length
>= 8);
398 if (!(Pipe_IsReadWriteAllowed()))
402 #if !defined(NO_STREAM_CALLBACKS)
403 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
404 return PIPE_RWSTREAM_CallbackAborted
;
407 if ((ErrorCode
= Pipe_WaitUntilReady()))
412 *(DataStream
++) = Pipe_Read_Byte();
417 return PIPE_RWSTREAM_NoError
;
420 uint8_t Pipe_Read_Stream_BE(void* Buffer
, uint16_t Length
421 #if !defined(NO_STREAM_CALLBACKS)
422 , StreamCallbackPtr_t Callback
426 uint8_t* DataStream
= (uint8_t*)(Buffer
+ Length
- 1);
429 Pipe_SetToken(PIPE_TOKEN_IN
);
431 if ((ErrorCode
= Pipe_WaitUntilReady()))
434 #if defined(FAST_STREAM_TRANSFERS)
435 uint8_t BytesRemToAlignment
= (Pipe_BytesInPipe() & 0x07);
439 Length
-= BytesRemToAlignment
;
441 switch (BytesRemToAlignment
)
446 if (!(Pipe_IsReadWriteAllowed()))
450 #if !defined(NO_STREAM_CALLBACKS)
451 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
452 return PIPE_RWSTREAM_CallbackAborted
;
455 if ((ErrorCode
= Pipe_WaitUntilReady()))
461 *(DataStream
--) = Pipe_Read_Byte();
462 case 7: *(DataStream
--) = Pipe_Read_Byte();
463 case 6: *(DataStream
--) = Pipe_Read_Byte();
464 case 5: *(DataStream
--) = Pipe_Read_Byte();
465 case 4: *(DataStream
--) = Pipe_Read_Byte();
466 case 3: *(DataStream
--) = Pipe_Read_Byte();
467 case 2: *(DataStream
--) = Pipe_Read_Byte();
468 case 1: *(DataStream
--) = Pipe_Read_Byte();
469 } while (Length
>= 8);
476 if (!(Pipe_IsReadWriteAllowed()))
480 #if !defined(NO_STREAM_CALLBACKS)
481 if ((Callback
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
))
482 return PIPE_RWSTREAM_CallbackAborted
;
485 if ((ErrorCode
= Pipe_WaitUntilReady()))
490 *(DataStream
--) = Pipe_Read_Byte();
495 return PIPE_RWSTREAM_NoError
;