3      Copyright (C) Dean Camera, 2015. 
   5   dean [at] fourwalledcubicle [dot] com 
  10   Copyright 2015  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 VirtualSerialMassStorage demo. This file contains the main tasks of 
  34  *  the demo and is responsible for the initial application hardware configuration. 
  37 #include "VirtualSerialMassStorage.h" 
  39 /** LUFA CDC Class driver interface configuration and state information. This structure is 
  40  *  passed to all CDC Class driver functions, so that multiple instances of the same class 
  41  *  within a device can be differentiated from one another. 
  43 USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface 
= 
  47                                 .ControlInterfaceNumber         
= INTERFACE_ID_CDC_CCI
, 
  50                                                 .Address                
= CDC_TX_EPADDR
, 
  51                                                 .Size                   
= CDC_TXRX_EPSIZE
, 
  56                                                 .Address                
= CDC_RX_EPADDR
, 
  57                                                 .Size                   
= CDC_TXRX_EPSIZE
, 
  60                                 .NotificationEndpoint           
= 
  62                                                 .Address                
= CDC_NOTIFICATION_EPADDR
, 
  63                                                 .Size                   
= CDC_NOTIFICATION_EPSIZE
, 
  69 /** LUFA Mass Storage Class driver interface configuration and state information. This structure is 
  70  *  passed to all Mass Storage Class driver functions, so that multiple instances of the same class 
  71  *  within a device can be differentiated from one another. 
  73 USB_ClassInfo_MS_Device_t Disk_MS_Interface 
= 
  77                                 .InterfaceNumber                
= INTERFACE_ID_MassStorage
, 
  80                                                 .Address                
= MASS_STORAGE_IN_EPADDR
, 
  81                                                 .Size                   
= MASS_STORAGE_IO_EPSIZE
, 
  86                                                 .Address                
= MASS_STORAGE_OUT_EPADDR
, 
  87                                                 .Size                   
= MASS_STORAGE_IO_EPSIZE
, 
  90                                 .TotalLUNs                      
= TOTAL_LUNS
, 
  94 /** Standard file stream for the CDC interface when set up, so that the virtual CDC COM port can be 
  95  *  used like any regular character stream in the C APIs. 
  97 static FILE USBSerialStream
; 
 100 /** Main program entry point. This routine contains the overall program flow, including initial 
 101  *  setup of all components and the main program loop. 
 107         /* Create a regular character stream for the interface so that it can be used with the stdio.h functions */ 
 108         CDC_Device_CreateStream(&VirtualSerial_CDC_Interface
, &USBSerialStream
); 
 110         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
); 
 111         GlobalInterruptEnable(); 
 115                 CheckJoystickMovement(); 
 117                 /* Must throw away unused bytes from the host, or it will lock up while waiting for the device */ 
 118                 CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface
); 
 120                 CDC_Device_USBTask(&VirtualSerial_CDC_Interface
); 
 121                 MS_Device_USBTask(&Disk_MS_Interface
); 
 126 /** Configures the board hardware and chip peripherals for the demo's functionality. */ 
 127 void SetupHardware(void) 
 129 #if (ARCH == ARCH_AVR8) 
 130         /* Disable watchdog if enabled by bootloader/fuses */ 
 131         MCUSR 
&= ~(1 << WDRF
); 
 134         /* Disable clock division */ 
 135         clock_prescale_set(clock_div_1
); 
 136 #elif (ARCH == ARCH_XMEGA) 
 137         /* Start the PLL to multiply the 2MHz RC oscillator to 32MHz and switch the CPU core to run from it */ 
 138         XMEGACLK_StartPLL(CLOCK_SRC_INT_RC2MHZ
, 2000000, F_CPU
); 
 139         XMEGACLK_SetCPUClockSource(CLOCK_SRC_PLL
); 
 141         /* Start the 32MHz internal RC oscillator and start the DFLL to increase it to 48MHz using the USB SOF as a reference */ 
 142         XMEGACLK_StartInternalOscillator(CLOCK_SRC_INT_RC32MHZ
); 
 143         XMEGACLK_StartDFLL(CLOCK_SRC_INT_RC32MHZ
, DFLL_REF_INT_USBSOF
, F_USB
); 
 145         PMIC
