Add in a new common Delay_MS() function, which provides a blocking delay for all...
[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) & AVR32_SR_GM);
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 __builtin_ssrf(AVR32_SR_GM_OFFSET, GlobalIntState);
98 GCC_MEMORY_BARRIER();
99 }
100
101 static inline void USB_INT_GlobalEnable(void) ATTR_ALWAYS_INLINE;
102 static inline void USB_INT_GlobalEnable(void)
103 {
104 GCC_MEMORY_BARRIER();
105 __builtin_csrf(AVR32_SR_GM_OFFSET);
106 GCC_MEMORY_BARRIER();
107 }
108
109 static inline void USB_INT_GlobalDisable(void) ATTR_ALWAYS_INLINE;
110 static inline void USB_INT_GlobalDisable(void)
111 {
112 GCC_MEMORY_BARRIER();
113 __builtin_ssrf(AVR32_SR_GM_OFFSET);
114 GCC_MEMORY_BARRIER();
115 }
116
117 static inline void USB_INT_Enable(const uint8_t Interrupt) ATTR_ALWAYS_INLINE;
118 static inline void USB_INT_Enable(const uint8_t Interrupt)
119 {
120 switch (Interrupt)
121 {
122 case USB_INT_VBUSTI:
123 AVR32_USBB.USBCON.vbuste = true;
124 break;
125 #if defined(USB_CAN_BE_BOTH)
126 case USB_INT_IDTI:
127 AVR32_USBB.USBCON.idte = true;
128 break;
129 #endif
130 #if defined(USB_CAN_BE_DEVICE)
131 case USB_INT_WAKEUPI:
132 AVR32_USBB.UDINTESET.wakeupes = true;
133 break;
134 case USB_INT_SUSPI:
135 AVR32_USBB.UDINTESET.suspes = true;
136 break;
137 case USB_INT_EORSTI:
138 AVR32_USBB.UDINTESET.eorstes = true;
139 break;
140 case USB_INT_SOFI:
141 AVR32_USBB.UDINTESET.sofes = true;
142 break;
143 #endif
144 #if defined(USB_CAN_BE_HOST)
145 case USB_INT_HSOFI:
146 AVR32_USBB.UHINTESET.hsofies = true;
147 break;
148 case USB_INT_DCONNI:
149 AVR32_USBB.UHINTESET.dconnies = true;
150 break;
151 case USB_INT_DDISCI:
152 AVR32_USBB.UHINTESET.ddiscies = true;
153 break;
154 case USB_INT_RSTI:
155 AVR32_USBB.UHINTESET.rsties = true;
156 break;
157 case USB_INT_BCERRI:
158 AVR32_USBB.USBCON.bcerre = true;
159 break;
160 case USB_INT_VBERRI:
161 AVR32_USBB.USBCON.vberre = true;
162 break;
163 #endif
164 }
165 }
166
167 static inline void USB_INT_Disable(const uint8_t Interrupt) ATTR_ALWAYS_INLINE;
168 static inline void USB_INT_Disable(const uint8_t Interrupt)
169 {
170 switch (Interrupt)
171 {
172 case USB_INT_VBUSTI:
173 AVR32_USBB.USBCON.vbuste = false;
174 break;
175 #if defined(USB_CAN_BE_BOTH)
176 case USB_INT_IDTI:
177 AVR32_USBB.USBCON.idte = false;
178 break;
179 #endif
180 #if defined(USB_CAN_BE_DEVICE)
181 case USB_INT_WAKEUPI:
182 AVR32_USBB.UDINTECLR.wakeupec = true;
183 break;
184 case USB_INT_SUSPI:
185 AVR32_USBB.UDINTECLR.suspec = true;
186 break;
187 case USB_INT_EORSTI:
188 AVR32_USBB.UDINTECLR.eorstec = true;
189 break;
190 case USB_INT_SOFI:
191 AVR32_USBB.UDINTECLR.sofec = true;
192 break;
193 #endif
194 #if defined(USB_CAN_BE_HOST)
195 case USB_INT_HSOFI:
196 AVR32_USBB.UHINTECLR.hsofiec = true;
197 break;
198 case USB_INT_DCONNI:
199 AVR32_USBB.UHINTECLR.dconniec = true;
200 break;
201 case USB_INT_DDISCI:
202 AVR32_USBB.UHINTECLR.ddisciec = true;
203 break;
204 case USB_INT_RSTI:
205 AVR32_USBB.UHINTECLR.rstiec = true;
206 break;
207 case USB_INT_BCERRI:
208 AVR32_USBB.USBCON.bcerre = false;
209 break;
210 case USB_INT_VBERRI:
211 AVR32_USBB.USBCON.vberre = false;
212 break;
213 #endif
214 }
215 }
216
217 static inline void USB_INT_Clear(const uint8_t Interrupt) ATTR_ALWAYS_INLINE;
218 static inline void USB_INT_Clear(const uint8_t Interrupt)
219 {
220 switch (Interrupt)
221 {
222 case USB_INT_VBUSTI:
223 AVR32_USBB.USBSTACLR.vbustic = true;
224 (void)AVR32_USBB.USBSTACLR;
225 break;
226 #if defined(USB_CAN_BE_BOTH)
227 case USB_INT_IDTI:
228 AVR32_USBB.USBSTACLR.idtic = true;
229 (void)AVR32_USBB.USBSTACLR;
230 break;
231 #endif
232 #if defined(USB_CAN_BE_DEVICE)
233 case USB_INT_WAKEUPI:
234 AVR32_USBB.UDINTCLR.wakeupc = true;
235 (void)AVR32_USBB.UDINTCLR;
236 break;
237 case USB_INT_SUSPI:
238 AVR32_USBB.UDINTCLR.suspc = true;
239 (void)AVR32_USBB.UDINTCLR;
240 break;
241 case USB_INT_EORSTI:
242 AVR32_USBB.UDINTCLR.eorstc = true;
243 (void)AVR32_USBB.UDINTCLR;
244 break;
245 case USB_INT_SOFI:
246 AVR32_USBB.UDINTCLR.sofc = true;
247 (void)AVR32_USBB.UDINTCLR;
248 break;
249 #endif
250 #if defined(USB_CAN_BE_HOST)
251 case USB_INT_HSOFI:
252 AVR32_USBB.UHINTCLR.hsofic = true;
253 (void)AVR32_USBB.UHINTCLR;
254 break;
255 case USB_INT_DCONNI:
256 AVR32_USBB.UHINTCLR.dconnic = true;
257 (void)AVR32_USBB.UHINTCLR;
258 break;
259 case USB_INT_DDISCI:
260 AVR32_USBB.UHINTCLR.ddiscic = true;
261 (void)AVR32_USBB.UHINTCLR;
262 break;
263 case USB_INT_RSTI:
264 AVR32_USBB.UHINTCLR.rstic = true;
265 (void)AVR32_USBB.UHINTCLR;
266 break;
267 case USB_INT_BCERRI:
268 AVR32_USBB.USBSTACLR.bcerric = true;
269 (void)AVR32_USBB.USBSTACLR;
270 break;
271 case USB_INT_VBERRI:
272 AVR32_USBB.USBSTACLR.vberric = true;
273 (void)AVR32_USBB.USBSTACLR;
274 break;
275 #endif
276 }
277 }
278
279 static inline bool USB_INT_IsEnabled(const uint8_t Interrupt) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;
280 static inline bool USB_INT_IsEnabled(const uint8_t Interrupt)
281 {
282 switch (Interrupt)
283 {
284 case USB_INT_VBUSTI:
285 return AVR32_USBB.USBCON.vbuste;
286 #if defined(USB_CAN_BE_BOTH)
287 case USB_INT_IDTI:
288 return AVR32_USBB.USBCON.idte;
289 #endif
290 #if defined(USB_CAN_BE_DEVICE)
291 case USB_INT_WAKEUPI:
292 return AVR32_USBB.UDINTE.wakeupe;
293 case USB_INT_SUSPI:
294 return AVR32_USBB.UDINTE.suspe;
295 case USB_INT_EORSTI:
296 return AVR32_USBB.UDINTE.eorste;
297 case USB_INT_SOFI:
298 return AVR32_USBB.UDINTE.sofe;
299 #endif
300 #if defined(USB_CAN_BE_HOST)
301 case USB_INT_HSOFI:
302 return AVR32_USBB.UHINTE.hsofie;
303 case USB_INT_DCONNI:
304 return AVR32_USBB.UHINTE.dconnie;
305 case USB_INT_DDISCI:
306 return AVR32_USBB.UHINTE.ddiscie;
307 case USB_INT_RSTI:
308 return AVR32_USBB.UHINTE.rstie;
309 case USB_INT_BCERRI:
310 return AVR32_USBB.USBCON.bcerre;
311 case USB_INT_VBERRI:
312 return AVR32_USBB.USBCON.vberre;
313 #endif
314 }
315
316 return false;
317 }
318
319 static inline bool USB_INT_HasOccurred(const uint8_t Interrupt) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT;
320 static inline bool USB_INT_HasOccurred(const uint8_t Interrupt)
321 {
322 switch (Interrupt)
323 {
324 case USB_INT_VBUSTI:
325 return AVR32_USBB.USBSTA.vbusti;
326 #if defined(USB_CAN_BE_BOTH)
327 case USB_INT_IDTI:
328 return AVR32_USBB.USBSTA.idti;
329 #endif
330 #if defined(USB_CAN_BE_DEVICE)
331 case USB_INT_WAKEUPI:
332 return AVR32_USBB.UDINT.wakeup;
333 case USB_INT_SUSPI:
334 return AVR32_USBB.UDINT.susp;
335 case USB_INT_EORSTI:
336 return AVR32_USBB.UDINT.eorst;
337 case USB_INT_SOFI:
338 return AVR32_USBB.UDINT.sof;
339 #endif
340 #if defined(USB_CAN_BE_HOST)
341 case USB_INT_HSOFI:
342 return AVR32_USBB.UHINT.hsofi;
343 case USB_INT_DCONNI:
344 return AVR32_USBB.UHINT.dconni;
345 case USB_INT_DDISCI:
346 return AVR32_USBB.UHINT.ddisci;
347 case USB_INT_RSTI:
348 return AVR32_USBB.UHINT.rsti;
349 case USB_INT_BCERRI:
350 return AVR32_USBB.USBSTA.bcerri;
351 case USB_INT_VBERRI:
352 return AVR32_USBB.USBSTA.vberri;
353 #endif
354 }
355
356 return false;
357 }
358
359 /* Includes: */
360 #include "../USBMode.h"
361 #include "../Events.h"
362 #include "../USBController.h"
363
364 /* Function Prototypes: */
365 void USB_INT_ClearAllInterrupts(void);
366 void USB_INT_DisableAllInterrupts(void);
367 #endif
368
369 /* Public Interface - May be used in end-application: */
370 /* ISR Prototypes: */
371 #if defined(__DOXYGEN__)
372 /** Interrupt service routine handler for the USB controller ISR group. This interrupt routine <b>must</b> be
373 * linked to the entire USB controller ISR vector group inside the AVR32's interrupt controller peripheral,
374 * using the user application's preferred USB controller driver.
375 */
376 void USB_GEN_vect(void);
377 #else
378 LUFA_ISR(USB_GEN_vect);
379 #endif
380
381 /* Disable C linkage for C++ Compilers: */
382 #if defined(__cplusplus)
383 }
384 #endif
385
386 #endif
387