3      Copyright (C) Dean Camera, 2013. 
   5   dean [at] fourwalledcubicle [dot] com 
  10   Copyright 2013  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 disclaims 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 "../../../../Common/Common.h" 
  32 #if (ARCH == ARCH_UC3) 
  34 #define  __INCLUDE_FROM_USB_DRIVER 
  35 #include "../USBMode.h" 
  37 #if defined(USB_CAN_BE_DEVICE) 
  39 #include "../Endpoint.h" 
  41 #if !defined(FIXED_CONTROL_ENDPOINT_SIZE) 
  42 uint8_t USB_Device_ControlEndpointSize 
= ENDPOINT_CONTROLEP_DEFAULT_SIZE
; 
  45 volatile uint32_t USB_Endpoint_SelectedEndpoint 
= ENDPOINT_CONTROLEP
; 
  46 volatile uint8_t* USB_Endpoint_FIFOPos
[ENDPOINT_TOTAL_ENDPOINTS
]; 
  48 bool Endpoint_ConfigureEndpointTable(const USB_Endpoint_Table_t
* const Table
, 
  49                                      const uint8_t Entries
) 
  51         for (uint8_t i 
= 0; i 
< Entries
; i
++) 
  53                 if (!(Table
[i
].Address
)) 
  56                 if (!(Endpoint_ConfigureEndpoint(Table
[i
].Address
, Table
[i
].Type
, Table
[i
].Size
, Table
[i
].Banks
))) 
  65 bool Endpoint_ConfigureEndpoint_Prv(const uint8_t Number
, 
  66                                     const uint32_t UECFG0Data
) 
  68         USB_Endpoint_FIFOPos
[Number
] = &AVR32_USBB_SLAVE
[Number 
* ENDPOINT_HSB_ADDRESS_SPACE_SIZE
]; 
  70 #if defined(CONTROL_ONLY_DEVICE) || defined(ORDERED_EP_CONFIG) 
  71         Endpoint_SelectEndpoint(Number
); 
  72         Endpoint_EnableEndpoint(); 
  74         (&AVR32_USBB
.uecfg0
)[Number
] = 0; 
  75         (&AVR32_USBB
.uecfg0
)[Number
] = UECFG0Data
; 
  77         return Endpoint_IsConfigured(); 
  79         for (uint8_t EPNum 
= Number
; EPNum 
< ENDPOINT_TOTAL_ENDPOINTS
; EPNum
++) 
  83                 Endpoint_SelectEndpoint(EPNum
); 
  87                         UECFG0Temp 
= UECFG0Data
; 
  91                         UECFG0Temp 
= (&AVR32_USBB
.uecfg0
)[EPNum
]; 
  94                 if (!(UECFG0Temp 
& AVR32_USBB_ALLOC_MASK
)) 
  97                 Endpoint_DisableEndpoint(); 
  98                 (&AVR32_USBB
.uecfg0
)[EPNum
] &= ~AVR32_USBB_ALLOC_MASK
; 
 100                 Endpoint_EnableEndpoint(); 
 101                 (&AVR32_USBB
.uecfg0
)[EPNum
] = UECFG0Temp
; 
 103                 if (!(Endpoint_IsConfigured())) 
 107         Endpoint_SelectEndpoint(Number
); 
 112 void Endpoint_ClearEndpoints(void) 
 114         for (uint8_t EPNum 
= 0; EPNum 
< ENDPOINT_TOTAL_ENDPOINTS
; EPNum
++) 
 116                 Endpoint_SelectEndpoint(EPNum
); 
 117                 (&AVR32_USBB
.uecfg0
)[EPNum
]    = 0; 
 118                 (&AVR32_USBB
.uecon0clr
)[EPNum
] = -1; 
 119                 USB_Endpoint_FIFOPos
[EPNum
]    = &AVR32_USBB_SLAVE
[EPNum 
* 0x10000]; 
 120                 Endpoint_DisableEndpoint(); 
 124 void Endpoint_ClearStatusStage(void) 
 126         if (USB_ControlRequest
.bmRequestType 
& REQDIR_DEVICETOHOST
) 
 128                 while (!(Endpoint_IsOUTReceived())) 
 130                         if (USB_DeviceState 
== DEVICE_STATE_Unattached
) 
 138                 while (!(Endpoint_IsINReady())) 
 140                         if (USB_DeviceState 
== DEVICE_STATE_Unattached
) 
 148 #if !defined(CONTROL_ONLY_DEVICE) 
 149 uint8_t Endpoint_WaitUntilReady(void) 
 151         #if (USB_STREAM_TIMEOUT_MS < 0xFF) 
 152         uint8_t  TimeoutMSRem 
= USB_STREAM_TIMEOUT_MS
; 
 154         uint16_t TimeoutMSRem 
= USB_STREAM_TIMEOUT_MS
; 
 157         uint16_t PreviousFrameNumber 
= USB_Device_GetFrameNumber(); 
 161                 if (Endpoint_GetEndpointDirection() == ENDPOINT_DIR_IN
) 
 163                         if (Endpoint_IsINReady()) 
 164                           return ENDPOINT_READYWAIT_NoError
; 
 168                         if (Endpoint_IsOUTReceived()) 
 169                           return ENDPOINT_READYWAIT_NoError
; 
 172                 uint8_t USB_DeviceState_LCL 
= USB_DeviceState
; 
 174                 if (USB_DeviceState_LCL 
== DEVICE_STATE_Unattached
) 
 175                   return ENDPOINT_READYWAIT_DeviceDisconnected
; 
 176                 else if (USB_DeviceState_LCL 
== DEVICE_STATE_Suspended
) 
 177                   return ENDPOINT_READYWAIT_BusSuspended
; 
 178                 else if (Endpoint_IsStalled()) 
 179                   return ENDPOINT_READYWAIT_EndpointStalled
; 
 181                 uint16_t CurrentFrameNumber 
= USB_Device_GetFrameNumber(); 
 183                 if (CurrentFrameNumber 
!= PreviousFrameNumber
) 
 185                         PreviousFrameNumber 
= CurrentFrameNumber
; 
 187                         if (!(TimeoutMSRem
--)) 
 188                           return ENDPOINT_READYWAIT_Timeout
;