a9ad5661175b28cda439f29ccd8d8406b118407a
[pub/USBasp.git] / LUFA / Drivers / USB / Core / UC3 / USBInterrupt_UC3.h
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 /** \file
32 * \brief USB Controller Interrupt definitions for the AVR32 UC3 microcontrollers.
33 *
34 * This file contains definitions required for the correct handling of low level USB service routine interrupts
35 * from the USB controller.
36 *
37 * \note This file should not be included directly. It is automatically included as needed by the USB driver
38 * dispatch header located in LUFA/Drivers/USB/USB.h.
39 */
40
41 #ifndef __USBINTERRUPT_UC3_H__
42 #define __USBINTERRUPT_UC3_H__
43
44 /* Includes: */
45 #include "../../../../Common/Common.h"
46
47 /* Enable C linkage for C++ Compilers: */
48 #if defined(__cplusplus)
49 extern "C" {
50 #endif
51
52 /* Preprocessor Checks: */
53 #if !defined(__INCLUDE_FROM_USB_DRIVER)
54 #error Do not include this file directly. Include LUFA/Drivers/USB/USB.h instead.
55 #endif
56
57 /* Private Interface - For use in library only: */
58 #if !defined(__DOXYGEN__)
59 /* Macros: */
60 #define LUFA_ISR(Name) void Name (void) __attribute__((__interrupt__)); void Name (void)
61
62 /* Enums: */
63 enum USB_Interrupts_t
64 {
65 USB_INT_VBUSTI = 0,
66 #if (defined(USB_CAN_BE_BOTH) || defined(__DOXYGEN__))
67 USB_INT_IDTI = 1,
68 #endif
69 #if (defined(USB_CAN_BE_DEVICE) || defined(__DOXYGEN__))
70 USB_INT_WAKEUPI = 2,
71 USB_INT_SUSPI = 3,
72 USB_INT_EORSTI = 4,
73 USB_INT_SOFI = 5,
74 #endif
75 #if (defined(USB_CAN_BE_HOST) || defined(__DOXYGEN__))
76 USB_INT_HSOFI = 6,
77 USB_INT_DCONNI = 7,
78 USB_INT_DDISCI = 8,
79 USB_INT_RSTI = 9,
80 USB_INT_BCERRI = 10,
81 USB_INT_VBERRI = 11,
82 #endif
83 };
84
85 /* Inline Functions: */
86 static inline uint_reg_t USB_INT_GetGlobalEnableState(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;
87 static inline uint_reg_t USB_INT_GetGlobalEnableState(void)
88 {
89 GCC_MEMORY_BARRIER();
90 return __builtin_mfsr(AVR32_SR);
91 }
92
93 static inline void USB_INT_SetGlobalEnableState(uint_reg_t GlobalIntState) ATTR_ALWAYS_INLINE;
94 static inline void USB_INT_SetGlobalEnableState(uint_reg_t GlobalIntState)
95 {
96 GCC_MEMORY_BARRIER();
97 if (GlobalIntState & AVR32_SR_GM)
98 __builtin_ssrf(AVR32_SR_GM_OFFSET);
99 GCC_MEMORY_BARRIER();
100 }
101
102 static inline void USB_INT_GlobalEnable(void) ATTR_ALWAYS_INLINE;
103 static inline void USB_INT_GlobalEnable(void)
104 {
105 GCC_MEMORY_BARRIER();
106 __builtin_csrf(AVR32_SR_GM_OFFSET);
107 GCC_MEMORY_BARRIER();
108 }
109
110 static inline void USB_INT_GlobalDisable(void) ATTR_ALWAYS_INLINE;
111 static inline void USB_INT_GlobalDisable(void)
112 {
113 GCC_MEMORY_BARRIER();
114 __builtin_ssrf(AVR32_SR_GM_OFFSET);
115 GCC_MEMORY_BARRIER();
116 }
117
118 static inline void USB_INT_Enable(const uint8_t Interrupt) ATTR_ALWAYS_INLINE;
119 static inline void USB_INT_Enable(const uint8_t Interrupt)
120 {
121 switch (Interrupt)
122 {
123 case USB_INT_VBUSTI:
124 AVR32_USBB.USBCON.vbuste = true;
125 break;
126 #if defined(USB_CAN_BE_BOTH)
127 case USB_INT_IDTI:
128 AVR32_USBB.USBCON.idte = true;
129 break;
130 #endif
131 #if defined(USB_CAN_BE_DEVICE)
132 case USB_INT_WAKEUPI:
133 AVR32_USBB.UDINTESET.wakeupes = true;
134 break;
135 case USB_INT_SUSPI:
136 AVR32_USBB.UDINTESET.suspes = true;
137 break;
138 case USB_INT_EORSTI:
139 AVR32_USBB.UDINTESET.eorstes = true;
140 break;
141 case USB_INT_SOFI:
142 AVR32_USBB.UDINTESET.sofes = true;
143 break;
144 #endif
145 #if defined(USB_CAN_BE_HOST)
146 case USB_INT_HSOFI:
147 AVR32_USBB.UHINTESET.hsofies = true;
148 break;
149 case USB_INT_DCONNI:
150 AVR32_USBB.UHINTESET.dconnies = true;
151 break;
152 case USB_INT_DDISCI:
153 AVR32_USBB.UHINTESET.ddiscies = true;
154 break;
155 case USB_INT_RSTI:
156 AVR32_USBB.UHINTESET.rsties = true;
157 break;
158 case USB_INT_BCERRI:
159 AVR32_USBB.USBCON.bcerre = true;
160 break;
161 case USB_INT_VBERRI:
162 AVR32_USBB.USBCON.vberre = true;
163 break;
164 #endif
165 }
166 }
167
168 static inline void USB_INT_Disable(const uint8_t Interrupt) ATTR_ALWAYS_INLINE;
169 static inline void USB_INT_Disable(const uint8_t Interrupt)
170 {
171 switch (Interrupt)
172 {
173 case USB_INT_VBUSTI:
174 AVR32_USBB.USBCON.vbuste = false;
175 break;
176 #if defined(USB_CAN_BE_BOTH)
177 case USB_INT_IDTI:
178 AVR32_USBB.USBCON.idte = false;
179 break;
180 #endif
181 #if defined(USB_CAN_BE_DEVICE)
182 case USB_INT_WAKEUPI:
183 AVR32_USBB.UDINTECLR.wakeupec = true;
184 break;
185 case USB_INT_SUSPI:
186 AVR32_USBB.UDINTECLR.suspec = true;
187 break;
188 case USB_INT_EORSTI:
189 AVR32_USBB.UDINTECLR.eorstec = true;
190 break;
191 case USB_INT_SOFI:
192 AVR32_USBB.UDINTECLR.sofec = true;
193 break;
194 #endif
195 #if defined(USB_CAN_BE_HOST)
196 case USB_INT_HSOFI:
197 AVR32_USBB.UHINTECLR.hsofiec = true;
198 break;
199 case USB_INT_DCONNI:
200 AVR32_USBB.UHINTECLR.dconniec = true;
201 break;
202 case USB_INT_DDISCI:
203 AVR32_USBB.UHINTECLR.ddisciec = true;
204 break;
205 case USB_INT_RSTI:
206 AVR32_USBB.UHINTECLR.rstiec = true;
207 break;
208 case USB_INT_BCERRI:
209 AVR32_USBB.USBCON.bcerre = false;
210 break;
211 case USB_INT_VBERRI:
212 AVR32_USBB.USBCON.vberre = false;
213 break;
214 #endif
215 }
216 }
217
218 static inline void USB_INT_Clear(const uint8_t Interrupt) ATTR_ALWAYS_INLINE;
219 static inline void USB_INT_Clear(const uint8_t Interrupt)
220 {
221 switch (Interrupt)
222 {
223 case USB_INT_VBUSTI:
224 AVR32_USBB.USBSTACLR.vbustic = true;
225 (void)AVR32_USBB.USBSTACLR;
226 break;
227 #if defined(USB_CAN_BE_BOTH)
228 case USB_INT_IDTI:
229 AVR32_USBB.USBSTACLR.idtic = true;
230 (void)AVR32_USBB.USBSTACLR;
231 break;
232 #endif
233 #if defined(USB_CAN_BE_DEVICE)
234 case USB_INT_WAKEUPI:
235 AVR32_USBB.UDINTCLR.wakeupc = true;
236 (void)AVR32_USBB.UDINTCLR;
237 break;
238 case USB_INT_SUSPI:
239 AVR32_USBB.UDINTCLR.suspc = true;
240 (void)AVR32_USBB.UDINTCLR;
241 break;
242 case USB_INT_EORSTI:
243 AVR32_USBB.UDINTCLR.eorstc = true;
244 (void)AVR32_USBB.UDINTCLR;
245 break;
246 case USB_INT_SOFI:
247 AVR32_USBB.UDINTCLR.sofc = true;
248 (void)AVR32_USBB.UDINTCLR;
249 break;
250 #endif
251 #if defined(USB_CAN_BE_HOST)
252 case USB_INT_HSOFI:
253 AVR32_USBB.UHINTCLR.hsofic = true;
254 (void)AVR32_USBB.UHINTCLR;
255 break;
256 case USB_INT_DCONNI:
257 AVR32_USBB.UHINTCLR.dconnic = true;
258 (void)AVR32_USBB.UHINTCLR;
259 break;
260 case USB_INT_DDISCI:
261 AVR32_USBB.UHINTCLR.ddiscic = true;
262 (void)AVR32_USBB.UHINTCLR;
263 break;
264 case USB_INT_RSTI:
265 AVR32_USBB.UHINTCLR.rstic = true;
266 (void)AVR32_USBB.UHINTCLR;
267 break;
268 case USB_INT_BCERRI:
269 AVR32_USBB.USBSTACLR.bcerric = true;
270 (void)AVR32_USBB.USBSTACLR;
271 break;
272 case USB_INT_VBERRI:
273 AVR32_USBB.USBSTACLR.vberric = true;
274 (void)AVR32_USBB.USBSTACLR;
275 break;
276 #endif
277 }
278 }
279
280 static inline bool USB_INT_IsEnabled(const uint8_t Interrupt) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;
281 static inline bool USB_INT_IsEnabled(const uint8_t Interrupt)
282 {
283 switch (Interrupt)
284 {
285 case USB_INT_VBUSTI:
286 return AVR32_USBB.USBCON.vbuste;
287 #if defined(USB_CAN_BE_BOTH)
288 case USB_INT_IDTI:
289 return AVR32_USBB.USBCON.idte;
290 #endif
291 #if defined(USB_CAN_BE_DEVICE)
292 case USB_INT_WAKEUPI:
293 return AVR32_USBB.UDINTE.wakeupe;
294 case USB_INT_SUSPI:
295 return AVR32_USBB.UDINTE.suspe;
296 case USB_INT_EORSTI:
297 return AVR32_USBB.UDINTE.eorste;
298 case USB_INT_SOFI:
299 return AVR32_USBB.UDINTE.sofe;
300 #endif
301 #if defined(USB_CAN_BE_HOST)
302 case USB_INT_HSOFI:
303 return AVR32_USBB.UHINTE.hsofie;
304 case USB_INT_DCONNI:
305 return AVR32_USBB.UHINTE.dconnie;
306 case USB_INT_DDISCI:
307 return AVR32_USBB.UHINTE.ddiscie;
308 case USB_INT_RSTI:
309 return AVR32_USBB.UHINTE.rstie;
310 case USB_INT_BCERRI:
311 return AVR32_USBB.USBCON.bcerre;
312 case USB_INT_VBERRI:
313 return AVR32_USBB.USBCON.vberre;
314 #endif
315 }
316
317 return false;
318 }
319
320 static inline bool USB_INT_HasOccurred(const uint8_t Interrupt) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;
321 static inline bool USB_INT_HasOccurred(const uint8_t Interrupt)
322 {
323 switch (Interrupt)
324 {
325 case USB_INT_VBUSTI:
326 return AVR32_USBB.USBSTA.vbusti;
327 #if defined(USB_CAN_BE_BOTH)
328 case USB_INT_IDTI:
329 return AVR32_USBB.USBSTA.idti;
330 #endif
331 #if defined(USB_CAN_BE_DEVICE)
332 case USB_INT_WAKEUPI:
333 return AVR32_USBB.UDINT.wakeup;
334 case USB_INT_SUSPI:
335 return AVR32_USBB.UDINT.susp;
336 case USB_INT_EORSTI:
337 return AVR32_USBB.UDINT.eorst;
338 case USB_INT_SOFI:
339 return AVR32_USBB.UDINT.sof;
340 #endif
341 #if defined(USB_CAN_BE_HOST)
342 case USB_INT_HSOFI:
343 return AVR32_USBB.UHINT.hsofi;
344 case USB_INT_DCONNI:
345 return AVR32_USBB.UHINT.dconni;
346 case USB_INT_DDISCI:
347 return AVR32_USBB.UHINT.ddisci;
348 case USB_INT_RSTI:
349 return AVR32_USBB.UHINT.rsti;
350 case USB_INT_BCERRI:
351 return AVR32_USBB.USBSTA.bcerri;
352 case USB_INT_VBERRI:
353 return AVR32_USBB.USBSTA.vberri;
354 #endif
355 }
356
357 return false;
358 }
359
360 /* Includes: */
361 #include "../USBMode.h"
362 #include "../Events.h"
363 #include "../USBController.h"
364
365 /* Function Prototypes: */
366 void USB_INT_ClearAllInterrupts(void);
367 void USB_INT_DisableAllInterrupts(void);
368 #endif
369
370 /* Public Interface - May be used in end-application: */
371 /* ISR Prototypes: */
372 #if defined(__DOXYGEN__)
373 /** Interrupt service routine handler for the USB controller ISR group. This interrupt routine <b>must</b> be
374 * linked to the entire USB controller ISR vector group inside the AVR32's interrupt controller peripheral,
375 * using the user application's preferred USB controller driver.
376 */
377 void USB_GEN_vect(void);
378 #else
379 LUFA_ISR(USB_GEN_vect);
380 #endif
381
382 /* Disable C linkage for C++ Compilers: */
383 #if defined(__cplusplus)
384 }
385 #endif
386
387 #endif
388