]> git.gir.st - tmk_keyboard.git/blob - tmk_core/protocol/lufa/LUFA-git/Demos/Device/LowLevel/VirtualSerial/VirtualSerial.c
Merge commit '20b787fc1284176834cbe7ca2134e4b36bec5828'
[tmk_keyboard.git] / tmk_core / protocol / lufa / LUFA-git / Demos / Device / LowLevel / 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 the demo and
34 * is responsible for the initial application hardware configuration.
35 */
36
37 #include "VirtualSerial.h"
38
39 /** Contains the current baud rate and other settings of the virtual serial port. While this demo does not use
40 * the physical USART and thus does not use these settings, they must still be retained and returned to the host
41 * upon request or the host will assume the device is non-functional.
42 *
43 * These values are set by the host via a class-specific request, however they are not required to be used accurately.
44 * It is possible to completely ignore these value or use other settings as the host is completely unaware of the physical
45 * serial link characteristics and instead sends and receives data in endpoint streams.
46 */
47 static CDC_LineEncoding_t LineEncoding = { .BaudRateBPS = 0,
48 .CharFormat = CDC_LINEENCODING_OneStopBit,
49 .ParityType = CDC_PARITY_None,
50 .DataBits = 8 };
51
52
53 /** Main program entry point. This routine contains the overall program flow, including initial
54 * setup of all components and the main program loop.
55 */
56 int main(void)
57 {
58 SetupHardware();
59
60 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
61 GlobalInterruptEnable();
62
63 for (;;)
64 {
65 CDC_Task();
66 USB_USBTask();
67 }
68 }
69
70 /** Configures the board hardware and chip peripherals for the demo's functionality. */
71 void SetupHardware(void)
72 {
73 #if (ARCH == ARCH_AVR8)
74 /* Disable watchdog if enabled by bootloader/fuses */
75 MCUSR &= ~(1 << WDRF);
76 wdt_disable();
77
78 /* Disable clock division */
79 clock_prescale_set(clock_div_1);
80 #elif (ARCH == ARCH_XMEGA)
81 /* Start the PLL to multiply the 2MHz RC oscillator to 32MHz and switch the CPU core to run from it */
82 XMEGACLK_StartPLL(CLOCK_SRC_INT_RC2MHZ, 2000000, F_CPU);
83 XMEGACLK_SetCPUClockSource(CLOCK_SRC_PLL);
84
85 /* Start the 32MHz internal RC oscillator and start the DFLL to increase it to 48MHz using the USB SOF as a reference */
86 XMEGACLK_StartInternalOscillator(CLOCK_SRC_INT_RC32MHZ);
87 XMEGACLK_StartDFLL(CLOCK_SRC_INT_RC32MHZ, DFLL_REF_INT_USBSOF, F_USB);
88
89 PMIC.CTRL = PMIC_LOLVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_HILVLEN_bm;
90 #endif
91
92 /* Hardware Initialization */
93 Joystick_Init();
94 LEDs_Init();
95 USB_Init();
96 }
97
98 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and
99 * starts the library USB task to begin the enumeration and USB management process.
100 */
101 void EVENT_USB_Device_Connect(void)
102 {
103 /* Indicate USB enumerating */
104 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
105 }
106
107 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via
108 * the status LEDs and stops the USB management and CDC management tasks.
109 */
110 void EVENT_USB_Device_Disconnect(void)
111 {
112 /* Indicate USB not ready */
113 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
114 }
115
116 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host set the current configuration
117 * of the USB device after enumeration - the device endpoints are configured and the CDC management task started.
118 */
119 void EVENT_USB_Device_ConfigurationChanged(void)
120 {
121 bool ConfigSuccess = true;
122
123 /* Setup CDC Data Endpoints */
124 ConfigSuccess &= Endpoint_ConfigureEndpoint(CDC_NOTIFICATION_EPADDR, EP_TYPE_INTERRUPT, CDC_NOTIFICATION_EPSIZE, 1);
125 ConfigSuccess &= Endpoint_ConfigureEndpoint(CDC_TX_EPADDR, EP_TYPE_BULK, CDC_TXRX_EPSIZE, 1);
126 ConfigSuccess &= Endpoint_ConfigureEndpoint(CDC_RX_EPADDR, EP_TYPE_BULK, CDC_TXRX_EPSIZE, 1);
127
128 /* Reset line encoding baud rate so that the host knows to send new values */
129 LineEncoding.BaudRateBPS = 0;
130
131 /* Indicate endpoint configuration success or failure */
132 LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
133 }
134
135 /** Event handler for the USB_ControlRequest event. This is used to catch and process control requests sent to
136 * the device from the USB host before passing along unhandled control requests to the library for processing
137 * internally.
138 */
139 void EVENT_USB_Device_ControlRequest(void)
140 {
141 /* Process CDC specific control requests */
142 switch (USB_ControlRequest.bRequest)
143 {
144 case CDC_REQ_GetLineEncoding:
145 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
146 {
147 Endpoint_ClearSETUP();
148
149 /* Write the line coding data to the control endpoint */
150 Endpoint_Write_Control_Stream_LE(&LineEncoding, sizeof(CDC_LineEncoding_t));
151 Endpoint_ClearOUT();
152 }
153
154 break;
155 case CDC_REQ_SetLineEncoding:
156 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
157 {
158 Endpoint_ClearSETUP();
159
160 /* Read the line coding data in from the host into the global struct */
161 Endpoint_Read_Control_Stream_LE(&LineEncoding, sizeof(CDC_LineEncoding_t));
162 Endpoint_ClearIN();
163 }
164
165 break;
166 case CDC_REQ_SetControlLineState:
167 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
168 {
169 Endpoint_ClearSETUP();
170 Endpoint_ClearStatusStage();
171
172 /* NOTE: Here you can read in the line state mask from the host, to get the current state of the output handshake
173 lines. The mask is read in from the wValue parameter in USB_ControlRequest, and can be masked against the
174 CONTROL_LINE_OUT_* masks to determine the RTS and DTR line states using the following code:
175 */
176 }
177
178 break;
179 }
180 }
181
182 /** Function to manage CDC data transmission and reception to and from the host. */
183 void CDC_Task(void)
184 {
185 char* ReportString = NULL;
186 uint8_t JoyStatus_LCL = Joystick_GetStatus();
187 static bool ActionSent = false;
188
189 /* Device must be connected and configured for the task to run */
190 if (USB_DeviceState != DEVICE_STATE_Configured)
191 return;
192
193 /* Determine if a joystick action has occurred */
194 if (JoyStatus_LCL & JOY_UP)
195 ReportString = "Joystick Up\r\n";
196 else if (JoyStatus_LCL & JOY_DOWN)
197 ReportString = "Joystick Down\r\n";
198 else if (JoyStatus_LCL & JOY_LEFT)
199 ReportString = "Joystick Left\r\n";
200 else if (JoyStatus_LCL & JOY_RIGHT)
201 ReportString = "Joystick Right\r\n";
202 else if (JoyStatus_LCL & JOY_PRESS)
203 ReportString = "Joystick Pressed\r\n";
204 else
205 ActionSent = false;
206
207 /* Flag management - Only allow one string to be sent per action */
208 if ((ReportString != NULL) && (ActionSent == false) && LineEncoding.BaudRateBPS)
209 {
210 ActionSent = true;
211
212 /* Select the Serial Tx Endpoint */
213 Endpoint_SelectEndpoint(CDC_TX_EPADDR);
214
215 /* Write the String to the Endpoint */
216 Endpoint_Write_Stream_LE(ReportString, strlen(ReportString), NULL);
217
218 /* Remember if the packet to send completely fills the endpoint */
219 bool IsFull = (Endpoint_BytesInEndpoint() == CDC_TXRX_EPSIZE);
220
221 /* Finalize the stream transfer to send the last packet */
222 Endpoint_ClearIN();
223
224 /* If the last packet filled the endpoint, send an empty packet to release the buffer on
225 * the receiver (otherwise all data will be cached until a non-full packet is received) */
226 if (IsFull)
227 {
228 /* Wait until the endpoint is ready for another packet */
229 Endpoint_WaitUntilReady();
230
231 /* Send an empty packet to ensure that the host does not buffer data sent to it */
232 Endpoint_ClearIN();
233 }
234 }
235
236 /* Select the Serial Rx Endpoint */
237 Endpoint_SelectEndpoint(CDC_RX_EPADDR);
238
239 /* Throw away any received data from the host */
240 if (Endpoint_IsOUTReceived())
241 Endpoint_ClearOUT();
242 }
243
Imprint / Impressum