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_DEVICE) 
  36 #include "../Endpoint.h" 
  38 #if !defined(FIXED_CONTROL_ENDPOINT_SIZE) 
  39 uint8_t USB_ControlEndpointSize 
= ENDPOINT_CONTROLEP_DEFAULT_SIZE
; 
  42 uint8_t USB_SelectedEndpoint 
= ENDPOINT_CONTROLEP
; 
  45 bool Endpoint_ConfigureEndpoint_Prv(const uint8_t Number
, 
  46                                     const uint32_t UECFGXData
) 
  48         Endpoint_SelectEndpoint(Number
); 
  49         Endpoint_EnableEndpoint(); 
  51         ((uint32_t*)AVR32_USBB_UECFG0
)[USB_SelectedEndpoint
] = 0; 
  52         ((uint32_t*)AVR32_USBB_UECFG0
)[USB_SelectedEndpoint
] = UECFGXData
; 
  54         return Endpoint_IsConfigured(); 
  57 void Endpoint_ClearEndpoints(void) 
  59         for (uint8_t EPNum 
= 0; EPNum 
< ENDPOINT_TOTAL_ENDPOINTS
; EPNum
++) 
  61                 Endpoint_SelectEndpoint(EPNum
); 
  62                 ((uint32_t*)AVR32_USBB_UECFG0
)[USB_SelectedEndpoint
]    = 0; 
  63                 ((uint32_t*)AVR32_USBB_UECON0CLR
)[USB_SelectedEndpoint
] = 0xFFFFFFFF; 
  64                 Endpoint_DisableEndpoint(); 
  68 void Endpoint_ClearStatusStage(void) 
  70         if (USB_ControlRequest
.bmRequestType 
& REQDIR_DEVICETOHOST
) 
  72                 while (!(Endpoint_IsOUTReceived())) 
  74                         if (USB_DeviceState 
== DEVICE_STATE_Unattached
) 
  82                 while (!(Endpoint_IsINReady())) 
  84                         if (USB_DeviceState 
== DEVICE_STATE_Unattached
) 
  92 #if !defined(CONTROL_ONLY_DEVICE) 
  93 uint8_t Endpoint_WaitUntilReady(void) 
  95         #if (USB_STREAM_TIMEOUT_MS < 0xFF) 
  96         uint8_t  TimeoutMSRem 
= USB_STREAM_TIMEOUT_MS
; 
  98         uint16_t TimeoutMSRem 
= USB_STREAM_TIMEOUT_MS
; 
 101         uint16_t PreviousFrameNumber 
= USB_Device_GetFrameNumber(); 
 105                 if (Endpoint_GetEndpointDirection() == ENDPOINT_DIR_IN
) 
 107                         if (Endpoint_IsINReady()) 
 108                           return ENDPOINT_READYWAIT_NoError
; 
 112                         if (Endpoint_IsOUTReceived()) 
 113                           return ENDPOINT_READYWAIT_NoError
; 
 116                 uint8_t USB_DeviceState_LCL 
= USB_DeviceState
; 
 118                 if (USB_DeviceState_LCL 
== DEVICE_STATE_Unattached
) 
 119                   return ENDPOINT_READYWAIT_DeviceDisconnected
; 
 120                 else if (USB_DeviceState_LCL 
== DEVICE_STATE_Suspended
) 
 121                   return ENDPOINT_READYWAIT_BusSuspended
; 
 122                 else if (Endpoint_IsStalled()) 
 123                   return ENDPOINT_READYWAIT_EndpointStalled
; 
 125                 uint16_t CurrentFrameNumber 
= USB_Device_GetFrameNumber(); 
 127                 if (CurrentFrameNumber 
!= PreviousFrameNumber
) 
 129                         PreviousFrameNumber 
= CurrentFrameNumber
; 
 131                         if (!(TimeoutMSRem
--)) 
 132                           return ENDPOINT_READYWAIT_Timeout
;