Increase timeout of Mass Storage and Still Image host commands to 10 seconds (up...
[pub/USBasp.git] / Bootloaders / DFU / BootloaderDFU.h
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
20
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
28 this software.
29 */
30
31 /** \file
32 *
33 * Header file for BootloaderDFU.c.
34 */
35
36 #ifndef _BOOTLOADER_H_
37 #define _BOOTLOADER_H_
38
39 /* Includes: */
40 #include <avr/io.h>
41 #include <avr/wdt.h>
42 #include <avr/boot.h>
43 #include <avr/pgmspace.h>
44 #include <avr/eeprom.h>
45 #include <avr/power.h>
46 #include <stdbool.h>
47
48 #include "Descriptors.h"
49
50 #include <LUFA/Drivers/USB/USB.h>
51
52 /* Macros: */
53 /** Configuration define. Define this token to true to case the bootloader to reject all memory commands
54 * until a memory erase has been performed. When used in conjunction with the lockbits of the AVR, this
55 * can protect the AVR's firmware from being dumped from a secured AVR. When false, memory operations are
56 * allowed at any time.
57 */
58 #define SECURE_MODE false
59
60 /** Major bootloader version number. */
61 #define BOOTLOADER_VERSION_MINOR 2
62
63 /** Minor bootloader version number. */
64 #define BOOTLOADER_VERSION_REV 0
65
66 /** Complete bootloader version number expressed as a packed byte, constructed from the
67 * two individual bootloader version macros.
68 */
69 #define BOOTLOADER_VERSION ((BOOTLOADER_VERSION_MINOR << 4) | BOOTLOADER_VERSION_REV)
70
71 /** First byte of the bootloader identification bytes, used to identify a device's bootloader. */
72 #define BOOTLOADER_ID_BYTE1 0xDC
73
74 /** Second byte of the bootloader identification bytes, used to identify a device's bootloader. */
75 #define BOOTLOADER_ID_BYTE2 0xFB
76
77 /** Convenience macro, used to determine if the issued command is the given one-byte long command.
78 *
79 * \param[in] dataarr Command byte array to check against
80 * \param[in] cb1 First command byte to check
81 */
82 #define IS_ONEBYTE_COMMAND(dataarr, cb1) (dataarr[0] == (cb1))
83
84 /** Convenience macro, used to determine if the issued command is the given two-byte long command.
85 *
86 * \param[in] dataarr Command byte array to check against
87 * \param[in] cb1 First command byte to check
88 * \param[in] cb2 Second command byte to check
89 */
90 #define IS_TWOBYTE_COMMAND(dataarr, cb1, cb2) ((dataarr[0] == (cb1)) && (dataarr[1] == (cb2)))
91
92 /** Length of the DFU file suffix block, appended to the end of each complete memory write command.
93 * The DFU file suffix is currently unused (but is designed to give extra file information, such as
94 * a CRC of the complete firmware for error checking) and so is discarded.
95 */
96 #define DFU_FILE_SUFFIX_SIZE 16
97
98 /** Length of the DFU file filler block, appended to the start of each complete memory write command.
99 * Filler bytes are added to the start of each complete memory write command, and must be discarded.
100 */
101 #define DFU_FILLER_BYTES_SIZE 26
102
103 /** DFU class command request to detach from the host. */
104 #define DFU_DETATCH 0x00
105
106 /** DFU class command request to send data from the host to the bootloader. */
107 #define DFU_DNLOAD 0x01
108
109 /** DFU class command request to send data from the bootloader to the host. */
110 #define DFU_UPLOAD 0x02
111
112 /** DFU class command request to get the current DFU status and state from the bootloader. */
113 #define DFU_GETSTATUS 0x03
114
115 /** DFU class command request to reset the current DFU status and state variables to their defaults. */
116 #define DFU_CLRSTATUS 0x04
117
118 /** DFU class command request to get the current DFU state of the bootloader. */
119 #define DFU_GETSTATE 0x05
120
121 /** DFU class command request to abort the current multi-request transfer and return to the dfuIDLE state. */
122 #define DFU_ABORT 0x06
123
124 /** DFU command to begin programming the device's memory. */
125 #define COMMAND_PROG_START 0x01
126
127 /** DFU command to begin reading the device's memory. */
128 #define COMMAND_DISP_DATA 0x03
129
130 /** DFU command to issue a write command. */
131 #define COMMAND_WRITE 0x04
132
133 /** DFU command to issue a read command. */
134 #define COMMAND_READ 0x05
135
136 /** DFU command to issue a memory base address change command, to set the current 64KB flash page
137 * that subsequent flash operations should use. */
138 #define COMMAND_CHANGE_BASE_ADDR 0x06
139
140 /* Type Defines: */
141 /** Type define for a non-returning function pointer to the loaded application. */
142 typedef void (*AppPtr_t)(void) ATTR_NO_RETURN;
143
144 /** Type define for a structure containing a complete DFU command issued by the host. */
145 typedef struct
146 {
147 uint8_t Command; /**< Single byte command to perform, one of the COMMAND_* macro values */
148 uint8_t Data[5]; /**< Command parameters */
149 uint16_t DataSize; /**< Size of the command parameters */
150 } DFU_Command_t;
151
152 /* Enums: */
153 /** DFU bootloader states. Refer to the DFU class specification for information on each state. */
154 enum DFU_State_t
155 {
156 appIDLE = 0,
157 appDETACH = 1,
158 dfuIDLE = 2,
159 dfuDNLOAD_SYNC = 3,
160 dfuDNBUSY = 4,
161 dfuDNLOAD_IDLE = 5,
162 dfuMANIFEST_SYNC = 6,
163 dfuMANIFEST = 7,
164 dfuMANIFEST_WAIT_RESET = 8,
165 dfuUPLOAD_IDLE = 9,
166 dfuERROR = 10
167 };
168
169 /** DFU command status error codes. Refer to the DFU class specification for information on each error code. */
170 enum DFU_Status_t
171 {
172 OK = 0,
173 errTARGET = 1,
174 errFILE = 2,
175 errWRITE = 3,
176 errERASE = 4,
177 errCHECK_ERASED = 5,
178 errPROG = 6,
179 errVERIFY = 7,
180 errADDRESS = 8,
181 errNOTDONE = 9,
182 errFIRMWARE = 10,
183 errVENDOR = 11,
184 errUSBR = 12,
185 errPOR = 13,
186 errUNKNOWN = 14,
187 errSTALLEDPKT = 15
188 };
189
190 /* Function Prototypes: */
191 void SetupHardware(void);
192 void ResetHardware(void);
193
194 void EVENT_USB_Device_UnhandledControlRequest(void);
195
196 #if defined(INCLUDE_FROM_BOOTLOADER_C)
197 static void DiscardFillerBytes(uint8_t NumberOfBytes);
198 static void ProcessBootloaderCommand(void);
199 static void LoadStartEndAddresses(void);
200 static void ProcessMemProgCommand(void);
201 static void ProcessMemReadCommand(void);
202 static void ProcessWriteCommand(void);
203 static void ProcessReadCommand(void);
204 #endif
205
206 #endif