]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - GenericHID.c
1fb6044135c1a807e90a2252739364ccaea3ab9d
[max/tmk_keyboard.git] / 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
34  *  the demo and is responsible for the initial application hardware configuration.
35  */
36
37 #include "GenericHID.h"
38
39 /** Buffer to hold the previously generated HID report, for comparison purposes inside the HID class driver. */
40 static uint8_t PrevHIDReportBuffer[GENERIC_REPORT_SIZE];
41
42 /** LUFA HID Class driver interface configuration and state information. This structure is
43  *  passed to all HID Class driver functions, so that multiple instances of the same class
44  *  within a device can be differentiated from one another.
45  */
46 USB_ClassInfo_HID_Device_t Generic_HID_Interface =
47         {
48                 .Config =
49                         {
50                                 .InterfaceNumber              = INTERFACE_ID_GenericHID,
51                                 .ReportINEndpoint             =
52                                         {
53                                                 .Address              = GENERIC_IN_EPADDR,
54                                                 .Size                 = GENERIC_EPSIZE,
55                                                 .Banks                = 1,
56                                         },
57                                 .PrevReportINBuffer           = PrevHIDReportBuffer,
58                                 .PrevReportINBufferSize       = sizeof(PrevHIDReportBuffer),
59                         },
60         };
61
62
63 /** Main program entry point. This routine contains the overall program flow, including initial
64  *  setup of all components and the main program loop.
65  */
66 int main(void)
67 {
68         SetupHardware();
69
70         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
71         GlobalInterruptEnable();
72
73         for (;;)
74         {
75                 HID_Device_USBTask(&Generic_HID_Interface);
76                 USB_USBTask();
77         }
78 }
79
80 /** Configures the board hardware and chip peripherals for the demo's functionality. */
81 void SetupHardware(void)
82 {
83 #if (ARCH == ARCH_AVR8)
84         /* Disable watchdog if enabled by bootloader/fuses */
85         MCUSR &= ~(1 << WDRF);
86         wdt_disable();
87
88         /* Disable clock division */
89         clock_prescale_set(clock_div_1);
90 #elif (ARCH == ARCH_XMEGA)
91         /* Start the PLL to multiply the 2MHz RC oscillator to 32MHz and switch the CPU core to run from it */
92         XMEGACLK_StartPLL(CLOCK_SRC_INT_RC2MHZ, 2000000, F_CPU);
93         XMEGACLK_SetCPUClockSource(CLOCK_SRC_PLL);
94
95         /* Start the 32MHz internal RC oscillator and start the DFLL to increase it to 48MHz using the USB SOF as a reference */
96         XMEGACLK_StartInternalOscillator(CLOCK_SRC_INT_RC32MHZ);
97         XMEGACLK_StartDFLL(CLOCK_SRC_INT_RC32MHZ, DFLL_REF_INT_USBSOF, F_USB);
98
99         PMIC.CTRL = PMIC_LOLVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_HILVLEN_bm;
100 #endif
101
102         /* Hardware Initialization */
103         LEDs_Init();
104         USB_Init();
105 }
106
107 /** Event handler for the library USB Connection event. */
108 void EVENT_USB_Device_Connect(void)
109 {
110         LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
111 }
112
113 /** Event handler for the library USB Disconnection event. */
114 void EVENT_USB_Device_Disconnect(void)
115 {
116         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
117 }
118
119 /** Event handler for the library USB Configuration Changed event. */
120 void EVENT_USB_Device_ConfigurationChanged(void)
121 {
122         bool ConfigSuccess = true;
123
124         ConfigSuccess &= HID_Device_ConfigureEndpoints(&Generic_HID_Interface);
125
126         USB_Device_EnableSOFEvents();
127
128         LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
129 }
130
131 /** Event handler for the library USB Control Request reception event. */
132 void EVENT_USB_Device_ControlRequest(void)
133 {
134         HID_Device_ProcessControlRequest(&Generic_HID_Interface);
135 }
136
137 /** Event handler for the USB device Start Of Frame event. */
138 void EVENT_USB_Device_StartOfFrame(void)
139 {
140         HID_Device_MillisecondElapsed(&Generic_HID_Interface);
141 }
142
143 /** HID class driver callback function for the creation of HID reports to the host.
144  *
145  *  \param[in]     HIDInterfaceInfo  Pointer to the HID class interface configuration structure being referenced
146  *  \param[in,out] ReportID    Report ID requested by the host if non-zero, otherwise callback should set to the generated report ID
147  *  \param[in]     ReportType  Type of the report to create, either HID_REPORT_ITEM_In or HID_REPORT_ITEM_Feature
148  *  \param[out]    ReportData  Pointer to a buffer where the created report should be stored
149  *  \param[out]    ReportSize  Number of bytes written in the report (or zero if no report is to be sent)
150  *
151  *  \return Boolean \c true to force the sending of the report, \c false to let the library determine if it needs to be sent
152  */
153 bool CALLBACK_HID_Device_CreateHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
154                                          uint8_t* const ReportID,
155                                          const uint8_t ReportType,
156                                          void* ReportData,
157                                          uint16_t* const ReportSize)
158 {
159         uint8_t* Data        = (uint8_t*)ReportData;
160         uint8_t  CurrLEDMask = LEDs_GetLEDs();
161
162         Data[0] = ((CurrLEDMask & LEDS_LED1) ? 1 : 0);
163         Data[1] = ((CurrLEDMask & LEDS_LED2) ? 1 : 0);
164         Data[2] = ((CurrLEDMask & LEDS_LED3) ? 1 : 0);
165         Data[3] = ((CurrLEDMask & LEDS_LED4) ? 1 : 0);
166
167         *ReportSize = GENERIC_REPORT_SIZE;
168         return false;
169 }
170
171 /** HID class driver callback function for the processing of HID reports from the host.
172  *
173  *  \param[in] HIDInterfaceInfo  Pointer to the HID class interface configuration structure being referenced
174  *  \param[in] ReportID    Report ID of the received report from the host
175  *  \param[in] ReportType  The type of report that the host has sent, either HID_REPORT_ITEM_Out or HID_REPORT_ITEM_Feature
176  *  \param[in] ReportData  Pointer to a buffer where the received report has been stored
177  *  \param[in] ReportSize  Size in bytes of the received HID report
178  */
179 void CALLBACK_HID_Device_ProcessHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
180                                           const uint8_t ReportID,
181                                           const uint8_t ReportType,
182                                           const void* ReportData,
183                                           const uint16_t ReportSize)
184 {
185         uint8_t* Data       = (uint8_t*)ReportData;
186         uint8_t  NewLEDMask = LEDS_NO_LEDS;
187
188         if (Data[0])
189           NewLEDMask |= LEDS_LED1;
190
191         if (Data[1])
192           NewLEDMask |= LEDS_LED2;
193
194         if (Data[2])
195           NewLEDMask |= LEDS_LED3;
196
197         if (Data[3])
198           NewLEDMask |= LEDS_LED4;
199
200         LEDs_SetAllLEDs(NewLEDMask);
201 }
202