3      Copyright (C) Dean Camera, 2011. 
   5   dean [at] fourwalledcubicle [dot] com 
  10   Copyright 2011  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) 
  38 uint8_t USB_ControlPipeSize 
= PIPE_CONTROLPIPE_DEFAULT_SIZE
; 
  40 bool Pipe_ConfigurePipe(const uint8_t Number
, 
  43                         const uint8_t EndpointNumber
, 
  47 #if defined(ORDERED_EP_CONFIG) 
  48         Pipe_SelectPipe(Number
); 
  53         UPCFG0X 
= ((Type 
<< EPTYPE0
) | Token 
| ((EndpointNumber 
& PIPE_EPNUM_MASK
) << PEPNUM0
)); 
  54         UPCFG1X 
= ((1 << ALLOC
) | Banks 
| Pipe_BytesToEPSizeMask(Size
)); 
  56         Pipe_SetInfiniteINRequests(); 
  58         return Pipe_IsConfigured(); 
  60         for (uint8_t PNum 
= Number
; PNum 
< PIPE_TOTAL_PIPES
; PNum
++) 
  69                 Pipe_SelectPipe(PNum
); 
  73                         UPCFG0XTemp 
= ((Type 
<< EPTYPE0
) | Token 
| ((EndpointNumber 
& PIPE_EPNUM_MASK
) << PEPNUM0
)); 
  74                         UPCFG1XTemp 
= ((1 << ALLOC
) | Banks 
| Pipe_BytesToEPSizeMask(Size
)); 
  76                         UPCONXTemp  
= ((1 << PEN
) | (1 << INMODE
)); 
  82                         UPCFG0XTemp 
= UPCFG0X
; 
  83                         UPCFG1XTemp 
= UPCFG1X
; 
  84                         UPCFG2XTemp 
= UPCFG2X
; 
  86                         UPINRQXTemp 
= UPINRQX
; 
  90                 if (!(UPCFG1XTemp 
& (1 << ALLOC
))) 
  94                 UPCFG1X 
&= (1 << ALLOC
); 
  97                 UPCFG0X 
= UPCFG0XTemp
; 
  98                 UPCFG1X 
= UPCFG1XTemp
; 
  99                 UPCFG2X 
= UPCFG2XTemp
; 
 101                 UPINRQX 
= UPINRQXTemp
; 
 104                 if (!(Pipe_IsConfigured())) 
 108         Pipe_SelectPipe(Number
);         
 113 void Pipe_ClearPipes(void) 
 117         for (uint8_t PNum 
= 0; PNum 
< PIPE_TOTAL_PIPES
; PNum
++) 
 119                 Pipe_SelectPipe(PNum
); 
 127 bool Pipe_IsEndpointBound(const uint8_t EndpointAddress
) 
 129         uint8_t PrevPipeNumber 
= Pipe_GetCurrentPipe(); 
 131         for (uint8_t PNum 
= 0; PNum 
< PIPE_TOTAL_PIPES
; PNum
++) 
 133                 Pipe_SelectPipe(PNum
); 
 135                 if (!(Pipe_IsConfigured())) 
 138                 uint8_t PipeToken        
= Pipe_GetPipeToken(); 
 139                 bool    PipeTokenCorrect 
= true; 
 141                 if (PipeToken 
!= PIPE_TOKEN_SETUP
) 
 142                   PipeTokenCorrect 
= (PipeToken 
== ((EndpointAddress 
& PIPE_EPDIR_MASK
) ? PIPE_TOKEN_IN 
: PIPE_TOKEN_OUT
)); 
 144                 if (PipeTokenCorrect 
&& (Pipe_BoundEndpointNumber() == (EndpointAddress 
& PIPE_EPNUM_MASK
))) 
 148         Pipe_SelectPipe(PrevPipeNumber
); 
 152 uint8_t Pipe_WaitUntilReady(void) 
 154         #if (USB_STREAM_TIMEOUT_MS < 0xFF) 
 155         uint8_t  TimeoutMSRem 
= USB_STREAM_TIMEOUT_MS
; 
 157         uint16_t TimeoutMSRem 
= USB_STREAM_TIMEOUT_MS
; 
 160         uint16_t PreviousFrameNumber 
= USB_Host_GetFrameNumber(); 
 164                 if (Pipe_GetPipeToken() == PIPE_TOKEN_IN
) 
 166                         if (Pipe_IsINReceived()) 
 167                           return PIPE_READYWAIT_NoError
; 
 171                         if (Pipe_IsOUTReady()) 
 172                           return PIPE_READYWAIT_NoError
; 
 175                 if (Pipe_IsStalled()) 
 176                   return PIPE_READYWAIT_PipeStalled
; 
 177                 else if (USB_HostState 
== HOST_STATE_Unattached
) 
 178                   return PIPE_READYWAIT_DeviceDisconnected
; 
 180                 uint16_t CurrentFrameNumber 
= USB_Host_GetFrameNumber(); 
 182                 if (CurrentFrameNumber 
!= PreviousFrameNumber
) 
 184                         PreviousFrameNumber 
= CurrentFrameNumber
; 
 186                         if (!(TimeoutMSRem
--)) 
 187                           return PIPE_READYWAIT_Timeout
;