3      Copyright (C) Dean Camera, 2014. 
   5   dean [at] fourwalledcubicle [dot] com 
  10   Copyright 2014  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  *  Header file for BootloaderDFU.c. 
  36 #ifndef _BOOTLOADER_H_ 
  37 #define _BOOTLOADER_H_ 
  43                 #include <avr/pgmspace.h> 
  44                 #include <avr/eeprom.h> 
  45                 #include <avr/power.h> 
  46                 #include <avr/interrupt.h> 
  47                 #include <util/delay.h> 
  50                 #include "Descriptors.h" 
  51                 #include "BootloaderAPI.h" 
  52                 #include "Config/AppConfig.h" 
  54                 #include <LUFA/Drivers/USB/USB.h> 
  55                 #include <LUFA/Drivers/Board/LEDs.h> 
  56                 #include <LUFA/Platform/Platform.h> 
  58         /* Preprocessor Checks: */ 
  59                 #if !defined(__OPTIMIZE_SIZE__) 
  60                         #error This bootloader requires that it be optimized for size, not speed, to fit into the target device. Change optimization settings and try again. 
  64                 /** Major bootloader version number. */ 
  65                 #define BOOTLOADER_VERSION_MINOR 2 
  67                 /** Minor bootloader version number. */ 
  68                 #define BOOTLOADER_VERSION_REV   0 
  70                 /** Magic bootloader key to unlock forced application start mode. */ 
  71                 #define MAGIC_BOOT_KEY           0xDC42 
  73                 /** Complete bootloader version number expressed as a packed byte, constructed from the 
  74                  *  two individual bootloader version macros. 
  76                 #define BOOTLOADER_VERSION       ((BOOTLOADER_VERSION_MINOR << 4) | BOOTLOADER_VERSION_REV) 
  78                 /** First byte of the bootloader identification bytes, used to identify a device's bootloader. */ 
  79                 #define BOOTLOADER_ID_BYTE1      0xDC 
  81                 /** Second byte of the bootloader identification bytes, used to identify a device's bootloader. */ 
  82                 #define BOOTLOADER_ID_BYTE2      0xFB 
  84                 /** Convenience macro, used to determine if the issued command is the given one-byte long command. 
  86                  *  \param[in] dataarr  Command byte array to check against 
  87                  *  \param[in] cb1      First command byte to check 
  89                 #define IS_ONEBYTE_COMMAND(dataarr, cb1)       (dataarr[0] == (cb1)) 
  91                 /** Convenience macro, used to determine if the issued command is the given two-byte long command. 
  93                  *  \param[in] dataarr  Command byte array to check against 
  94                  *  \param[in] cb1      First command byte to check 
  95                  *  \param[in] cb2      Second command byte to check 
  97                 #define IS_TWOBYTE_COMMAND(dataarr, cb1, cb2) ((dataarr[0] == (cb1)) && (dataarr[1] == (cb2))) 
  99                 /** Length of the DFU file suffix block, appended to the end of each complete memory write command. 
 100                  *  The DFU file suffix is currently unused (but is designed to give extra file information, such as 
 101                  *  a CRC of the complete firmware for error checking) and so is discarded. 
 103                 #define DFU_FILE_SUFFIX_SIZE     16 
 105                 /** Length of the DFU file filler block, appended to the start of each complete memory write command. 
 106                  *  Filler bytes are added to the start of each complete memory write command, and must be discarded. 
 108                 #define DFU_FILLER_BYTES_SIZE    26 
 110                 /** DFU class command request to detach from the host. */ 
 111                 #define DFU_REQ_DETATCH          0x00 
 113                 /** DFU class command request to send data from the host to the bootloader. */ 
 114                 #define DFU_REQ_DNLOAD           0x01 
 116                 /** DFU class command request to send data from the bootloader to the host. */ 
 117                 #define DFU_REQ_UPLOAD           0x02 
 119                 /** DFU class command request to get the current DFU status and state from the bootloader. */ 
 120                 #define DFU_REQ_GETSTATUS        0x03 
 122                 /** DFU class command request to reset the current DFU status and state variables to their defaults. */ 
 123                 #define DFU_REQ_CLRSTATUS        0x04 
 125                 /** DFU class command request to get the current DFU state of the bootloader. */ 
 126                 #define DFU_REQ_GETSTATE         0x05 
 128                 /** DFU class command request to abort the current multi-request transfer and return to the dfuIDLE state. */ 
 129                 #define DFU_REQ_ABORT            0x06 
 131                 /** DFU command to begin programming the device's memory. */ 
 132                 #define COMMAND_PROG_START       0x01 
 134                 /** DFU command to begin reading the device's memory. */ 
 135                 #define COMMAND_DISP_DATA        0x03 
 137                 /** DFU command to issue a write command. */ 
 138                 #define COMMAND_WRITE            0x04 
 140                 /** DFU command to issue a read command. */ 
 141                 #define COMMAND_READ             0x05 
 143                 /** DFU command to issue a memory base address change command, to set the current 64KB flash page 
 144                  *  that subsequent flash operations should use. */ 
 145                 #define COMMAND_CHANGE_BASE_ADDR 0x06 
 148                 /** Type define for a non-returning function pointer to the loaded application. */ 
 149                 typedef void (*AppPtr_t
)(void) ATTR_NO_RETURN
; 
 151                 /** Type define for a structure containing a complete DFU command issued by the host. */ 
 154                         uint8_t  Command
; /**< Single byte command to perform, one of the \c COMMAND_* macro values */ 
 155                         uint8_t  Data
[5]; /**< Command parameters */ 
 156                         uint16_t DataSize
; /**< Size of the command parameters */ 
 160                 /** DFU bootloader states. Refer to the DFU class specification for information on each state. */ 
 169                         dfuMANIFEST_SYNC             
= 6, 
 171                         dfuMANIFEST_WAIT_RESET       
= 8, 
 176                 /** DFU command status error codes. Refer to the DFU class specification for information on each error code. */ 
 197         /* Function Prototypes: */ 
 198                 static void SetupHardware(void); 
 199                 static void ResetHardware(void); 
 201                 void EVENT_USB_Device_ControlRequest(void); 
 203                 #if defined(INCLUDE_FROM_BOOTLOADER_C) 
 204                         static void DiscardFillerBytes(uint8_t NumberOfBytes
); 
 205                         static void ProcessBootloaderCommand(void); 
 206                         static void LoadStartEndAddresses(void); 
 207                         static void ProcessMemProgCommand(void); 
 208                         static void ProcessMemReadCommand(void); 
 209                         static void ProcessWriteCommand(void); 
 210                         static void ProcessReadCommand(void); 
 213                 void Application_Jump_Check(void) ATTR_INIT_SECTION(3);