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