Added start of a port of the core USB driver code to the AVR32 UC3B architecture.
[pub/USBasp.git] / LUFA / Drivers / USB / Core / UC3B / Endpoint.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2011.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2011 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
20
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
28 this software.
29 */
30
31 #define __INCLUDE_FROM_USB_DRIVER
32 #include "../USBMode.h"
33
34 #if defined(USB_CAN_BE_DEVICE)
35
36 #include "Endpoint.h"
37
38 #if !defined(FIXED_CONTROL_ENDPOINT_SIZE)
39 uint8_t USB_ControlEndpointSize = ENDPOINT_CONTROLEP_DEFAULT_SIZE;
40 #endif
41
42 uint8_t USB_SelectedEndpoint = ENDPOINT_CONTROLEP;
43
44 bool Endpoint_ConfigureEndpoint_Prv(const uint8_t Number,
45 const uint32_t UECFGXData)
46 {
47 #if defined(CONTROL_ONLY_DEVICE) || defined(ORDERED_EP_CONFIG)
48 Endpoint_SelectEndpoint(Number);
49 Endpoint_EnableEndpoint();
50
51 *((uint32_t*)AVR32_USBB_UECFG0)[USB_SelectedEndpoint] = 0;
52 *((uint32_t*)AVR32_USBB_UECFG0)[USB_SelectedEndpoint] = UECFGXData;
53
54 return Endpoint_IsConfigured();
55 #else
56 for (uint8_t EPNum = Number; EPNum < ENDPOINT_TOTAL_ENDPOINTS; EPNum++)
57 {
58 uint32_t UECFGXTemp;
59 uint32_t UEIENXTemp;
60
61 Endpoint_SelectEndpoint(EPNum);
62
63 if (EPNum == Number)
64 {
65 UECFGXTemp = UECFGXData;
66 UEIENXTemp = 0;
67 }
68 else
69 {
70 UECFGXTemp = *((uint32_t*)AVR32_USBB_UECFG0)[EPNum];
71 UEIENXTemp = *((uint32_t*)AVR32_USBB_UEINT0)[EPNum];
72 }
73
74 if (!(UECFGXTemp & AVR32_USBB_ALLOC))
75 continue;
76
77 Endpoint_DisableEndpoint();
78 *((uint32_t*)AVR32_USBB_UECFG0)[USB_SelectedEndpoint] &= ~AVR32_USBB_ALLOC;
79
80 Endpoint_EnableEndpoint();
81 *((uint32_t*)AVR32_USBB_UECFG0)[EPNum] = UECFGXTemp;
82 *((uint32_t*)AVR32_USBB_UEINT0)[EPNum] = UEINTXTemp;
83
84 if (!(Endpoint_IsConfigured()))
85 return false;
86 }
87
88 Endpoint_SelectEndpoint(Number);
89 return true;
90 #endif
91 }
92
93 void Endpoint_ClearEndpoints(void)
94 {
95 UEINT = 0;
96
97 for (uint8_t EPNum = 0; EPNum < ENDPOINT_TOTAL_ENDPOINTS; EPNum++)
98 {
99 Endpoint_SelectEndpoint(EPNum);
100 UEIENX = 0;
101 UEINTX = 0;
102 UECFG1X = 0;
103 Endpoint_DisableEndpoint();
104 }
105 }
106
107 void Endpoint_ClearStatusStage(void)
108 {
109 if (USB_ControlRequest.bmRequestType & REQDIR_DEVICETOHOST)
110 {
111 while (!(Endpoint_IsOUTReceived()))
112 {
113 if (USB_DeviceState == DEVICE_STATE_Unattached)
114 return;
115 }
116
117 Endpoint_ClearOUT();
118 }
119 else
120 {
121 while (!(Endpoint_IsINReady()))
122 {
123 if (USB_DeviceState == DEVICE_STATE_Unattached)
124 return;
125 }
126
127 Endpoint_ClearIN();
128 }
129 }
130
131 #if !defined(CONTROL_ONLY_DEVICE)
132 uint8_t Endpoint_WaitUntilReady(void)
133 {
134 #if (USB_STREAM_TIMEOUT_MS < 0xFF)
135 uint8_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
136 #else
137 uint16_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
138 #endif
139
140 uint16_t PreviousFrameNumber = USB_Device_GetFrameNumber();
141
142 for (;;)
143 {
144 if (Endpoint_GetEndpointDirection() == ENDPOINT_DIR_IN)
145 {
146 if (Endpoint_IsINReady())
147 return ENDPOINT_READYWAIT_NoError;
148 }
149 else
150 {
151 if (Endpoint_IsOUTReceived())
152 return ENDPOINT_READYWAIT_NoError;
153 }
154
155 uint8_t USB_DeviceState_LCL = USB_DeviceState;
156
157 if (USB_DeviceState_LCL == DEVICE_STATE_Unattached)
158 return ENDPOINT_READYWAIT_DeviceDisconnected;
159 else if (USB_DeviceState_LCL == DEVICE_STATE_Suspended)
160 return ENDPOINT_READYWAIT_BusSuspended;
161 else if (Endpoint_IsStalled())
162 return ENDPOINT_READYWAIT_EndpointStalled;
163
164 uint16_t CurrentFrameNumber = USB_Device_GetFrameNumber();
165
166 if (CurrentFrameNumber != PreviousFrameNumber)
167 {
168 PreviousFrameNumber = CurrentFrameNumber;
169
170 if (!(TimeoutMSRem--))
171 return ENDPOINT_READYWAIT_Timeout;
172 }
173 }
174 }
175 #endif
176
177 #endif
178