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 /* Project Tags, for reading out using the ButtLoad project */ 
  40 BUTTLOADTAG(ProjName
,    "LUFA DualCDC App"); 
  41 BUTTLOADTAG(BuildTime
,   __TIME__
); 
  42 BUTTLOADTAG(BuildDate
,   __DATE__
); 
  43 BUTTLOADTAG(LUFAVersion
, "LUFA V" LUFA_VERSION_STRING
); 
  45 /* Scheduler Task List */ 
  48         { Task
: USB_USBTask          
, TaskStatus
: TASK_STOP 
}, 
  49         { Task
: CDC1_Task            
, TaskStatus
: TASK_STOP 
}, 
  50         { Task
: CDC2_Task            
, TaskStatus
: TASK_STOP 
}, 
  54 /** Contains the current baud rate and other settings of the first virtual serial port. While this demo does not use 
  55  *  the physical USART and thus does not use these settings, they must still be retained and returned to the host 
  56  *  upon request or the host will assume the device is non-functional. 
  58  *  These values are set by the host via a class-specific request, however they are not required to be used accurately. 
  59  *  It is possible to completely ignore these value or use other settings as the host is completely unaware of the physical 
  60  *  serial link characteristics and instead sends and recieves data in endpoint streams. 
  62 CDC_Line_Coding_t LineCoding1 
= { BaudRateBPS
: 9600, 
  63                                   CharFormat
:  OneStopBit
, 
  64                                   ParityType
:  Parity_None
, 
  67 /** Contains the current baud rate and other settings of the second virtual serial port. While this demo does not use 
  68  *  the physical USART and thus does not use these settings, they must still be retained and returned to the host 
  69  *  upon request or the host will assume the device is non-functional. 
  71  *  These values are set by the host via a class-specific request, however they are not required to be used accurately. 
  72  *  It is possible to completely ignore these value or use other settings as the host is completely unaware of the physical 
  73  *  serial link characteristics and instead sends and recieves data in endpoint streams. 
  75 CDC_Line_Coding_t LineCoding2 
= { BaudRateBPS
: 9600, 
  76                                   CharFormat
:  OneStopBit
, 
  77                                   ParityType
:  Parity_None
, 
  80 /** String to print through the first virtual serial port when the joystick is pressed upwards. */ 
  81 char JoystickUpString
[]      = "Joystick Up\r\n"; 
  83 /** String to print through the first virtual serial port when the joystick is pressed downwards. */ 
  84 char JoystickDownString
[]    = "Joystick Down\r\n"; 
  86 /** String to print through the first virtual serial port when the joystick is pressed left. */ 
  87 char JoystickLeftString
[]    = "Joystick Left\r\n"; 
  89 /** String to print through the first virtual serial port when the joystick is pressed right. */ 
  90 char JoystickRightString
[]   = "Joystick Right\r\n"; 
  92 /** String to print through the first virtual serial port when the joystick is pressed inwards. */ 
  93 char JoystickPressedString
[] = "Joystick Pressed\r\n"; 
  95 /** Main program entry point. This routine configures the hardware required by the application, then 
  96  *  starts the scheduler to run the application tasks. 
 100         /* Disable watchdog if enabled by bootloader/fuses */ 
 101         MCUSR 
