Fixed minor issue with the RNDISEthernet demo DHCP protocol decoder routine using...
[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 /* Project Tags, for reading out using the ButtLoad project */
40 BUTTLOADTAG(ProjName, "LUFA GenHID App");
41 BUTTLOADTAG(BuildTime, __TIME__);
42 BUTTLOADTAG(BuildDate, __DATE__);
43 BUTTLOADTAG(LUFAVersion, "LUFA V" LUFA_VERSION_STRING);
44
45 /* Scheduler Task List */
46 TASK_LIST
47 {
48 #if !defined(INTERRUPT_CONTROL_ENDPOINT)
49 { Task: USB_USBTask , TaskStatus: TASK_STOP },
50 #endif
51
52 #if !defined(INTERRUPT_DATA_ENDPOINT)
53 { Task: USB_HID_Report , TaskStatus: TASK_STOP },
54 #endif
55 };
56
57 /** Static buffer to hold the last received report from the host, so that it can be echoed back in the next sent report */
58 static uint8_t LastReceived[GENERIC_REPORT_SIZE];
59
60
61 /** Main program entry point. This routine configures the hardware required by the application, then
62 * starts the scheduler to run the USB management task.
63 */
64 int main(void)
65 {
66 /* Disable watchdog if enabled by bootloader/fuses */
67 MCUSR &= ~(1 << WDRF);
68 wdt_disable();
69
70 /* Disable clock division */
71 clock_prescale_set(clock_div_1);
72
73 /* Indicate USB not ready */
74 UpdateStatus(Status_USBNotReady);
75
76 /* Initialize Scheduler so that it can be used */
77 Scheduler_Init();
78
79 /* Initialize USB Subsystem */
80 USB_Init();
81
82 /* Scheduling - routine never returns, so put this last in the main function */
83 Scheduler_Start();
84 }
85
86 /** Event handler for the USB_Reset event. This fires when the USB interface is reset by the USB host, before the
87 * enumeration process begins, and enables the control endpoint interrupt so that control requests can be handled
88 * asynchronously when they arrive rather than when the control endpoint is polled manually.
89 */
90 EVENT_HANDLER(USB_Reset)
91 {
92 #if defined(INTERRUPT_CONTROL_ENDPOINT)
93 /* Select the control endpoint */
94 Endpoint_SelectEndpoint(ENDPOINT_CONTROLEP);
95
96 /* Enable the endpoint SETUP interrupt ISR for the control endpoint */
97 USB_INT_Enable(ENDPOINT_INT_SETUP);
98 #endif
99 }
100
101 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and
102 * starts the library USB task to begin the enumeration and USB management process.
103 */
104 EVENT_HANDLER(USB_Connect)
105 {
106 #if !defined(INTERRUPT_CONTROL_ENDPOINT)
107 /* Start USB management task */
108 Scheduler_SetTaskMode(USB_USBTask, TASK_RUN);
109 #endif
110
111 /* Indicate USB enumerating */
112 UpdateStatus(Status_USBEnumerating);
113 }
114
115 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via
116 * the status LEDs and stops the USB management task.
117 */
118 EVENT_HANDLER(USB_Disconnect)
119 {
120 /* Stop running HID reporting and USB management tasks */
121 #if !defined(INTERRUPT_DATA_ENDPOINT)
122 Scheduler_SetTaskMode(USB_HID_Report, TASK_STOP);
123 #endif
124
125 #if !defined(INTERRUPT_CONTROL_ENDPOINT)
126 Scheduler_SetTaskMode(USB_USBTask, TASK_STOP);
127 #endif
128
129 /* Indicate USB not ready */
130 UpdateStatus(Status_USBNotReady);
131 }
132
133 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host sets the current configuration
134 * of the USB device after enumeration, and configures the generic HID device endpoints.
135 */
136 EVENT_HANDLER(USB_ConfigurationChanged)
137 {
138 /* Setup Generic IN Report Endpoint */
139 Endpoint_ConfigureEndpoint(GENERIC_IN_EPNUM, EP_TYPE_INTERRUPT,
140 ENDPOINT_DIR_IN, GENERIC_EPSIZE,
141 ENDPOINT_BANK_SINGLE);
142
143 #if defined(INTERRUPT_DATA_ENDPOINT)
144 /* Enable the endpoint IN interrupt ISR for the report endpoint */
145 USB_INT_Enable(ENDPOINT_INT_IN);
146 #endif
147
148 /* Setup Generic OUT Report Endpoint */
149 Endpoint_ConfigureEndpoint(GENERIC_OUT_EPNUM, EP_TYPE_INTERRUPT,
150 ENDPOINT_DIR_OUT, GENERIC_EPSIZE,
151 ENDPOINT_BANK_SINGLE);
152
153 #if defined(INTERRUPT_DATA_ENDPOINT)
154 /* Enable the endpoint OUT interrupt ISR for the report endpoint */
155 USB_INT_Enable(ENDPOINT_INT_OUT);
156 #endif
157
158 /* Indicate USB connected and ready */
159 UpdateStatus(Status_USBReady);
160 }
161
162 /** Event handler for the USB_UnhandledControlPacket event. This is used to catch standard and class specific
163 * control requests that are not handled internally by the USB library (including the HID commands, which are
164 * all issued via the control endpoint), so that they can be handled appropriately for the application.
165 */
166 EVENT_HANDLER(USB_UnhandledControlPacket)
167 {
168 /* Handle HID Class specific requests */
169 switch (bRequest)
170 {
171 case REQ_GetReport:
172 if (bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
173 {
174 Endpoint_ClearSetupReceived();
175
176 uint8_t GenericData[GENERIC_REPORT_SIZE];
177
178 CreateGenericHIDReport(GenericData);
179
180 /* Write the report data to the control endpoint */
181 Endpoint_Write_Control_Stream_LE(&GenericData, sizeof(GenericData));
182
183 /* Finalize the stream transfer to send the last packet or clear the host abort */
184 Endpoint_ClearSetupOUT();
185 }
186
187 break;
188 case REQ_SetReport:
189 if (bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
190 {
191 Endpoint_ClearSetupReceived();
192
193 /* Wait until the generic report has been sent by the host */
194 while (!(Endpoint_IsSetupOUTReceived()));
195
196 uint8_t GenericData[GENERIC_REPORT_SIZE];
197
198 Endpoint_Read_Control_Stream(&GenericData, sizeof(GenericData));
199
200 ProcessGenericHIDReport(GenericData);
201
202 /* Clear the endpoint data */
203 Endpoint_ClearSetupOUT();
204
205 /* Wait until the host is ready to receive the request confirmation */
206 while (!(Endpoint_IsSetupINReady()));
207
208 /* Handshake the request by sending an empty IN packet */
209 Endpoint_ClearSetupIN();
210 }
211
212 break;
213 }
214 }
215
216 /** Function to manage status updates to the user. This is done via LEDs on the given board, if available, but may be changed to
217 * log to a serial port, or anything else that is suitable for status updates.
218 *
219 * \param CurrentStatus Current status of the system, from the GenericHID_StatusCodes_t enum
220 */
221 void UpdateStatus(uint8_t CurrentStatus)
222 {
223 uint8_t LEDMask = LEDS_NO_LEDS;
224
225 /* Set the LED mask to the appropriate LED mask based on the given status code */
226 switch (CurrentStatus)
227 {
228 case Status_USBNotReady:
229 LEDMask = (LEDS_LED1);
230 break;
231 case Status_USBEnumerating:
232 LEDMask = (LEDS_LED1 | LEDS_LED2);
233 break;
234 case Status_USBReady:
235 LEDMask = (LEDS_LED2 | LEDS_LED4);
236 break;
237 }
238
239 /* Set the board LEDs to the new LED mask */
240 LEDs_SetAllLEDs(LEDMask);
241 }
242
243 /** Function to process the lest received report from the host.
244 *
245 * \param DataArray Pointer to a buffer where the last report data is stored
246 */
247 void ProcessGenericHIDReport(uint8_t* DataArray)
248 {
249 /*
250 This is where you need to process the reports being sent from the host to the device.
251 DataArray is an array holding the last report from the host. This function is called
252 each time the host has sent a report to the device.
253 */
254
255 for (uint8_t i = 0; i < GENERIC_REPORT_SIZE; i++)
256 LastReceived[i] = DataArray[i];
257 }
258
259 /** Function to create the next report to send back to the host at the next reporting interval.
260 *
261 * \param DataArray Pointer to a buffer where the next report data should be stored
262 */
263 void CreateGenericHIDReport(uint8_t* DataArray)
264 {
265 /*
266 This is where you need to create reports to be sent to the host from the device. This
267 function is called each time the host is ready to accept a new report. DataArray is
268 an array to hold the report to the host.
269 */
270
271 for (uint8_t i = 0; i < GENERIC_REPORT_SIZE; i++)
272 DataArray[i] = LastReceived[i];
273 }
274
275 #if !defined(INTERRUPT_DATA_ENDPOINT)
276 TASK(USB_HID_Report)
277 {
278 /* Check if the USB system is connected to a host */
279 if (USB_IsConnected)
280 {
281 Endpoint_SelectEndpoint(GENERIC_OUT_EPNUM);
282
283 if (Endpoint_ReadWriteAllowed())
284 {
285 /* Create a temporary buffer to hold the read in report from the host */
286 uint8_t GenericData[GENERIC_REPORT_SIZE];
287
288 /* Read Generic Report Data */
289 Endpoint_Read_Stream_LE(&GenericData, sizeof(GenericData));
290
291 /* Process Generic Report Data */
292 ProcessGenericHIDReport(GenericData);
293
294 /* Finalize the stream transfer to send the last packet */
295 Endpoint_ClearCurrentBank();
296 }
297
298 Endpoint_SelectEndpoint(GENERIC_IN_EPNUM);
299
300 if (Endpoint_ReadWriteAllowed())
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_ClearCurrentBank();
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_ClearCurrentBank();
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_ClearCurrentBank();
392 }
393 #endif
394
395 /* Restore previously selected endpoint */
396 Endpoint_SelectEndpoint(PrevSelectedEndpoint);
397 }