3      Copyright (C) Dean Camera, 2010. 
   5   dean [at] fourwalledcubicle [dot] com 
   6       www.fourwalledcubicle.com 
  10   Copyright 2010  Dean Camera (dean [at] fourwalledcubicle [dot] com) 
  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. 
  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 #define  __INCLUDE_FROM_USB_DRIVER 
  32 #include "../HighLevel/USBMode.h" 
  34 #if defined(USB_CAN_BE_HOST) 
  36 #define  __INCLUDE_FROM_PIPE_C 
  39 uint8_t USB_ControlPipeSize 
= PIPE_CONTROLPIPE_DEFAULT_SIZE
; 
  41 bool Pipe_ConfigurePipe(const uint8_t Number
, const uint8_t Type
, const uint8_t Token
, const uint8_t EndpointNumber
, 
  42                                                 const uint16_t Size
, const uint8_t Banks
) 
  44         Pipe_SelectPipe(Number
); 
  49         UPCFG0X 
= ((Type 
<< EPTYPE0
) | Token 
| ((EndpointNumber 
& PIPE_EPNUM_MASK
) << PEPNUM0
)); 
  50         UPCFG1X 
= ((1 << ALLOC
) | Banks 
| Pipe_BytesToEPSizeMask(Size
)); 
  52         Pipe_SetInfiniteINRequests(); 
  54         return Pipe_IsConfigured(); 
  57 void Pipe_ClearPipes(void) 
  61         for (uint8_t PNum 
= 0; PNum 
< PIPE_TOTAL_PIPES
; PNum
++) 
  63                 Pipe_SelectPipe(PNum
); 
  71 bool Pipe_IsEndpointBound(const uint8_t EndpointAddress
) 
  73         uint8_t PrevPipeNumber 
= Pipe_GetCurrentPipe(); 
  75         for (uint8_t PNum 
= 0; PNum 
< PIPE_TOTAL_PIPES
; PNum
++) 
  77                 Pipe_SelectPipe(PNum
); 
  79                 if (!(Pipe_IsConfigured())) 
  82                 uint8_t PipeToken        
= Pipe_GetPipeToken(); 
  83                 bool    PipeTokenCorrect 
= true; 
  85                 if (PipeToken 
!= PIPE_TOKEN_SETUP
) 
  86                   PipeTokenCorrect 
= (PipeToken 
== ((EndpointAddress 
& PIPE_EPDIR_MASK
) ? PIPE_TOKEN_IN 
: PIPE_TOKEN_OUT
)); 
  88                 if (PipeTokenCorrect 
&& (Pipe_BoundEndpointNumber() == (EndpointAddress 
& PIPE_EPNUM_MASK
))) 
  92         Pipe_SelectPipe(PrevPipeNumber
); 
  96 uint8_t Pipe_WaitUntilReady(void) 
  98         #if (USB_STREAM_TIMEOUT_MS < 0xFF) 
  99         uint8_t  TimeoutMSRem 
= USB_STREAM_TIMEOUT_MS
;   
 101         uint16_t TimeoutMSRem 
= USB_STREAM_TIMEOUT_MS
; 
 106                 if (Pipe_GetPipeToken() == PIPE_TOKEN_IN
) 
 108                         if (Pipe_IsINReceived()) 
 109                           return PIPE_READYWAIT_NoError
; 
 113                         if (Pipe_IsOUTReady()) 
 114                           return PIPE_READYWAIT_NoError
;                 
 117                 if (Pipe_IsStalled()) 
 118                   return PIPE_READYWAIT_PipeStalled
; 
 119                 else if (USB_HostState 
== HOST_STATE_Unattached
) 
 120                   return PIPE_READYWAIT_DeviceDisconnected
; 
 122                 if (USB_INT_HasOccurred(USB_INT_HSOFI
)) 
 124                         USB_INT_Clear(USB_INT_HSOFI
); 
 126                         if (!(TimeoutMSRem
--)) 
 127                           return PIPE_READYWAIT_Timeout