&= ~(1 << WDRF
); 
 104         /* Disable clock division */ 
 105         clock_prescale_set(clock_div_1
); 
 107         /* Hardware Initialization */ 
 111         /* Indicate USB not ready */ 
 112         UpdateStatus(Status_USBNotReady
); 
 114         /* Initialize Scheduler so that it can be used */ 
 117         /* Initialize USB Subsystem */ 
 120         /* Scheduling - routine never returns, so put this last in the main function */ 
 124 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and 
 125  *  starts the library USB task to begin the enumeration and USB management process. 
 127 EVENT_HANDLER(USB_Connect
) 
 129         /* Start USB management task */ 
 130         Scheduler_SetTaskMode(USB_USBTask
, TASK_RUN
); 
 132         /* Indicate USB enumerating */ 
 133         UpdateStatus(Status_USBEnumerating
); 
 136 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via 
 137  *  the status LEDs and stops the USB management and CDC management tasks. 
 139 EVENT_HANDLER(USB_Disconnect
) 
 141         /* Stop running CDC and USB management tasks */ 
 142         Scheduler_SetTaskMode(CDC1_Task
, TASK_STOP
); 
 143         Scheduler_SetTaskMode(CDC2_Task
, TASK_STOP
); 
 144         Scheduler_SetTaskMode(USB_USBTask
, TASK_STOP
); 
 146         /* Indicate USB not ready */ 
 147         UpdateStatus(Status_USBNotReady
); 
 150 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host set the current configuration 
 151  *  of the USB device after enumeration - the device endpoints are configured and the CDC management tasks are started. 
 153 EVENT_HANDLER(USB_ConfigurationChanged
) 
 155         /* Setup CDC Notification, Rx and Tx Endpoints for the first CDC */ 
 156         Endpoint_ConfigureEndpoint(CDC1_NOTIFICATION_EPNUM
, EP_TYPE_INTERRUPT
, 
 157                                        ENDPOINT_DIR_IN
, CDC_NOTIFICATION_EPSIZE
, 
 158                                    ENDPOINT_BANK_SINGLE
); 
 160         Endpoint_ConfigureEndpoint(CDC1_TX_EPNUM
, EP_TYPE_BULK
, 
 161                                        ENDPOINT_DIR_IN
, CDC_TXRX_EPSIZE
, 
 162                                    ENDPOINT_BANK_SINGLE
); 
 164         Endpoint_ConfigureEndpoint(CDC1_RX_EPNUM
, EP_TYPE_BULK
, 
 165                                        ENDPOINT_DIR_OUT
, CDC_TXRX_EPSIZE
, 
 166                                    ENDPOINT_BANK_SINGLE
); 
 168         /* Setup CDC Notification, Rx and Tx Endpoints for the second CDC */ 
 169         Endpoint_ConfigureEndpoint(CDC2_NOTIFICATION_EPNUM
, EP_TYPE_INTERRUPT
, 
 170                                        ENDPOINT_DIR_IN
, CDC_NOTIFICATION_EPSIZE
, 
 171                                    ENDPOINT_BANK_SINGLE
); 
 173         Endpoint_ConfigureEndpoint(CDC2_TX_EPNUM
, EP_TYPE_BULK
, 
 174                                        ENDPOINT_DIR_IN
, CDC_TXRX_EPSIZE
, 
 175                                    ENDPOINT_BANK_SINGLE
); 
 177         Endpoint_ConfigureEndpoint(CDC2_RX_EPNUM
, EP_TYPE_BULK
, 
 178                                        ENDPOINT_DIR_OUT
, CDC_TXRX_EPSIZE
, 
 179                                    ENDPOINT_BANK_SINGLE
); 
 181         /* Indicate USB connected and ready */ 
 182         UpdateStatus(Status_USBReady
); 
 184         /* Start CDC tasks */ 
 185         Scheduler_SetTaskMode(CDC1_Task
, TASK_RUN
); 
 186         Scheduler_SetTaskMode(CDC2_Task
, TASK_RUN
); 
 189 /** Event handler for the USB_UnhandledControlPacket event. This is used to catch standard and class specific 
 190  *  control requests that are not handled internally by the USB library (including the CDC control commands, 
 191  *  which are all issued via the control endpoint), so that they can be handled appropriately for the application. 
 193 EVENT_HANDLER(USB_UnhandledControlPacket
) 
 195         uint8_t* LineCodingData
; 
 197         /* Discard the unused wValue parameter */ 
 198         Endpoint_Ignore_Word(); 
 200         /* wIndex indicates the interface being controlled */ 
 201         uint16_t wIndex 
= Endpoint_Read_Word_LE(); 
 203         /* Determine which interface's Line Coding data is being set from the wIndex parameter */ 
 204         LineCodingData 
= (wIndex 
== 0) ? 
(uint8_t*)&LineCoding1 
: (uint8_t*)&LineCoding2
; 
 206         /* Process CDC specific control requests */ 
 209                 case REQ_GetLineEncoding
: 
 210                         if (bmRequestType 
== (REQDIR_DEVICETOHOST 
| REQTYPE_CLASS 
| REQREC_INTERFACE
)) 
 212                                 /* Acknowedge the SETUP packet, ready for data transfer */ 
 213                                 Endpoint_ClearSetupReceived(); 
 215                                 /* Write the line coding data to the control endpoint */ 
 216                                 Endpoint_Write_Control_Stream_LE(LineCodingData
, sizeof(CDC_Line_Coding_t
)); 
 218                                 /* Finalize the stream transfer to send the last packet or clear the host abort */ 
 219                                 Endpoint_ClearSetupOUT(); 
 223                 case REQ_SetLineEncoding
: 
 224                         if (bmRequestType 
== (REQDIR_HOSTTODEVICE 
| REQTYPE_CLASS 
| REQREC_INTERFACE
)) 
 226                                 /* Acknowedge the SETUP packet, ready for data transfer */ 
 227                                 Endpoint_ClearSetupReceived(); 
 229                                 /* Read the line coding data in from the host into the global struct */ 
 230                                 Endpoint_Read_Control_Stream_LE(LineCodingData
, sizeof(CDC_Line_Coding_t
)); 
 232                                 /* Finalize the stream transfer to clear the last packet from the host */ 
 233                                 Endpoint_ClearSetupIN(); 
 237                 case REQ_SetControlLineState
