2993b90cae97b0cf71e4c43e0d66fa6a82286ff8
[pub/USBasp.git] / Demos / Device / LowLevel / DualVirtualSerial / DualVirtualSerial.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, and distribute this software
13 and its documentation for any purpose and without fee is hereby
14 granted, provided that the above copyright notice appear in all
15 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 disclaim 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 DualVirtualSerial demo. This file contains the main tasks of the demo and
34 * is responsible for the initial application hardware configuration.
35 */
36
37 #include "DualVirtualSerial.h"
38
39 /** Contains the current baud rate and other settings of the first 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 CDC_Line_Coding_t LineEncoding1 = { .BaudRateBPS = 0,
48 .CharFormat = OneStopBit,
49 .ParityType = Parity_None,
50 .DataBits = 8 };
51
52 /** Contains the current baud rate and other settings of the second virtual serial port. While this demo does not use
53 * the physical USART and thus does not use these settings, they must still be retained and returned to the host
54 * upon request or the host will assume the device is non-functional.
55 *
56 * These values are set by the host via a class-specific request, however they are not required to be used accurately.
57 * It is possible to completely ignore these value or use other settings as the host is completely unaware of the physical
58 * serial link characteristics and instead sends and receives data in endpoint streams.
59 */
60 CDC_Line_Coding_t LineEncoding2 = { .BaudRateBPS = 0,
61 .CharFormat = OneStopBit,
62 .ParityType = Parity_None,
63 .DataBits = 8 };
64
65
66 /** Main program entry point. This routine configures the hardware required by the application, then
67 * enters a loop to run the application tasks in sequence.
68 */
69 int main(void)
70 {
71 SetupHardware();
72
73 for (;;)
74 {
75 CDC1_Task();
76 CDC2_Task();
77 USB_USBTask();
78 }
79 }
80
81 /** Configures the board hardware and chip peripherals for the demo's functionality. */
82 void SetupHardware(void)
83 {
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
91 /* Hardware Initialization */
92 Joystick_Init();
93 LEDs_Init();
94 USB_Init();
95 }
96
97 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and
98 * starts the library USB task to begin the enumeration and USB management process.
99 */
100 void EVENT_USB_Device_Connect(void)
101 {
102 /* Indicate USB enumerating */
103 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
104 }
105
106 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via
107 * the status LEDs and stops the USB management and CDC management tasks.
108 */
109 void EVENT_USB_Device_Disconnect(void)
110 {
111 /* Indicate USB not ready */
112 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
113 }
114
115 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host set the current configuration
116 * of the USB device after enumeration - the device endpoints are configured and the CDC management tasks are started.
117 */
118 void EVENT_USB_Device_ConfigurationChanged(void)
119 {
120 /* Indicate USB connected and ready */
121 LEDs_SetAllLEDs(LEDMASK_USB_READY);
122
123 /* Setup CDC Notification, Rx and Tx Endpoints for the first CDC */
124 if (!(Endpoint_ConfigureEndpoint(CDC1_NOTIFICATION_EPNUM, EP_TYPE_INTERRUPT,
125 ENDPOINT_DIR_IN, CDC_NOTIFICATION_EPSIZE,
126 ENDPOINT_BANK_SINGLE)))
127 {
128 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
129 }
130
131 if (!(Endpoint_ConfigureEndpoint(CDC1_TX_EPNUM, EP_TYPE_BULK,
132 ENDPOINT_DIR_IN, CDC_TXRX_EPSIZE,
133 ENDPOINT_BANK_SINGLE)))
134 {
135 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
136 }
137
138 if (!(Endpoint_ConfigureEndpoint(CDC1_RX_EPNUM, EP_TYPE_BULK,
139 ENDPOINT_DIR_OUT, CDC_TXRX_EPSIZE,
140 ENDPOINT_BANK_SINGLE)))
141 {
142 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
143 }
144
145 /* Setup CDC Notification, Rx and Tx Endpoints for the second CDC */
146 if (!(Endpoint_ConfigureEndpoint(CDC2_NOTIFICATION_EPNUM, EP_TYPE_INTERRUPT,
147 ENDPOINT_DIR_IN, CDC_NOTIFICATION_EPSIZE,
148 ENDPOINT_BANK_SINGLE)))
149 {
150 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
151 }
152
153 if (!(Endpoint_ConfigureEndpoint(CDC2_TX_EPNUM, EP_TYPE_BULK,
154 ENDPOINT_DIR_IN, CDC_TXRX_EPSIZE,
155 ENDPOINT_BANK_SINGLE)))
156 {
157 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
158 }
159
160 if (!(Endpoint_ConfigureEndpoint(CDC2_RX_EPNUM, EP_TYPE_BULK,
161 ENDPOINT_DIR_OUT, CDC_TXRX_EPSIZE,
162 ENDPOINT_BANK_SINGLE)))
163 {
164 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
165 }
166
167 /* Reset line encoding baud rates so that the host knows to send new values */
168 LineEncoding1.BaudRateBPS = 0;
169 LineEncoding2.BaudRateBPS = 0;
170 }
171
172 /** Event handler for the USB_UnhandledControlRequest event. This is used to catch standard and class specific
173 * control requests that are not handled internally by the USB library (including the CDC control commands,
174 * which are all issued via the control endpoint), so that they can be handled appropriately for the application.
175 */
176 void EVENT_USB_Device_UnhandledControlRequest(void)
177 {
178 /* Determine which interface's Line Coding data is being set from the wIndex parameter */
179 uint8_t* LineEncodingData = (USB_ControlRequest.wIndex == 0) ? (uint8_t*)&LineEncoding1 : (uint8_t*)&LineEncoding2;
180
181 /* Process CDC specific control requests */
182 switch (USB_ControlRequest.bRequest)
183 {
184 case REQ_GetLineEncoding:
185 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
186 {
187 /* Acknowledge the SETUP packet, ready for data transfer */
188 Endpoint_ClearSETUP();
189
190 /* Write the line coding data to the control endpoint */
191 Endpoint_Write_Control_Stream_LE(LineEncodingData, sizeof(CDC_Line_Coding_t));
192
193 /* Finalize the stream transfer to send the last packet or clear the host abort */
194 Endpoint_ClearOUT();
195 }
196
197 break;
198 case REQ_SetLineEncoding:
199 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
200 {
201 /* Acknowledge the SETUP packet, ready for data transfer */
202 Endpoint_ClearSETUP();
203
204 /* Read the line coding data in from the host into the global struct */
205 Endpoint_Read_Control_Stream_LE(LineEncodingData, sizeof(CDC_Line_Coding_t));
206
207 /* Finalize the stream transfer to clear the last packet from the host */
208 Endpoint_ClearIN();
209 }
210
211 break;
212 case REQ_SetControlLineState:
213 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
214 {
215 /* Acknowledge the SETUP packet, ready for data transfer */
216 Endpoint_ClearSETUP();
217
218 Endpoint_ClearStatusStage();
219 }
220
221 break;
222 }
223 }
224
225 /** Function to manage CDC data transmission and reception to and from the host for the first CDC interface, which sends joystick
226 * movements to the host as ASCII strings.
227 */
228 void CDC1_Task(void)
229 {
230 char* ReportString = NULL;
231 uint8_t JoyStatus_LCL = Joystick_GetStatus();
232 static bool ActionSent = false;
233
234 /* Device must be connected and configured for the task to run */
235 if (USB_DeviceState != DEVICE_STATE_Configured)
236 return;
237
238 /* Determine if a joystick action has occurred */
239 if (JoyStatus_LCL & JOY_UP)
240 ReportString = "Joystick Up\r\n";
241 else if (JoyStatus_LCL & JOY_DOWN)
242 ReportString = "Joystick Down\r\n";
243 else if (JoyStatus_LCL & JOY_LEFT)
244 ReportString = "Joystick Left\r\n";
245 else if (JoyStatus_LCL & JOY_RIGHT)
246 ReportString = "Joystick Right\r\n";
247 else if (JoyStatus_LCL & JOY_PRESS)
248 ReportString = "Joystick Pressed\r\n";
249 else
250 ActionSent = false;
251
252 /* Flag management - Only allow one string to be sent per action */
253 if ((ReportString != NULL) && (ActionSent == false) && LineEncoding1.BaudRateBPS)
254 {
255 ActionSent = true;
256
257 /* Select the Serial Tx Endpoint */
258 Endpoint_SelectEndpoint(CDC1_TX_EPNUM);
259
260 /* Write the String to the Endpoint */
261 Endpoint_Write_Stream_LE(ReportString, strlen(ReportString));
262
263 /* Finalize the stream transfer to send the last packet */
264 Endpoint_ClearIN();
265
266 /* Wait until the endpoint is ready for another packet */
267 Endpoint_WaitUntilReady();
268
269 /* Send an empty packet to ensure that the host does not buffer data sent to it */
270 Endpoint_ClearIN();
271 }
272
273 /* Select the Serial Rx Endpoint */
274 Endpoint_SelectEndpoint(CDC1_RX_EPNUM);
275
276 /* Throw away any received data from the host */
277 if (Endpoint_IsOUTReceived())
278 Endpoint_ClearOUT();
279 }
280
281 /** Function to manage CDC data transmission and reception to and from the host for the second CDC interface, which echoes back
282 * all data sent to it from the host.
283 */
284 void CDC2_Task(void)
285 {
286 /* Device must be connected and configured for the task to run */
287 if (USB_DeviceState != DEVICE_STATE_Configured)
288 return;
289
290 /* Select the Serial Rx Endpoint */
291 Endpoint_SelectEndpoint(CDC2_RX_EPNUM);
292
293 /* Check to see if any data has been received */
294 if (Endpoint_IsOUTReceived())
295 {
296 /* Create a temp buffer big enough to hold the incoming endpoint packet */
297 uint8_t Buffer[Endpoint_BytesInEndpoint()];
298
299 /* Remember how large the incoming packet is */
300 uint16_t DataLength = Endpoint_BytesInEndpoint();
301
302 /* Read in the incoming packet into the buffer */
303 Endpoint_Read_Stream_LE(&Buffer, DataLength);
304
305 /* Finalize the stream transfer to send the last packet */
306 Endpoint_ClearOUT();
307
308 /* Select the Serial Tx Endpoint */
309 Endpoint_SelectEndpoint(CDC2_TX_EPNUM);
310
311 /* Write the received data to the endpoint */
312 Endpoint_Write_Stream_LE(&Buffer, DataLength);
313
314 /* Finalize the stream transfer to send the last packet */
315 Endpoint_ClearIN();
316
317 /* Wait until the endpoint is ready for the next packet */
318 Endpoint_WaitUntilReady();
319
320 /* Send an empty packet to prevent host buffering */
321 Endpoint_ClearIN();
322 }
323 }