3 Copyright (C) Dean Camera, 2009.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
12 Permission to use, copy, modify, and distribute this software
13 and its documentation for any purpose and without fee is hereby
14 granted, provided that the above copyright notice appear in all
15 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.
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
31 /** \ingroup Group_USB
32 * @defgroup Group_USBInterrupt Endpoint and Pipe Interrupts
34 * This module manages the main USB interrupt vector, for handling such events as VBUS interrupts
35 * (on supported USB AVR models), device connections and disconnections, etc. as well as providing
36 * easy to use macros for the management of the unified Endpoint/Pipe interrupt vector.
41 #ifndef __USBINTERRUPT_H__
42 #define __USBINTERRUPT_H__
48 #include "../../../Common/Common.h"
49 #include "../LowLevel/LowLevel.h"
53 /* Enable C linkage for C++ Compilers: */
54 #if defined(__cplusplus)
58 /* Public Interface - May be used in end-application: */
60 /** Vector name for the common endpoint and pipe vector. This can be used to write an ISR handler
61 * for the endpoint and pipe events, to make certain USB functions interrupt rather than poll
64 #define ENDPOINT_PIPE_vect USB_COM_vect
66 /** Enables the given USB interrupt vector (such as the ENDPOINT_INT_* and PIPE_INT_* vectors in
67 * Endpoint.h and Pipe.h).
69 #define USB_INT_Enable(int) MACROS{ USB_INT_GET_EN_REG(int) |= USB_INT_GET_EN_MASK(int); }MACROE
71 /** Disables the given USB interrupt vector.
73 * \see USB_INT_Enable()
75 #define USB_INT_Disable(int) MACROS{ USB_INT_GET_EN_REG(int) &= ~(USB_INT_GET_EN_MASK(int)); }MACROE
77 /** Resets the given USB interrupt flag, so that the interrupt is re-primed for the next firing. */
78 #define USB_INT_Clear(int) MACROS{ USB_INT_GET_INT_REG(int) &= ~(USB_INT_GET_INT_MASK(int)); }MACROE
80 /** Returns boolean false if the given USB interrupt is disabled, or true if the interrupt is currently
83 #define USB_INT_IsEnabled(int) ((USB_INT_GET_EN_REG(int) & USB_INT_GET_EN_MASK(int)) ? true : false)
85 /** Returns boolean true if the given interrupt flag is set (i.e. the condition for the interrupt has occurred,
86 * but the interrupt vector is not necessarily enabled), otherwise returns false.
88 #define USB_INT_HasOccurred(int) ((USB_INT_GET_INT_REG(int) & USB_INT_GET_INT_MASK(int)) ? true : false)
90 /* Throwable Events: */
91 /** This module raises the USB Connected interrupt when the AVR is attached to a host while in device
94 * \note For the smaller USB AVRs (AT90USBXX2) with limited USB controllers, VBUS is not available to the USB controller.
95 * this means that the current connection state is derived from the bus suspension and wake up events by default,
96 * which is not always accurate (host may suspend the bus while still connected). If the actual connection state
97 * needs to be determined, VBUS should be routed to an external pin, and the auto-detect behaviour turned off by
98 * passing the NO_LIMITED_CONTROLLER_CONNECT token to the compiler via the -D switch at compile time. The connection
99 * and disconnection events may be manually fired by RAISE_EVENT(), and the USB_IsConnected global changed manually.
101 RAISES_EVENT(USB_Connect
);
103 /** This module raises the USB Disconnected interrupt when the AVR is removed from a host while in
106 * \note For the smaller USB AVRs (AT90USBXX2) with limited USB controllers, VBUS is not available to the USB controller.
107 * this means that the current connection state is derived from the bus suspension and wake up events by default,
108 * which is not always accurate (host may suspend the bus while still connected). If the actual connection state
109 * needs to be determined, VBUS should be routed to an external pin, and the auto-detect behaviour turned off by
110 * passing the NO_LIMITED_CONTROLLER_CONNECT token to the compiler via the -D switch at compile time. The connection
111 * and disconnection events may be manually fired by RAISE_EVENT(), and the USB_IsConnected global changed manually.
113 RAISES_EVENT(USB_Disconnect
);
115 #if defined(USB_FULL_CONTROLLER) || defined(USB_MODIFIED_FULL_CONTROLLER) || defined(__DOXYGEN__)
116 /** This module raises the VBUS Change event when the current VBUS status (present or not present) has
119 * \note Not all USB AVR models support VBUS interrupts; this event only exists on supported AVRs.
121 * \see Events.h for more information on this event.
123 RAISES_EVENT(USB_VBUSChange
);
125 /** This module raises the VBUS Connect event when the VBUS line is powered.
127 * \note Not all USB AVR models support VBUS interrupts; this event only exists on supported AVRs.
129 * \see Events.h for more information on this event.
131 RAISES_EVENT(USB_VBUSConnect
);
133 /** This module raises the VBUS Disconnect event when power is removed from the VBUS line.
135 * \note Not all USB AVR models support VBUS interrupts; this event only exists on supported AVRs.
137 * \see Events.h for more information on this event.
139 RAISES_EVENT(USB_VBUSDisconnect
);
142 #if defined(USB_CAN_BE_DEVICE) || defined(__DOXYGEN__)
143 /** This module raises the Suspended event when the host suspends the USB interface of the AVR
144 * whilst running in device mode.
146 * \see Events.h for more information on this event.
148 RAISES_EVENT(USB_Suspend
);
150 /** This module raises the Wake Up event when the host resumes the USB interface of the AVR
151 * whilst running in device mode.
153 * \see Events.h for more information on this event.
155 RAISES_EVENT(USB_WakeUp
);
157 /** This module raises the USB Reset event when the host resets the USB interface of the AVR
158 * whilst running in device mode.
160 * \see Events.h for more information on this event.
162 RAISES_EVENT(USB_Reset
);
165 #if defined(USB_CAN_BE_HOST) || defined(__DOXYGEN__)
166 /** This module raises the Host Error event when the VBUS line voltage dips below the minimum threshold
167 * while running in host mode.
169 * \note Not all USB AVR models support host mode; this event only exists on supported AVRs.
171 * \see Events.h for more information on this event.
173 RAISES_EVENT(USB_HostError
);
175 /** This module raises the Device Unattached event when an attached device is removed from the AVR whilst
176 * running in host mode.
178 * \note Not all USB AVR models support host mode; this event only exists on supported AVRs.
180 * \see Events.h for more information on this event.
182 RAISES_EVENT(USB_DeviceUnattached
);
185 #if defined(USB_CAN_BE_BOTH) || defined(__DOXYGEN__)
186 /** This module raises the UID Change event when the UID line changes in value on dual-role devices.
188 * \note Not all USB AVR models support host mode and thus the UID pin; this event only exists on
191 * \see Events.h for more information on this event.
193 RAISES_EVENT(USB_UIDChange
);
196 /* Private Interface - For use in library only: */
197 #if !defined(__DOXYGEN__)
199 #define USB_INT_GET_EN_REG(a, b, c, d) a
200 #define USB_INT_GET_EN_MASK(a, b, c, d) b
201 #define USB_INT_GET_INT_REG(a, b, c, d) c
202 #define USB_INT_GET_INT_MASK(a, b, c, d) d
204 #define USB_INT_VBUS USBCON, (1 << VBUSTE) , USBINT, (1 << VBUSTI)
205 #define USB_INT_IDTI USBCON, (1 << IDTE) , USBINT, (1 << IDTI)
206 #define USB_INT_WAKEUP UDIEN , (1 << WAKEUPE), UDINT , (1 << WAKEUPI)
207 #define USB_INT_SUSPEND UDIEN , (1 << SUSPE) , UDINT , (1 << SUSPI)
208 #define USB_INT_EORSTI UDIEN , (1 << EORSTE) , UDINT , (1 << EORSTI)
209 #define USB_INT_DCONNI UHIEN , (1 << DCONNE) , UHINT , (1 << DCONNI)
210 #define USB_INT_DDISCI UHIEN , (1 << DDISCE) , UHINT , (1 << DDISCI)
211 #define USB_INT_BCERRI OTGIEN, (1 << BCERRE) , OTGINT, (1 << BCERRI)
212 #define USB_INT_VBERRI OTGIEN, (1 << VBERRE) , OTGINT, (1 << VBERRI)
213 #define USB_INT_SOFI UDIEN, (1 << SOFE) , UDINT , (1 << SOFI)
214 #define USB_INT_HSOFI UHIEN, (1 << HSOFE) , UHINT , (1 << HSOFI)
215 #define USB_INT_RSTI UHIEN , (1 << RSTE) , UHINT , (1 << RSTI)
216 #define USB_INT_SRPI OTGIEN, (1 << SRPE) , OTGINT, (1 << SRPI)
218 /* Function Prototypes: */
219 void USB_INT_ClearAllInterrupts(void);
220 void USB_INT_DisableAllInterrupts(void);
223 /* Disable C linkage for C++ Compilers: */
224 #if defined(__cplusplus)