]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/protocol/lufa/LUFA-git/Demos/Device/ClassDriver/VirtualSerial/VirtualSerial.c
allow overriding of TARGET
[qmk_firmware.git] / tmk_core / protocol / lufa / LUFA-git / Demos / Device / ClassDriver / VirtualSerial / VirtualSerial.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 VirtualSerial demo. This file contains the main tasks of
34  *  the demo and is responsible for the initial application hardware configuration.
35  */
36
37 #include "VirtualSerial.h"
38
39 /** LUFA CDC Class driver interface configuration and state information. This structure is
40  *  passed to all CDC Class driver functions, so that multiple instances of the same class
41  *  within a device can be differentiated from one another.
42  */
43 USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface =
44         {
45                 .Config =
46                         {
47                                 .ControlInterfaceNumber   = INTERFACE_ID_CDC_CCI,
48                                 .DataINEndpoint           =
49                                         {
50                                                 .Address          = CDC_TX_EPADDR,
51                                                 .Size             = CDC_TXRX_EPSIZE,
52                                                 .Banks            = 1,
53                                         },
54                                 .DataOUTEndpoint =
55                                         {
56                                                 .Address          = CDC_RX_EPADDR,
57                                                 .Size             = CDC_TXRX_EPSIZE,
58                                                 .Banks            = 1,
59                                         },
60                                 .NotificationEndpoint =
61                                         {
62                                                 .Address          = CDC_NOTIFICATION_EPADDR,
63                                                 .Size             = CDC_NOTIFICATION_EPSIZE,
64                                                 .Banks            = 1,
65                                         },
66                         },
67         };
68
69 /** Standard file stream for the CDC interface when set up, so that the virtual CDC COM port can be
70  *  used like any regular character stream in the C APIs.
71  */
72 static FILE USBSerialStream;
73
74
75 /** Main program entry point. This routine contains the overall program flow, including initial
76  *  setup of all components and the main program loop.
77  */
78 int main(void)
79 {
80         SetupHardware();
81
82         /* Create a regular character stream for the interface so that it can be used with the stdio.h functions */
83         CDC_Device_CreateStream(&VirtualSerial_CDC_Interface, &USBSerialStream);
84
85         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
86         GlobalInterruptEnable();
87
88         for (;;)
89         {
90                 CheckJoystickMovement();
91
92                 /* Must throw away unused bytes from the host, or it will lock up while waiting for the device */
93                 CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
94
95                 CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
96                 USB_USBTask();
97         }
98 }
99
100 /** Configures the board hardware and chip peripherals for the demo's functionality. */
101 void SetupHardware(void)
102 {
103 #if (ARCH == ARCH_AVR8)
104         /* Disable watchdog if enabled by bootloader/fuses */
105         MCUSR &= ~(1 << WDRF);
106         wdt_disable();
107
108         /* Disable clock division */
109         clock_prescale_set(clock_div_1);
110 #elif (ARCH == ARCH_XMEGA)
111         /* Start the PLL to multiply the 2MHz RC oscillator to 32MHz and switch the CPU core to run from it */
112         XMEGACLK_StartPLL(CLOCK_SRC_INT_RC2MHZ, 2000000, F_CPU);
113         XMEGACLK_SetCPUClockSource(CLOCK_SRC_PLL);
114
115         /* Start the 32MHz internal RC oscillator and start the DFLL to increase it to 48MHz using the USB SOF as a reference */
116         XMEGACLK_StartInternalOscillator(CLOCK_SRC_INT_RC32MHZ);
117         XMEGACLK_StartDFLL(CLOCK_SRC_INT_RC32MHZ, DFLL_REF_INT_USBSOF, F_USB);
118
119         PMIC.CTRL = PMIC_LOLVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_HILVLEN_bm;
120 #endif
121
122         /* Hardware Initialization */
123         Joystick_Init();
124         LEDs_Init();
125         USB_Init();
126 }
127
128 /** Checks for changes in the position of the board joystick, sending strings to the host upon each change. */
129 void CheckJoystickMovement(void)
130 {
131         uint8_t     JoyStatus_LCL = Joystick_GetStatus();
132         char*       ReportString  = NULL;
133         static bool ActionSent    = false;
134
135         if (JoyStatus_LCL & JOY_UP)
136           ReportString = "Joystick Up\r\n";
137         else if (JoyStatus_LCL & JOY_DOWN)
138           ReportString = "Joystick Down\r\n";
139         else if (JoyStatus_LCL & JOY_LEFT)
140           ReportString = "Joystick Left\r\n";
141         else if (JoyStatus_LCL & JOY_RIGHT)
142           ReportString = "Joystick Right\r\n";
143         else if (JoyStatus_LCL & JOY_PRESS)
144           ReportString = "Joystick Pressed\r\n";
145         else
146           ActionSent = false;
147
148         if ((ReportString != NULL) && (ActionSent == false))
149         {
150                 ActionSent = true;
151
152                 /* Write the string to the virtual COM port via the created character stream */
153                 fputs(ReportString, &USBSerialStream);
154
155                 /* Alternatively, without the stream: */
156                 // CDC_Device_SendString(&VirtualSerial_CDC_Interface, ReportString);
157         }
158 }
159
160 /** Event handler for the library USB Connection event. */
161 void EVENT_USB_Device_Connect(void)
162 {
163         LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
164 }
165
166 /** Event handler for the library USB Disconnection event. */
167 void EVENT_USB_Device_Disconnect(void)
168 {
169         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
170 }
171
172 /** Event handler for the library USB Configuration Changed event. */
173 void EVENT_USB_Device_ConfigurationChanged(void)
174 {
175         bool ConfigSuccess = true;
176
177         ConfigSuccess &= CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface);
178
179         LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
180 }
181
182 /** Event handler for the library USB Control Request reception event. */
183 void EVENT_USB_Device_ControlRequest(void)
184 {
185         CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface);
186 }
187