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
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.
37 #include "GenericHID.h"
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
);
45 /** Static buffer to hold the last received report from the host, so that it can be echoed back in the next sent report */
46 static uint8_t LastReceived
[GENERIC_REPORT_SIZE
];
49 /** Main program entry point. This routine configures the hardware required by the application, then
50 * starts the scheduler to run the USB management task.
54 /* Disable watchdog if enabled by bootloader/fuses */
55 MCUSR
&= ~(1 << WDRF
);
58 /* Disable clock division */
59 clock_prescale_set(clock_div_1
);
61 /* Indicate USB not ready */
62 UpdateStatus(Status_USBNotReady
);
64 /* Initialize USB Subsystem */
67 /* >> APPLICATION INIT CODE HERE << */
71 /** Event handler for the USB_Reset event. This fires when the USB interface is reset by the USB host, before the
72 * enumeration process begins, and enables the control endpoint interrupt so that control requests can be handled
73 * asynchronously when they arrive rather than when the control endpoint is polled manually.
75 EVENT_HANDLER(USB_Reset
)
77 /* Select the control endpoint */
78 Endpoint_SelectEndpoint(ENDPOINT_CONTROLEP
);
80 /* Enable the endpoint SETUP interrupt ISR for the control endpoint */
81 USB_INT_Enable(ENDPOINT_INT_SETUP
);
84 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and
85 * starts the library USB task to begin the enumeration and USB management process.
87 EVENT_HANDLER(USB_Connect
)
89 /* Indicate USB enumerating */
90 UpdateStatus(Status_USBEnumerating
);
93 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via
94 * the status LEDs and stops the USB management task.
96 EVENT_HANDLER(USB_Disconnect
)
98 /* Indicate USB not ready */
99 UpdateStatus(Status_USBNotReady
);
102 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host sets the current configuration
103 * of the USB device after enumeration, and configures the generic HID device endpoints.
105 EVENT_HANDLER(USB_ConfigurationChanged
)
107 /* Setup Generic IN Report Endpoint */
108 Endpoint_ConfigureEndpoint(GENERIC_IN_EPNUM
, EP_TYPE_INTERRUPT
,
109 ENDPOINT_DIR_IN
, GENERIC_EPSIZE
,
110 ENDPOINT_BANK_SINGLE
);
112 /* Enable the endpoint IN interrupt ISR for the report endpoint */
113 USB_INT_Enable(ENDPOINT_INT_IN
);
115 /* Setup Generic OUT Report Endpoint */
116 Endpoint_ConfigureEndpoint(GENERIC_OUT_EPNUM
, EP_TYPE_INTERRUPT
,
117 ENDPOINT_DIR_OUT
, GENERIC_EPSIZE
,
118 ENDPOINT_BANK_SINGLE
);
120 /* Enable the endpoint OUT interrupt ISR for the report endpoint */
121 USB_INT_Enable(ENDPOINT_INT_OUT
);
123 /* Indicate USB connected and ready */
124 UpdateStatus(Status_USBReady
);
127 /** Event handler for the USB_UnhandledControlPacket event. This is used to catch standard and class specific
128 * control requests that are not handled internally by the USB library (including the HID commands, which are
129 * all issued via the control endpoint), so that they can be handled appropriately for the application.
131 EVENT_HANDLER(USB_UnhandledControlPacket
)
133 /* Handle HID Class specific requests */
137 if (bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
))
139 Endpoint_ClearSetupReceived();
141 uint8_t GenericData
[GENERIC_REPORT_SIZE
];
143 CreateGenericHIDReport(GenericData
);
145 /* Write the report data to the control endpoint */
146 Endpoint_Write_Control_Stream_LE(&GenericData
, sizeof(GenericData
));
148 /* Finalize the stream transfer to send the last packet or clear the host abort */
149 Endpoint_ClearSetupOUT();
154 if (bmRequestType
== (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
))
156 Endpoint_ClearSetupReceived();
158 /* Wait until the generic report has been sent by the host */
159 while (!(Endpoint_IsSetupOUTReceived()));
161 uint8_t GenericData
[GENERIC_REPORT_SIZE
];
163 Endpoint_Read_Control_Stream(&GenericData
, sizeof(GenericData
));
165 ProcessGenericHIDReport(GenericData
);
167 /* Clear the endpoint data */
168 Endpoint_ClearSetupOUT();
170 /* Wait until the host is ready to receive the request confirmation */
171 while (!(Endpoint_IsSetupINReady()));
173 /* Handshake the request by sending an empty IN packet */
174 Endpoint_ClearSetupIN();
181 /** Function to manage status updates to the user. This is done via LEDs on the given board, if available, but may be changed to
182 * log to a serial port, or anything else that is suitable for status updates.
184 * \param CurrentStatus Current status of the system, from the GenericHID_StatusCodes_t enum
186 void UpdateStatus(uint8_t CurrentStatus
)
188 uint8_t LEDMask
= LEDS_NO_LEDS
;
190 /* Set the LED mask to the appropriate LED mask based on the given status code */
191 switch (CurrentStatus
)
193 case Status_USBNotReady
:
194 LEDMask
= (LEDS_LED1
);
196 case Status_USBEnumerating
:
197 LEDMask
= (LEDS_LED1
| LEDS_LED2
);
199 case Status_USBReady
:
200 LEDMask
= (LEDS_LED2
| LEDS_LED4
);
204 /* Set the board LEDs to the new LED mask */
205 LEDs_SetAllLEDs(LEDMask
);
208 /** Function to process the lest received report from the host.
210 * \param DataArray Pointer to a buffer where the last report data is stored
212 void ProcessGenericHIDReport(uint8_t* DataArray
)
215 This is where you need to process the reports being sent from the host to the device.
216 DataArray is an array holding the last report from the host. This function is called
217 each time the host has sent a report to the device.
220 for (uint8_t i
= 0; i
< GENERIC_REPORT_SIZE
; i
++)
221 LastReceived
[i
] = DataArray
[i
];
224 /** Function to create the next report to send back to the host at the next reporting interval.
226 * \param DataArray Pointer to a buffer where the next report data should be stored
228 void CreateGenericHIDReport(uint8_t* DataArray
)
231 This is where you need to create reports to be sent to the host from the device. This
232 function is called each time the host is ready to accept a new report. DataArray is
233 an array to hold the report to the host.
236 for (uint8_t i
= 0; i
< GENERIC_REPORT_SIZE
; i
++)
237 DataArray
[i
] = LastReceived
[i
];
240 /** ISR for the general Pipe/Endpoint interrupt vector. This ISR fires when an endpoint's status changes (such as
241 * a packet has been received) on an endpoint with its corresponding ISR enabling bits set. This is used to send
242 * HID packets to the host each time the HID interrupt endpoints polling period elapses, as managed by the USB
245 ISR(ENDPOINT_PIPE_vect
, ISR_BLOCK
)
247 /* Save previously selected endpoint before selecting a new endpoint */
248 uint8_t PrevSelectedEndpoint
= Endpoint_GetCurrentEndpoint();
250 /* Check if the control endpoint has received a request */
251 if (Endpoint_HasEndpointInterrupted(ENDPOINT_CONTROLEP
))
253 /* Clear the endpoint interrupt */
254 Endpoint_ClearEndpointInterrupt(ENDPOINT_CONTROLEP
);
256 /* Process the control request */
259 /* Handshake the endpoint setup interrupt - must be after the call to USB_USBTask() */
260 USB_INT_Clear(ENDPOINT_INT_SETUP
);
263 /* Check if Generic IN endpoint has interrupted */
264 if (Endpoint_HasEndpointInterrupted(GENERIC_IN_EPNUM
))
266 /* Select the Generic IN Report Endpoint */
267 Endpoint_SelectEndpoint(GENERIC_IN_EPNUM
);
269 /* Clear the endpoint IN interrupt flag */
270 USB_INT_Clear(ENDPOINT_INT_IN
);
272 /* Clear the Generic IN Report endpoint interrupt and select the endpoint */
273 Endpoint_ClearEndpointInterrupt(GENERIC_IN_EPNUM
);
275 /* Create a tempoary buffer to hold the report to send to the host */
276 uint8_t GenericData
[GENERIC_REPORT_SIZE
];
278 /* Create Generic Report Data */
279 CreateGenericHIDReport(GenericData
);
281 /* Write Generic Report Data */
282 Endpoint_Write_Stream_LE(&GenericData
, sizeof(GenericData
));
284 /* Finalize the stream transfer to send the last packet */
285 Endpoint_ClearCurrentBank();
288 /* Check if Generic OUT endpoint has interrupted */
289 if (Endpoint_HasEndpointInterrupted(GENERIC_OUT_EPNUM
))
291 /* Select the Generic OUT Report Endpoint */
292 Endpoint_SelectEndpoint(GENERIC_OUT_EPNUM
);
294 /* Clear the endpoint OUT Interrupt flag */
295 USB_INT_Clear(ENDPOINT_INT_OUT
);
297 /* Clear the Generic OUT Report endpoint interrupt and select the endpoint */
298 Endpoint_ClearEndpointInterrupt(GENERIC_OUT_EPNUM
);
300 /* Create a tempoary buffer to hold the read in report from the host */
301 uint8_t GenericData
[GENERIC_REPORT_SIZE
];
303 /* Read Generic Report Data */
304 Endpoint_Read_Stream_LE(&GenericData
, sizeof(GenericData
));
306 /* Process Generic Report Data */
307 ProcessGenericHIDReport(GenericData
);
309 /* Finalize the stream transfer to send the last packet */
310 Endpoint_ClearCurrentBank();
313 /* Restore previously selected endpoint */
314 Endpoint_SelectEndpoint(PrevSelectedEndpoint
);