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