Removed Pipe_ClearErrorFlags(), pipe error flags are now automatically cleared when...
[pub/USBasp.git] / LUFA / Drivers / USB / Core / UC3B / USBInterrupt_UC3B.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 EVENT_USB_Device_Reset();
168 }
169 #endif
170
171 #if defined(USB_CAN_BE_HOST)
172 #if !defined(NO_SOF_EVENTS)
173 if (USB_INT_HasOccurred(USB_INT_HSOFI) && USB_INT_IsEnabled(USB_INT_HSOFI))
174 {
175 USB_INT_Clear(USB_INT_HSOFI);
176
177 EVENT_USB_Host_StartOfFrame();
178 }
179 #endif
180
181 if (USB_INT_HasOccurred(USB_INT_DDISCI) && USB_INT_IsEnabled(USB_INT_DDISCI))
182 {
183 USB_INT_Clear(USB_INT_DDISCI);
184 USB_INT_Clear(USB_INT_DCONNI);
185 USB_INT_Disable(USB_INT_DDISCI);
186
187 EVENT_USB_Host_DeviceUnattached();
188
189 USB_ResetInterface();
190 }
191
192 if (USB_INT_HasOccurred(USB_INT_VBERRI) && USB_INT_IsEnabled(USB_INT_VBERRI))
193 {
194 USB_INT_Clear(USB_INT_VBERRI);
195
196 USB_Host_VBUS_Manual_Off();
197 USB_Host_VBUS_Auto_Off();
198
199 EVENT_USB_Host_HostError(HOST_ERROR_VBusVoltageDip);
200 EVENT_USB_Host_DeviceUnattached();
201
202 USB_HostState = HOST_STATE_Unattached;
203 }
204
205 if (USB_INT_HasOccurred(USB_INT_SRPI) && USB_INT_IsEnabled(USB_INT_SRPI))
206 {
207 USB_INT_Clear(USB_INT_SRPI);
208 USB_INT_Disable(USB_INT_SRPI);
209
210 EVENT_USB_Host_DeviceAttached();
211
212 USB_INT_Enable(USB_INT_DDISCI);
213
214 USB_HostState = HOST_STATE_Powered;
215 }
216
217 if (USB_INT_HasOccurred(USB_INT_BCERRI) && USB_INT_IsEnabled(USB_INT_BCERRI))
218 {
219 USB_INT_Clear(USB_INT_BCERRI);
220
221 EVENT_USB_Host_DeviceEnumerationFailed(HOST_ENUMERROR_NoDeviceDetected, 0);
222 EVENT_USB_Host_DeviceUnattached();
223
224 USB_ResetInterface();
225 }
226 #endif
227
228 #if defined(USB_CAN_BE_BOTH)
229 if (USB_INT_HasOccurred(USB_INT_IDTI) && USB_INT_IsEnabled(USB_INT_IDTI))
230 {
231 USB_INT_Clear(USB_INT_IDTI);
232
233 if (USB_DeviceState != DEVICE_STATE_Unattached)
234 EVENT_USB_Device_Disconnect();
235
236 if (USB_HostState != HOST_STATE_Unattached)
237 EVENT_USB_Host_DeviceUnattached();
238
239 USB_CurrentMode = USB_GetUSBModeFromUID();
240 USB_ResetInterface();
241
242 EVENT_USB_UIDChange();
243 }
244 #endif
245 }