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