3      Copyright (C) Dean Camera, 2011. 
   5   dean [at] fourwalledcubicle [dot] com 
  10   Copyright 2011  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 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 HID class bootloader. This file contains the complete bootloader logic. 
  36 #include "BootloaderHID.h" 
  38 /** Flag to indicate if the bootloader should be running, or should exit and allow the application code to run 
  39  *  via a soft reset. When cleared, the bootloader will abort, the USB interface will shut down and the application 
  40  *  started via a forced watchdog reset. 
  42 static bool RunBootloader 
= true; 
  44 /** Main program entry point. This routine configures the hardware required by the bootloader, then continuously  
  45  *  runs the bootloader processing routine until instructed to soft-exit. 
  49         /* Setup hardware required for the bootloader */ 
  52         /* Enable global interrupts so that the USB stack can function */ 
  58         /* Disconnect from the host - USB interface will be reset later along with the AVR */ 
  61         /* Enable the watchdog and force a timeout to reset the AVR */ 
  62         wdt_enable(WDTO_250MS
); 
  67 /** Configures all hardware required for the bootloader. */ 
  68 void SetupHardware(void) 
  70         /* Disable watchdog if enabled by bootloader/fuses */ 
  71         MCUSR 
&= ~(1 << WDRF
); 
  74         /* Relocate the interrupt vector table to the bootloader section */ 
  78         /* Initialize USB subsystem */ 
  82 /** Event handler for the USB_ConfigurationChanged event. This configures the device's endpoints ready 
  83  *  to relay data to and from the attached USB host. 
  85 void EVENT_USB_Device_ConfigurationChanged(void) 
  87         /* Setup HID Report Endpoint */ 
  88         Endpoint_ConfigureEndpoint(HID_IN_EPNUM
, EP_TYPE_INTERRUPT
, 
  89                                        ENDPOINT_DIR_IN
, HID_IN_EPSIZE
, 
  90                                    ENDPOINT_BANK_SINGLE
); 
  93 /** Event handler for the USB_ControlRequest event. This is used to catch and process control requests sent to 
  94  *  the device from the USB host before passing along unhandled control requests to the library for processing 
  97 void EVENT_USB_Device_ControlRequest(void) 
  99         /* Ignore any requests that aren't directed to the HID interface */ 
 100         if ((USB_ControlRequest
.bmRequestType 
& (CONTROL_REQTYPE_TYPE 
| CONTROL_REQTYPE_RECIPIENT
)) != 
 101             (REQTYPE_CLASS 
| REQREC_INTERFACE
)) 
 106         /* Process HID specific control requests */ 
 107         switch (USB_ControlRequest
.bRequest
) 
 109                 case HID_REQ_SetReport
: 
 110                         Endpoint_ClearSETUP(); 
 112                         /* Wait until the command has been sent by the host */ 
 113                         while (!(Endpoint_IsOUTReceived())); 
 115                         /* Read in the write destination address */ 
 116                         #if (FLASHEND > 0xFFFF) 
 117                         uint32_t PageAddress 
= ((uint32_t)Endpoint_Read_16_LE() << 8); 
 119                         uint16_t PageAddress 
= Endpoint_Read_16_LE(); 
 122                         /* Check if the command is a program page command, or a start application command */ 
 123                         #if (FLASHEND > 0xFFFF) 
 124                         if ((uint16_t)(PageAddress 
>> 8) == COMMAND_STARTAPPLICATION
) 
 126                         if (PageAddress 
== COMMAND_STARTAPPLICATION
) 
 129                                 RunBootloader 
= false; 
 133                                 /* Erase the given FLASH page, ready to be programmed */ 
 134                                 boot_page_erase(PageAddress
); 
 135                                 boot_spm_busy_wait(); 
 137                                 /* Write each of the FLASH page's bytes in sequence */ 
 138                                 for (uint8_t PageWord 
= 0; PageWord 
< (SPM_PAGESIZE 
/ 2); PageWord
++)                            
 140                                         /* Check if endpoint is empty - if so clear it and wait until ready for next packet */ 
 141                                         if (!(Endpoint_BytesInEndpoint())) 
 144                                                 while (!(Endpoint_IsOUTReceived())); 
 147                                         /* Write the next data word to the FLASH page */ 
 148                                         boot_page_fill(PageAddress 
+ ((uint16_t)PageWord 
<< 1), Endpoint_Read_16_LE()); 
 151                                 /* Write the filled FLASH page to memory */ 
 152                                 boot_page_write(PageAddress
); 
 153                                 boot_spm_busy_wait(); 
 155                                 /* Re-enable RWW section */ 
 161                         Endpoint_ClearStatusStage();