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 "../USBMode.h" 
  34 #if defined(USB_CAN_BE_HOST) 
  38 uint8_t USB_ControlPipeSize 
= PIPE_CONTROLPIPE_DEFAULT_SIZE
; 
  40 volatile uint8_t  USB_SelectedPipe 
= PIPE_CONTROLPIPE
; 
  41 volatile uint8_t* USB_PipeFIFOPos
[PIPE_TOTAL_PIPES
]; 
  43 bool Pipe_ConfigurePipe(const uint8_t Number
, 
  46                         const uint8_t EndpointNumber
, 
  50         Pipe_SelectPipe(Number
); 
  53         (&AVR32_USBB
.upcfg0
)[Number
] = 0; 
  54         (&AVR32_USBB
.upcfg0
)[Number
] = (AVR32_USBB_ALLOC_MASK 
| 
  55                                         ((uint32_t)Type  
<< AVR32_USBB_PTYPE_OFFSET
)  | 
  56                                         ((uint32_t)Token 
<< AVR32_USBB_PTOKEN_OFFSET
) | 
  57                                         ((uint32_t)Banks 
<< AVR32_USBB_PBK_OFFSET
)    | 
  58                                         ((EndpointNumber 
& PIPE_EPNUM_MASK
) << AVR32_USBB_PEPNUM_OFFSET
)); 
  59         USB_PipeFIFOPos
[Number
]      = &AVR32_USBB_SLAVE
[Number 
* 0x10000]; 
  61         Pipe_SetInfiniteINRequests(); 
  63         return Pipe_IsConfigured(); 
  66 void Pipe_ClearPipes(void) 
  68         for (uint8_t PNum 
= 0; PNum 
< PIPE_TOTAL_PIPES
; PNum
++) 
  70                 Pipe_SelectPipe(PNum
); 
  71                 (&AVR32_USBB
.upcfg0
)[PNum
]    = 0; 
  72                 (&AVR32_USBB
.upcon0clr
)[PNum
] = 0xFFFFFFFF; 
  73                 USB_PipeFIFOPos
[PNum
]         = &AVR32_USBB_SLAVE
[PNum 
* 0x10000]; 
  78 bool Pipe_IsEndpointBound(const uint8_t EndpointAddress
) 
  80         uint8_t PrevPipeNumber 
= Pipe_GetCurrentPipe(); 
  82         for (uint8_t PNum 
= 0; PNum 
< PIPE_TOTAL_PIPES
; PNum
++) 
  84                 Pipe_SelectPipe(PNum
); 
  86                 if (!(Pipe_IsConfigured())) 
  89                 uint8_t PipeToken        
= Pipe_GetPipeToken(); 
  90                 bool    PipeTokenCorrect 
= true; 
  92                 if (PipeToken 
!= PIPE_TOKEN_SETUP
) 
  93                   PipeTokenCorrect 
= (PipeToken 
== ((EndpointAddress 
& PIPE_EPDIR_MASK
) ? PIPE_TOKEN_IN 
: PIPE_TOKEN_OUT
)); 
  95                 if (PipeTokenCorrect 
&& (Pipe_BoundEndpointNumber() == (EndpointAddress 
& PIPE_EPNUM_MASK
))) 
  99         Pipe_SelectPipe(PrevPipeNumber
); 
 103 uint8_t Pipe_WaitUntilReady(void) 
 105         #if (USB_STREAM_TIMEOUT_MS < 0xFF) 
 106         uint8_t  TimeoutMSRem 
= USB_STREAM_TIMEOUT_MS
; 
 108         uint16_t TimeoutMSRem 
= USB_STREAM_TIMEOUT_MS
; 
 111         uint16_t PreviousFrameNumber 
= USB_Host_GetFrameNumber(); 
 115                 if (Pipe_GetPipeToken() == PIPE_TOKEN_IN
) 
 117                         if (Pipe_IsINReceived()) 
 118                           return PIPE_READYWAIT_NoError
; 
 122                         if (Pipe_IsOUTReady()) 
 123                           return PIPE_READYWAIT_NoError
; 
 126                 if (Pipe_IsStalled()) 
 127                   return PIPE_READYWAIT_PipeStalled
; 
 128                 else if (USB_HostState 
== HOST_STATE_Unattached
) 
 129                   return PIPE_READYWAIT_DeviceDisconnected
; 
 131                 uint16_t CurrentFrameNumber 
= USB_Host_GetFrameNumber(); 
 133                 if (CurrentFrameNumber 
!= PreviousFrameNumber
) 
 135                         PreviousFrameNumber 
= CurrentFrameNumber
; 
 137                         if (!(TimeoutMSRem
--)) 
 138                           return PIPE_READYWAIT_Timeout
;