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 { .Task
= USB_USBTask
, .TaskStatus
= TASK_STOP
},
43 { .Task
= USB_HID_Report
, .TaskStatus
= TASK_STOP
},
46 /** Static buffer to hold the last received report from the host, so that it can be echoed back in the next sent report */
47 static uint8_t LastReceived
[GENERIC_REPORT_SIZE
];
50 /** Main program entry point. This routine configures the hardware required by the application, then
51 * starts the scheduler to run the USB management task.
55 /* Disable watchdog if enabled by bootloader/fuses */
56 MCUSR
&= ~(1 << WDRF
);
59 /* Disable clock division */
60 clock_prescale_set(clock_div_1
);
62 /* Hardware Initialization */
65 /* Indicate USB not ready */
66 UpdateStatus(Status_USBNotReady
);
68 /* Initialize Scheduler so that it can be used */
71 /* Initialize USB Subsystem */
74 /* Scheduling - routine never returns, so put this last in the main function */
78 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and
79 * starts the library USB task to begin the enumeration and USB management process.
81 void EVENT_USB_Connect(void)
83 /* Start USB management task */
84 Scheduler_SetTaskMode(USB_USBTask
, TASK_RUN
);
86 /* Indicate USB enumerating */
87 UpdateStatus(Status_USBEnumerating
);
90 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via
91 * the status LEDs and stops the USB management task.
93 void EVENT_USB_Disconnect(void)
95 /* Stop running HID reporting and USB management tasks */
96 Scheduler_SetTaskMode(USB_HID_Report
, TASK_STOP
);
97 Scheduler_SetTaskMode(USB_USBTask
, TASK_STOP
);
99 /* Indicate USB not ready */
100 UpdateStatus(Status_USBNotReady
);
103 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host sets the current configuration
104 * of the USB device after enumeration, and configures the generic HID device endpoints.
106 void EVENT_USB_ConfigurationChanged(void)
108 /* Setup Generic IN Report Endpoint */
109 Endpoint_ConfigureEndpoint(GENERIC_IN_EPNUM
, EP_TYPE_INTERRUPT
,
110 ENDPOINT_DIR_IN
, GENERIC_EPSIZE
,
111 ENDPOINT_BANK_SINGLE
);
113 /* Setup Generic OUT Report Endpoint */
114 Endpoint_ConfigureEndpoint(GENERIC_OUT_EPNUM
, EP_TYPE_INTERRUPT
,
115 ENDPOINT_DIR_OUT
, GENERIC_EPSIZE
,
116 ENDPOINT_BANK_SINGLE
);
118 /* Indicate USB connected and ready */
119 UpdateStatus(Status_USBReady
);
122 /** Event handler for the USB_UnhandledControlPacket event. This is used to catch standard and class specific
123 * control requests that are not handled internally by the USB library (including the HID commands, which are
124 * all issued via the control endpoint), so that they can be handled appropriately for the application.
126 void EVENT_USB_UnhandledControlPacket(void)
128 /* Handle HID Class specific requests */
129 switch (USB_ControlRequest
.bRequest
)
132 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
))
134 uint8_t GenericData
[GENERIC_REPORT_SIZE
];
136 Endpoint_ClearSETUP();
138 CreateGenericHIDReport(GenericData
);
140 /* Write the report data to the control endpoint */
141 Endpoint_Write_Control_Stream_LE(&GenericData
, sizeof(GenericData
));
143 /* Finalize the stream transfer to send the last packet or clear the host abort */
149 if (USB_ControlRequest
.bmRequestType
== (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
))
151 uint8_t GenericData
[GENERIC_REPORT_SIZE
];
153 Endpoint_ClearSETUP();
155 /* Wait until the generic report has been sent by the host */
156 while (!(Endpoint_IsOUTReceived()));
158 Endpoint_Read_Control_Stream_LE(&GenericData
, sizeof(GenericData
));
160 ProcessGenericHIDReport(GenericData
);
162 /* Clear the endpoint data */
165 /* Wait until the host is ready to receive the request confirmation */
166 while (!(Endpoint_IsINReady()));
168 /* Handshake the request by sending an empty IN packet */
176 /** Function to manage status updates to the user. This is done via LEDs on the given board, if available, but may be changed to
177 * log to a serial port, or anything else that is suitable for status updates.
179 * \param CurrentStatus Current status of the system, from the GenericHID_StatusCodes_t enum
181 void UpdateStatus(uint8_t CurrentStatus
)
183 uint8_t LEDMask
= LEDS_NO_LEDS
;
185 /* Set the LED mask to the appropriate LED mask based on the given status code */
186 switch (CurrentStatus
)
188 case Status_USBNotReady
:
189 LEDMask
= (LEDS_LED1
);
191 case Status_USBEnumerating
:
192 LEDMask
= (LEDS_LED1
| LEDS_LED2
);
194 case Status_USBReady
:
195 LEDMask
= (LEDS_LED2
| LEDS_LED4
);
199 /* Set the board LEDs to the new LED mask */
200 LEDs_SetAllLEDs(LEDMask
);
203 /** Function to process the lest received report from the host.
205 * \param DataArray Pointer to a buffer where the last report data is stored
207 void ProcessGenericHIDReport(uint8_t* DataArray
)
210 This is where you need to process the reports being sent from the host to the device.
211 DataArray is an array holding the last report from the host. This function is called
212 each time the host has sent a report to the device.
215 for (uint8_t i
= 0; i
< GENERIC_REPORT_SIZE
; i
++)
216 LastReceived
[i
] = DataArray
[i
];
219 /** Function to create the next report to send back to the host at the next reporting interval.
221 * \param DataArray Pointer to a buffer where the next report data should be stored
223 void CreateGenericHIDReport(uint8_t* DataArray
)
226 This is where you need to create reports to be sent to the host from the device. This
227 function is called each time the host is ready to accept a new report. DataArray is
228 an array to hold the report to the host.
231 for (uint8_t i
= 0; i
< GENERIC_REPORT_SIZE
; i
++)
232 DataArray
[i
] = LastReceived
[i
];
237 /* Check if the USB system is connected to a host */
240 Endpoint_SelectEndpoint(GENERIC_OUT_EPNUM
);
242 /* Check to see if a packet has been sent from the host */
243 if (Endpoint_IsOUTReceived())
245 /* Check to see if the packet contains data */
246 if (Endpoint_IsReadWriteAllowed())
248 /* Create a temporary buffer to hold the read in report from the host */
249 uint8_t GenericData
[GENERIC_REPORT_SIZE
];
251 /* Read Generic Report Data */
252 Endpoint_Read_Stream_LE(&GenericData
, sizeof(GenericData
));
254 /* Process Generic Report Data */
255 ProcessGenericHIDReport(GenericData
);
258 /* Finalize the stream transfer to send the last packet */
262 Endpoint_SelectEndpoint(GENERIC_IN_EPNUM
);
264 /* Check to see if the host is ready to accept another packet */
265 if (Endpoint_IsINReady())
267 /* Create a temporary buffer to hold the report to send to the host */
268 uint8_t GenericData
[GENERIC_REPORT_SIZE
];
270 /* Create Generic Report Data */
271 CreateGenericHIDReport(GenericData
);
273 /* Write Generic Report Data */
274 Endpoint_Write_Stream_LE(&GenericData
, sizeof(GenericData
));
276 /* Finalize the stream transfer to send the last packet */