3      Copyright (C) Dean Camera, 2013. 
   5   dean [at] fourwalledcubicle [dot] com 
  10   Copyright 2013  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 Mass Storage class bootloader. This file contains the complete bootloader logic. 
  36 #include "BootloaderMassStorage.h" 
  38 /** LUFA Mass Storage Class driver interface configuration and state information. This structure is 
  39  *  passed to all Mass Storage Class driver functions, so that multiple instances of the same class 
  40  *  within a device can be differentiated from one another. 
  42 USB_ClassInfo_MS_Device_t Disk_MS_Interface 
= 
  49                                                 .Address           
= MASS_STORAGE_IN_EPADDR
, 
  50                                                 .Size              
= MASS_STORAGE_IO_EPSIZE
, 
  55                                                 .Address           
= MASS_STORAGE_OUT_EPADDR
, 
  56                                                 .Size              
= MASS_STORAGE_IO_EPSIZE
, 
  64 void Application_Jump_Check(void) 
  66         bool JumpToApplication 
= false; 
  68         #if (BOARD == BOARD_LEONARDO) 
  69                 /* Enable pull-up on the IO13 pin so we can use it to select the mode */ 
  73                 /* If IO13 is not jumpered to ground, start the user application instead */ 
  74                 JumpToApplication 
|= ((PINC 
& (1 << 7)) != 0); 
  76                 /* Disable pull-up after the check has completed */ 
  78         #elif ((BOARD == BOARD_XPLAIN) || (BOARD == BOARD_XPLAIN_REV1)) 
  79                 /* Disable JTAG debugging */ 
  82                 /* Enable pull-up on the JTAG TCK pin so we can use it to select the mode */ 
  86                 /* If the TCK pin is not jumpered to ground, start the user application instead */ 
  87                 JumpToApplication 
|= ((PINF 
& (1 << 4)) != 0); 
  89                 /* Re-enable JTAG debugging */ 
  93         if (JumpToApplication
) 
  95                 // cppcheck-suppress constStatement 
  96                 ((void (*)(void))0x0000)(); 
 100 /** Main program entry point. This routine configures the hardware required by the application, then 
 101  *  enters a loop to run the application tasks in sequence. 
 107         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
); 
 108         GlobalInterruptEnable(); 
 112                 MS_Device_USBTask(&Disk_MS_Interface
); 
 117 /** Configures the board hardware and chip peripherals for the demo's functionality. */ 
 118 static void SetupHardware(void) 
 120         /* Disable watchdog if enabled by bootloader/fuses */ 
 121         MCUSR 
&= ~(1 << WDRF
); 
 124         /* Disable clock division */ 
 125         clock_prescale_set(clock_div_1
); 
 127         /* Relocate the interrupt vector table to the bootloader section */ 
 129         MCUCR 
= (1 << IVSEL
); 
 131         /* Hardware Initialization */ 
 135         /* Bootloader active LED toggle timer initialization */ 
 136         TIMSK1 
= (1 << TOIE1
); 
 137         TCCR1B 
= ((1 << CS11
) | (1 << CS10
)); 
 140 /** ISR to periodically toggle the LEDs on the board to indicate that the bootloader is active. */ 
 141 ISR(TIMER1_OVF_vect
, ISR_BLOCK
) 
 143         LEDs_ToggleLEDs(LEDS_LED1 
| LEDS_LED2
); 
 146 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs. */ 
 147 void EVENT_USB_Device_Connect(void) 
 149         /* Indicate USB enumerating */ 
 150         LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
); 
 153 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via 
 154  *  the status LEDs and stops the Mass Storage management task. 
 156 void EVENT_USB_Device_Disconnect(void) 
 158         /* Indicate USB not ready */ 
 159         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
); 
 162 /** Event handler for the library USB Configuration Changed event. */ 
 163 void EVENT_USB_Device_ConfigurationChanged(void) 
 165         bool ConfigSuccess 
= true; 
 167         /* Setup Mass Storage Data Endpoints */ 
 168         ConfigSuccess 
&= MS_Device_ConfigureEndpoints(&Disk_MS_Interface
); 
 170         /* Indicate endpoint configuration success or failure */ 
 171         LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY 
: LEDMASK_USB_ERROR
); 
 174 /** Event handler for the library USB Control Request reception event. */ 
 175 void EVENT_USB_Device_ControlRequest(void) 
 177         MS_Device_ProcessControlRequest(&Disk_MS_Interface
); 
 180 /** Mass Storage class driver callback function the reception of SCSI commands from the host, which must be processed. 
 182  *  \param[in] MSInterfaceInfo  Pointer to the Mass Storage class interface configuration structure being referenced 
 184 bool CALLBACK_MS_Device_SCSICommandReceived(USB_ClassInfo_MS_Device_t
* const MSInterfaceInfo
) 
 188         LEDs_SetAllLEDs(LEDMASK_USB_BUSY
); 
 189         CommandSuccess 
= SCSI_DecodeSCSICommand(MSInterfaceInfo
); 
 190         LEDs_SetAllLEDs(LEDMASK_USB_READY
); 
 192         return CommandSuccess
;