Fixed USB_RemoteWakeupEnabled flag never being set (the REMOTE WAKEUP Set Feature...
[pub/USBasp.git] / Demos / Device / GenericHID / GenericHID.c
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 /** \file
32 *
33 * Main source file for the GenericHID demo. This file contains the main tasks of the demo and
34 * is responsible for the initial application hardware configuration.
35 */
36
37 #include "GenericHID.h"
38
39 /* Scheduler Task List */
40 TASK_LIST
41 {
42 #if !defined(INTERRUPT_CONTROL_ENDPOINT)
43 { .Task = USB_USBTask , .TaskStatus = TASK_STOP },
44 #endif
45
46 #if !defined(INTERRUPT_DATA_ENDPOINT)
47 { .Task = USB_HID_Report , .TaskStatus = TASK_STOP },
48 #endif
49 };
50
51 /** Static buffer to hold the last received report from the host, so that it can be echoed back in the next sent report */
52 static uint8_t LastReceived[GENERIC_REPORT_SIZE];
53
54
55 /** Main program entry point. This routine configures the hardware required by the application, then
56 * starts the scheduler to run the USB management task.
57 */
58 int main(void)
59 {
60 /* Disable watchdog if enabled by bootloader/fuses */
61 MCUSR &= ~(1 << WDRF);
62 wdt_disable();
63
64 /* Disable clock division */
65 clock_prescale_set(clock_div_1);
66
67 /* Indicate USB not ready */
68 UpdateStatus(Status_USBNotReady);
69
70 /* Initialize Scheduler so that it can be used */
71 Scheduler_Init();
72
73 /* Initialize USB Subsystem */
74 USB_Init();
75
76 /* Scheduling - routine never returns, so put this last in the main function */
77 Scheduler_Start();
78 }
79
80 /** Event handler for the USB_Reset event. This fires when the USB interface is reset by the USB host, before the
81 * enumeration process begins, and enables the control endpoint interrupt so that control requests can be handled
82 * asynchronously when they arrive rather than when the control endpoint is polled manually.
83 */
84 EVENT_HANDLER(USB_Reset)
85 {
86 #if defined(INTERRUPT_CONTROL_ENDPOINT)
87 /* Select the control endpoint */
88 Endpoint_SelectEndpoint(ENDPOINT_CONTROLEP);
89
90 /* Enable the endpoint SETUP interrupt ISR for the control endpoint */
91 USB_INT_Enable(ENDPOINT_INT_SETUP);
92 #endif
93 }
94
95 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and
96 * starts the library USB task to begin the enumeration and USB management process.
97 */
98 EVENT_HANDLER(USB_Connect)
99 {
100 #if !defined(INTERRUPT_CONTROL_ENDPOINT)
101 /* Start USB management task */
102 Scheduler_SetTaskMode(USB_USBTask, TASK_RUN);
103 #endif
104
105 /* Indicate USB enumerating */
106 UpdateStatus(Status_USBEnumerating);
107 }
108
109 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via
110 * the status LEDs and stops the USB management task.
111 */
112 EVENT_HANDLER(USB_Disconnect)
113 {
114 /* Stop running HID reporting and USB management tasks */
115 #if !defined(INTERRUPT_DATA_ENDPOINT)
116 Scheduler_SetTaskMode(USB_HID_Report, TASK_STOP);
117 #endif
118
119 #if !defined(INTERRUPT_CONTROL_ENDPOINT)
120 Scheduler_SetTaskMode(USB_USBTask, TASK_STOP);
121 #endif
122
123 /* Indicate USB not ready */
124 UpdateStatus(Status_USBNotReady);
125 }
126
127 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host sets the current configuration
128 * of the USB device after enumeration, and configures the generic HID device endpoints.
129 */
130 EVENT_HANDLER(USB_ConfigurationChanged)
131 {
132 /* Setup Generic IN Report Endpoint */
133 Endpoint_ConfigureEndpoint(GENERIC_IN_EPNUM, EP_TYPE_INTERRUPT,
134 ENDPOINT_DIR_IN, GENERIC_EPSIZE,
135 ENDPOINT_BANK_SINGLE);
136
137 #if defined(INTERRUPT_DATA_ENDPOINT)
138 /* Enable the endpoint IN interrupt ISR for the report endpoint */
139 USB_INT_Enable(ENDPOINT_INT_IN);
140 #endif
141
142 /* Setup Generic OUT Report Endpoint */
143 Endpoint_ConfigureEndpoint(GENERIC_OUT_EPNUM, EP_TYPE_INTERRUPT,
144 ENDPOINT_DIR_OUT, GENERIC_EPSIZE,
145 ENDPOINT_BANK_SINGLE);
146
147 #if defined(INTERRUPT_DATA_ENDPOINT)
148 /* Enable the endpoint OUT interrupt ISR for the report endpoint */
149 USB_INT_Enable(ENDPOINT_INT_OUT);
150 #endif
151
152 /* Indicate USB connected and ready */
153 UpdateStatus(Status_USBReady);
154 }
155
156 /** Event handler for the USB_UnhandledControlPacket event. This is used to catch standard and class specific
157 * control requests that are not handled internally by the USB library (including the HID commands, which are
158 * all issued via the control endpoint), so that they can be handled appropriately for the application.
159 */
160 EVENT_HANDLER(USB_UnhandledControlPacket)
161 {
162 /* Handle HID Class specific requests */
163 switch (USB_ControlRequest.bRequest)
164 {
165 case REQ_GetReport:
166 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
167 {
168 uint8_t GenericData[GENERIC_REPORT_SIZE];
169
170 Endpoint_ClearSETUP();
171
172 CreateGenericHIDReport(GenericData);
173
174 /* Write the report data to the control endpoint */
175 Endpoint_Write_Control_Stream_LE(&GenericData, sizeof(GenericData));
176
177 /* Finalize the stream transfer to send the last packet or clear the host abort */
178 Endpoint_ClearOUT();
179 }
180
181 break;
182 case REQ_SetReport:
183 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
184 {
185 uint8_t GenericData[GENERIC_REPORT_SIZE];
186
187 Endpoint_ClearSETUP();
188
189 /* Wait until the generic report has been sent by the host */
190 while (!(Endpoint_IsOUTReceived()));
191
192 Endpoint_Read_Control_Stream_LE(&GenericData, sizeof(GenericData));
193
194 ProcessGenericHIDReport(GenericData);
195
196 /* Clear the endpoint data */
197 Endpoint_ClearOUT();
198
199 /* Wait until the host is ready to receive the request confirmation */
200 while (!(Endpoint_IsINReady()));
201
202 /* Handshake the request by sending an empty IN packet */
203 Endpoint_ClearIN();
204 }
205
206 break;
207 }
208 }
209
210 /** Function to manage status updates to the user. This is done via LEDs on the given board, if available, but may be changed to
211 * log to a serial port, or anything else that is suitable for status updates.
212 *
213 * \param CurrentStatus Current status of the system, from the GenericHID_StatusCodes_t enum
214 */
215 void UpdateStatus(uint8_t CurrentStatus)
216 {
217 uint8_t LEDMask = LEDS_NO_LEDS;
218
219 /* Set the LED mask to the appropriate LED mask based on the given status code */
220 switch (CurrentStatus)
221 {
222 case Status_USBNotReady:
223 LEDMask = (LEDS_LED1);
224 break;
225 case Status_USBEnumerating:
226 LEDMask = (LEDS_LED1 | LEDS_LED2);
227 break;
228 case Status_USBReady:
229 LEDMask = (LEDS_LED2 | LEDS_LED4);
230 break;
231 }
232
233 /* Set the board LEDs to the new LED mask */
234 LEDs_SetAllLEDs(LEDMask);
235 }
236
237 /** Function to process the lest received report from the host.
238 *
239 * \param DataArray Pointer to a buffer where the last report data is stored
240 */
241 void ProcessGenericHIDReport(uint8_t* DataArray)
242 {
243 /*
244 This is where you need to process the reports being sent from the host to the device.
245 DataArray is an array holding the last report from the host. This function is called
246 each time the host has sent a report to the device.
247 */
248
249 for (uint8_t i = 0; i < GENERIC_REPORT_SIZE; i++)
250 LastReceived[i] = DataArray[i];
251 }
252
253 /** Function to create the next report to send back to the host at the next reporting interval.
254 *
255 * \param DataArray Pointer to a buffer where the next report data should be stored
256 */
257 void CreateGenericHIDReport(uint8_t* DataArray)
258 {
259 /*
260 This is where you need to create reports to be sent to the host from the device. This
261 function is called each time the host is ready to accept a new report. DataArray is
262 an array to hold the report to the host.
263 */
264
265 for (uint8_t i = 0; i < GENERIC_REPORT_SIZE; i++)
266 DataArray[i] = LastReceived[i];
267 }
268
269 #if !defined(INTERRUPT_DATA_ENDPOINT)
270 TASK(USB_HID_Report)
271 {
272 /* Check if the USB system is connected to a host */
273 if (USB_IsConnected)
274 {
275 Endpoint_SelectEndpoint(GENERIC_OUT_EPNUM);
276
277 /* Check to see if a packet has been sent from the host */
278 if (Endpoint_IsOUTReceived())
279 {
280 /* Check to see if the packet contains data */
281 if (Endpoint_IsReadWriteAllowed())
282 {
283 /* Create a temporary buffer to hold the read in report from the host */
284 uint8_t GenericData[GENERIC_REPORT_SIZE];
285
286 /* Read Generic Report Data */
287 Endpoint_Read_Stream_LE(&GenericData, sizeof(GenericData));
288
289 /* Process Generic Report Data */
290 ProcessGenericHIDReport(GenericData);
291 }
292
293 /* Finalize the stream transfer to send the last packet */
294 Endpoint_ClearOUT();
295 }
296
297 Endpoint_SelectEndpoint(GENERIC_IN_EPNUM);
298
299 /* Check to see if the host is ready to accept another packet */
300 if (Endpoint_IsINReady())
301 {
302 /* Create a temporary buffer to hold the report to send to the host */
303 uint8_t GenericData[GENERIC_REPORT_SIZE];
304
305 /* Create Generic Report Data */
306 CreateGenericHIDReport(GenericData);
307
308 /* Write Generic Report Data */
309 Endpoint_Write_Stream_LE(&GenericData, sizeof(GenericData));
310
311 /* Finalize the stream transfer to send the last packet */
312 Endpoint_ClearIN();
313 }
314 }
315 }
316 #endif
317
318 /** ISR for the general Pipe/Endpoint interrupt vector. This ISR fires when an endpoint's status changes (such as
319 * a packet has been received) on an endpoint with its corresponding ISR enabling bits set. This is used to send
320 * HID packets to the host each time the HID interrupt endpoints polling period elapses, as managed by the USB
321 * controller.
322 */
323 ISR(ENDPOINT_PIPE_vect, ISR_BLOCK)
324 {
325 /* Save previously selected endpoint before selecting a new endpoint */
326 uint8_t PrevSelectedEndpoint = Endpoint_GetCurrentEndpoint();
327
328 #if defined(INTERRUPT_CONTROL_ENDPOINT)
329 /* Check if the control endpoint has received a request */
330 if (Endpoint_HasEndpointInterrupted(ENDPOINT_CONTROLEP))
331 {
332 /* Clear the endpoint interrupt */
333 Endpoint_ClearEndpointInterrupt(ENDPOINT_CONTROLEP);
334
335 /* Process the control request */
336 USB_USBTask();
337
338 /* Handshake the endpoint setup interrupt - must be after the call to USB_USBTask() */
339 USB_INT_Clear(ENDPOINT_INT_SETUP);
340 }
341 #endif
342
343 #if defined(INTERRUPT_DATA_ENDPOINT)
344 /* Check if Generic IN endpoint has interrupted */
345 if (Endpoint_HasEndpointInterrupted(GENERIC_IN_EPNUM))
346 {
347 /* Select the Generic IN Report Endpoint */
348 Endpoint_SelectEndpoint(GENERIC_IN_EPNUM);
349
350 /* Clear the endpoint IN interrupt flag */
351 USB_INT_Clear(ENDPOINT_INT_IN);
352
353 /* Clear the Generic IN Report endpoint interrupt and select the endpoint */
354 Endpoint_ClearEndpointInterrupt(GENERIC_IN_EPNUM);
355
356 /* Create a temporary buffer to hold the report to send to the host */
357 uint8_t GenericData[GENERIC_REPORT_SIZE];
358
359 /* Create Generic Report Data */
360 CreateGenericHIDReport(GenericData);
361
362 /* Write Generic Report Data */
363 Endpoint_Write_Stream_LE(&GenericData, sizeof(GenericData));
364
365 /* Finalize the stream transfer to send the last packet */
366 Endpoint_ClearIN();
367 }
368
369 /* Check if Generic OUT endpoint has interrupted */
370 if (Endpoint_HasEndpointInterrupted(GENERIC_OUT_EPNUM))
371 {
372 /* Select the Generic OUT Report Endpoint */
373 Endpoint_SelectEndpoint(GENERIC_OUT_EPNUM);
374
375 /* Clear the endpoint OUT Interrupt flag */
376 USB_INT_Clear(ENDPOINT_INT_OUT);
377
378 /* Clear the Generic OUT Report endpoint interrupt and select the endpoint */
379 Endpoint_ClearEndpointInterrupt(GENERIC_OUT_EPNUM);
380
381 /* Create a temporary buffer to hold the read in report from the host */
382 uint8_t GenericData[GENERIC_REPORT_SIZE];
383
384 /* Read Generic Report Data */
385 Endpoint_Read_Stream_LE(&GenericData, sizeof(GenericData));
386
387 /* Process Generic Report Data */
388 ProcessGenericHIDReport(GenericData);
389
390 /* Finalize the stream transfer to send the last packet */
391 Endpoint_ClearOUT();
392 }
393 #endif
394
395 /* Restore previously selected endpoint */
396 Endpoint_SelectEndpoint(PrevSelectedEndpoint);
397 }