3      Copyright (C) Dean Camera, 2012. 
   5   dean [at] fourwalledcubicle [dot] com 
  10   Copyright 2012  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 /** Magic lock for forced application start. If the HWBE fuse is programmed and BOOTRST is unprogrammed, the bootloader 
  45  *  will start if the /HWB line of the AVR is held low and the system is reset. However, if the /HWB line is still held 
  46  *  low when the application attempts to start via a watchdog reset, the bootloader will re-start. If set to the value 
  47  *  \ref MAGIC_BOOT_KEY the special init function \ref Application_Jump_Check() will force the application to start. 
  49 uint32_t MagicBootKey ATTR_NO_INIT
; 
  52 /** Special startup routine to check if the bootloader was started via a watchdog reset, and if the magic application 
  53  *  start key has been loaded into \ref MagicBootKey. If the bootloader started via the watchdog and the key is valid, 
  54  *  this will force the user application to start via a software jump. 
  56 void Application_Jump_Check(void) 
  58         /* If the reset source was the bootloader and the key is correct, clear it and jump to the application */ 
  59         if ((MCUSR 
& (1 << WDRF
)) && (MagicBootKey 
== MAGIC_BOOT_KEY
)) 
  62                 // cppcheck-suppress constStatement 
  63                 ((void (*)(void))0x0000)(); 
  67 /** Main program entry point. This routine configures the hardware required by the bootloader, then continuously 
  68  *  runs the bootloader processing routine until instructed to soft-exit. 
  72         /* Setup hardware required for the bootloader */ 
  75         /* Enable global interrupts so that the USB stack can function */ 
  81         /* Disconnect from the host - USB interface will be reset later along with the AVR */ 
  84         /* Unlock the forced application start mode of the bootloader if it is restarted */ 
  85         MagicBootKey 
= MAGIC_BOOT_KEY
; 
  87         /* Enable the watchdog and force a timeout to reset the AVR */ 
  88         wdt_enable(WDTO_250MS
); 
  93 /** Configures all hardware required for the bootloader. */ 
  94 static void SetupHardware(void) 
  96         /* Disable watchdog if enabled by bootloader/fuses */ 
  97         MCUSR 
&= ~(1 << WDRF
); 
 100         /* Relocate the interrupt vector table to the bootloader section */ 
 102         MCUCR 
= (1 << IVSEL
); 
 104         /* Initialize USB subsystem */ 
 108 /** Event handler for the USB_ConfigurationChanged event. This configures the device's endpoints ready 
 109  *  to relay data to and from the attached USB host. 
 111 void EVENT_USB_Device_ConfigurationChanged(void) 
 113         /* Setup HID Report Endpoint */ 
 114         Endpoint_ConfigureEndpoint(HID_IN_EPADDR
, EP_TYPE_INTERRUPT
, HID_IN_EPSIZE
, 1); 
 117 /** Event handler for the USB_ControlRequest event. This is used to catch and process control requests sent to 
 118  *  the device from the USB host before passing along unhandled control requests to the library for processing 
 121 void EVENT_USB_Device_ControlRequest(void) 
 123         /* Ignore any requests that aren't directed to the HID interface */ 
 124         if ((USB_ControlRequest
.bmRequestType 
& (CONTROL_REQTYPE_TYPE 
| CONTROL_REQTYPE_RECIPIENT
)) != 
 125             (REQTYPE_CLASS 
| REQREC_INTERFACE
)) 
 130         /* Process HID specific control requests */ 
 131         switch (USB_ControlRequest
.bRequest
) 
 133                 case HID_REQ_SetReport
: 
 134                         Endpoint_ClearSETUP(); 
 136                         /* Wait until the command has been sent by the host */ 
 137                         while (!(Endpoint_IsOUTReceived())); 
 139                         /* Read in the write destination address */ 
 140                         #if (FLASHEND > 0xFFFF) 
 141                         uint32_t PageAddress 
= ((uint32_t)Endpoint_Read_16_LE() << 8); 
 143                         uint16_t PageAddress 
= Endpoint_Read_16_LE(); 
 146                         /* Check if the command is a program page command, or a start application command */ 
 147                         #if (FLASHEND > 0xFFFF) 
 148                         if ((uint16_t)(PageAddress 
>> 8) == COMMAND_STARTAPPLICATION
) 
 150                         if (PageAddress 
== COMMAND_STARTAPPLICATION
) 
 153                                 RunBootloader 
= false; 
 157                                 /* Erase the given FLASH page, ready to be programmed */ 
 158                                 boot_page_erase(PageAddress
); 
 159                                 boot_spm_busy_wait(); 
 161                                 /* Write each of the FLASH page's bytes in sequence */ 
 162                                 for (uint8_t PageWord 
= 0; PageWord 
< (SPM_PAGESIZE 
/ 2); PageWord
++) 
 164                                         /* Check if endpoint is empty - if so clear it and wait until ready for next packet */ 
 165                                         if (!(Endpoint_BytesInEndpoint())) 
 168                                                 while (!(Endpoint_IsOUTReceived())); 
 171                                         /* Write the next data word to the FLASH page */ 
 172                                         boot_page_fill(PageAddress 
+ ((uint16_t)PageWord 
<< 1), Endpoint_Read_16_LE()); 
 175                                 /* Write the filled FLASH page to memory */ 
 176                                 boot_page_write(PageAddress
); 
 177                                 boot_spm_busy_wait(); 
 179                                 /* Re-enable RWW section */ 
 185                         Endpoint_ClearStatusStage();