Added new GenericHID device demo application.
[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 /** 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];
47
48
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.
51 */
52 int main(void)
53 {
54 /* Disable watchdog if enabled by bootloader/fuses */
55 MCUSR &= ~(1 << WDRF);
56 wdt_disable();
57
58 /* Disable clock division */
59 clock_prescale_set(clock_div_1);
60
61 /* Indicate USB not ready */
62 UpdateStatus(Status_USBNotReady);
63
64 /* Initialize USB Subsystem */
65 USB_Init();
66
67 /* >> APPLICATION INIT CODE HERE << */
68 for (;;);
69 }
70
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.
74 */
75 EVENT_HANDLER(USB_Reset)
76 {
77 /* Select the control endpoint */
78 Endpoint_SelectEndpoint(ENDPOINT_CONTROLEP);
79
80 /* Enable the endpoint SETUP interrupt ISR for the control endpoint */
81 USB_INT_Enable(ENDPOINT_INT_SETUP);
82 }
83
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.
86 */
87 EVENT_HANDLER(USB_Connect)
88 {
89 /* Indicate USB enumerating */
90 UpdateStatus(Status_USBEnumerating);
91 }
92
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.
95 */
96 EVENT_HANDLER(USB_Disconnect)
97 {
98 /* Indicate USB not ready */
99 UpdateStatus(Status_USBNotReady);
100 }
101
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.
104 */
105 EVENT_HANDLER(USB_ConfigurationChanged)
106 {
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);
111
112 /* Enable the endpoint IN interrupt ISR for the report endpoint */
113 USB_INT_Enable(ENDPOINT_INT_IN);
114
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);
119
120 /* Enable the endpoint OUT interrupt ISR for the report endpoint */
121 USB_INT_Enable(ENDPOINT_INT_OUT);
122
123 /* Indicate USB connected and ready */
124 UpdateStatus(Status_USBReady);
125 }
126
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.
130 */
131 EVENT_HANDLER(USB_UnhandledControlPacket)
132 {
133 /* Handle HID Class specific requests */
134 switch (bRequest)
135 {
136 case REQ_GetReport:
137 if (bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
138 {
139 Endpoint_ClearSetupReceived();
140
141 uint8_t GenericData[GENERIC_REPORT_SIZE];
142
143 CreateGenericHIDReport(GenericData);
144
145 /* Write the report data to the control endpoint */
146 Endpoint_Write_Control_Stream_LE(&GenericData, sizeof(GenericData));
147
148 /* Finalize the stream transfer to send the last packet or clear the host abort */
149 Endpoint_ClearSetupOUT();
150 }
151
152 break;
153 case REQ_SetReport:
154 if (bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
155 {
156 Endpoint_ClearSetupReceived();
157
158 /* Wait until the generic report has been sent by the host */
159 while (!(Endpoint_IsSetupOUTReceived()));
160
161 uint8_t GenericData[GENERIC_REPORT_SIZE];
162
163 Endpoint_Read_Control_Stream(&GenericData, sizeof(GenericData));
164
165 ProcessGenericHIDReport(GenericData);
166
167 /* Clear the endpoint data */
168 Endpoint_ClearSetupOUT();
169
170 /* Wait until the host is ready to receive the request confirmation */
171 while (!(Endpoint_IsSetupINReady()));
172
173 /* Handshake the request by sending an empty IN packet */
174 Endpoint_ClearSetupIN();
175 }
176
177 break;
178 }
179 }
180
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.
183 *
184 * \param CurrentStatus Current status of the system, from the GenericHID_StatusCodes_t enum
185 */
186 void UpdateStatus(uint8_t CurrentStatus)
187 {
188 uint8_t LEDMask = LEDS_NO_LEDS;
189
190 /* Set the LED mask to the appropriate LED mask based on the given status code */
191 switch (CurrentStatus)
192 {
193 case Status_USBNotReady:
194 LEDMask = (LEDS_LED1);
195 break;
196 case Status_USBEnumerating:
197 LEDMask = (LEDS_LED1 | LEDS_LED2);
198 break;
199 case Status_USBReady:
200 LEDMask = (LEDS_LED2 | LEDS_LED4);
201 break;
202 }
203
204 /* Set the board LEDs to the new LED mask */
205 LEDs_SetAllLEDs(LEDMask);
206 }
207
208 /** Function to process the lest received report from the host.
209 *
210 * \param DataArray Pointer to a buffer where the last report data is stored
211 */
212 void ProcessGenericHIDReport(uint8_t* DataArray)
213 {
214 /*
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.
218 */
219
220 for (uint8_t i = 0; i < GENERIC_REPORT_SIZE; i++)
221 LastReceived[i] = DataArray[i];
222 }
223
224 /** Function to create the next report to send back to the host at the next reporting interval.
225 *
226 * \param DataArray Pointer to a buffer where the next report data should be stored
227 */
228 void CreateGenericHIDReport(uint8_t* DataArray)
229 {
230 /*
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.
234 */
235
236 for (uint8_t i = 0; i < GENERIC_REPORT_SIZE; i++)
237 DataArray[i] = LastReceived[i];
238 }
239
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
243 * controller.
244 */
245 ISR(ENDPOINT_PIPE_vect, ISR_BLOCK)
246 {
247 /* Save previously selected endpoint before selecting a new endpoint */
248 uint8_t PrevSelectedEndpoint = Endpoint_GetCurrentEndpoint();
249
250 /* Check if the control endpoint has received a request */
251 if (Endpoint_HasEndpointInterrupted(ENDPOINT_CONTROLEP))
252 {
253 /* Clear the endpoint interrupt */
254 Endpoint_ClearEndpointInterrupt(ENDPOINT_CONTROLEP);
255
256 /* Process the control request */
257 USB_USBTask();
258
259 /* Handshake the endpoint setup interrupt - must be after the call to USB_USBTask() */
260 USB_INT_Clear(ENDPOINT_INT_SETUP);
261 }
262
263 /* Check if Generic IN endpoint has interrupted */
264 if (Endpoint_HasEndpointInterrupted(GENERIC_IN_EPNUM))
265 {
266 /* Select the Generic IN Report Endpoint */
267 Endpoint_SelectEndpoint(GENERIC_IN_EPNUM);
268
269 /* Clear the endpoint IN interrupt flag */
270 USB_INT_Clear(ENDPOINT_INT_IN);
271
272 /* Clear the Generic IN Report endpoint interrupt and select the endpoint */
273 Endpoint_ClearEndpointInterrupt(GENERIC_IN_EPNUM);
274
275 /* Create a tempoary buffer to hold the report to send to the host */
276 uint8_t GenericData[GENERIC_REPORT_SIZE];
277
278 /* Create Generic Report Data */
279 CreateGenericHIDReport(GenericData);
280
281 /* Write Generic Report Data */
282 Endpoint_Write_Stream_LE(&GenericData, sizeof(GenericData));
283
284 /* Finalize the stream transfer to send the last packet */
285 Endpoint_ClearCurrentBank();
286 }
287
288 /* Check if Generic OUT endpoint has interrupted */
289 if (Endpoint_HasEndpointInterrupted(GENERIC_OUT_EPNUM))
290 {
291 /* Select the Generic OUT Report Endpoint */
292 Endpoint_SelectEndpoint(GENERIC_OUT_EPNUM);
293
294 /* Clear the endpoint OUT Interrupt flag */
295 USB_INT_Clear(ENDPOINT_INT_OUT);
296
297 /* Clear the Generic OUT Report endpoint interrupt and select the endpoint */
298 Endpoint_ClearEndpointInterrupt(GENERIC_OUT_EPNUM);
299
300 /* Create a tempoary buffer to hold the read in report from the host */
301 uint8_t GenericData[GENERIC_REPORT_SIZE];
302
303 /* Read Generic Report Data */
304 Endpoint_Read_Stream_LE(&GenericData, sizeof(GenericData));
305
306 /* Process Generic Report Data */
307 ProcessGenericHIDReport(GenericData);
308
309 /* Finalize the stream transfer to send the last packet */
310 Endpoint_ClearCurrentBank();
311 }
312
313 /* Restore previously selected endpoint */
314 Endpoint_SelectEndpoint(PrevSelectedEndpoint);
315 }