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