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 /* Scheduler Task List */
42 #if !defined(INTERRUPT_CONTROL_ENDPOINT)
43 { .Task
= USB_USBTask
, .TaskStatus
= TASK_STOP
},
46 #if !defined(INTERRUPT_DATA_ENDPOINT)
47 { .Task
= USB_HID_Report
, .TaskStatus
= TASK_STOP
},
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
];
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.
60 /* Disable watchdog if enabled by bootloader/fuses */
61 MCUSR
&= ~(1 << WDRF
);
64 /* Disable clock division */
65 clock_prescale_set(clock_div_1
);
67 /* Indicate USB not ready */
68 UpdateStatus(Status_USBNotReady
);
70 /* Initialize Scheduler so that it can be used */
73 /* Initialize USB Subsystem */
76 /* Scheduling - routine never returns, so put this last in the main function */
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.
84 EVENT_HANDLER(USB_Reset
)
86 #if defined(INTERRUPT_CONTROL_ENDPOINT)
87 /* Select the control endpoint */
88 Endpoint_SelectEndpoint(ENDPOINT_CONTROLEP
);
90 /* Enable the endpoint SETUP interrupt ISR for the control endpoint */
91 USB_INT_Enable(ENDPOINT_INT_SETUP
);
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.
98 EVENT_HANDLER(USB_Connect
)
100 #if !defined(INTERRUPT_CONTROL_ENDPOINT)
101 /* Start USB management task */
102 Scheduler_SetTaskMode(USB_USBTask
, TASK_RUN
);
105 /* Indicate USB enumerating */
106 UpdateStatus(Status_USBEnumerating
);
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.
112 EVENT_HANDLER(USB_Disconnect
)
114 /* Stop running HID reporting and USB management tasks */
115 #if !defined(INTERRUPT_DATA_ENDPOINT)
116 Scheduler_SetTaskMode(USB_HID_Report
, TASK_STOP
);
119 #if !defined(INTERRUPT_CONTROL_ENDPOINT)
120 Scheduler_SetTaskMode(USB_USBTask
, TASK_STOP
);
123 /* Indicate USB not ready */
124 UpdateStatus(Status_USBNotReady
);
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.
130 EVENT_HANDLER(USB_ConfigurationChanged
)
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
);
137 #if defined(INTERRUPT_DATA_ENDPOINT)
138 /* Enable the endpoint IN interrupt ISR for the report endpoint */
139 USB_INT_Enable(ENDPOINT_INT_IN
);
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
);
147 #if defined(INTERRUPT_DATA_ENDPOINT)
148 /* Enable the endpoint OUT interrupt ISR for the report endpoint */
149 USB_INT_Enable(ENDPOINT_INT_OUT
);
152 /* Indicate USB connected and ready */
153 UpdateStatus(Status_USBReady
);
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.
160 EVENT_HANDLER(USB_UnhandledControlPacket
)
162 /* Handle HID Class specific requests */
163 switch (USB_ControlRequest
.bRequest
)
166 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
))
168 uint8_t GenericData
[GENERIC_REPORT_SIZE
];
170 Endpoint_ClearSETUP();
172 CreateGenericHIDReport(GenericData
);
174 /* Write the report data to the control endpoint */
175 Endpoint_Write_Control_Stream_LE(&GenericData
, sizeof(GenericData
));
177 /* Finalize the stream transfer to send the last packet or clear the host abort */
183 if (USB_ControlRequest
.bmRequestType
== (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
))
185 uint8_t GenericData
[GENERIC_REPORT_SIZE
];
187 Endpoint_ClearSETUP();
189 /* Wait until the generic report has been sent by the host */
190 while (!(Endpoint_IsOUTReceived()));
192 Endpoint_Read_Control_Stream_LE(&GenericData
, sizeof(GenericData
));
194 ProcessGenericHIDReport(GenericData
);
196 /* Clear the endpoint data */
199 /* Wait until the host is ready to receive the request confirmation */
200 while (!(Endpoint_IsINReady()));
202 /* Handshake the request by sending an empty IN packet */
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.
213 * \param CurrentStatus Current status of the system, from the GenericHID_StatusCodes_t enum
215 void UpdateStatus(uint8_t CurrentStatus
)
217 uint8_t LEDMask
= LEDS_NO_LEDS
;
219 /* Set the LED mask to the appropriate LED mask based on the given status code */
220 switch (CurrentStatus
)
222 case Status_USBNotReady
:
223 LEDMask
= (LEDS_LED1
);
225 case Status_USBEnumerating
:
226 LEDMask
= (LEDS_LED1
| LEDS_LED2
);
228 case Status_USBReady
:
229 LEDMask
= (LEDS_LED2
| LEDS_LED4
);
233 /* Set the board LEDs to the new LED mask */
234 LEDs_SetAllLEDs(LEDMask
);
237 /** Function to process the lest received report from the host.
239 * \param DataArray Pointer to a buffer where the last report data is stored
241 void ProcessGenericHIDReport(uint8_t* DataArray
)
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.
249 for (uint8_t i
= 0; i
< GENERIC_REPORT_SIZE
; i
++)
250 LastReceived
[i
] = DataArray
[i
];
253 /** Function to create the next report to send back to the host at the next reporting interval.
255 * \param DataArray Pointer to a buffer where the next report data should be stored
257 void CreateGenericHIDReport(uint8_t* DataArray
)
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.
265 for (uint8_t i
= 0; i
< GENERIC_REPORT_SIZE
; i
++)
266 DataArray
[i
] = LastReceived
[i
];
269 #if !defined(INTERRUPT_DATA_ENDPOINT)
272 /* Check if the USB system is connected to a host */
275 Endpoint_SelectEndpoint(GENERIC_OUT_EPNUM
);
277 /* Check to see if a packet has been sent from the host */
278 if (Endpoint_IsOUTReceived())
280 /* Check to see if the packet contains data */
281 if (Endpoint_IsReadWriteAllowed())
283 /* Create a temporary buffer to hold the read in report from the host */
284 uint8_t GenericData
[GENERIC_REPORT_SIZE
];
286 /* Read Generic Report Data */
287 Endpoint_Read_Stream_LE(&GenericData
, sizeof(GenericData
));
289 /* Process Generic Report Data */
290 ProcessGenericHIDReport(GenericData
);
293 /* Finalize the stream transfer to send the last packet */
297 Endpoint_SelectEndpoint(GENERIC_IN_EPNUM
);
299 /* Check to see if the host is ready to accept another packet */
300 if (Endpoint_IsINReady())
302 /* Create a temporary buffer to hold the report to send to the host */
303 uint8_t GenericData
[GENERIC_REPORT_SIZE
];
305 /* Create Generic Report Data */
306 CreateGenericHIDReport(GenericData
);
308 /* Write Generic Report Data */
309 Endpoint_Write_Stream_LE(&GenericData
, sizeof(GenericData
));
311 /* Finalize the stream transfer to send the last packet */
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
323 ISR(ENDPOINT_PIPE_vect
, ISR_BLOCK
)
325 /* Save previously selected endpoint before selecting a new endpoint */
326 uint8_t PrevSelectedEndpoint
= Endpoint_GetCurrentEndpoint();
328 #if defined(INTERRUPT_CONTROL_ENDPOINT)
329 /* Check if the control endpoint has received a request */
330 if (Endpoint_HasEndpointInterrupted(ENDPOINT_CONTROLEP
))
332 /* Clear the endpoint interrupt */
333 Endpoint_ClearEndpointInterrupt(ENDPOINT_CONTROLEP
);
335 /* Process the control request */
338 /* Handshake the endpoint setup interrupt - must be after the call to USB_USBTask() */
339 USB_INT_Clear(ENDPOINT_INT_SETUP
);
343 #if defined(INTERRUPT_DATA_ENDPOINT)
344 /* Check if Generic IN endpoint has interrupted */
345 if (Endpoint_HasEndpointInterrupted(GENERIC_IN_EPNUM
))
347 /* Select the Generic IN Report Endpoint */
348 Endpoint_SelectEndpoint(GENERIC_IN_EPNUM
);
350 /* Clear the endpoint IN interrupt flag */
351 USB_INT_Clear(ENDPOINT_INT_IN
);
353 /* Clear the Generic IN Report endpoint interrupt and select the endpoint */
354 Endpoint_ClearEndpointInterrupt(GENERIC_IN_EPNUM
);
356 /* Create a temporary buffer to hold the report to send to the host */
357 uint8_t GenericData
[GENERIC_REPORT_SIZE
];
359 /* Create Generic Report Data */
360 CreateGenericHIDReport(GenericData
);
362 /* Write Generic Report Data */
363 Endpoint_Write_Stream_LE(&GenericData
, sizeof(GenericData
));
365 /* Finalize the stream transfer to send the last packet */
369 /* Check if Generic OUT endpoint has interrupted */
370 if (Endpoint_HasEndpointInterrupted(GENERIC_OUT_EPNUM
))
372 /* Select the Generic OUT Report Endpoint */
373 Endpoint_SelectEndpoint(GENERIC_OUT_EPNUM
);
375 /* Clear the endpoint OUT Interrupt flag */
376 USB_INT_Clear(ENDPOINT_INT_OUT
);
378 /* Clear the Generic OUT Report endpoint interrupt and select the endpoint */
379 Endpoint_ClearEndpointInterrupt(GENERIC_OUT_EPNUM
);
381 /* Create a temporary buffer to hold the read in report from the host */
382 uint8_t GenericData
[GENERIC_REPORT_SIZE
];
384 /* Read Generic Report Data */
385 Endpoint_Read_Stream_LE(&GenericData
, sizeof(GenericData
));
387 /* Process Generic Report Data */
388 ProcessGenericHIDReport(GenericData
);
390 /* Finalize the stream transfer to send the last packet */
395 /* Restore previously selected endpoint */
396 Endpoint_SelectEndpoint(PrevSelectedEndpoint
);