Merge in latest trunk.
[pub/USBasp.git] / Demos / Device / LowLevel / GenericHID / GenericHID.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2012.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2012 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_EPADDR, EP_TYPE_INTERRUPT, GENERIC_EPSIZE, 1);
99 ConfigSuccess &= Endpoint_ConfigureEndpoint(GENERIC_OUT_EPADDR, EP_TYPE_INTERRUPT, GENERIC_EPSIZE, 1);
100
101 /* Indicate endpoint configuration success or failure */
102 LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
103 }
104
105 /** Event handler for the USB_ControlRequest event. This is used to catch and process control requests sent to
106 * the device from the USB host before passing along unhandled control requests to the library for processing
107 * internally.
108 */
109 void EVENT_USB_Device_ControlRequest(void)
110 {
111 /* Handle HID Class specific requests */
112 switch (USB_ControlRequest.bRequest)
113 {
114 case HID_REQ_GetReport:
115 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
116 {
117 uint8_t GenericData[GENERIC_REPORT_SIZE];
118 CreateGenericHIDReport(GenericData);
119
120 Endpoint_ClearSETUP();
121
122 /* Write the report data to the control endpoint */
123 Endpoint_Write_Control_Stream_LE(&GenericData, sizeof(GenericData));
124 Endpoint_ClearOUT();
125 }
126
127 break;
128 case HID_REQ_SetReport:
129 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
130 {
131 uint8_t GenericData[GENERIC_REPORT_SIZE];
132
133 Endpoint_ClearSETUP();
134
135 /* Read the report data from the control endpoint */
136 Endpoint_Read_Control_Stream_LE(&GenericData, sizeof(GenericData));
137 Endpoint_ClearIN();
138
139 ProcessGenericHIDReport(GenericData);
140 }
141
142 break;
143 }
144 }
145
146 /** Function to process the last received report from the host.
147 *
148 * \param[in] DataArray Pointer to a buffer where the last received report has been stored
149 */
150 void ProcessGenericHIDReport(uint8_t* DataArray)
151 {
152 /*
153 This is where you need to process reports sent from the host to the device. This
154 function is called each time the host has sent a new report. DataArray is an array
155 holding the report sent from the host.
156 */
157
158 uint8_t NewLEDMask = LEDS_NO_LEDS;
159
160 if (DataArray[0])
161 NewLEDMask |= LEDS_LED1;
162
163 if (DataArray[1])
164 NewLEDMask |= LEDS_LED1;
165
166 if (DataArray[2])
167 NewLEDMask |= LEDS_LED1;
168
169 if (DataArray[3])
170 NewLEDMask |= LEDS_LED1;
171
172 LEDs_SetAllLEDs(NewLEDMask);
173 }
174
175 /** Function to create the next report to send back to the host at the next reporting interval.
176 *
177 * \param[out] DataArray Pointer to a buffer where the next report data should be stored
178 */
179 void CreateGenericHIDReport(uint8_t* DataArray)
180 {
181 /*
182 This is where you need to create reports to be sent to the host from the device. This
183 function is called each time the host is ready to accept a new report. DataArray is
184 an array to hold the report to the host.
185 */
186
187 uint8_t CurrLEDMask = LEDs_GetLEDs();
188
189 DataArray[0] = ((CurrLEDMask & LEDS_LED1) ? 1 : 0);
190 DataArray[1] = ((CurrLEDMask & LEDS_LED2) ? 1 : 0);
191 DataArray[2] = ((CurrLEDMask & LEDS_LED3) ? 1 : 0);
192 DataArray[3] = ((CurrLEDMask & LEDS_LED4) ? 1 : 0);
193 }
194
195 void HID_Task(void)
196 {
197 /* Device must be connected and configured for the task to run */
198 if (USB_DeviceState != DEVICE_STATE_Configured)
199 return;
200
201 Endpoint_SelectEndpoint(GENERIC_OUT_EPADDR);
202
203 /* Check to see if a packet has been sent from the host */
204 if (Endpoint_IsOUTReceived())
205 {
206 /* Check to see if the packet contains data */
207 if (Endpoint_IsReadWriteAllowed())
208 {
209 /* Create a temporary buffer to hold the read in report from the host */
210 uint8_t GenericData[GENERIC_REPORT_SIZE];
211
212 /* Read Generic Report Data */
213 Endpoint_Read_Stream_LE(&GenericData, sizeof(GenericData), NULL);
214
215 /* Process Generic Report Data */
216 ProcessGenericHIDReport(GenericData);
217 }
218
219 /* Finalize the stream transfer to send the last packet */
220 Endpoint_ClearOUT();
221 }
222
223 Endpoint_SelectEndpoint(GENERIC_IN_EPADDR);
224
225 /* Check to see if the host is ready to accept another packet */
226 if (Endpoint_IsINReady())
227 {
228 /* Create a temporary buffer to hold the report to send to the host */
229 uint8_t GenericData[GENERIC_REPORT_SIZE];
230
231 /* Create Generic Report Data */
232 CreateGenericHIDReport(GenericData);
233
234 /* Write Generic Report Data */
235 Endpoint_Write_Stream_LE(&GenericData, sizeof(GenericData), NULL);
236
237 /* Finalize the stream transfer to send the last packet */
238 Endpoint_ClearIN();
239 }
240 }
241