; 
 132 uint8_t Pipe_Discard_Stream(uint16_t Length
 
 133 #if !defined(NO_STREAM_CALLBACKS) 
 134                             , StreamCallbackPtr_t Callback
 
 140         Pipe_SetPipeToken(PIPE_TOKEN_IN
); 
 142         if ((ErrorCode 
= Pipe_WaitUntilReady())) 
 145         #if defined(FAST_STREAM_TRANSFERS) 
 146         uint8_t BytesRemToAlignment 
= (Pipe_BytesInPipe() & 0x07); 
 150                 Length 
-= BytesRemToAlignment
; 
 152                 switch (BytesRemToAlignment
) 
 157                                         if (!(Pipe_IsReadWriteAllowed())) 
 161                                                 #if !defined(NO_STREAM_CALLBACKS) 
 162                                                 if ((Callback 
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
)) 
 163                                                   return PIPE_RWSTREAM_CallbackAborted
; 
 166                                                 if ((ErrorCode 
= Pipe_WaitUntilReady())) 
 173                         case 7: Pipe_Discard_Byte(); 
 174                         case 6: Pipe_Discard_Byte(); 
 175                         case 5: Pipe_Discard_Byte(); 
 176                         case 4: Pipe_Discard_Byte(); 
 177                         case 3: Pipe_Discard_Byte(); 
 178                         case 2: Pipe_Discard_Byte(); 
 179                         case 1: Pipe_Discard_Byte(); 
 180                                 } while (Length 
>= 8);   
 187                 if (!(Pipe_IsReadWriteAllowed())) 
 191                         #if !defined(NO_STREAM_CALLBACKS) 
 192                         if ((Callback 
!= NULL
) && (Callback() == STREAMCALLBACK_Abort
)) 
 193                           return PIPE_RWSTREAM_CallbackAborted
; 
 196                         if ((ErrorCode 
= Pipe_WaitUntilReady())) 
 206         return PIPE_RWSTREAM_NoError
