Rewritten event system to remove all macros, to make user code clearer.
[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 /* 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 /* Indicate USB not ready */
63 UpdateStatus(Status_USBNotReady);
64
65 /* Initialize Scheduler so that it can be used */
66 Scheduler_Init();
67
68 /* Initialize USB Subsystem */
69 USB_Init();
70
71 /* Scheduling - routine never returns, so put this last in the main function */
72 Scheduler_Start();
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_Connect(void)
79 {
80 /* Start USB management task */
81 Scheduler_SetTaskMode(USB_USBTask, TASK_RUN);
82
83 /* Indicate USB enumerating */
84 UpdateStatus(Status_USBEnumerating);
85 }
86
87 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via
88 * the status LEDs and stops the USB management task.
89 */
90 void EVENT_USB_Disconnect(void)
91 {
92 /* Stop running HID reporting and USB management tasks */
93 Scheduler_SetTaskMode(USB_HID_Report, TASK_STOP);
94 Scheduler_SetTaskMode(USB_USBTask, TASK_STOP);
95
96 /* Indicate USB not ready */
97 UpdateStatus(Status_USBNotReady);
98 }
99
100 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host sets the current configuration
101 * of the USB device after enumeration, and configures the generic HID device endpoints.
102 */
103 void EVENT_USB_ConfigurationChanged(void)
104 {
105 /* Setup Generic IN Report Endpoint */
106 Endpoint_ConfigureEndpoint(GENERIC_IN_EPNUM, EP_TYPE_INTERRUPT,
107 ENDPOINT_DIR_IN, GENERIC_EPSIZE,
108 ENDPOINT_BANK_SINGLE);
109
110 /* Setup Generic OUT Report Endpoint */
111 Endpoint_ConfigureEndpoint(GENERIC_OUT_EPNUM, EP_TYPE_INTERRUPT,
112 ENDPOINT_DIR_OUT, GENERIC_EPSIZE,
113 ENDPOINT_BANK_SINGLE);
114
115 /* Indicate USB connected and ready */
116 UpdateStatus(Status_USBReady);
117 }
118
119 /** Event handler for the USB_UnhandledControlPacket event. This is used to catch standard and class specific
120 * control requests that are not handled internally by the USB library (including the HID commands, which are
121 * all issued via the control endpoint), so that they can be handled appropriately for the application.
122 */
123 void EVENT_USB_UnhandledControlPacket(void)
124 {
125 /* Handle HID Class specific requests */
126 switch (USB_ControlRequest.bRequest)
127 {
128 case REQ_GetReport:
129 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
130 {
131 uint8_t GenericData[GENERIC_REPORT_SIZE];
132
133 Endpoint_ClearSETUP();
134
135 CreateGenericHIDReport(GenericData);
136
137 /* Write the report data to the control endpoint */
138 Endpoint_Write_Control_Stream_LE(&GenericData, sizeof(GenericData));
139
140 /* Finalize the stream transfer to send the last packet or clear the host abort */
141 Endpoint_ClearOUT();
142 }
143
144 break;
145 case REQ_SetReport:
146 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
147 {
148 uint8_t GenericData[GENERIC_REPORT_SIZE];
149
150 Endpoint_ClearSETUP();
151
152 /* Wait until the generic report has been sent by the host */
153 while (!(Endpoint_IsOUTReceived()));
154
155 Endpoint_Read_Control_Stream_LE(&GenericData, sizeof(GenericData));
156
157 ProcessGenericHIDReport(GenericData);
158
159 /* Clear the endpoint data */
160 Endpoint_ClearOUT();
161
162 /* Wait until the host is ready to receive the request confirmation */
163 while (!(Endpoint_IsINReady()));
164
165 /* Handshake the request by sending an empty IN packet */
166 Endpoint_ClearIN();
167 }
168
169 break;
170 }
171 }
172
173 /** Function to manage status updates to the user. This is done via LEDs on the given board, if available, but may be changed to
174 * log to a serial port, or anything else that is suitable for status updates.
175 *
176 * \param CurrentStatus Current status of the system, from the GenericHID_StatusCodes_t enum
177 */
178 void UpdateStatus(uint8_t CurrentStatus)
179 {
180 uint8_t LEDMask = LEDS_NO_LEDS;
181
182 /* Set the LED mask to the appropriate LED mask based on the given status code */
183 switch (CurrentStatus)
184 {
185 case Status_USBNotReady:
186 LEDMask = (LEDS_LED1);
187 break;
188 case Status_USBEnumerating:
189 LEDMask = (LEDS_LED1 | LEDS_LED2);
190 break;
191 case Status_USBReady:
192 LEDMask = (LEDS_LED2 | LEDS_LED4);
193 break;
194 }
195
196 /* Set the board LEDs to the new LED mask */
197 LEDs_SetAllLEDs(LEDMask);
198 }
199
200 /** Function to process the lest received report from the host.
201 *
202 * \param DataArray Pointer to a buffer where the last report data is stored
203 */
204 void ProcessGenericHIDReport(uint8_t* DataArray)
205 {
206 /*
207 This is where you need to process the reports being sent from the host to the device.
208 DataArray is an array holding the last report from the host. This function is called
209 each time the host has sent a report to the device.
210 */
211
212 for (uint8_t i = 0; i < GENERIC_REPORT_SIZE; i++)
213 LastReceived[i] = DataArray[i];
214 }
215
216 /** Function to create the next report to send back to the host at the next reporting interval.
217 *
218 * \param DataArray Pointer to a buffer where the next report data should be stored
219 */
220 void CreateGenericHIDReport(uint8_t* DataArray)
221 {
222 /*
223 This is where you need to create reports to be sent to the host from the device. This
224 function is called each time the host is ready to accept a new report. DataArray is
225 an array to hold the report to the host.
226 */
227
228 for (uint8_t i = 0; i < GENERIC_REPORT_SIZE; i++)
229 DataArray[i] = LastReceived[i];
230 }
231
232 TASK(USB_HID_Report)
233 {
234 /* Check if the USB system is connected to a host */
235 if (USB_IsConnected)
236 {
237 Endpoint_SelectEndpoint(GENERIC_OUT_EPNUM);
238
239 /* Check to see if a packet has been sent from the host */
240 if (Endpoint_IsOUTReceived())
241 {
242 /* Check to see if the packet contains data */
243 if (Endpoint_IsReadWriteAllowed())
244 {
245 /* Create a temporary buffer to hold the read in report from the host */
246 uint8_t GenericData[GENERIC_REPORT_SIZE];
247
248 /* Read Generic Report Data */
249 Endpoint_Read_Stream_LE(&GenericData, sizeof(GenericData));
250
251 /* Process Generic Report Data */
252 ProcessGenericHIDReport(GenericData);
253 }
254
255 /* Finalize the stream transfer to send the last packet */
256 Endpoint_ClearOUT();
257 }
258
259 Endpoint_SelectEndpoint(GENERIC_IN_EPNUM);
260
261 /* Check to see if the host is ready to accept another packet */
262 if (Endpoint_IsINReady())
263 {
264 /* Create a temporary buffer to hold the report to send to the host */
265 uint8_t GenericData[GENERIC_REPORT_SIZE];
266
267 /* Create Generic Report Data */
268 CreateGenericHIDReport(GenericData);
269
270 /* Write Generic Report Data */
271 Endpoint_Write_Stream_LE(&GenericData, sizeof(GenericData));
272
273 /* Finalize the stream transfer to send the last packet */
274 Endpoint_ClearIN();
275 }
276 }
277 }