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