More spell checking of all source files -- correct missed errors, switch to EN-GB...
[pub/USBasp.git] / Demos / Device / LowLevel / GenericHID / GenericHID.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all 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 /** Static buffer to hold the last received report from the host, so that it can be echoed back in the next sent report */
40 static uint8_t LastReceived[GENERIC_REPORT_SIZE];
41
42
43 /** Main program entry point. This routine configures the hardware required by the application, then
44 * enters a loop to run the application tasks in sequence.
45 */
46 int main(void)
47 {
48 SetupHardware();
49
50 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
51 sei();
52
53 for (;;)
54 {
55 HID_Task();
56 USB_USBTask();
57 }
58 }
59
60 /** Configures the board hardware and chip peripherals for the demo's functionality. */
61 void SetupHardware(void)
62 {
63 /* Disable watchdog if enabled by bootloader/fuses */
64 MCUSR &= ~(1 << WDRF);
65 wdt_disable();
66
67 /* Disable clock division */
68 clock_prescale_set(clock_div_1);
69
70 /* Hardware Initialization */
71 LEDs_Init();
72 USB_Init();
73 }
74
75 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and
76 * starts the library USB task to begin the enumeration and USB management process.
77 */
78 void EVENT_USB_Device_Connect(void)
79 {
80 /* Indicate USB enumerating */
81 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
82 }
83
84 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via
85 * the status LEDs and stops the USB management task.
86 */
87 void EVENT_USB_Device_Disconnect(void)
88 {
89 /* Indicate USB not ready */
90 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
91 }
92
93 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host sets the current configuration
94 * of the USB device after enumeration, and configures the generic HID device endpoints.
95 */
96 void EVENT_USB_Device_ConfigurationChanged(void)
97 {
98 /* Indicate USB connected and ready */
99 LEDs_SetAllLEDs(LEDMASK_USB_READY);
100
101 /* Setup Generic IN Report Endpoint */
102 if (!(Endpoint_ConfigureEndpoint(GENERIC_IN_EPNUM, EP_TYPE_INTERRUPT,
103 ENDPOINT_DIR_IN, GENERIC_EPSIZE,
104 ENDPOINT_BANK_SINGLE)))
105 {
106 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
107 }
108
109 /* Setup Generic OUT Report Endpoint */
110 if (!(Endpoint_ConfigureEndpoint(GENERIC_OUT_EPNUM, EP_TYPE_INTERRUPT,
111 ENDPOINT_DIR_OUT, GENERIC_EPSIZE,
112 ENDPOINT_BANK_SINGLE)))
113 {
114 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
115 }
116 }
117
118 /** Event handler for the USB_UnhandledControlRequest event. This is used to catch standard and class specific
119 * control requests that are not handled internally by the USB library (including the HID commands, which are
120 * all issued via the control endpoint), so that they can be handled appropriately for the application.
121 */
122 void EVENT_USB_Device_UnhandledControlRequest(void)
123 {
124 /* Handle HID Class specific requests */
125 switch (USB_ControlRequest.bRequest)
126 {
127 case REQ_GetReport:
128 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
129 {
130 uint8_t GenericData[GENERIC_REPORT_SIZE];
131
132 Endpoint_ClearSETUP();
133
134 CreateGenericHIDReport(GenericData);
135
136 /* Write the report data to the control endpoint */
137 Endpoint_Write_Control_Stream_LE(&GenericData, sizeof(GenericData));
138
139 /* Finalize the stream transfer to send the last packet or clear the host abort */
140 Endpoint_ClearOUT();
141 }
142
143 break;
144 case REQ_SetReport:
145 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
146 {
147 uint8_t GenericData[GENERIC_REPORT_SIZE];
148
149 Endpoint_ClearSETUP();
150
151 /* Wait until the generic report has been sent by the host */
152 while (!(Endpoint_IsOUTReceived()))
153 {
154 if (USB_DeviceState == DEVICE_STATE_Unattached)
155 return;
156 }
157
158 Endpoint_Read_Control_Stream_LE(&GenericData, sizeof(GenericData));
159
160 ProcessGenericHIDReport(GenericData);
161
162 /* Clear the endpoint data */
163 Endpoint_ClearOUT();
164
165 /* Wait until the host is ready to receive the request confirmation */
166 while (!(Endpoint_IsINReady()))
167 {
168 if (USB_DeviceState == DEVICE_STATE_Unattached)
169 return;
170 }
171
172 /* Handshake the request by sending an empty IN packet */
173 Endpoint_ClearIN();
174 }
175
176 break;
177 }
178 }
179
180 /** Function to process the lest received report from the host.
181 *
182 * \param[in] DataArray Pointer to a buffer where the last report data is stored
183 */
184 void ProcessGenericHIDReport(uint8_t* DataArray)
185 {
186 /*
187 This is where you need to process the reports being sent from the host to the device.
188 DataArray is an array holding the last report from the host. This function is called
189 each time the host has sent a report to the device.
190 */
191
192 for (uint8_t i = 0; i < GENERIC_REPORT_SIZE; i++)
193 LastReceived[i] = DataArray[i];
194 }
195
196 /** Function to create the next report to send back to the host at the next reporting interval.
197 *
198 * \param[out] DataArray Pointer to a buffer where the next report data should be stored
199 */
200 void CreateGenericHIDReport(uint8_t* DataArray)
201 {
202 /*
203 This is where you need to create reports to be sent to the host from the device. This
204 function is called each time the host is ready to accept a new report. DataArray is
205 an array to hold the report to the host.
206 */
207
208 for (uint8_t i = 0; i < GENERIC_REPORT_SIZE; i++)
209 DataArray[i] = LastReceived[i];
210 }
211
212 void HID_Task(void)
213 {
214 /* Device must be connected and configured for the task to run */
215 if (USB_DeviceState != DEVICE_STATE_Configured)
216 return;
217
218 Endpoint_SelectEndpoint(GENERIC_OUT_EPNUM);
219
220 /* Check to see if a packet has been sent from the host */
221 if (Endpoint_IsOUTReceived())
222 {
223 /* Check to see if the packet contains data */
224 if (Endpoint_IsReadWriteAllowed())
225 {
226 /* Create a temporary buffer to hold the read in report from the host */
227 uint8_t GenericData[GENERIC_REPORT_SIZE];
228
229 /* Read Generic Report Data */
230 Endpoint_Read_Stream_LE(&GenericData, sizeof(GenericData));
231
232 /* Process Generic Report Data */
233 ProcessGenericHIDReport(GenericData);
234 }
235
236 /* Finalize the stream transfer to send the last packet */
237 Endpoint_ClearOUT();
238 }
239
240 Endpoint_SelectEndpoint(GENERIC_IN_EPNUM);
241
242 /* Check to see if the host is ready to accept another packet */
243 if (Endpoint_IsINReady())
244 {
245 /* Create a temporary buffer to hold the report to send to the host */
246 uint8_t GenericData[GENERIC_REPORT_SIZE];
247
248 /* Create Generic Report Data */
249 CreateGenericHIDReport(GenericData);
250
251 /* Write Generic Report Data */
252 Endpoint_Write_Stream_LE(&GenericData, sizeof(GenericData));
253
254 /* Finalize the stream transfer to send the last packet */
255 Endpoint_ClearIN();
256 }
257 }