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