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 
<< PEPNUM0
)); 
  49         UPCFG1X 
= ((1 << ALLOC
) | Banks 
| Pipe_BytesToEPSizeMask(Size
)); 
  51         return Pipe_IsConfigured(); 
  54 void Pipe_ClearPipes(void) 
  58         for (uint8_t PNum 
= 0; PNum 
< PIPE_TOTAL_PIPES
; PNum
++) 
  61                 Pipe_SelectPipe(PNum
); 
  65                 Pipe_ClearErrorFlags(); 
  66                 Pipe_DeallocateMemory(); 
  71 uint8_t Pipe_WaitUntilReady(void) 
  73         uint16_t TimeoutMSRem 
= USB_STREAM_TIMEOUT_MS
; 
  75         USB_INT_Clear(USB_INT_HSOFI
); 
  77         while (!(Pipe_ReadWriteAllowed())) 
  80                   return PIPE_READYWAIT_PipeStalled
; 
  81                 else if (!(USB_IsConnected
)) 
  82                   return PIPE_READYWAIT_DeviceDisconnected
; 
  84                 if (USB_INT_HasOccurred(USB_INT_HSOFI
)) 
  86                         USB_INT_Clear(USB_INT_HSOFI
); 
  88                         if (!(TimeoutMSRem
--)) 
  89                           return PIPE_READYWAIT_Timeout
; 
  93         return PIPE_READYWAIT_NoError
; 
  96 uint8_t Pipe_Write_Stream_LE(const void* Data
, uint16_t Length
 
  97 #if !defined(NO_STREAM_CALLBACKS) 
  98                                  , uint8_t (* const Callback
)(void) 
 102         uint8_t* DataStream 
= (uint8_t*)Data
; 
 105         if ((ErrorCode 
= Pipe_WaitUntilReady())) 
 110                 if (!(Pipe_ReadWriteAllowed())) 
 112                         Pipe_ClearCurrentBank(); 
 114                         #if !defined(NO_STREAM_CALLBACKS) 
 115                         if ((Callback 
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
)) 
 116                           return PIPE_RWSTREAM_ERROR_CallbackAborted
; 
 119                         if ((ErrorCode 
= Pipe_WaitUntilReady())) 
 123                 Pipe_Write_Byte(*(DataStream
++)); 
 126         return PIPE_RWSTREAM_ERROR_NoError
; 
 129 uint8_t Pipe_Write_Stream_BE(const void* Data
, uint16_t Length
 
 130 #if !defined(NO_STREAM_CALLBACKS) 
 131                                  , uint8_t (* const Callback
)(void) 
 135         uint8_t* DataStream 
= (uint8_t*)(Data 
+ Length 
- 1); 
 138         if ((ErrorCode 
= Pipe_WaitUntilReady())) 
 143                 if (!(Pipe_ReadWriteAllowed())) 
 145                         Pipe_ClearCurrentBank(); 
 147                         #if !defined(NO_STREAM_CALLBACKS) 
 148                         if ((Callback 
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
)) 
 149                           return PIPE_RWSTREAM_ERROR_CallbackAborted
; 
 152                         if ((ErrorCode 
= Pipe_WaitUntilReady())) 
 156                 Pipe_Write_Byte(*(DataStream
--)); 
 159         return PIPE_RWSTREAM_ERROR_NoError
; 
 162 uint8_t Pipe_Discard_Stream(uint16_t Length
 
 163 #if !defined(NO_STREAM_CALLBACKS) 
 164                                  , uint8_t (* const Callback
)(void) 
 170         if ((ErrorCode 
= Pipe_WaitUntilReady())) 
 175                 if (!(Pipe_ReadWriteAllowed())) 
 177                         Pipe_ClearCurrentBank(); 
 179                         #if !defined(NO_STREAM_CALLBACKS) 
 180                         if ((Callback 
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
)) 
 181                           return PIPE_RWSTREAM_ERROR_CallbackAborted
; 
 184                         if ((ErrorCode 
= Pipe_WaitUntilReady())) 
 191         return PIPE_RWSTREAM_ERROR_NoError
; 
 194 uint8_t Pipe_Read_Stream_LE(void* Buffer
, uint16_t Length
 
 195 #if !defined(NO_STREAM_CALLBACKS) 
 196                                  , uint8_t (* const Callback
)(void) 
 200         uint8_t* DataStream 
= (uint8_t*)Buffer
; 
 203         if ((ErrorCode 
= Pipe_WaitUntilReady())) 
 208                 if (!(Pipe_ReadWriteAllowed())) 
 210                         Pipe_ClearCurrentBank(); 
 212                         #if !defined(NO_STREAM_CALLBACKS) 
 213                         if ((Callback 
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
)) 
 214                           return PIPE_RWSTREAM_ERROR_CallbackAborted
; 
 217                         if ((ErrorCode 
= Pipe_WaitUntilReady())) 
 221                 *(DataStream
++) = Pipe_Read_Byte(); 
 224         return PIPE_RWSTREAM_ERROR_NoError
; 
 227 uint8_t Pipe_Read_Stream_BE(void* Buffer
, uint16_t Length
 
 228 #if !defined(NO_STREAM_CALLBACKS) 
 229                                  , uint8_t (* const Callback
)(void) 
 233         uint8_t* DataStream 
= (uint8_t*)(Buffer 
+ Length 
- 1); 
 236         if ((ErrorCode 
= Pipe_WaitUntilReady())) 
 241                 if (!(Pipe_ReadWriteAllowed())) 
 243                         Pipe_ClearCurrentBank(); 
 245                         #if !defined(NO_STREAM_CALLBACKS) 
 246                         if ((Callback 
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
)) 
 247                           return PIPE_RWSTREAM_ERROR_CallbackAborted
; 
 250                         if ((ErrorCode 
= Pipe_WaitUntilReady())) 
 254                 *(DataStream
--) = Pipe_Read_Byte(); 
 257         return PIPE_RWSTREAM_ERROR_NoError
;