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