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