Be doubly-certain that the incomming CDC class driver's endpoint/pipe is flushed...
[pub/USBasp.git] / LUFA / Drivers / USB / HighLevel / USBTask.h
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2010 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 #ifndef __USBTASK_H__
32 #define __USBTASK_H__
33
34 /* Includes: */
35 #include <avr/io.h>
36 #include <avr/interrupt.h>
37 #include <stdbool.h>
38
39 #include "../LowLevel/LowLevel.h"
40 #include "Events.h"
41 #include "StdRequestType.h"
42 #include "StdDescriptors.h"
43 #include "USBMode.h"
44
45 #if defined(USB_CAN_BE_DEVICE)
46 #include "../LowLevel/DevChapter9.h"
47 #endif
48
49 #if defined(USB_CAN_BE_HOST)
50 #include "../LowLevel/HostChapter9.h"
51 #endif
52
53 /* Enable C linkage for C++ Compilers: */
54 #if defined(__cplusplus)
55 extern "C" {
56 #endif
57
58 /* Public Interface - May be used in end-application: */
59 /* Global Variables: */
60 /** Indicates if the USB interface is currently initialized but not necessarily connected to a host
61 * or device (i.e. if \ref USB_Init() has been run). If this is false, all other library globals are invalid.
62 *
63 * \note This variable should be treated as read-only in the user application, and never manually
64 * changed in value.
65 *
66 * \ingroup Group_USBManagement
67 */
68 extern volatile bool USB_IsInitialized;
69
70 /** Structure containing the last received Control request when in Device mode (for use in user-applications
71 * inside of the \ref EVENT_USB_Device_UnhandledControlRequest() event, or for filling up with a control request to issue when
72 * in Host mode before calling \ref USB_Host_SendControlRequest().
73 *
74 * \ingroup Group_USBManagement
75 */
76 extern USB_Request_Header_t USB_ControlRequest;
77
78 #if defined(USB_CAN_BE_HOST) || defined(__DOXYGEN__)
79 #if !defined(HOST_STATE_AS_GPIOR) || defined(__DOXYGEN__)
80 /** Indicates the current host state machine state. When in host mode, this indicates the state
81 * via one of the values of the \ref USB_Host_States_t enum values.
82 *
83 * This value may be altered by the user application to implement the \ref HOST_STATE_Addressed,
84 * \ref HOST_STATE_Configured and \ref HOST_STATE_Suspended states which are not implemented by
85 * the library.
86 *
87 * To reduce program size and speed up checks of this global, it can be placed into one of the AVR's
88 * GPIOR hardware registers instead of RAM by defining the HOST_STATE_AS_GPIOR token to a value
89 * between 0 and 2 in the project makefile and passing it to the compiler via the -D switch. When
90 * defined, the corresponding GPIOR register should not be used in the user application except
91 * implicitly via the library APIs.
92 *
93 * \note This global is only present if the user application can be a USB host.
94 *
95 * \see \ref USB_Host_States_t for a list of possible device states
96 *
97 * \ingroup Group_Host
98 */
99 extern volatile uint8_t USB_HostState;
100 #else
101 #define _GET_HOST_GPIOR_NAME2(y) GPIOR ## y
102 #define _GET_HOST_GPIOR_NAME(x) _GET_HOST_GPIOR_NAME2(x)
103 #define USB_HostState _GET_HOST_GPIOR_NAME(HOST_STATE_AS_GPIOR)
104 #endif
105 #endif
106
107 #if defined(USB_CAN_BE_DEVICE) || defined(__DOXYGEN__)
108 #if !defined(DEVICE_STATE_AS_GPIOR) || defined(__DOXYGEN__)
109 /** Indicates the current device state machine state. When in device mode, this indicates the state
110 * via one of the values of the \ref USB_Device_States_t enum values.
111 *
112 * This value should not be altered by the user application as it is handled automatically by the
113 * library. The only exception to this rule is if the NO_LIMITED_CONTROLLER_CONNECT token is used
114 * (see \ref EVENT_USB_Device_Connect() and \ref EVENT_USB_Device_Disconnect() events).
115 *
116 * To reduce program size and speed up checks of this global, it can be placed into one of the AVR's
117 * GPIOR hardware registers instead of RAM by defining the DEVICE_STATE_AS_GPIOR token to a value
118 * between 0 and 2 in the project makefile and passing it to the compiler via the -D switch. When
119 * defined, the corresponding GPIOR register should not be used in the user application except
120 * implicitly via the library APIs.
121 *
122 * \note This global is only present if the user application can be a USB device.
123 *
124 * \note This variable should be treated as read-only in the user application, and never manually
125 * changed in value except in the circumstances outlined above.
126 *
127 * \see \ref USB_Device_States_t for a list of possible device states
128 *
129 * \ingroup Group_Device
130 */
131 extern volatile uint8_t USB_DeviceState;
132 #else
133 #define _GET_DEVICE_GPIOR_NAME2(y) GPIOR ## y
134 #define _GET_DEVICE_GPIOR_NAME(x) _GET_DEVICE_GPIOR_NAME2(x)
135 #define USB_DeviceState _GET_DEVICE_GPIOR_NAME(DEVICE_STATE_AS_GPIOR)
136 #endif
137 #endif
138
139 /* Function Prototypes: */
140 /** This is the main USB management task. The USB driver requires that this task be executed
141 * continuously when the USB system is active (device attached in host mode, or attached to a host
142 * in device mode) in order to manage USB communications. This task may be executed inside an RTOS,
143 * fast timer ISR or the main user application loop.
144 *
145 * The USB task must be serviced within 30ms while in device mode, or within 1ms while in host mode.
146 * The task may be serviced at all times, or (for minimum CPU consumption):
147 *
148 * - In device mode, it may be disabled at start-up, enabled on the firing of the \ref EVENT_USB_Device_Connect()
149 * event and disabled again on the firing of the \ref EVENT_USB_Device_Disconnect() event.
150 *
151 * - In host mode, it may be disabled at start-up, enabled on the firing of the \ref EVENT_USB_Host_DeviceAttached()
152 * event and disabled again on the firing of the \ref EVENT_USB_Host_DeviceEnumerationComplete() or
153 * \ref EVENT_USB_Host_DeviceEnumerationFailed() events.
154 *
155 * If in device mode (only), the control endpoint can instead be managed via interrupts entirely by the library
156 * by defining the INTERRUPT_CONTROL_ENDPOINT token and passing it to the compiler via the -D switch.
157 *
158 * \see \ref Group_Events for more information on the USB events.
159 *
160 * \ingroup Group_USBManagement
161 */
162 void USB_USBTask(void);
163
164 /* Private Interface - For use in library only: */
165 #if !defined(__DOXYGEN__)
166 /* Function Prototypes: */
167 #if defined(INCLUDE_FROM_USBTASK_C)
168 #if defined(USB_CAN_BE_HOST)
169 static void USB_HostTask(void);
170 #endif
171
172 #if defined(USB_CAN_BE_DEVICE)
173 static void USB_DeviceTask(void);
174 #endif
175 #endif
176
177 /* Macros: */
178 #define HOST_TASK_NONBLOCK_WAIT(duration, nextstate) MACROS{ USB_HostState = HOST_STATE_WaitForDevice; \
179 WaitMSRemaining = (duration); \
180 PostWaitState = (nextstate); }MACROE
181 #endif
182
183 /* Disable C linkage for C++ Compilers: */
184 #if defined(__cplusplus)
185 }
186 #endif
187
188 #endif