Fixed CDC class drivers not saving and sending all 16-bits of the control line states...
[pub/USBasp.git] / Demos / Device / LowLevel / GenericHID / GenericHID.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2011.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2011 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
40 /** Main program entry point. This routine configures the hardware required by the application, then
41 * enters a loop to run the application tasks in sequence.
42 */
43 int main(void)
44 {
45 SetupHardware();
46
47 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
48 sei();
49
50 for (;;)
51 {
52 HID_Task();
53 USB_USBTask();
54 }
55 }
56
57 /** Configures the board hardware and chip peripherals for the demo's functionality. */
58 void SetupHardware(void)
59 {
60 /* Disable watchdog if enabled by bootloader/fuses */
61 MCUSR &= ~(1 << WDRF);
62 wdt_disable();
63
64 /* Disable clock division */
65 clock_prescale_set(clock_div_1);
66
67 /* Hardware Initialization */
68 LEDs_Init();
69 USB_Init();
70 }
71
72 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and
73 * starts the library USB task to begin the enumeration and USB management process.
74 */
75 void EVENT_USB_Device_Connect(void)
76 {
77 /* Indicate USB enumerating */
78 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
79 }
80
81 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via
82 * the status LEDs and stops the USB management task.
83 */
84 void EVENT_USB_Device_Disconnect(void)
85 {
86 /* Indicate USB not ready */
87 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
88 }
89
90 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host sets the current configuration
91 * of the USB device after enumeration, and configures the generic HID device endpoints.
92 */
93 void EVENT_USB_Device_ConfigurationChanged(void)
94 {
95 bool ConfigSuccess = true;
96
97 /* Setup HID Report Endpoints */
98 ConfigSuccess &= Endpoint_ConfigureEndpoint(GENERIC_IN_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
99 GENERIC_EPSIZE, ENDPOINT_BANK_SINGLE);
100 ConfigSuccess &= Endpoint_ConfigureEndpoint(GENERIC_OUT_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_OUT,
101 GENERIC_EPSIZE, ENDPOINT_BANK_SINGLE);
102
103 /* Indicate endpoint configuration success or failure */
104 LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
105 }
106
107 /** Event handler for the USB_ControlRequest event. This is used to catch and process control requests sent to
108 * the device from the USB host before passing along unhandled control requests to the library for processing
109 * internally.
110 */
111 void EVENT_USB_Device_ControlRequest(void)
112 {
113 /* Handle HID Class specific requests */
114 switch (USB_ControlRequest.bRequest)
115 {
116 case HID_REQ_GetReport:
117 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
118 {
119 uint8_t GenericData[GENERIC_REPORT_SIZE];
120 CreateGenericHIDReport(GenericData);
121
122 Endpoint_ClearSETUP();
123
124 /* Write the report data to the control endpoint */
125 Endpoint_Write_Control_Stream_LE(&GenericData, sizeof(GenericData));
126 Endpoint_ClearOUT();
127 }
128
129 break;
130 case HID_REQ_SetReport:
131 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
132 {
133 uint8_t GenericData[GENERIC_REPORT_SIZE];
134
135 Endpoint_ClearSETUP();
136
137 /* Read the report data from the control endpoint */
138 Endpoint_Read_Control_Stream_LE(&GenericData, sizeof(GenericData));
139 Endpoint_ClearIN();
140
141 ProcessGenericHIDReport(GenericData);
142 }
143
144 break;
145 }
146 }
147
148 /** Function to process the last received report from the host.
149 *
150 * \param[in] DataArray Pointer to a buffer where the last received report has been stored
151 */
152 void ProcessGenericHIDReport(uint8_t* DataArray)
153 {
154 /*
155 This is where you need to process reports sent from the host to the device. This
156 function is called each time the host has sent a new report. DataArray is an array
157 holding the report sent from the host.
158 */
159
160 uint8_t NewLEDMask = LEDS_NO_LEDS;
161
162 if (DataArray[0])
163 NewLEDMask |= LEDS_LED1;
164
165 if (DataArray[1])
166 NewLEDMask |= LEDS_LED1;
167
168 if (DataArray[2])
169 NewLEDMask |= LEDS_LED1;
170
171 if (DataArray[3])
172 NewLEDMask |= LEDS_LED1;
173
174 LEDs_SetAllLEDs(NewLEDMask);
175 }
176
177 /** Function to create the next report to send back to the host at the next reporting interval.
178 *
179 * \param[out] DataArray Pointer to a buffer where the next report data should be stored
180 */
181 void CreateGenericHIDReport(uint8_t* DataArray)
182 {
183 /*
184 This is where you need to create reports to be sent to the host from the device. This
185 function is called each time the host is ready to accept a new report. DataArray is
186 an array to hold the report to the host.
187 */
188
189 uint8_t CurrLEDMask = LEDs_GetLEDs();
190
191 DataArray[0] = ((CurrLEDMask & LEDS_LED1) ? 1 : 0);
192 DataArray[1] = ((CurrLEDMask & LEDS_LED2) ? 1 : 0);
193 DataArray[2] = ((CurrLEDMask & LEDS_LED3) ? 1 : 0);
194 DataArray[3] = ((CurrLEDMask & LEDS_LED4) ? 1 : 0);
195 }
196
197 void HID_Task(void)
198 {
199 /* Device must be connected and configured for the task to run */
200 if (USB_DeviceState != DEVICE_STATE_Configured)
201 return;
202
203 Endpoint_SelectEndpoint(GENERIC_OUT_EPNUM);
204
205 /* Check to see if a packet has been sent from the host */
206 if (Endpoint_IsOUTReceived())
207 {
208 /* Check to see if the packet contains data */
209 if (Endpoint_IsReadWriteAllowed())
210 {
211 /* Create a temporary buffer to hold the read in report from the host */
212 uint8_t GenericData[GENERIC_REPORT_SIZE];
213
214 /* Read Generic Report Data */
215 Endpoint_Read_Stream_LE(&GenericData, sizeof(GenericData), NULL);
216
217 /* Process Generic Report Data */
218 ProcessGenericHIDReport(GenericData);
219 }
220
221 /* Finalize the stream transfer to send the last packet */
222 Endpoint_ClearOUT();
223 }
224
225 Endpoint_SelectEndpoint(GENERIC_IN_EPNUM);
226
227 /* Check to see if the host is ready to accept another packet */
228 if (Endpoint_IsINReady())
229 {
230 /* Create a temporary buffer to hold the report to send to the host */
231 uint8_t GenericData[GENERIC_REPORT_SIZE];
232
233 /* Create Generic Report Data */
234 CreateGenericHIDReport(GenericData);
235
236 /* Write Generic Report Data */
237 Endpoint_Write_Stream_LE(&GenericData, sizeof(GenericData), NULL);
238
239 /* Finalize the stream transfer to send the last packet */
240 Endpoint_ClearIN();
241 }
242 }
243