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.
39 /* Scheduler Task List */
42 { .Task
= USB_USBTask
, .TaskStatus
= TASK_STOP
},
43 { .Task
= CDC1_Task
, .TaskStatus
= TASK_STOP
},
44 { .Task
= CDC2_Task
, .TaskStatus
= TASK_STOP
},
48 /** Contains the current baud rate and other settings of the first virtual serial port. While this demo does not use
49 * the physical USART and thus does not use these settings, they must still be retained and returned to the host
50 * upon request or the host will assume the device is non-functional.
52 * These values are set by the host via a class-specific request, however they are not required to be used accurately.
53 * It is possible to completely ignore these value or use other settings as the host is completely unaware of the physical
54 * serial link characteristics and instead sends and receives data in endpoint streams.
56 CDC_Line_Coding_t LineCoding1
= { .BaudRateBPS
= 9600,
57 .CharFormat
= OneStopBit
,
58 .ParityType
= Parity_None
,
61 /** Contains the current baud rate and other settings of the second virtual serial port. While this demo does not use
62 * the physical USART and thus does not use these settings, they must still be retained and returned to the host
63 * upon request or the host will assume the device is non-functional.
65 * These values are set by the host via a class-specific request, however they are not required to be used accurately.
66 * It is possible to completely ignore these value or use other settings as the host is completely unaware of the physical
67 * serial link characteristics and instead sends and receives data in endpoint streams.
69 CDC_Line_Coding_t LineCoding2
= { .BaudRateBPS
= 9600,
70 .CharFormat
= OneStopBit
,
71 .ParityType
= Parity_None
,
74 /** String to print through the first virtual serial port when the joystick is pressed upwards. */
75 char JoystickUpString
[] = "Joystick Up\r\n";
77 /** String to print through the first virtual serial port when the joystick is pressed downward. */
78 char JoystickDownString
[] = "Joystick Down\r\n";
80 /** String to print through the first virtual serial port when the joystick is pressed left. */
81 char JoystickLeftString
[] = "Joystick Left\r\n";
83 /** String to print through the first virtual serial port when the joystick is pressed right. */
84 char JoystickRightString
[] = "Joystick Right\r\n";
86 /** String to print through the first virtual serial port when the joystick is pressed inwards. */
87 char JoystickPressedString
[] = "Joystick Pressed\r\n";
89 /** Main program entry point. This routine configures the hardware required by the application, then
90 * starts the scheduler to run the application tasks.
94 /* Disable watchdog if enabled by bootloader/fuses */
95 MCUSR
&= ~(1 << WDRF
);
98 /* Disable clock division */
99 clock_prescale_set(clock_div_1
);
101 /* Hardware Initialization */
105 /* Indicate USB not ready */
106 UpdateStatus(Status_USBNotReady
);
108 /* Initialize Scheduler so that it can be used */
111 /* Initialize USB Subsystem */
114 /* Scheduling - routine never returns, so put this last in the main function */
118 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and
119 * starts the library USB task to begin the enumeration and USB management process.
121 EVENT_HANDLER(USB_Connect
)
123 /* Start USB management task */
124 Scheduler_SetTaskMode(USB_USBTask
, TASK_RUN
);
126 /* Indicate USB enumerating */
127 UpdateStatus(Status_USBEnumerating
);
130 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via
131 * the status LEDs and stops the USB management and CDC management tasks.
133 EVENT_HANDLER(USB_Disconnect
)
135 /* Stop running CDC and USB management tasks */
136 Scheduler_SetTaskMode(CDC1_Task
, TASK_STOP
);
137 Scheduler_SetTaskMode(CDC2_Task
, TASK_STOP
);
138 Scheduler_SetTaskMode(USB_USBTask
, TASK_STOP
);
140 /* Indicate USB not ready */
141 UpdateStatus(Status_USBNotReady
);
144 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host set the current configuration
145 * of the USB device after enumeration - the device endpoints are configured and the CDC management tasks are started.
147 EVENT_HANDLER(USB_ConfigurationChanged
)
149 /* Setup CDC Notification, Rx and Tx Endpoints for the first CDC */
150 Endpoint_ConfigureEndpoint(CDC1_NOTIFICATION_EPNUM
, EP_TYPE_INTERRUPT
,
151 ENDPOINT_DIR_IN
, CDC_NOTIFICATION_EPSIZE
,
152 ENDPOINT_BANK_SINGLE
);
154 Endpoint_ConfigureEndpoint(CDC1_TX_EPNUM
, EP_TYPE_BULK
,
155 ENDPOINT_DIR_IN
, CDC_TXRX_EPSIZE
,
156 ENDPOINT_BANK_SINGLE
);
158 Endpoint_ConfigureEndpoint(CDC1_RX_EPNUM
, EP_TYPE_BULK
,
159 ENDPOINT_DIR_OUT
, CDC_TXRX_EPSIZE
,
160 ENDPOINT_BANK_SINGLE
);
162 /* Setup CDC Notification, Rx and Tx Endpoints for the second CDC */
163 Endpoint_ConfigureEndpoint(CDC2_NOTIFICATION_EPNUM
, EP_TYPE_INTERRUPT
,
164 ENDPOINT_DIR_IN
, CDC_NOTIFICATION_EPSIZE
,
165 ENDPOINT_BANK_SINGLE
);
167 Endpoint_ConfigureEndpoint(CDC2_TX_EPNUM
, EP_TYPE_BULK
,
168 ENDPOINT_DIR_IN
, CDC_TXRX_EPSIZE
,
169 ENDPOINT_BANK_SINGLE
);
171 Endpoint_ConfigureEndpoint(CDC2_RX_EPNUM
, EP_TYPE_BULK
,
172 ENDPOINT_DIR_OUT
, CDC_TXRX_EPSIZE
,
173 ENDPOINT_BANK_SINGLE
);
175 /* Indicate USB connected and ready */
176 UpdateStatus(Status_USBReady
);
178 /* Start CDC tasks */
179 Scheduler_SetTaskMode(CDC1_Task
, TASK_RUN
);
180 Scheduler_SetTaskMode(CDC2_Task
, TASK_RUN
);
183 /** Event handler for the USB_UnhandledControlPacket event. This is used to catch standard and class specific
184 * control requests that are not handled internally by the USB library (including the CDC control commands,
185 * which are all issued via the control endpoint), so that they can be handled appropriately for the application.
187 EVENT_HANDLER(USB_UnhandledControlPacket
)
189 uint8_t* LineCodingData
;
191 /* Discard the unused wValue parameter */
192 Endpoint_Discard_Word();
194 /* wIndex indicates the interface being controlled */
195 uint16_t wIndex
= Endpoint_Read_Word_LE();
197 /* Determine which interface's Line Coding data is being set from the wIndex parameter */
198 LineCodingData
= (wIndex
== 0) ?
(uint8_t*)&LineCoding1
: (uint8_t*)&LineCoding2
;
200 /* Process CDC specific control requests */
201 switch (USB_ControlRequest
.bRequest
)
203 case REQ_GetLineEncoding
:
204 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
))
206 /* Acknowledge the SETUP packet, ready for data transfer */
207 Endpoint_ClearSETUP();
209 /* Write the line coding data to the control endpoint */
210 Endpoint_Write_Control_Stream_LE(LineCodingData
, sizeof(CDC_Line_Coding_t
));
212 /* Finalize the stream transfer to send the last packet or clear the host abort */
217 case REQ_SetLineEncoding
:
218 if (USB_ControlRequest
.bmRequestType
== (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
))
220 /* Acknowledge the SETUP packet, ready for data transfer */
221 Endpoint_ClearSETUP();
223 /* Read the line coding data in from the host into the global struct */
224 Endpoint_Read_Control_Stream_LE(LineCodingData
, sizeof(CDC_Line_Coding_t
));
226 /* Finalize the stream transfer to clear the last packet from the host */
231 case REQ_SetControlLineState
:
232 if (USB_ControlRequest
.bmRequestType
== (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
))
234 /* Acknowledge the SETUP packet, ready for data transfer */
235 Endpoint_ClearSETUP();
237 /* Acknowledge status stage */
238 while (!(Endpoint_IsINReady()));
246 /** Function to manage status updates to the user. This is done via LEDs on the given board, if available, but may be changed to
247 * log to a serial port, or anything else that is suitable for status updates.
249 * \param CurrentStatus Current status of the system, from the DualCDC_StatusCodes_t enum
251 void UpdateStatus(uint8_t CurrentStatus
)
253 uint8_t LEDMask
= LEDS_NO_LEDS
;
255 /* Set the LED mask to the appropriate LED mask based on the given status code */
256 switch (CurrentStatus
)
258 case Status_USBNotReady
:
259 LEDMask
= (LEDS_LED1
);
261 case Status_USBEnumerating
:
262 LEDMask
= (LEDS_LED1
| LEDS_LED2
);
264 case Status_USBReady
:
265 LEDMask
= (LEDS_LED2
| LEDS_LED4
);
269 /* Set the board LEDs to the new LED mask */
270 LEDs_SetAllLEDs(LEDMask
);
273 /** Function to manage CDC data transmission and reception to and from the host for the first CDC interface, which sends joystick
274 * movements to the host as ASCII strings.
278 char* ReportString
= NULL
;
279 uint8_t JoyStatus_LCL
= Joystick_GetStatus();
280 static bool ActionSent
= false;
282 /* Determine if a joystick action has occurred */
283 if (JoyStatus_LCL
& JOY_UP
)
284 ReportString
= JoystickUpString
;
285 else if (JoyStatus_LCL
& JOY_DOWN
)
286 ReportString
= JoystickDownString
;
287 else if (JoyStatus_LCL
& JOY_LEFT
)
288 ReportString
= JoystickLeftString
;
289 else if (JoyStatus_LCL
& JOY_RIGHT
)
290 ReportString
= JoystickRightString
;
291 else if (JoyStatus_LCL
& JOY_PRESS
)
292 ReportString
= JoystickPressedString
;
294 /* Flag management - Only allow one string to be sent per action */
295 if (ReportString
== NULL
)
299 else if (ActionSent
== false)
303 /* Select the Serial Tx Endpoint */
304 Endpoint_SelectEndpoint(CDC1_TX_EPNUM
);
306 /* Write the String to the Endpoint */
307 Endpoint_Write_Stream_LE(ReportString
, strlen(ReportString
));
309 /* Finalize the stream transfer to send the last packet */
312 /* Wait until the endpoint is ready for another packet */
313 while (!(Endpoint_IsINReady()));
315 /* Send an empty packet to ensure that the host does not buffer data sent to it */
319 /* Select the Serial Rx Endpoint */
320 Endpoint_SelectEndpoint(CDC1_RX_EPNUM
);
322 /* Throw away any received data from the host */
323 if (Endpoint_IsOUTReceived())
327 /** Function to manage CDC data transmission and reception to and from the host for the second CDC interface, which echoes back
328 * all data sent to it from the host.
332 /* Select the Serial Rx Endpoint */
333 Endpoint_SelectEndpoint(CDC2_RX_EPNUM
);
335 /* Check to see if any data has been received */
336 if (Endpoint_IsOUTReceived())
338 /* Create a temp buffer big enough to hold the incoming endpoint packet */
339 uint8_t Buffer
[Endpoint_BytesInEndpoint()];
341 /* Remember how large the incoming packet is */
342 uint16_t DataLength
= Endpoint_BytesInEndpoint();
344 /* Read in the incoming packet into the buffer */
345 Endpoint_Read_Stream_LE(&Buffer
, DataLength
);
347 /* Finalize the stream transfer to send the last packet */
350 /* Select the Serial Tx Endpoint */
351 Endpoint_SelectEndpoint(CDC2_TX_EPNUM
);
353 /* Write the received data to the endpoint */
354 Endpoint_Write_Stream_LE(&Buffer
, DataLength
);
356 /* Finalize the stream transfer to send the last packet */
359 /* Wait until the endpoint is ready for the next packet */
360 while (!(Endpoint_IsINReady()));
362 /* Send an empty packet to prevent host buffering */