a604bbe8d35e73a3c605c77783981cb1f4cd76d6
[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 /** 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 * starts the scheduler to run the USB management task.
45 */
46 int main(void)
47 {
48 SetupHardware();
49
50 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
51
52 for (;;)
53 {
54 HID_Task();
55 USB_USBTask();
56 }
57 }
58
59 /** Configures the board hardware and chip peripherals for the demo's functionality. */
60 void SetupHardware(void)
61 {
62 /* Disable watchdog if enabled by bootloader/fuses */
63 MCUSR &= ~(1 << WDRF);
64 wdt_disable();
65
66 /* Disable clock division */
67 clock_prescale_set(clock_div_1);
68
69 /* Hardware Initialization */
70 LEDs_Init();
71 USB_Init();
72 }
73
74 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and
75 * starts the library USB task to begin the enumeration and USB management process.
76 */
77 void EVENT_USB_Connect(void)
78 {
79 /* Indicate USB enumerating */
80 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
81 }
82
83 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via
84 * the status LEDs and stops the USB management task.
85 */
86 void EVENT_USB_Disconnect(void)
87 {
88 /* Indicate USB not ready */
89 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
90 }
91
92 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host sets the current configuration
93 * of the USB device after enumeration, and configures the generic HID device endpoints.
94 */
95 void EVENT_USB_ConfigurationChanged(void)
96 {
97 /* Setup Generic IN Report Endpoint */
98 Endpoint_ConfigureEndpoint(GENERIC_IN_EPNUM, EP_TYPE_INTERRUPT,
99 ENDPOINT_DIR_IN, GENERIC_EPSIZE,
100 ENDPOINT_BANK_SINGLE);
101
102 /* Setup Generic OUT Report Endpoint */
103 Endpoint_ConfigureEndpoint(GENERIC_OUT_EPNUM, EP_TYPE_INTERRUPT,
104 ENDPOINT_DIR_OUT, GENERIC_EPSIZE,
105 ENDPOINT_BANK_SINGLE);
106
107 /* Indicate USB connected and ready */
108 LEDs_SetAllLEDs(LEDMASK_USB_READY);
109 }
110
111 /** Event handler for the USB_UnhandledControlPacket event. This is used to catch standard and class specific
112 * control requests that are not handled internally by the USB library (including the HID commands, which are
113 * all issued via the control endpoint), so that they can be handled appropriately for the application.
114 */
115 void EVENT_USB_UnhandledControlPacket(void)
116 {
117 /* Handle HID Class specific requests */
118 switch (USB_ControlRequest.bRequest)
119 {
120 case REQ_GetReport:
121 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
122 {
123 uint8_t GenericData[GENERIC_REPORT_SIZE];
124
125 Endpoint_ClearSETUP();
126
127 CreateGenericHIDReport(GenericData);
128
129 /* Write the report data to the control endpoint */
130 Endpoint_Write_Control_Stream_LE(&GenericData, sizeof(GenericData));
131
132 /* Finalize the stream transfer to send the last packet or clear the host abort */
133 Endpoint_ClearOUT();
134 }
135
136 break;
137 case REQ_SetReport:
138 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
139 {
140 uint8_t GenericData[GENERIC_REPORT_SIZE];
141
142 Endpoint_ClearSETUP();
143
144 /* Wait until the generic report has been sent by the host */
145 while (!(Endpoint_IsOUTReceived()));
146
147 Endpoint_Read_Control_Stream_LE(&GenericData, sizeof(GenericData));
148
149 ProcessGenericHIDReport(GenericData);
150
151 /* Clear the endpoint data */
152 Endpoint_ClearOUT();
153
154 /* Wait until the host is ready to receive the request confirmation */
155 while (!(Endpoint_IsINReady()));
156
157 /* Handshake the request by sending an empty IN packet */
158 Endpoint_ClearIN();
159 }
160
161 break;
162 }
163 }
164
165 /** Function to process the lest received report from the host.
166 *
167 * \param DataArray Pointer to a buffer where the last report data is stored
168 */
169 void ProcessGenericHIDReport(uint8_t* DataArray)
170 {
171 /*
172 This is where you need to process the reports being sent from the host to the device.
173 DataArray is an array holding the last report from the host. This function is called
174 each time the host has sent a report to the device.
175 */
176
177 for (uint8_t i = 0; i < GENERIC_REPORT_SIZE; i++)
178 LastReceived[i] = DataArray[i];
179 }
180
181 /** Function to create the next report to send back to the host at the next reporting interval.
182 *
183 * \param DataArray Pointer to a buffer where the next report data should be stored
184 */
185 void CreateGenericHIDReport(uint8_t* DataArray)
186 {
187 /*
188 This is where you need to create reports to be sent to the host from the device. This
189 function is called each time the host is ready to accept a new report. DataArray is
190 an array to hold the report to the host.
191 */
192
193 for (uint8_t i = 0; i < GENERIC_REPORT_SIZE; i++)
194 DataArray[i] = LastReceived[i];
195 }
196
197 void HID_Task(void)
198 {
199 /* Check if the USB system is connected to a host */
200 if (USB_IsConnected)
201 {
202 Endpoint_SelectEndpoint(GENERIC_OUT_EPNUM);
203
204 /* Check to see if a packet has been sent from the host */
205 if (Endpoint_IsOUTReceived())
206 {
207 /* Check to see if the packet contains data */
208 if (Endpoint_IsReadWriteAllowed())
209 {
210 /* Create a temporary buffer to hold the read in report from the host */
211 uint8_t GenericData[GENERIC_REPORT_SIZE];
212
213 /* Read Generic Report Data */
214 Endpoint_Read_Stream_LE(&GenericData, sizeof(GenericData));
215
216 /* Process Generic Report Data */
217 ProcessGenericHIDReport(GenericData);
218 }
219
220 /* Finalize the stream transfer to send the last packet */
221 Endpoint_ClearOUT();
222 }
223
224 Endpoint_SelectEndpoint(GENERIC_IN_EPNUM);
225
226 /* Check to see if the host is ready to accept another packet */
227 if (Endpoint_IsINReady())
228 {
229 /* Create a temporary buffer to hold the report to send to the host */
230 uint8_t GenericData[GENERIC_REPORT_SIZE];
231
232 /* Create Generic Report Data */
233 CreateGenericHIDReport(GenericData);
234
235 /* Write Generic Report Data */
236 Endpoint_Write_Stream_LE(&GenericData, sizeof(GenericData));
237
238 /* Finalize the stream transfer to send the last packet */
239 Endpoint_ClearIN();
240 }
241 }
242 }