; 
 209 /* The following abuses the C preprocessor in order to copy-past common code with slight alterations, 
 210  * so that the code needs to be written once. It is a crude form of templating to reduce code maintenance. */ 
 212 #define  TEMPLATE_FUNC_NAME                        Pipe_Write_Stream_LE 
 213 #define  TEMPLATE_BUFFER_TYPE                      const void* 
 214 #define  TEMPLATE_TOKEN                            PIPE_TOKEN_OUT 
 215 #define  TEMPLATE_CLEAR_PIPE()                     Pipe_ClearOUT() 
 216 #define  TEMPLATE_BUFFER_OFFSET(Length)            0 
 217 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Pipe_Write_Byte(*((uint8_t*)BufferPtr++)) 
 218 #include "Template/Template_Pipe_RW.c" 
 220 #define  TEMPLATE_FUNC_NAME                        Pipe_Write_PStream_LE 
 221 #define  TEMPLATE_BUFFER_TYPE                      const void* 
 222 #define  TEMPLATE_TOKEN                            PIPE_TOKEN_OUT 
 223 #define  TEMPLATE_CLEAR_PIPE()                     Pipe_ClearOUT() 
 224 #define  TEMPLATE_BUFFER_OFFSET(Length)            0 
 225 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Pipe_Write_Byte(pgm_read_byte((uint8_t*)BufferPtr++)) 
 226 #include "Template/Template_Pipe_RW.c" 
 228 #define  TEMPLATE_FUNC_NAME                        Pipe_Write_EStream_LE 
 229 #define  TEMPLATE_BUFFER_TYPE                      const void* 
 230 #define  TEMPLATE_TOKEN                            PIPE_TOKEN_OUT 
 231 #define  TEMPLATE_CLEAR_PIPE()                     Pipe_ClearOUT() 
 232 #define  TEMPLATE_BUFFER_OFFSET(Length)            0 
 233 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Pipe_Write_Byte(eeprom_read_byte((uint8_t*)BufferPtr++)) 
 234 #include "Template/Template_Pipe_RW.c" 
 236 #define  TEMPLATE_FUNC_NAME                        Pipe_Write_Stream_BE 
 237 #define  TEMPLATE_BUFFER_TYPE                      const void* 
 238 #define  TEMPLATE_TOKEN                            PIPE_TOKEN_OUT 
 239 #define  TEMPLATE_CLEAR_PIPE()                     Pipe_ClearOUT() 
 240 #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1) 
 241 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Pipe_Write_Byte(*((uint8_t*)BufferPtr--)) 
 242 #include "Template/Template_Pipe_RW.c" 
 244 #define  TEMPLATE_FUNC_NAME                        Pipe_Write_PStream_BE 
 245 #define  TEMPLATE_BUFFER_TYPE                      const void* 
 246 #define  TEMPLATE_TOKEN                            PIPE_TOKEN_OUT 
 247 #define  TEMPLATE_CLEAR_PIPE()                     Pipe_ClearOUT() 
 248 #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1) 
 249 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Pipe_Write_Byte(pgm_read_byte((uint8_t*)BufferPtr--)) 
 250 #include "Template/Template_Pipe_RW.c" 
 252 #define  TEMPLATE_FUNC_NAME                        Pipe_Write_EStream_BE 
 253 #define  TEMPLATE_BUFFER_TYPE                      const void* 
 254 #define  TEMPLATE_TOKEN                            PIPE_TOKEN_OUT 
 255 #define  TEMPLATE_CLEAR_PIPE()                     Pipe_ClearOUT() 
 256 #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1) 
 257 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Pipe_Write_Byte(eeprom_read_byte((uint8_t*)BufferPtr--)) 
 258 #include "Template/Template_Pipe_RW.c" 
 260 #define  TEMPLATE_FUNC_NAME                        Pipe_Read_Stream_LE 
 261 #define  TEMPLATE_BUFFER_TYPE                      void* 
 262 #define  TEMPLATE_TOKEN                            PIPE_TOKEN_IN 
 263 #define  TEMPLATE_CLEAR_PIPE()                     Pipe_ClearIN() 
 264 #define  TEMPLATE_BUFFER_OFFSET(Length)            0 
 265 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         *((uint8_t*)BufferPtr++) = Pipe_Read_Byte() 
 266 #include "Template/Template_Pipe_RW.c" 
 268 #define  TEMPLATE_FUNC_NAME                        Pipe_Read_EStream_LE 
 269 #define  TEMPLATE_BUFFER_TYPE                      void* 
 270 #define  TEMPLATE_TOKEN                            PIPE_TOKEN_IN 
 271 #define  TEMPLATE_CLEAR_PIPE()                     Pipe_ClearIN() 
 272 #define  TEMPLATE_BUFFER_OFFSET(Length)            0 
 273 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         eeprom_update_byte((uint8_t*)BufferPtr++, Pipe_Read_Byte()) 
 274 #include "Template/Template_Pipe_RW.c" 
 276 #define  TEMPLATE_FUNC_NAME                        Pipe_Read_Stream_BE 
 277 #define  TEMPLATE_BUFFER_TYPE                      void* 
 278 #define  TEMPLATE_TOKEN                            PIPE_TOKEN_IN 
 279 #define  TEMPLATE_CLEAR_PIPE()                     Pipe_ClearIN() 
 280 #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1) 
 281 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         *((uint8_t*)BufferPtr--) = Pipe_Read_Byte() 
 282 #include "Template/Template_Pipe_RW.c" 
 284 #define  TEMPLATE_FUNC_NAME                        Pipe_Read_EStream_BE 
 285 #define  TEMPLATE_BUFFER_TYPE                      void* 
 286 #define  TEMPLATE_TOKEN                            PIPE_TOKEN_IN 
 287 #define  TEMPLATE_CLEAR_PIPE()                     Pipe_ClearIN() 
 288 #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1) 
 289 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         eeprom_update_byte((uint8_t*)BufferPtr--, Pipe_Read_Byte()) 
 290 #include "Template/Template_Pipe_RW.c"