.CTRL 
= PMIC_LOLVLEN_bm 
| PMIC_MEDLVLEN_bm 
| PMIC_HILVLEN_bm
; 
 148         /* Hardware Initialization */ 
 154         /* Check if the Dataflash is working, abort if not */ 
 155         if (!(DataflashManager_CheckDataflashOperation())) 
 157                 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
); 
 161         /* Clear Dataflash sector protections, if enabled */ 
 162         DataflashManager_ResetDataflashProtections(); 
 165 /** Checks for changes in the position of the board joystick, sending strings to the host upon each change. */ 
 166 void CheckJoystickMovement(void) 
 168         uint8_t     JoyStatus_LCL 
= Joystick_GetStatus(); 
 169         char*       ReportString  
= NULL
; 
 170         static bool ActionSent    
= false; 
 172         if (JoyStatus_LCL 
& JOY_UP
) 
 173           ReportString 
= "Joystick Up\r\n"; 
 174         else if (JoyStatus_LCL 
& JOY_DOWN
) 
 175           ReportString 
= "Joystick Down\r\n"; 
 176         else if (JoyStatus_LCL 
& JOY_LEFT
) 
 177           ReportString 
= "Joystick Left\r\n"; 
 178         else if (JoyStatus_LCL 
& JOY_RIGHT
) 
 179           ReportString 
= "Joystick Right\r\n"; 
 180         else if (JoyStatus_LCL 
& JOY_PRESS
) 
 181           ReportString 
= "Joystick Pressed\r\n"; 
 185         if ((ReportString 
!= NULL
) && (ActionSent 
== false)) 
 189                 /* Write the string to the virtual COM port via the created character stream */ 
 190                 fputs(ReportString
, &USBSerialStream
); 
 192                 /* Alternatively, without the stream: */ 
 193                 // CDC_Device_SendString(&VirtualSerial_CDC_Interface, ReportString); 
 197 /** Event handler for the library USB Connection event. */ 
 198 void EVENT_USB_Device_Connect(void) 
 200         LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
); 
 203 /** Event handler for the library USB Disconnection event. */ 
 204 void EVENT_USB_Device_Disconnect(void) 
 206         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
); 
 209 /** Event handler for the library USB Configuration Changed event. */ 
 210 void EVENT_USB_Device_ConfigurationChanged(void) 
 212         bool ConfigSuccess 
= true; 
 214         ConfigSuccess 
&= CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface
); 
 215         ConfigSuccess 
&= MS_Device_ConfigureEndpoints(&Disk_MS_Interface
); 
 217         LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY 
: LEDMASK_USB_ERROR
); 
 220 /** Event handler for the library USB Control Request reception event. */ 
 221 void EVENT_USB_Device_ControlRequest(void) 
 223         CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface
); 
 224         MS_Device_ProcessControlRequest(&Disk_MS_Interface
); 
 227 /** CDC class driver callback function the processing of changes to the virtual 
 228  *  control lines sent from the host.. 
 230  *  \param[in] CDCInterfaceInfo  Pointer to the CDC class interface configuration structure being referenced 
 232 void EVENT_CDC_Device_ControLineStateChanged(USB_ClassInfo_CDC_Device_t 
*const CDCInterfaceInfo
) 
 234         /* You can get changes to the virtual CDC lines in this callback; a common 
 235            use-case is to use the Data Terminal Ready (DTR) flag to enable and 
 236            disable CDC communications in your application when set to avoid the 
 237            application blocking while waiting for a host to become ready and read 
 238            in the pending data from the USB endpoints. 
 240         bool HostReady 
= (CDCInterfaceInfo
->State
.ControlLineStates
.HostToDevice 
& CDC_CONTROL_LINE_OUT_DTR
) != 0; 
 243 /** Mass Storage class driver callback function the reception of SCSI commands from the host, which must be processed. 
 245  *  \param[in] MSInterfaceInfo  Pointer to the Mass Storage class interface configuration structure being referenced 
 247 bool CALLBACK_MS_Device_SCSICommandReceived(USB_ClassInfo_MS_Device_t
* const MSInterfaceInfo
) 
 251         LEDs_SetAllLEDs(LEDMASK_USB_BUSY
); 
 252         CommandSuccess 
= SCSI_DecodeSCSICommand(MSInterfaceInfo
); 
 253         LEDs_SetAllLEDs(LEDMASK_USB_READY
); 
 255         return CommandSuccess
;