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