3 Copyright (C) Dean Camera, 2012.
\r
5 dean [at] fourwalledcubicle [dot] com
\r
10 Copyright 2012 Dean Camera (dean [at] fourwalledcubicle [dot] com)
\r
12 Permission to use, copy, modify, distribute, and sell this
\r
13 software and its documentation for any purpose is hereby granted
\r
14 without fee, provided that the above copyright notice appear in
\r
15 all copies and that both that the copyright notice and this
\r
16 permission notice and warranty disclaimer appear in supporting
\r
17 documentation, and that the name of the author not be used in
\r
18 advertising or publicity pertaining to distribution of the
\r
19 software without specific, written prior permission.
\r
21 The author disclaim all warranties with regard to this
\r
22 software, including all implied warranties of merchantability
\r
23 and fitness. In no event shall the author be liable for any
\r
24 special, indirect or consequential damages or any damages
\r
25 whatsoever resulting from loss of use, data or profits, whether
\r
26 in an action of contract, negligence or other tortious action,
\r
27 arising out of or in connection with the use or performance of
\r
31 #include "../../../../Common/Common.h"
\r
32 #if (ARCH == ARCH_UC3)
\r
34 #define __INCLUDE_FROM_USB_DRIVER
\r
35 #include "../USBMode.h"
\r
37 #if defined(USB_CAN_BE_DEVICE)
\r
39 #include "../Endpoint.h"
\r
41 #if !defined(FIXED_CONTROL_ENDPOINT_SIZE)
\r
42 uint8_t USB_Device_ControlEndpointSize = ENDPOINT_CONTROLEP_DEFAULT_SIZE;
\r
45 volatile uint32_t USB_Endpoint_SelectedEndpoint = ENDPOINT_CONTROLEP;
\r
46 volatile uint8_t* USB_Endpoint_FIFOPos[ENDPOINT_TOTAL_ENDPOINTS];
\r
48 bool Endpoint_ConfigureEndpointTable(const USB_Endpoint_Table_t* const Table,
\r
49 const uint8_t Entries)
\r
51 for (uint8_t i = 0; i < Entries; i++)
\r
53 if (!(Table[i].Address))
\r
56 if (!(Endpoint_ConfigureEndpoint(Table[i].Address, Table[i].Type, Table[i].Size, Table[i].Banks)))
\r
65 bool Endpoint_ConfigureEndpoint_Prv(const uint8_t Number,
\r
66 const uint32_t UECFG0Data)
\r
68 USB_Endpoint_FIFOPos[Number] = &AVR32_USBB_SLAVE[Number * ENDPOINT_HSB_ADDRESS_SPACE_SIZE];
\r
70 #if defined(CONTROL_ONLY_DEVICE) || defined(ORDERED_EP_CONFIG)
\r
71 Endpoint_SelectEndpoint(Number);
\r
72 Endpoint_EnableEndpoint();
\r
74 (&AVR32_USBB.uecfg0)[Number] = 0;
\r
75 (&AVR32_USBB.uecfg0)[Number] = UECFG0Data;
\r
77 return Endpoint_IsConfigured();
\r
79 for (uint8_t EPNum = Number; EPNum < ENDPOINT_TOTAL_ENDPOINTS; EPNum++)
\r
81 uint32_t UECFG0Temp;
\r
83 Endpoint_SelectEndpoint(EPNum);
\r
85 if (EPNum == Number)
\r
87 UECFG0Temp = UECFG0Data;
\r
91 UECFG0Temp = (&AVR32_USBB.uecfg0)[EPNum];
\r
94 if (!(UECFG0Temp & AVR32_USBB_ALLOC_MASK))
\r
97 Endpoint_DisableEndpoint();
\r
98 (&AVR32_USBB.uecfg0)[EPNum] &= ~AVR32_USBB_ALLOC_MASK;
\r
100 Endpoint_EnableEndpoint();
\r
101 (&AVR32_USBB.uecfg0)[EPNum] = UECFG0Temp;
\r
103 if (!(Endpoint_IsConfigured()))
\r
107 Endpoint_SelectEndpoint(Number);
\r
112 void Endpoint_ClearEndpoints(void)
\r
114 for (uint8_t EPNum = 0; EPNum < ENDPOINT_TOTAL_ENDPOINTS; EPNum++)
\r
116 Endpoint_SelectEndpoint(EPNum);
\r
117 (&AVR32_USBB.uecfg0)[EPNum] = 0;
\r
118 (&AVR32_USBB.uecon0clr)[EPNum] = -1;
\r
119 USB_Endpoint_FIFOPos[EPNum] = &AVR32_USBB_SLAVE[EPNum * 0x10000];
\r
120 Endpoint_DisableEndpoint();
\r
124 void Endpoint_ClearStatusStage(void)
\r
126 if (USB_ControlRequest.bmRequestType & REQDIR_DEVICETOHOST)
\r
128 while (!(Endpoint_IsOUTReceived()))
\r
130 if (USB_DeviceState == DEVICE_STATE_Unattached)
\r
134 Endpoint_ClearOUT();
\r
138 while (!(Endpoint_IsINReady()))
\r
140 if (USB_DeviceState == DEVICE_STATE_Unattached)
\r
144 Endpoint_ClearIN();
\r
148 #if !defined(CONTROL_ONLY_DEVICE)
\r
149 uint8_t Endpoint_WaitUntilReady(void)
\r
151 #if (USB_STREAM_TIMEOUT_MS < 0xFF)
\r
152 uint8_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
\r
154 uint16_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
\r
157 uint16_t PreviousFrameNumber = USB_Device_GetFrameNumber();
\r
161 if (Endpoint_GetEndpointDirection() == ENDPOINT_DIR_IN)
\r
163 if (Endpoint_IsINReady())
\r
164 return ENDPOINT_READYWAIT_NoError;
\r
168 if (Endpoint_IsOUTReceived())
\r
169 return ENDPOINT_READYWAIT_NoError;
\r
172 uint8_t USB_DeviceState_LCL = USB_DeviceState;
\r
174 if (USB_DeviceState_LCL == DEVICE_STATE_Unattached)
\r
175 return ENDPOINT_READYWAIT_DeviceDisconnected;
\r
176 else if (USB_DeviceState_LCL == DEVICE_STATE_Suspended)
\r
177 return ENDPOINT_READYWAIT_BusSuspended;
\r
178 else if (Endpoint_IsStalled())
\r
179 return ENDPOINT_READYWAIT_EndpointStalled;
\r
181 uint16_t CurrentFrameNumber = USB_Device_GetFrameNumber();
\r
183 if (CurrentFrameNumber != PreviousFrameNumber)
\r
185 PreviousFrameNumber = CurrentFrameNumber;
\r
187 if (!(TimeoutMSRem--))
\r
188 return ENDPOINT_READYWAIT_Timeout;
\r