: 
 238                         if (bmRequestType 
== (REQDIR_HOSTTODEVICE 
| REQTYPE_CLASS 
| REQREC_INTERFACE
)) 
 240                                 /* Acknowedge the SETUP packet, ready for data transfer */ 
 241                                 Endpoint_ClearSetupReceived(); 
 243                                 /* Send an empty packet to acknowedge the command (currently unused) */ 
 244                                 Endpoint_ClearSetupIN(); 
 251 /** Function to manage status updates to the user. This is done via LEDs on the given board, if available, but may be changed to 
 252  *  log to a serial port, or anything else that is suitable for status updates. 
 254  *  \param CurrentStatus  Current status of the system, from the DualCDC_StatusCodes_t enum 
 256 void UpdateStatus(uint8_t CurrentStatus
) 
 258         uint8_t LEDMask 
= LEDS_NO_LEDS
; 
 260         /* Set the LED mask to the appropriate LED mask based on the given status code */ 
 261         switch (CurrentStatus
) 
 263                 case Status_USBNotReady
: 
 264                         LEDMask 
= (LEDS_LED1
); 
 266                 case Status_USBEnumerating
: 
 267                         LEDMask 
= (LEDS_LED1 
| LEDS_LED2
); 
 269                 case Status_USBReady
: 
 270                         LEDMask 
= (LEDS_LED2 
| LEDS_LED4
); 
 274         /* Set the board LEDs to the new LED mask */ 
 275         LEDs_SetAllLEDs(LEDMask
); 
 278 /** Function to manage CDC data transmission and reception to and from the host for the first CDC interface, which sends joystick 
 279  *  movements to the host as ASCII strings. 
 283         char*       ReportString    
= NULL
; 
 284         uint8_t     JoyStatus_LCL   
= Joystick_GetStatus(); 
 285         static bool ActionSent      
= false; 
 287         /* Determine if a joystick action has occurred */ 
 288         if (JoyStatus_LCL 
& JOY_UP
) 
 289           ReportString 
= JoystickUpString
; 
 290         else if (JoyStatus_LCL 
& JOY_DOWN
) 
 291           ReportString 
= JoystickDownString
; 
 292         else if (JoyStatus_LCL 
& JOY_LEFT
) 
 293           ReportString 
= JoystickLeftString
; 
 294         else if (JoyStatus_LCL 
& JOY_RIGHT
) 
 295           ReportString 
= JoystickRightString
; 
 296         else if (JoyStatus_LCL 
& JOY_PRESS
) 
 297           ReportString 
= JoystickPressedString
; 
 299         /* Flag management - Only allow one string to be sent per action */ 
 300         if (ReportString 
== NULL
) 
 304         else if (ActionSent 
== false) 
 308                 /* Select the Serial Tx Endpoint */ 
 309                 Endpoint_SelectEndpoint(CDC1_TX_EPNUM
); 
 311                 /* Write the String to the Endpoint */ 
 312                 Endpoint_Write_Stream_LE(ReportString
, strlen(ReportString
)); 
 314                 /* Finalize the stream transfer to send the last packet */ 
 315                 Endpoint_ClearCurrentBank(); 
 318         /* Select the Serial Rx Endpoint */ 
 319         Endpoint_SelectEndpoint(CDC1_RX_EPNUM
); 
 321         /* Throw away any received data from the host */ 
 322         if (Endpoint_ReadWriteAllowed()) 
 323           Endpoint_ClearCurrentBank(); 
 326 /** Function to manage CDC data transmission and reception to and from the host for the second CDC interface, which echos back 
 327  *  all data sent to it from the host. 
 331         /* Select the Serial Rx Endpoint */ 
 332         Endpoint_SelectEndpoint(CDC2_RX_EPNUM
); 
 334         /* Check to see if any data has been received */ 
 335         if (Endpoint_ReadWriteAllowed()) 
 337                 /* Create a temp buffer big enough to hold the incomming endpoint packet */ 
 338                 uint8_t  Buffer
[Endpoint_BytesInEndpoint()]; 
 340                 /* Remember how large the incomming packet is */ 
 341                 uint16_t DataLength 
= Endpoint_BytesInEndpoint(); 
 343                 /* Read in the incomming packet into the buffer */ 
 344                 Endpoint_Read_Stream_LE(&Buffer
, DataLength
); 
 346                 /* Finalize the stream transfer to send the last packet */ 
 347                 Endpoint_ClearCurrentBank(); 
 349                 /* Select the Serial Tx Endpoint */ 
 350                 Endpoint_SelectEndpoint(CDC2_TX_EPNUM
); 
 352                 /* Write the received data to the endpoint */ 
 353                 Endpoint_Write_Stream_LE(&Buffer
, DataLength
); 
 355                 /* Finalize the stream transfer to send the last packet */ 
 356                 Endpoint_ClearCurrentBank();