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 #if defined(USB_CAN_BE_HOST) 
  33 #define  INCLUDE_FROM_PIPE_C 
  36 uint8_t USB_ControlPipeSize 
= PIPE_CONTROLPIPE_DEFAULT_SIZE
; 
  38 bool Pipe_ConfigurePipe(const uint8_t  Number
, const uint8_t Type
, const uint8_t Token
, const uint8_t EndpointNumber
, 
  39                                                 const uint16_t Size
, const uint8_t Banks
) 
  41         Pipe_SelectPipe(Number
); 
  46         UPCFG0X 
= ((Type 
<< EPTYPE0
) | Token 
| (EndpointNumber 
<< PEPNUM0
)); 
  47         UPCFG1X 
= ((1 << ALLOC
) | Banks 
| Pipe_BytesToEPSizeMask(Size
)); 
  49         return Pipe_IsConfigured(); 
  52 void Pipe_ClearPipes(void) 
  56         for (uint8_t PNum 
= 0; PNum 
< PIPE_TOTAL_PIPES
; PNum
++) 
  59                 Pipe_SelectPipe(PNum
); 
  63                 Pipe_ClearErrorFlags(); 
  64                 Pipe_DeallocateMemory(); 
  69 uint8_t Pipe_WaitUntilReady(void) 
  71         uint8_t TimeoutMSRem 
= USB_STREAM_TIMEOUT_MS
; 
  73         USB_INT_Clear(USB_INT_HSOFI
); 
  75         while (!(Pipe_ReadWriteAllowed())) 
  78                   return PIPE_READYWAIT_PipeStalled
; 
  79                 else if (!(USB_IsConnected
)) 
  80                   return PIPE_READYWAIT_DeviceDisconnected
; 
  82                 if (USB_INT_HasOccurred(USB_INT_HSOFI
)) 
  84                         USB_INT_Clear(USB_INT_HSOFI
); 
  86                         if (!(TimeoutMSRem
--)) 
  87                           return PIPE_READYWAIT_Timeout
; 
  91         return PIPE_READYWAIT_NoError
; 
  94 uint8_t Pipe_Write_Stream_LE(const void* Data
, uint16_t Length
 
  95 #if !defined(NO_STREAM_CALLBACKS) 
  96                                  , uint8_t (* const Callback
)(void) 
 100         uint8_t* DataStream 
= (uint8_t*)Data
; 
 103         if ((ErrorCode 
= Pipe_WaitUntilReady())) 
 108                 if (!(Pipe_ReadWriteAllowed())) 
 110                         Pipe_ClearCurrentBank(); 
 112                         #if !defined(NO_STREAM_CALLBACKS) 
 113                         if ((Callback 
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
)) 
 114                           return PIPE_RWSTREAM_ERROR_CallbackAborted
; 
 117                         if ((ErrorCode 
= Pipe_WaitUntilReady())) 
 121                 Pipe_Write_Byte(*(DataStream
++)); 
 124         return PIPE_RWSTREAM_ERROR_NoError
; 
 127 uint8_t Pipe_Write_Stream_BE(const void* Data
, uint16_t Length
 
 128 #if !defined(NO_STREAM_CALLBACKS) 
 129                                  , uint8_t (* const Callback
)(void) 
 133         uint8_t* DataStream 
= (uint8_t*)(Data 
+ Length 
- 1); 
 136         if ((ErrorCode 
= Pipe_WaitUntilReady())) 
 141                 if (!(Pipe_ReadWriteAllowed())) 
 143                         Pipe_ClearCurrentBank(); 
 145                         #if !defined(NO_STREAM_CALLBACKS) 
 146                         if ((Callback 
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
)) 
 147                           return PIPE_RWSTREAM_ERROR_CallbackAborted
; 
 150                         if ((ErrorCode 
= Pipe_WaitUntilReady())) 
 154                 Pipe_Write_Byte(*(DataStream
--)); 
 157         return PIPE_RWSTREAM_ERROR_NoError
; 
 160 uint8_t Pipe_Discard_Stream(uint16_t Length
 
 161 #if !defined(NO_STREAM_CALLBACKS) 
 162                                  , uint8_t (* const Callback
)(void) 
 168         if ((ErrorCode 
= Pipe_WaitUntilReady())) 
 173                 if (!(Pipe_ReadWriteAllowed())) 
 175                         Pipe_ClearCurrentBank(); 
 177                         #if !defined(NO_STREAM_CALLBACKS) 
 178                         if ((Callback 
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
)) 
 179                           return PIPE_RWSTREAM_ERROR_CallbackAborted
; 
 182                         if ((ErrorCode 
= Pipe_WaitUntilReady())) 
 189         return PIPE_RWSTREAM_ERROR_NoError
; 
 192 uint8_t Pipe_Read_Stream_LE(void* Buffer
, uint16_t Length
 
 193                                                         #if !defined(NO_STREAM_CALLBACKS) 
 194                                  , uint8_t (* const Callback
)(void) 
 198         uint8_t* DataStream 
= (uint8_t*)Buffer
; 
 201         if ((ErrorCode 
= Pipe_WaitUntilReady())) 
 206                 if (!(Pipe_ReadWriteAllowed())) 
 208                         Pipe_ClearCurrentBank(); 
 210                         #if !defined(NO_STREAM_CALLBACKS) 
 211                         if ((Callback 
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
)) 
 212                           return PIPE_RWSTREAM_ERROR_CallbackAborted
; 
 215                         if ((ErrorCode 
= Pipe_WaitUntilReady())) 
 219                 *(DataStream
++) = Pipe_Read_Byte(); 
 222         return PIPE_RWSTREAM_ERROR_NoError
; 
 225 uint8_t Pipe_Read_Stream_BE(void* Buffer
, uint16_t Length
 
 226                                                         #if !defined(NO_STREAM_CALLBACKS) 
 227                                  , uint8_t (* const Callback
)(void) 
 231         uint8_t* DataStream 
= (uint8_t*)(Buffer 
+ Length 
- 1); 
 234         if ((ErrorCode 
= Pipe_WaitUntilReady())) 
 239                 if (!(Pipe_ReadWriteAllowed())) 
 241                         Pipe_ClearCurrentBank(); 
 243                         #if !defined(NO_STREAM_CALLBACKS) 
 244                         if ((Callback 
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
)) 
 245                           return PIPE_RWSTREAM_ERROR_CallbackAborted
; 
 248                         if ((ErrorCode 
= Pipe_WaitUntilReady())) 
 252                 *(DataStream
--) = Pipe_Read_Byte(); 
 255         return PIPE_RWSTREAM_ERROR_NoError
;