Add tag for the 120219 release.
[pub/USBasp.git] / LUFA / Drivers / USB / Core / UC3 / Endpoint_UC3.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2012.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2012 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_Device_ControlEndpointSize = ENDPOINT_CONTROLEP_DEFAULT_SIZE;
40 #endif
41
42 volatile uint32_t USB_Endpoint_SelectedEndpoint = ENDPOINT_CONTROLEP;
43 volatile uint8_t* USB_Endpoint_FIFOPos[ENDPOINT_TOTAL_ENDPOINTS];
44
45 bool Endpoint_ConfigureEndpoint_Prv(const uint8_t Number,
46 const uint32_t UECFG0Data)
47 {
48 USB_Endpoint_FIFOPos[Number] = &AVR32_USBB_SLAVE[Number * 0x10000];
49
50 #if defined(CONTROL_ONLY_DEVICE) || defined(ORDERED_EP_CONFIG)
51 Endpoint_SelectEndpoint(Number);
52 Endpoint_EnableEndpoint();
53
54 (&AVR32_USBB.uecfg0)[Number] = 0;
55 (&AVR32_USBB.uecfg0)[Number] = UECFG0Data;
56
57 return Endpoint_IsConfigured();
58 #else
59 for (uint8_t EPNum = Number; EPNum < ENDPOINT_TOTAL_ENDPOINTS; EPNum++)
60 {
61 uint32_t UECFG0Temp;
62
63 Endpoint_SelectEndpoint(EPNum);
64
65 if (EPNum == Number)
66 {
67 UECFG0Temp = UECFG0Data;
68 }
69 else
70 {
71 UECFG0Temp = (&AVR32_USBB.uecfg0)[EPNum];
72 }
73
74 if (!(UECFG0Temp & AVR32_USBB_ALLOC_MASK))
75 continue;
76
77 Endpoint_DisableEndpoint();
78 (&AVR32_USBB.uecfg0)[EPNum] &= ~AVR32_USBB_ALLOC_MASK;
79
80 Endpoint_EnableEndpoint();
81 (&AVR32_USBB.uecfg0)[EPNum] = UECFG0Temp;
82
83 if (!(Endpoint_IsConfigured()))
84 return false;
85 }
86
87 Endpoint_SelectEndpoint(Number);
88 return true;
89 #endif
90 }
91
92 void Endpoint_ClearEndpoints(void)
93 {
94 for (uint8_t EPNum = 0; EPNum < ENDPOINT_TOTAL_ENDPOINTS; EPNum++)
95 {
96 Endpoint_SelectEndpoint(EPNum);
97 (&AVR32_USBB.uecfg0)[EPNum] = 0;
98 (&AVR32_USBB.uecon0clr)[EPNum] = -1;
99 USB_Endpoint_FIFOPos[EPNum] = &AVR32_USBB_SLAVE[EPNum * 0x10000];
100 Endpoint_DisableEndpoint();
101 }
102 }
103
104 void Endpoint_ClearStatusStage(void)
105 {
106 if (USB_ControlRequest.bmRequestType & REQDIR_DEVICETOHOST)
107 {
108 while (!(Endpoint_IsOUTReceived()))
109 {
110 if (USB_DeviceState == DEVICE_STATE_Unattached)
111 return;
112 }
113
114 Endpoint_ClearOUT();
115 }
116 else
117 {
118 while (!(Endpoint_IsINReady()))
119 {
120 if (USB_DeviceState == DEVICE_STATE_Unattached)
121 return;
122 }
123
124 Endpoint_ClearIN();
125 }
126 }
127
128 #if !defined(CONTROL_ONLY_DEVICE)
129 uint8_t Endpoint_WaitUntilReady(void)
130 {
131 #if (USB_STREAM_TIMEOUT_MS < 0xFF)
132 uint8_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
133 #else
134 uint16_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
135 #endif
136
137 uint16_t PreviousFrameNumber = USB_Device_GetFrameNumber();
138
139 for (;;)
140 {
141 if (Endpoint_GetEndpointDirection() == ENDPOINT_DIR_IN)
142 {
143 if (Endpoint_IsINReady())
144 return ENDPOINT_READYWAIT_NoError;
145 }
146 else
147 {
148 if (Endpoint_IsOUTReceived())
149 return ENDPOINT_READYWAIT_NoError;
150 }
151
152 uint8_t USB_DeviceState_LCL = USB_DeviceState;
153
154 if (USB_DeviceState_LCL == DEVICE_STATE_Unattached)
155 return ENDPOINT_READYWAIT_DeviceDisconnected;
156 else if (USB_DeviceState_LCL == DEVICE_STATE_Suspended)
157 return ENDPOINT_READYWAIT_BusSuspended;
158 else if (Endpoint_IsStalled())
159 return ENDPOINT_READYWAIT_EndpointStalled;
160
161 uint16_t CurrentFrameNumber = USB_Device_GetFrameNumber();
162
163 if (CurrentFrameNumber != PreviousFrameNumber)
164 {
165 PreviousFrameNumber = CurrentFrameNumber;
166
167 if (!(TimeoutMSRem--))
168 return ENDPOINT_READYWAIT_Timeout;
169 }
170 }
171 }
172 #endif
173
174 #endif
175