]> git.gir.st - tmk_keyboard.git/blob - tmk_core/protocol/lufa/LUFA-git/Demos/Device/LowLevel/GenericHID/GenericHID.c
Merge commit '20b787fc1284176834cbe7ca2134e4b36bec5828'
[tmk_keyboard.git] / tmk_core / protocol / lufa / LUFA-git / Demos / Device / LowLevel / GenericHID / GenericHID.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2014.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2014 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 disclaims 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 GlobalInterruptEnable();
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 #if (ARCH == ARCH_AVR8)
61 /* Disable watchdog if enabled by bootloader/fuses */
62 MCUSR &= ~(1 << WDRF);
63 wdt_disable();
64
65 /* Disable clock division */
66 clock_prescale_set(clock_div_1);
67 #elif (ARCH == ARCH_XMEGA)
68 /* Start the PLL to multiply the 2MHz RC oscillator to 32MHz and switch the CPU core to run from it */
69 XMEGACLK_StartPLL(CLOCK_SRC_INT_RC2MHZ, 2000000, F_CPU);
70 XMEGACLK_SetCPUClockSource(CLOCK_SRC_PLL);
71
72 /* Start the 32MHz internal RC oscillator and start the DFLL to increase it to 48MHz using the USB SOF as a reference */
73 XMEGACLK_StartInternalOscillator(CLOCK_SRC_INT_RC32MHZ);
74 XMEGACLK_StartDFLL(CLOCK_SRC_INT_RC32MHZ, DFLL_REF_INT_USBSOF, F_USB);
75
76 PMIC.CTRL = PMIC_LOLVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_HILVLEN_bm;
77 #endif
78
79 /* Hardware Initialization */
80 LEDs_Init();
81 USB_Init();
82 }
83
84 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and
85 * starts the library USB task to begin the enumeration and USB management process.
86 */
87 void EVENT_USB_Device_Connect(void)
88 {
89 /* Indicate USB enumerating */
90 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
91 }
92
93 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via
94 * the status LEDs and stops the USB management task.
95 */
96 void EVENT_USB_Device_Disconnect(void)
97 {
98 /* Indicate USB not ready */
99 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
100 }
101
102 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host sets the current configuration
103 * of the USB device after enumeration, and configures the generic HID device endpoints.
104 */
105 void EVENT_USB_Device_ConfigurationChanged(void)
106 {
107 bool ConfigSuccess = true;
108
109 /* Setup HID Report Endpoints */
110 ConfigSuccess &= Endpoint_ConfigureEndpoint(GENERIC_IN_EPADDR, EP_TYPE_INTERRUPT, GENERIC_EPSIZE, 1);
111 ConfigSuccess &= Endpoint_ConfigureEndpoint(GENERIC_OUT_EPADDR, EP_TYPE_INTERRUPT, GENERIC_EPSIZE, 1);
112
113 /* Indicate endpoint configuration success or failure */
114 LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
115 }
116
117 /** Event handler for the USB_ControlRequest event. This is used to catch and process control requests sent to
118 * the device from the USB host before passing along unhandled control requests to the library for processing
119 * internally.
120 */
121 void EVENT_USB_Device_ControlRequest(void)
122 {
123 /* Handle HID Class specific requests */
124 switch (USB_ControlRequest.bRequest)
125 {
126 case HID_REQ_GetReport:
127 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
128 {
129 uint8_t GenericData[GENERIC_REPORT_SIZE];
130 CreateGenericHIDReport(GenericData);
131
132 Endpoint_ClearSETUP();
133
134 /* Write the report data to the control endpoint */
135 Endpoint_Write_Control_Stream_LE(&GenericData, sizeof(GenericData));
136 Endpoint_ClearOUT();
137 }
138
139 break;
140 case HID_REQ_SetReport:
141 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
142 {
143 uint8_t GenericData[GENERIC_REPORT_SIZE];
144
145 Endpoint_ClearSETUP();
146
147 /* Read the report data from the control endpoint */
148 Endpoint_Read_Control_Stream_LE(&GenericData, sizeof(GenericData));
149 Endpoint_ClearIN();
150
151 ProcessGenericHIDReport(GenericData);
152 }
153
154 break;
155 }
156 }
157
158 /** Function to process the last received report from the host.
159 *
160 * \param[in] DataArray Pointer to a buffer where the last received report has been stored
161 */
162 void ProcessGenericHIDReport(uint8_t* DataArray)
163 {
164 /*
165 This is where you need to process reports sent from the host to the device. This
166 function is called each time the host has sent a new report. DataArray is an array
167 holding the report sent from the host.
168 */
169
170 uint8_t NewLEDMask = LEDS_NO_LEDS;
171
172 if (DataArray[0])
173 NewLEDMask |= LEDS_LED1;
174
175 if (DataArray[1])
176 NewLEDMask |= LEDS_LED2;
177
178 if (DataArray[2])
179 NewLEDMask |= LEDS_LED3;
180
181 if (DataArray[3])
182 NewLEDMask |= LEDS_LED4;
183
184 LEDs_SetAllLEDs(NewLEDMask);
185 }
186
187 /** Function to create the next report to send back to the host at the next reporting interval.
188 *
189 * \param[out] DataArray Pointer to a buffer where the next report data should be stored
190 */
191 void CreateGenericHIDReport(uint8_t* DataArray)
192 {
193 /*
194 This is where you need to create reports to be sent to the host from the device. This
195 function is called each time the host is ready to accept a new report. DataArray is
196 an array to hold the report to the host.
197 */
198
199 uint8_t CurrLEDMask = LEDs_GetLEDs();
200
201 DataArray[0] = ((CurrLEDMask & LEDS_LED1) ? 1 : 0);
202 DataArray[1] = ((CurrLEDMask & LEDS_LED2) ? 1 : 0);
203 DataArray[2] = ((CurrLEDMask & LEDS_LED3) ? 1 : 0);
204 DataArray[3] = ((CurrLEDMask & LEDS_LED4) ? 1 : 0);
205 }
206
207 void HID_Task(void)
208 {
209 /* Device must be connected and configured for the task to run */
210 if (USB_DeviceState != DEVICE_STATE_Configured)
211 return;
212
213 Endpoint_SelectEndpoint(GENERIC_OUT_EPADDR);
214
215 /* Check to see if a packet has been sent from the host */
216 if (Endpoint_IsOUTReceived())
217 {
218 /* Check to see if the packet contains data */
219 if (Endpoint_IsReadWriteAllowed())
220 {
221 /* Create a temporary buffer to hold the read in report from the host */
222 uint8_t GenericData[GENERIC_REPORT_SIZE];
223
224 /* Read Generic Report Data */
225 Endpoint_Read_Stream_LE(&GenericData, sizeof(GenericData), NULL);
226
227 /* Process Generic Report Data */
228 ProcessGenericHIDReport(GenericData);
229 }
230
231 /* Finalize the stream transfer to send the last packet */
232 Endpoint_ClearOUT();
233 }
234
235 Endpoint_SelectEndpoint(GENERIC_IN_EPADDR);
236
237 /* Check to see if the host is ready to accept another packet */
238 if (Endpoint_IsINReady())
239 {
240 /* Create a temporary buffer to hold the report to send to the host */
241 uint8_t GenericData[GENERIC_REPORT_SIZE];
242
243 /* Create Generic Report Data */
244 CreateGenericHIDReport(GenericData);
245
246 /* Write Generic Report Data */
247 Endpoint_Write_Stream_LE(&GenericData, sizeof(GenericData), NULL);
248
249 /* Finalize the stream transfer to send the last packet */
250 Endpoint_ClearIN();
251 }
252 }
253
Imprint / Impressum