3      Copyright (C) Dean Camera, 2009. 
   5   dean [at] fourwalledcubicle [dot] com 
   6       www.fourwalledcubicle.com 
  10   Copyright 2009  Dean Camera (dean [at] fourwalledcubicle [dot] com) 
  12   Permission to use, copy, modify, and distribute this software 
  13   and its documentation for any purpose and without fee is hereby 
  14   granted, provided that the above copyright notice appear in all 
  15   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  *  Header file for BootloaderDFU.c. 
  36 #ifndef _BOOTLOADER_H_ 
  37 #define _BOOTLOADER_H_ 
  43                 #include <avr/eeprom.h> 
  46                 #include "Descriptors.h" 
  48                 #include <LUFA/Drivers/USB/USB.h>                // USB Functionality 
  51                 /** Major bootloader version number. */ 
  52                 #define BOOTLOADER_VERSION_MINOR 2 
  54                 /** Minor bootloader version number. */ 
  55                 #define BOOTLOADER_VERSION_REV   0 
  57                 /** Complete bootloder version number expressed as a packed byte, constructed from the  
  58                  *  two individual bootloader version macros. 
  60                 #define BOOTLOADER_VERSION       ((BOOTLOADER_VERSION_MINOR << 4) | BOOTLOADER_VERSION_REV) 
  62                 /** First byte of the bootloader identification bytes, used to identify a device's bootloader. */ 
  63                 #define BOOTLOADER_ID_BYTE1      0xDC 
  65                 /** Second byte of the bootloader identification bytes, used to identify a device's bootloader. */ 
  66                 #define BOOTLOADER_ID_BYTE2      0xFB 
  68                 /** Convenience macro, used to determine if the issued command is the given one-byte long command. 
  70                  *  \param dataarr  Command byte array to check against 
  71                  *  \param cb1      First command byte to check 
  73                 #define IS_ONEBYTE_COMMAND(dataarr, cb1)       (dataarr[0] == cb1) 
  75                 /** Convenience macro, used to determine if the issued command is the given two-byte long command. 
  77                  *  \param dataarr  Command byte array to check against 
  78                  *  \param cb1      First command byte to check 
  79                  *  \param cb2      Second command byte to check 
  81                 #define IS_TWOBYTE_COMMAND(dataarr, cb1, cb2) ((dataarr[0] == cb1) && (dataarr[1] == cb2)) 
  83                 /** Length of the DFU file suffix block, appended to the end of each complete memory write command. 
  84                  *  The DFU file suffix is currently unused (but is designed to give extra file information, such as 
  85                  *  a CRC of the complete firmware for error checking) and so is discarded. 
  87                 #define DFU_FILE_SUFFIX_SIZE     16 
  89                 /** Length of the DFU file filler block, appended to the start of each complete memory write command. 
  90                  *  Filler bytes are added to the start of each complete memory write command, and must be discarded. 
  92                 #define DFU_FILLER_BYTES_SIZE    26 
  94                 /** DFU class command request to detatch from the host. */ 
  95                 #define DFU_DETATCH              0x00 
  97                 /** DFU class command request to send data from the host to the bootloader. */ 
  98                 #define DFU_DNLOAD               0x01 
 100                 /** DFU class command request to send data from the bootloader to the host. */ 
 101                 #define DFU_UPLOAD               0x02 
 103                 /** DFU class command request to get the current DFU status and state from the bootloader. */ 
 104                 #define DFU_GETSTATUS            0x03 
 106                 /** DFU class command request to reset the current DFU status and state variables to their defaults. */ 
 107                 #define DFU_CLRSTATUS            0x04 
 109                 /** DFU class command request to get the current DFU state of the bootloader. */ 
 110                 #define DFU_GETSTATE             0x05 
 112                 /** DFU class command request to abort the current multi-request transfer and return to the dfuIDLE state. */ 
 113                 #define DFU_ABORT                0x06 
 115                 /** DFU command to begin programming the device's memory. */ 
 116                 #define COMMAND_PROG_START       0x01 
 118                 /** DFU command to begin reading the device's memory. */ 
 119                 #define COMMAND_DISP_DATA        0x03 
 121                 /** DFU command to issue a write command. */ 
 122                 #define COMMAND_WRITE            0x04 
 124                 /** DFU command to issue a read command. */ 
 125                 #define COMMAND_READ             0x05 
 127                 /** DFU command to issue a memory base address change command, to set the current 64KB flash page 
 128                  *  that subsequent flash operations should use. */ 
 129                 #define COMMAND_CHANGE_BASE_ADDR 0x06 
 132                 /** Type define for a non-returning function pointer to the loaded application. */ 
 133                 typedef void (*AppPtr_t
)(void) ATTR_NO_RETURN
; 
 135                 /** Type define for a strucuture containing a complete DFU command issued by the host. */ 
 138                         uint8_t  Command
; /**< Single byte command to perform, one of the COMMAND_* macro values */ 
 139                         uint8_t  Data
[5]; /**< Command parameters */ 
 140                         uint16_t DataSize
; /**< Size of the command parameters */ 
 144                 /** DFU bootloader states. Refer to the DFU class specification for information on each state. */ 
 153                         dfuMANIFEST_SYNC             
= 6, 
 155                         dfuMANIFEST_WAIT_RESET       
= 8, 
 160                 /** DFU command status error codes. Refer to the DFU class specification for information on each error code. */ 
 181         /* Event Handlers: */ 
 182                 /** Indicates that this module will catch the USB_Disconnect event when thrown by the library. */ 
 183                 HANDLES_EVENT(USB_Disconnect
); 
 185                 /** Indicates that this module will catch the USB_UnhandledControlPacket event when thrown by the library. */ 
 186                 HANDLES_EVENT(USB_UnhandledControlPacket
); 
 188         /* Function Prototypes: */ 
 189                 #if defined(INCLUDE_FROM_BOOTLOADER_C) 
 190                         static void DiscardFillerBytes(uint8_t NumberOfBytes
); 
 191                         static void ProcessBootloaderCommand(void); 
 192                         static void LoadStartEndAddresses(void); 
 193                         static void ProcessMemProgCommand(void); 
 194                         static void ProcessMemReadCommand(void); 
 195                         static void ProcessWriteCommand(void); 
 196                         static void ProcessReadCommand(void);