Add architecture guards to all architecture-specific files, so that they can be bulk...
[pub/USBasp.git] / LUFA / Drivers / USB / Core / AVR8 / USBInterrupt_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 "../USBInterrupt.h"
36
37 void USB_INT_DisableAllInterrupts(void)
38 {
39 #if defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR)
40 USBCON &= ~((1 << VBUSTE) | (1 << IDTE));
41 #elif defined(USB_SERIES_4_AVR)
42 USBCON &= ~(1 << VBUSTE);
43 #endif
44
45 #if defined(USB_CAN_BE_BOTH)
46 OTGIEN = 0;
47 #endif
48
49 #if defined(USB_CAN_BE_HOST)
50 UHIEN = 0;
51 #endif
52
53 #if defined(USB_CAN_BE_DEVICE)
54 UDIEN = 0;
55 #endif
56 }
57
58 void USB_INT_ClearAllInterrupts(void)
59 {
60 #if defined(USB_SERIES_4_AVR) || defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR)
61 USBINT = 0;
62 #endif
63
64 #if defined(USB_CAN_BE_BOTH)
65 OTGINT = 0;
66 #endif
67
68 #if defined(USB_CAN_BE_HOST)
69 UHINT = 0;
70 #endif
71
72 #if defined(USB_CAN_BE_DEVICE)
73 UDINT = 0;
74 #endif
75 }
76
77 ISR(USB_GEN_vect, ISR_BLOCK)
78 {
79 #if defined(USB_CAN_BE_DEVICE)
80 #if !defined(NO_SOF_EVENTS)
81 if (USB_INT_HasOccurred(USB_INT_SOFI) && USB_INT_IsEnabled(USB_INT_SOFI))
82 {
83 USB_INT_Clear(USB_INT_SOFI);
84
85 EVENT_USB_Device_StartOfFrame();
86 }
87 #endif
88
89 #if defined(USB_SERIES_4_AVR) || defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR)
90 if (USB_INT_HasOccurred(USB_INT_VBUSTI) && USB_INT_IsEnabled(USB_INT_VBUSTI))
91 {
92 USB_INT_Clear(USB_INT_VBUSTI);
93
94 if (USB_VBUS_GetStatus())
95 {
96 if (!(USB_Options & USB_OPT_MANUAL_PLL))
97 {
98 USB_PLL_On();
99 while (!(USB_PLL_IsReady()));
100 }
101
102 USB_DeviceState = DEVICE_STATE_Powered;
103 EVENT_USB_Device_Connect();
104 }
105 else
106 {
107 if (!(USB_Options & USB_OPT_MANUAL_PLL))
108 USB_PLL_Off();
109
110 USB_DeviceState = DEVICE_STATE_Unattached;
111 EVENT_USB_Device_Disconnect();
112 }
113 }
114 #endif
115
116 if (USB_INT_HasOccurred(USB_INT_SUSPI) && USB_INT_IsEnabled(USB_INT_SUSPI))
117 {
118 USB_INT_Disable(USB_INT_SUSPI);
119 USB_INT_Enable(USB_INT_WAKEUPI);
120
121 USB_CLK_Freeze();
122
123 if (!(USB_Options & USB_OPT_MANUAL_PLL))
124 USB_PLL_Off();
125
126 #if defined(USB_SERIES_2_AVR) && !defined(NO_LIMITED_CONTROLLER_CONNECT)
127 USB_DeviceState = DEVICE_STATE_Unattached;
128 EVENT_USB_Device_Disconnect();
129 #else
130 USB_DeviceState = DEVICE_STATE_Suspended;
131 EVENT_USB_Device_Suspend();
132 #endif
133 }
134
135 if (USB_INT_HasOccurred(USB_INT_WAKEUPI) && USB_INT_IsEnabled(USB_INT_WAKEUPI))
136 {
137 if (!(USB_Options & USB_OPT_MANUAL_PLL))
138 {
139 USB_PLL_On();
140 while (!(USB_PLL_IsReady()));
141 }
142
143 USB_CLK_Unfreeze();
144
145 USB_INT_Clear(USB_INT_WAKEUPI);
146
147 USB_INT_Disable(USB_INT_WAKEUPI);
148 USB_INT_Enable(USB_INT_SUSPI);
149
150 if (USB_Device_ConfigurationNumber)
151 USB_DeviceState = DEVICE_STATE_Configured;
152 else
153 USB_DeviceState = (USB_Device_IsAddressSet()) ? DEVICE_STATE_Configured : DEVICE_STATE_Powered;
154
155 #if defined(USB_SERIES_2_AVR) && !defined(NO_LIMITED_CONTROLLER_CONNECT)
156 EVENT_USB_Device_Connect();
157 #else
158 EVENT_USB_Device_WakeUp();
159 #endif
160 }
161
162 if (USB_INT_HasOccurred(USB_INT_EORSTI) && USB_INT_IsEnabled(USB_INT_EORSTI))
163 {
164 USB_INT_Clear(USB_INT_EORSTI);
165
166 USB_DeviceState = DEVICE_STATE_Default;
167 USB_Device_ConfigurationNumber = 0;
168
169 USB_INT_Clear(USB_INT_SUSPI);
170 USB_INT_Disable(USB_INT_SUSPI);
171 USB_INT_Enable(USB_INT_WAKEUPI);
172
173 Endpoint_ConfigureEndpoint(ENDPOINT_CONTROLEP, EP_TYPE_CONTROL,
174 ENDPOINT_DIR_OUT, USB_Device_ControlEndpointSize,
175 ENDPOINT_BANK_SINGLE);
176
177 #if defined(INTERRUPT_CONTROL_ENDPOINT)
178 USB_INT_Enable(USB_INT_RXSTPI);
179 #endif
180
181 EVENT_USB_Device_Reset();
182 }
183 #endif
184
185 #if defined(USB_CAN_BE_HOST)
186 #if !defined(NO_SOF_EVENTS)
187 if (USB_INT_HasOccurred(USB_INT_HSOFI) && USB_INT_IsEnabled(USB_INT_HSOFI))
188 {
189 USB_INT_Clear(USB_INT_HSOFI);
190
191 EVENT_USB_Host_StartOfFrame();
192 }
193 #endif
194
195 if (USB_INT_HasOccurred(USB_INT_DDISCI) && USB_INT_IsEnabled(USB_INT_DDISCI))
196 {
197 USB_INT_Clear(USB_INT_DDISCI);
198 USB_INT_Clear(USB_INT_DCONNI);
199 USB_INT_Disable(USB_INT_DDISCI);
200
201 EVENT_USB_Host_DeviceUnattached();
202
203 USB_ResetInterface();
204 }
205
206 if (USB_INT_HasOccurred(USB_INT_VBERRI) && USB_INT_IsEnabled(USB_INT_VBERRI))
207 {
208 USB_INT_Clear(USB_INT_VBERRI);
209
210 USB_Host_VBUS_Manual_Off();
211 USB_Host_VBUS_Auto_Off();
212
213 EVENT_USB_Host_HostError(HOST_ERROR_VBusVoltageDip);
214 EVENT_USB_Host_DeviceUnattached();
215
216 USB_HostState = HOST_STATE_Unattached;
217 }
218
219 if (USB_INT_HasOccurred(USB_INT_SRPI) && USB_INT_IsEnabled(USB_INT_SRPI))
220 {
221 USB_INT_Clear(USB_INT_SRPI);
222 USB_INT_Disable(USB_INT_SRPI);
223
224 EVENT_USB_Host_DeviceAttached();
225
226 USB_INT_Enable(USB_INT_DDISCI);
227
228 USB_HostState = HOST_STATE_Powered;
229 }
230
231 if (USB_INT_HasOccurred(USB_INT_BCERRI) && USB_INT_IsEnabled(USB_INT_BCERRI))
232 {
233 USB_INT_Clear(USB_INT_BCERRI);
234
235 EVENT_USB_Host_DeviceEnumerationFailed(HOST_ENUMERROR_NoDeviceDetected, 0);
236 EVENT_USB_Host_DeviceUnattached();
237
238 USB_ResetInterface();
239 }
240 #endif
241
242 #if defined(USB_CAN_BE_BOTH)
243 if (USB_INT_HasOccurred(USB_INT_IDTI) && USB_INT_IsEnabled(USB_INT_IDTI))
244 {
245 USB_INT_Clear(USB_INT_IDTI);
246
247 if (USB_DeviceState != DEVICE_STATE_Unattached)
248 EVENT_USB_Device_Disconnect();
249
250 if (USB_HostState != HOST_STATE_Unattached)
251 EVENT_USB_Host_DeviceUnattached();
252
253 USB_CurrentMode = USB_GetUSBModeFromUID();
254 USB_ResetInterface();
255
256 EVENT_USB_UIDChange();
257 }
258 #endif
259 }
260
261 #if defined(INTERRUPT_CONTROL_ENDPOINT) && defined(USB_CAN_BE_DEVICE)
262 ISR(USB_COM_vect, ISR_BLOCK)
263 {
264 uint8_t PrevSelectedEndpoint = Endpoint_GetCurrentEndpoint();
265
266 Endpoint_SelectEndpoint(ENDPOINT_CONTROLEP);
267 USB_INT_Disable(USB_INT_RXSTPI);
268
269 GlobalInterruptEnable();
270
271 USB_Device_ProcessControlRequest();
272
273 Endpoint_SelectEndpoint(ENDPOINT_CONTROLEP);
274 USB_INT_Enable(USB_INT_RXSTPI);
275 Endpoint_SelectEndpoint(PrevSelectedEndpoint);
276 }
277 #endif
278
279 #endif
280