3 Copyright (C) Dean Camera, 2009.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
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.
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
33 * Main source file for the DualCDC demo. This file contains the main tasks of the demo and
34 * is responsible for the initial application hardware configuration.
40 /** Contains the current baud rate and other settings of the first virtual serial port. While this demo does not use
41 * the physical USART and thus does not use these settings, they must still be retained and returned to the host
42 * upon request or the host will assume the device is non-functional.
44 * These values are set by the host via a class-specific request, however they are not required to be used accurately.
45 * It is possible to completely ignore these value or use other settings as the host is completely unaware of the physical
46 * serial link characteristics and instead sends and receives data in endpoint streams.
48 CDC_Line_Coding_t LineCoding1
= { .BaudRateBPS
= 9600,
49 .CharFormat
= OneStopBit
,
50 .ParityType
= Parity_None
,
53 /** Contains the current baud rate and other settings of the second virtual serial port. While this demo does not use
54 * the physical USART and thus does not use these settings, they must still be retained and returned to the host
55 * upon request or the host will assume the device is non-functional.
57 * These values are set by the host via a class-specific request, however they are not required to be used accurately.
58 * It is possible to completely ignore these value or use other settings as the host is completely unaware of the physical
59 * serial link characteristics and instead sends and receives data in endpoint streams.
61 CDC_Line_Coding_t LineCoding2
= { .BaudRateBPS
= 9600,
62 .CharFormat
= OneStopBit
,
63 .ParityType
= Parity_None
,
66 /** Main program entry point. This routine configures the hardware required by the application, then
67 * starts the scheduler to run the application tasks.
81 /** Configures the board hardware and chip peripherals for the demo's functionality. */
82 void SetupHardware(void)
84 /* Disable watchdog if enabled by bootloader/fuses */
85 MCUSR
&= ~(1 << WDRF
);
88 /* Disable clock division */
89 clock_prescale_set(clock_div_1
);
91 /* Hardware Initialization */
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.
100 void EVENT_USB_Connect(void)
102 /* Indicate USB enumerating */
103 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
);
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.
109 void EVENT_USB_Disconnect(void)
111 /* Indicate USB not ready */
112 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
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.
118 void EVENT_USB_ConfigurationChanged(void)
120 /* Setup CDC Notification, Rx and Tx Endpoints for the first CDC */
121 Endpoint_ConfigureEndpoint(CDC1_NOTIFICATION_EPNUM
, EP_TYPE_INTERRUPT
,
122 ENDPOINT_DIR_IN
, CDC_NOTIFICATION_EPSIZE
,
123 ENDPOINT_BANK_SINGLE
);
125 Endpoint_ConfigureEndpoint(CDC1_TX_EPNUM
, EP_TYPE_BULK
,
126 ENDPOINT_DIR_IN
, CDC_TXRX_EPSIZE
,
127 ENDPOINT_BANK_SINGLE
);
129 Endpoint_ConfigureEndpoint(CDC1_RX_EPNUM
, EP_TYPE_BULK
,
130 ENDPOINT_DIR_OUT
, CDC_TXRX_EPSIZE
,
131 ENDPOINT_BANK_SINGLE
);
133 /* Setup CDC Notification, Rx and Tx Endpoints for the second CDC */
134 Endpoint_ConfigureEndpoint(CDC2_NOTIFICATION_EPNUM
, EP_TYPE_INTERRUPT
,
135 ENDPOINT_DIR_IN
, CDC_NOTIFICATION_EPSIZE
,
136 ENDPOINT_BANK_SINGLE
);
138 Endpoint_ConfigureEndpoint(CDC2_TX_EPNUM
, EP_TYPE_BULK
,
139 ENDPOINT_DIR_IN
, CDC_TXRX_EPSIZE
,
140 ENDPOINT_BANK_SINGLE
);
142 Endpoint_ConfigureEndpoint(CDC2_RX_EPNUM
, EP_TYPE_BULK
,
143 ENDPOINT_DIR_OUT
, CDC_TXRX_EPSIZE
,
144 ENDPOINT_BANK_SINGLE
);
146 /* Indicate USB connected and ready */
147 LEDs_SetAllLEDs(LEDMASK_USB_READY
);
150 /** Event handler for the USB_UnhandledControlPacket event. This is used to catch standard and class specific
151 * control requests that are not handled internally by the USB library (including the CDC control commands,
152 * which are all issued via the control endpoint), so that they can be handled appropriately for the application.
154 void EVENT_USB_UnhandledControlPacket(void)
156 /* Determine which interface's Line Coding data is being set from the wIndex parameter */
157 uint8_t* LineCodingData
= (USB_ControlRequest
.wIndex
== 0) ?
(uint8_t*)&LineCoding1
: (uint8_t*)&LineCoding2
;
159 /* Process CDC specific control requests */
160 switch (USB_ControlRequest
.bRequest
)
162 case REQ_GetLineEncoding
:
163 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
))
165 /* Acknowledge the SETUP packet, ready for data transfer */
166 Endpoint_ClearSETUP();
168 /* Write the line coding data to the control endpoint */
169 Endpoint_Write_Control_Stream_LE(LineCodingData
, sizeof(CDC_Line_Coding_t
));
171 /* Finalize the stream transfer to send the last packet or clear the host abort */
176 case REQ_SetLineEncoding
:
177 if (USB_ControlRequest
.bmRequestType
== (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
))
179 /* Acknowledge the SETUP packet, ready for data transfer */
180 Endpoint_ClearSETUP();
182 /* Read the line coding data in from the host into the global struct */
183 Endpoint_Read_Control_Stream_LE(LineCodingData
, sizeof(CDC_Line_Coding_t
));
185 /* Finalize the stream transfer to clear the last packet from the host */
190 case REQ_SetControlLineState
:
191 if (USB_ControlRequest
.bmRequestType
== (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
))
193 /* Acknowledge the SETUP packet, ready for data transfer */
194 Endpoint_ClearSETUP();
196 /* Acknowledge status stage */
197 while (!(Endpoint_IsINReady()));
205 /** Function to manage CDC data transmission and reception to and from the host for the first CDC interface, which sends joystick
206 * movements to the host as ASCII strings.
210 char* ReportString
= NULL
;
211 uint8_t JoyStatus_LCL
= Joystick_GetStatus();
212 static bool ActionSent
= false;
214 char* JoystickStrings
[] =
219 "Joystick Right\r\n",
220 "Joystick Pressed\r\n",
223 /* Determine if a joystick action has occurred */
224 if (JoyStatus_LCL
& JOY_UP
)
225 ReportString
= JoystickStrings
[0];
226 else if (JoyStatus_LCL
& JOY_DOWN
)
227 ReportString
= JoystickStrings
[1];
228 else if (JoyStatus_LCL
& JOY_LEFT
)
229 ReportString
= JoystickStrings
[2];
230 else if (JoyStatus_LCL
& JOY_RIGHT
)
231 ReportString
= JoystickStrings
[3];
232 else if (JoyStatus_LCL
& JOY_PRESS
)
233 ReportString
= JoystickStrings
[4];
235 /* Flag management - Only allow one string to be sent per action */
236 if (ReportString
== NULL
)
240 else if (ActionSent
== false)
244 /* Select the Serial Tx Endpoint */
245 Endpoint_SelectEndpoint(CDC1_TX_EPNUM
);
247 /* Write the String to the Endpoint */
248 Endpoint_Write_Stream_LE(ReportString
, strlen(ReportString
));
250 /* Finalize the stream transfer to send the last packet */
253 /* Wait until the endpoint is ready for another packet */
254 while (!(Endpoint_IsINReady()));
256 /* Send an empty packet to ensure that the host does not buffer data sent to it */
260 /* Select the Serial Rx Endpoint */
261 Endpoint_SelectEndpoint(CDC1_RX_EPNUM
);
263 /* Throw away any received data from the host */
264 if (Endpoint_IsOUTReceived())
268 /** Function to manage CDC data transmission and reception to and from the host for the second CDC interface, which echoes back
269 * all data sent to it from the host.
273 /* Select the Serial Rx Endpoint */
274 Endpoint_SelectEndpoint(CDC2_RX_EPNUM
);
276 /* Check to see if any data has been received */
277 if (Endpoint_IsOUTReceived())
279 /* Create a temp buffer big enough to hold the incoming endpoint packet */
280 uint8_t Buffer
[Endpoint_BytesInEndpoint()];
282 /* Remember how large the incoming packet is */
283 uint16_t DataLength
= Endpoint_BytesInEndpoint();
285 /* Read in the incoming packet into the buffer */
286 Endpoint_Read_Stream_LE(&Buffer
, DataLength
);
288 /* Finalize the stream transfer to send the last packet */
291 /* Select the Serial Tx Endpoint */
292 Endpoint_SelectEndpoint(CDC2_TX_EPNUM
);
294 /* Write the received data to the endpoint */
295 Endpoint_Write_Stream_LE(&Buffer
, DataLength
);
297 /* Finalize the stream transfer to send the last packet */
300 /* Wait until the endpoint is ready for the next packet */
301 while (!(Endpoint_IsINReady()));
303 /* Send an empty packet to prevent host buffering */