3 Copyright (C) Dean Camera, 2012.
5 dean [at] fourwalledcubicle [dot] com
10 Copyright 2012 Dean Camera (dean [at] fourwalledcubicle [dot] com)
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.
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
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.
37 #include "DualVirtualSerial.h"
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.
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.
47 static CDC_LineEncoding_t LineEncoding1
= { .BaudRateBPS
= 0,
48 .CharFormat
= CDC_LINEENCODING_OneStopBit
,
49 .ParityType
= CDC_PARITY_None
,
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.
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.
60 static CDC_LineEncoding_t LineEncoding2
= { .BaudRateBPS
= 0,
61 .CharFormat
= CDC_LINEENCODING_OneStopBit
,
62 .ParityType
= CDC_PARITY_None
,
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.
73 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
74 GlobalInterruptEnable();
84 /** Configures the board hardware and chip peripherals for the demo's functionality. */
85 void SetupHardware(void)
87 /* Disable watchdog if enabled by bootloader/fuses */
88 MCUSR
&= ~(1 << WDRF
);
91 /* Disable clock division */
92 clock_prescale_set(clock_div_1
);
94 /* Hardware Initialization */
100 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and
101 * starts the library USB task to begin the enumeration and USB management process.
103 void EVENT_USB_Device_Connect(void)
105 /* Indicate USB enumerating */
106 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
);
109 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via
110 * the status LEDs and stops the USB management and CDC management tasks.
112 void EVENT_USB_Device_Disconnect(void)
114 /* Indicate USB not ready */
115 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
118 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host set the current configuration
119 * of the USB device after enumeration - the device endpoints are configured and the CDC management tasks are started.
121 void EVENT_USB_Device_ConfigurationChanged(void)
123 bool ConfigSuccess
= true;
125 /* Setup first CDC Interface's Endpoints */
126 ConfigSuccess
&= Endpoint_ConfigureEndpoint(CDC1_TX_EPADDR
, EP_TYPE_BULK
, CDC_TXRX_EPSIZE
, 1);
127 ConfigSuccess
&= Endpoint_ConfigureEndpoint(CDC1_RX_EPADDR
, EP_TYPE_BULK
, CDC_TXRX_EPSIZE
, 1);
128 ConfigSuccess
&= Endpoint_ConfigureEndpoint(CDC1_NOTIFICATION_EPADDR
, EP_TYPE_INTERRUPT
, CDC_NOTIFICATION_EPSIZE
, 1);
130 /* Setup second CDC Interface's Endpoints */
131 ConfigSuccess
&= Endpoint_ConfigureEndpoint(CDC2_TX_EPADDR
, EP_TYPE_BULK
, CDC_TXRX_EPSIZE
, 1);
132 ConfigSuccess
&= Endpoint_ConfigureEndpoint(CDC2_RX_EPADDR
, EP_TYPE_BULK
, CDC_TXRX_EPSIZE
, 1);
133 ConfigSuccess
&= Endpoint_ConfigureEndpoint(CDC2_NOTIFICATION_EPADDR
, EP_TYPE_INTERRUPT
, CDC_NOTIFICATION_EPSIZE
, 1);
135 /* Reset line encoding baud rates so that the host knows to send new values */
136 LineEncoding1
.BaudRateBPS
= 0;
137 LineEncoding2
.BaudRateBPS
= 0;
139 /* Indicate endpoint configuration success or failure */
140 LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY
: LEDMASK_USB_ERROR
);
143 /** Event handler for the USB_ControlRequest event. This is used to catch and process control requests sent to
144 * the device from the USB host before passing along unhandled control requests to the library for processing
147 void EVENT_USB_Device_ControlRequest(void)
149 /* Determine which interface's Line Coding data is being set from the wIndex parameter */
150 void* LineEncodingData
= (USB_ControlRequest
.wIndex
== 0) ?
&LineEncoding1
: &LineEncoding2
;
152 /* Process CDC specific control requests */
153 switch (USB_ControlRequest
.bRequest
)
155 case CDC_REQ_GetLineEncoding
:
156 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
))
158 Endpoint_ClearSETUP();
160 /* Write the line coding data to the control endpoint */
161 Endpoint_Write_Control_Stream_LE(LineEncodingData
, sizeof(CDC_LineEncoding_t
));
166 case CDC_REQ_SetLineEncoding
:
167 if (USB_ControlRequest
.bmRequestType
== (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
))
169 Endpoint_ClearSETUP();
171 /* Read the line coding data in from the host into the global struct */
172 Endpoint_Read_Control_Stream_LE(LineEncodingData
, sizeof(CDC_LineEncoding_t
));
177 case CDC_REQ_SetControlLineState
:
178 if (USB_ControlRequest
.bmRequestType
== (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
))
180 Endpoint_ClearSETUP();
181 Endpoint_ClearStatusStage();
188 /** Function to manage CDC data transmission and reception to and from the host for the first CDC interface, which sends joystick
189 * movements to the host as ASCII strings.
193 char* ReportString
= NULL
;
194 uint8_t JoyStatus_LCL
= Joystick_GetStatus();
195 static bool ActionSent
= false;
197 /* Device must be connected and configured for the task to run */
198 if (USB_DeviceState
!= DEVICE_STATE_Configured
)
201 /* Determine if a joystick action has occurred */
202 if (JoyStatus_LCL
& JOY_UP
)
203 ReportString
= "Joystick Up\r\n";
204 else if (JoyStatus_LCL
& JOY_DOWN
)
205 ReportString
= "Joystick Down\r\n";
206 else if (JoyStatus_LCL
& JOY_LEFT
)
207 ReportString
= "Joystick Left\r\n";
208 else if (JoyStatus_LCL
& JOY_RIGHT
)
209 ReportString
= "Joystick Right\r\n";
210 else if (JoyStatus_LCL
& JOY_PRESS
)
211 ReportString
= "Joystick Pressed\r\n";
215 /* Flag management - Only allow one string to be sent per action */
216 if ((ReportString
!= NULL
) && (ActionSent
== false) && LineEncoding1
.BaudRateBPS
)
220 /* Select the Serial Tx Endpoint */
221 Endpoint_SelectEndpoint(CDC1_TX_EPADDR
);
223 /* Write the String to the Endpoint */
224 Endpoint_Write_Stream_LE(ReportString
, strlen(ReportString
), NULL
);
226 /* Finalize the stream transfer to send the last packet */
229 /* Wait until the endpoint is ready for another packet */
230 Endpoint_WaitUntilReady();
232 /* Send an empty packet to ensure that the host does not buffer data sent to it */
236 /* Select the Serial Rx Endpoint */
237 Endpoint_SelectEndpoint(CDC1_RX_EPADDR
);
239 /* Throw away any received data from the host */
240 if (Endpoint_IsOUTReceived())
244 /** Function to manage CDC data transmission and reception to and from the host for the second CDC interface, which echoes back
245 * all data sent to it from the host.
249 /* Device must be connected and configured for the task to run */
250 if (USB_DeviceState
!= DEVICE_STATE_Configured
)
253 /* Select the Serial Rx Endpoint */
254 Endpoint_SelectEndpoint(CDC2_RX_EPADDR
);
256 /* Check to see if any data has been received */
257 if (Endpoint_IsOUTReceived())
259 /* Create a temp buffer big enough to hold the incoming endpoint packet */
260 uint8_t Buffer
[Endpoint_BytesInEndpoint()];
262 /* Remember how large the incoming packet is */
263 uint16_t DataLength
= Endpoint_BytesInEndpoint();
265 /* Read in the incoming packet into the buffer */
266 Endpoint_Read_Stream_LE(&Buffer
, DataLength
, NULL
);
268 /* Finalize the stream transfer to send the last packet */
271 /* Select the Serial Tx Endpoint */
272 Endpoint_SelectEndpoint(CDC2_TX_EPADDR
);
274 /* Write the received data to the endpoint */
275 Endpoint_Write_Stream_LE(&Buffer
, DataLength
, NULL
);
277 /* Finalize the stream transfer to send the last packet */
280 /* Wait until the endpoint is ready for the next packet */
281 Endpoint_WaitUntilReady();
283 /* Send an empty packet to prevent host buffering */