Remove outdated AVRStudio project files from demos, projects, bootloaders. Fix makefi...
[pub/USBasp.git] / Demos / Device / LowLevel / 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 /* Scheduler Task List */
40 TASK_LIST
41 {
42 { .Task = USB_USBTask , .TaskStatus = TASK_STOP },
43 { .Task = USB_HID_Report , .TaskStatus = TASK_STOP },
44 };
45
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];
48
49
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.
52 */
53 int main(void)
54 {
55 /* Disable watchdog if enabled by bootloader/fuses */
56 MCUSR &= ~(1 << WDRF);
57 wdt_disable();
58
59 /* Disable clock division */
60 clock_prescale_set(clock_div_1);
61
62 /* Hardware Initialization */
63 LEDs_Init();
64
65 /* Indicate USB not ready */
66 UpdateStatus(Status_USBNotReady);
67
68 /* Initialize Scheduler so that it can be used */
69 Scheduler_Init();
70
71 /* Initialize USB Subsystem */
72 USB_Init();
73
74 /* Scheduling - routine never returns, so put this last in the main function */
75 Scheduler_Start();
76 }
77
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.
80 */
81 void EVENT_USB_Connect(void)
82 {
83 /* Start USB management task */
84 Scheduler_SetTaskMode(USB_USBTask, TASK_RUN);
85
86 /* Indicate USB enumerating */
87 UpdateStatus(Status_USBEnumerating);
88 }
89
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.
92 */
93 void EVENT_USB_Disconnect(void)
94 {
95 /* Stop running HID reporting and USB management tasks */
96 Scheduler_SetTaskMode(USB_HID_Report, TASK_STOP);
97 Scheduler_SetTaskMode(USB_USBTask, TASK_STOP);
98
99 /* Indicate USB not ready */
100 UpdateStatus(Status_USBNotReady);
101 }
102
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.
105 */
106 void EVENT_USB_ConfigurationChanged(void)
107 {
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);
112
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);
117
118 /* Indicate USB connected and ready */
119 UpdateStatus(Status_USBReady);
120 }
121
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.
125 */
126 void EVENT_USB_UnhandledControlPacket(void)
127 {
128 /* Handle HID Class specific requests */
129 switch (USB_ControlRequest.bRequest)
130 {
131 case REQ_GetReport:
132 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
133 {
134 uint8_t GenericData[GENERIC_REPORT_SIZE];
135
136 Endpoint_ClearSETUP();
137
138 CreateGenericHIDReport(GenericData);
139
140 /* Write the report data to the control endpoint */
141 Endpoint_Write_Control_Stream_LE(&GenericData, sizeof(GenericData));
142
143 /* Finalize the stream transfer to send the last packet or clear the host abort */
144 Endpoint_ClearOUT();
145 }
146
147 break;
148 case REQ_SetReport:
149 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
150 {
151 uint8_t GenericData[GENERIC_REPORT_SIZE];
152
153 Endpoint_ClearSETUP();
154
155 /* Wait until the generic report has been sent by the host */
156 while (!(Endpoint_IsOUTReceived()));
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 /* Handshake the request by sending an empty IN packet */
169 Endpoint_ClearIN();
170 }
171
172 break;
173 }
174 }
175
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.
178 *
179 * \param CurrentStatus Current status of the system, from the GenericHID_StatusCodes_t enum
180 */
181 void UpdateStatus(uint8_t CurrentStatus)
182 {
183 uint8_t LEDMask = LEDS_NO_LEDS;
184
185 /* Set the LED mask to the appropriate LED mask based on the given status code */
186 switch (CurrentStatus)
187 {
188 case Status_USBNotReady:
189 LEDMask = (LEDS_LED1);
190 break;
191 case Status_USBEnumerating:
192 LEDMask = (LEDS_LED1 | LEDS_LED2);
193 break;
194 case Status_USBReady:
195 LEDMask = (LEDS_LED2 | LEDS_LED4);
196 break;
197 }
198
199 /* Set the board LEDs to the new LED mask */
200 LEDs_SetAllLEDs(LEDMask);
201 }
202
203 /** Function to process the lest received report from the host.
204 *
205 * \param DataArray Pointer to a buffer where the last report data is stored
206 */
207 void ProcessGenericHIDReport(uint8_t* DataArray)
208 {
209 /*
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.
213 */
214
215 for (uint8_t i = 0; i < GENERIC_REPORT_SIZE; i++)
216 LastReceived[i] = DataArray[i];
217 }
218
219 /** Function to create the next report to send back to the host at the next reporting interval.
220 *
221 * \param DataArray Pointer to a buffer where the next report data should be stored
222 */
223 void CreateGenericHIDReport(uint8_t* DataArray)
224 {
225 /*
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.
229 */
230
231 for (uint8_t i = 0; i < GENERIC_REPORT_SIZE; i++)
232 DataArray[i] = LastReceived[i];
233 }
234
235 TASK(USB_HID_Report)
236 {
237 /* Check if the USB system is connected to a host */
238 if (USB_IsConnected)
239 {
240 Endpoint_SelectEndpoint(GENERIC_OUT_EPNUM);
241
242 /* Check to see if a packet has been sent from the host */
243 if (Endpoint_IsOUTReceived())
244 {
245 /* Check to see if the packet contains data */
246 if (Endpoint_IsReadWriteAllowed())
247 {
248 /* Create a temporary buffer to hold the read in report from the host */
249 uint8_t GenericData[GENERIC_REPORT_SIZE];
250
251 /* Read Generic Report Data */
252 Endpoint_Read_Stream_LE(&GenericData, sizeof(GenericData));
253
254 /* Process Generic Report Data */
255 ProcessGenericHIDReport(GenericData);
256 }
257
258 /* Finalize the stream transfer to send the last packet */
259 Endpoint_ClearOUT();
260 }
261
262 Endpoint_SelectEndpoint(GENERIC_IN_EPNUM);
263
264 /* Check to see if the host is ready to accept another packet */
265 if (Endpoint_IsINReady())
266 {
267 /* Create a temporary buffer to hold the report to send to the host */
268 uint8_t GenericData[GENERIC_REPORT_SIZE];
269
270 /* Create Generic Report Data */
271 CreateGenericHIDReport(GenericData);
272
273 /* Write Generic Report Data */
274 Endpoint_Write_Stream_LE(&GenericData, sizeof(GenericData));
275
276 /* Finalize the stream transfer to send the last packet */
277 Endpoint_ClearIN();
278 }
279 }
280 }