Add architecture guards to all architecture-specific files, so that they can be bulk...
[pub/USBasp.git] / LUFA / Drivers / USB / Core / AVR8 / Endpoint_AVR8.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 #include "../../../../Common/Common.h"
32 #if (ARCH == ARCH_AVR8)
33
34 #define __INCLUDE_FROM_USB_DRIVER
35 #include "../USBMode.h"
36
37 #if defined(USB_CAN_BE_DEVICE)
38
39 #include "../Endpoint.h"
40
41 #if !defined(FIXED_CONTROL_ENDPOINT_SIZE)
42 uint8_t USB_Device_ControlEndpointSize = ENDPOINT_CONTROLEP_DEFAULT_SIZE;
43 #endif
44
45 bool Endpoint_ConfigureEndpoint_Prv(const uint8_t Number,
46 const uint8_t UECFG0XData,
47 const uint8_t UECFG1XData)
48 {
49 #if defined(CONTROL_ONLY_DEVICE) || defined(ORDERED_EP_CONFIG)
50 Endpoint_SelectEndpoint(Number);
51 Endpoint_EnableEndpoint();
52
53 UECFG1X = 0;
54 UECFG0X = UECFG0XData;
55 UECFG1X = UECFG1XData;
56
57 return Endpoint_IsConfigured();
58 #else
59 for (uint8_t EPNum = Number; EPNum < ENDPOINT_TOTAL_ENDPOINTS; EPNum++)
60 {
61 uint8_t UECFG0XTemp;
62 uint8_t UECFG1XTemp;
63 uint8_t UEIENXTemp;
64
65 Endpoint_SelectEndpoint(EPNum);
66
67 if (EPNum == Number)
68 {
69 UECFG0XTemp = UECFG0XData;
70 UECFG1XTemp = UECFG1XData;
71 UEIENXTemp = 0;
72 }
73 else
74 {
75 UECFG0XTemp = UECFG0X;
76 UECFG1XTemp = UECFG1X;
77 UEIENXTemp = UEIENX;
78 }
79
80 if (!(UECFG1XTemp & (1 << ALLOC)))
81 continue;
82
83 Endpoint_DisableEndpoint();
84 UECFG1X &= ~(1 << ALLOC);
85
86 Endpoint_EnableEndpoint();
87 UECFG0X = UECFG0XTemp;
88 UECFG1X = UECFG1XTemp;
89 UEIENX = UEIENXTemp;
90
91 if (!(Endpoint_IsConfigured()))
92 return false;
93 }
94
95 Endpoint_SelectEndpoint(Number);
96 return true;
97 #endif
98 }
99
100 void Endpoint_ClearEndpoints(void)
101 {
102 UEINT = 0;
103
104 for (uint8_t EPNum = 0; EPNum < ENDPOINT_TOTAL_ENDPOINTS; EPNum++)
105 {
106 Endpoint_SelectEndpoint(EPNum);
107 UEIENX = 0;
108 UEINTX = 0;
109 UECFG1X = 0;
110 Endpoint_DisableEndpoint();
111 }
112 }
113
114 void Endpoint_ClearStatusStage(void)
115 {
116 if (USB_ControlRequest.bmRequestType & REQDIR_DEVICETOHOST)
117 {
118 while (!(Endpoint_IsOUTReceived()))
119 {
120 if (USB_DeviceState == DEVICE_STATE_Unattached)
121 return;
122 }
123
124 Endpoint_ClearOUT();
125 }
126 else
127 {
128 while (!(Endpoint_IsINReady()))
129 {
130 if (USB_DeviceState == DEVICE_STATE_Unattached)
131 return;
132 }
133
134 Endpoint_ClearIN();
135 }
136 }
137
138 #if !defined(CONTROL_ONLY_DEVICE)
139 uint8_t Endpoint_WaitUntilReady(void)
140 {
141 #if (USB_STREAM_TIMEOUT_MS < 0xFF)
142 uint8_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
143 #else
144 uint16_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;
145 #endif
146
147 uint16_t PreviousFrameNumber = USB_Device_GetFrameNumber();
148
149 for (;;)
150 {
151 if (Endpoint_GetEndpointDirection() == ENDPOINT_DIR_IN)
152 {
153 if (Endpoint_IsINReady())
154 return ENDPOINT_READYWAIT_NoError;
155 }
156 else
157 {
158 if (Endpoint_IsOUTReceived())
159 return ENDPOINT_READYWAIT_NoError;
160 }
161
162 uint8_t USB_DeviceState_LCL = USB_DeviceState;
163
164 if (USB_DeviceState_LCL == DEVICE_STATE_Unattached)
165 return ENDPOINT_READYWAIT_DeviceDisconnected;
166 else if (USB_DeviceState_LCL == DEVICE_STATE_Suspended)
167 return ENDPOINT_READYWAIT_BusSuspended;
168 else if (Endpoint_IsStalled())
169 return ENDPOINT_READYWAIT_EndpointStalled;
170
171 uint16_t CurrentFrameNumber = USB_Device_GetFrameNumber();
172
173 if (CurrentFrameNumber != PreviousFrameNumber)
174 {
175 PreviousFrameNumber = CurrentFrameNumber;
176
177 if (!(TimeoutMSRem--))
178 return ENDPOINT_READYWAIT_Timeout;
179 }
180 }
181 }
182 #endif
183
184 #endif
185
186 #endif