Debian Codegraph @

src/dfu-device.h

raw
1 #ifndef __DFU_DEVICE_H__
2 #define __DFU_DEVICE_H__
3
4 #include <stdint.h>
5 #include <libusb-1.0/libusb.h>
6
7 #ifdef __cplusplus
8 extern "C" {
9 #endif
10
11 // Atmel device classes are now defined with one bit per class.
12 // This simplifies checking in functions which handle more than one class.
13 #define ADC_8051 (1<<0)
14 #define ADC_AVR (1<<1)
15 #define ADC_AVR32 (1<<2)
16 #define ADC_XMEGA (1<<3)
17 #define DC_STM32 (1<<4)
18
19 // Most commands fall into one of 2 groups.
20 #define GRP_AVR32 (ADC_AVR32 | ADC_XMEGA)
21 #define GRP_AVR (ADC_AVR | ADC_8051)
22 #define GRP_STM32 (DC_STM32)
23
24 typedef unsigned atmel_device_class_t;
25
26 typedef struct {
27 struct libusb_device_handle *handle;
28 int32_t interface;
29 atmel_device_class_t type;
30 int security_bit_state;
31 uint16_t transaction;
32 } dfu_device_t;
33
34 #ifdef __cplusplus
35 }
36 #endif
37
38 #endif /* __DFU_DEVICE_H__ */
39
40 /******************* S T M 3 2 D F U C O M M A N D S ******************
41 ========= USB DFU Bootloader Requests ==============
42 DFU_DETACH - [ 0x00 ] wTimeout interface zero none
43 > Requests the device to leave DFU mode and enter the application.
44 NOTE : The Detach request is not meaningful in the case of the bootloader.
45 The bootloader is started by a system reset depending on the boot
46 mode configuration settings, which means that no other application
47 is running at this time.
48 DFU_DNLOAD - [ 0x01 ] wBlockNum interface length firmware
49 > Requests data transfer from Host to the device in order to load them
50 into device internal Flash. Includes also erase commands.
51 DFU_UPLOAD - [ 0x02 ] zero interface length firmware
52 > Requests data transfer from device to Host in order to load content of
53 device internal Flash into a Host file.
54 DFU_GETSTATUS - [ 0x03 ] zero interface 6 status
55 > Requests device to send status report to the Host (including status
56 resulting from the last request execution and the state the device will
57 enter immediately after this request).
58 DFU_CLRSTATUS - [ 0x04 ] zero interface zero none
59 > Requests device to clear error status and move to next step.
60 DFU_GETSTATE - [ 0x05 ] zero interface 1 state
61 > Requests the device to send only the state it will enter immediately
62 after this request.
63 DFU_ABORT - [ 0x06 ] zero interface zero none
64 > Requests device to exit the current state/operation and enter idle
65 state immediately.
66
67 ========= STM32 DFU Commands ==============
68 . DFU_UPLOAD DFU_DNLOAD cmd
69 . wValue wValue first byte
70 READ MEMORY > 1
71 GET 0
72 WRITE MEMORY > 1
73 ERASE 0 0x41
74 READ UNPROTECT 0 0x92
75 SET ADDRESS POINTER 0 0x21
76 LEAVE DFU MODE 0 -- (0 data length)
77
78 NOTES
79 > before issuing a DNLOAD request the host should check that the device
80 is in a correct sate dfuIDLE or dfuDNLOAD-IDLE and that there are no
81 errors. if the device is not in the correct state a DFU_CLRSTATUS must
82 be called
83 > after a command is sent using DFU_DNLOAD, DFU_GETSTATUS must be called
84 to trigger command execution. a second DFU_GETSTATUS is required to
85 check for success (except when writing to the option bytes because device
86 resets immediately after write)
87
88 READ COMMAND DFU_UPLOAD with wValue > 1
89 > host request device send a specified number of data bytes (wLength)
90 where wLength can be 2 to 2048 bytes, for option bytes read size should
91 be the option byte block size
92 > the address to read data from is computed using wBlockNumber (wValue)
93 and the address pointer using the formula:
94 address = ((wBlockNum - 2) x wTransferSize) + Address_Pointer
95 where: wTransferSize is the length of the requested data buffer
96 > the address should be previously specified using SET ADDRESS POINTER
97 otherwise the start of internal flash is assumed (0x0800000)
98
99 GET COMMAND DFU_UPLOAD with wValue = 0
100 > host sends DFU_UPLOAD command with wLength = 0, the device will return
101 N bytes representing the command codes where N = 4
102
103 WRITE COMMAND DFU_DNLOAD with wValue > 1
104 > in a write memory operation the write size can be from 2 to 2048 bytes
105 and the start address must be valid. when writing the option bytes the
106 start address must be the start of the option byte area and the size
107 should cover the entire option byte area
108 > the address is computed using the value of the wBlockNumber (wValue) and
109 the address pointer according the formula:
110 address = ((wBlockNum - 2) x wTransferSize) + Address_Pointer
111 where: wTransferSize is the length of the buffer sent by host
112 wBlockNumber is the value of wValue parameter
113
114 SET ADDRESS POINTER - [ 0x21, mem ptr LSB A[7:0], A[8:15], A[23:16], A[31:24] ]
115 > wValue should be zero
116 > after sending the command host must send a DFU_GETSTATUS in order to
117 execute the command. a second DFU_GETSTATUS is needed to check if the
118 command was correctly executed
119
120 ERASE COMMAND - [ 0x41, page LSB A[7:0], A[15:8], A[23:16], A[31:24] ]
121 MASS ERASE - [ 0x41 ]
122 > this can take a long time to execute so timeout should be long
123
124 READ UNPROTECT - [ 0x92 ]
125
126 LEAVE DFU MODE - DFU_DNLOAD with 0 data length
127 > device disconnect
128 > init registers of peripherals to default reset values
129 > initialize the user application main stack pointer
130 > jumps to memory location programmed in the received 'address pointer + 4'
131 which corresponds to the address of the application's reset handler
132
133 > the address pointer has to be set (using set address pointer) before
134 launching the LEAVE DFU command otherwise the bootloader will jump to the
135 default memory start address (0x0800000)
136
137 - see an2606: STM32 microcontroller system memory boot mode
138 - see an3156: USB DFU protocol used in the STM32 bootloader
139 **************************************************************************/
140
141
142 /******************* A T M E L D F U C O M M A N D S ******************
143 // ---- A L L D E V I C E S -----------------------------------
144 PROGRAM_MEMORY - [ 0x01, 0x00, start_MSB, start LSB, end_MSB, end_LSB ]
145 > send the command, start should align with page boundary
146 > send DFU_DNLOAD data, use non-significant bytes to align program start
147 > optionally request status: 0 OK, 8 bad address, 3 memory locked
148 > on all error DFU_CLRSTATUS
149 NOTE: batchISP/FLIP send buffers with max size 0x800 bytes including prefix
150
151 READ_MEMORY - [ 0x03, 0x00, start_MSB, start_LSB, end_MSB, end_LSB ]
152 > send the command
153 > send DFU_UPLOAD
154 > seems like you can request status, possibly only on GRP_AVR32
155 NOTE: batchISP/FLIP request buffers with max size of 0x400 (1kb)
156
157 BLANK_CHECK - [ 0x03, 0x01, start_MSB, start_LSB, end_MSB, end_LSB ]
158 > send the command
159 > request status: 0 OK, 5 not blank, 8 bad address, 3 memory locked
160 > on errCHECK_ERASED (5) send DFU_UPLOAD to get first non-blank address
161 > on errWRITE (3), errADDRESS (8) and other error DFU_CLRSTATUS
162
163 CHIP_ERASE - [ 0x04, 0x00, 0xFF ]
164 > send command
165 > optionally request status: 0 OK, 9 in progress 3 memory locked
166 > on errNOTDONE (9) may need to call CHIP_ERASE again (GRP_AVR)
167 > on errWRITE (3) and any other error DFU_CLRSTATUS
168
169 START_APP - [ 0x04, 0x03, 0x00/0x01 ]
170 > send command: 0x00 for start by reset, 0x01 for rjmp
171 for start by jmp GRP_AVR devices also provide [ MSB, LSB ] of jump address
172 > for start by reset device code must disable the watchdog timer
173 > no DFU_GETSTATUS
174
175 SELECT_64KB_PAGE - [ 0x06, 0x03, 0x01, PP_MSB, PP_LSB ]
176 > send command
177 > page (PP) is a 64kb memory page (0x10000 bytes), not the flash page
178 > optionally request status: 0 OK, 8 page value out of range
179 > on errADDRESS (8) and any other error DFU_CLRSTATUS
180 NOTE: not documented for AVR in (doc7618) but must be possible
181
182 // ---- G R O U P A V R 3 2 -----------------------------------
183 SELECT_MEMORY_UNIT - [ 0x06, 0x03, 0x00, unit_num ]
184 > send command
185 > unit_num [ flash, eeprom, security, config, bootloader, signature, user,
186 int_ram, ext_mem_cs0 - ext_mem_cs7, ext_mem_df ]
187 - XMEGA ONLY: eeprom (1) and all other sections above user (6)
188 > optionally request status: 0 OK, 8 unsupported memory
189 > on errADDRESS (8) and any other error DFU_CLRSTATUS
190
191 // ---- G R O U P A V R ---------------------------------------
192 PROGRAM_EEPROM - [ 0x01, 0x00, start_MSB, start_LSB, end_MSB, end_LSB ]
193 > send the command, start should align with page boundary
194 > send DFU_DNLOAD data
195 > optionally request status: 0 OK, all other unspecified error
196 > on all error DFU_CLRSTATUS
197 NOTE: batchISP/FLIP send buffers with max size 0x800 bytes including prefix
198
199 READ_EEPROM - [ 0x03, 0x02, start_MSB, start_LSB, end_MSB, end_LSB ]
200 > send the command
201 > send DFU_UPLOAD
202 NOTE: batchISP/FLIP request buffers with max size of 0x400 (1kb)
203
204 READ_CONFIG - [ 0x05, rnum0, rnum1 ]
205 > send command
206 > rnum [0x00, 0x00-0x02] enum boot version, boot ID1, boot ID2
207 > 8051 only: rnum [0x01, 0x00-6] enum BSB, SBV, P1_CF, P3_CF, P4_CF, SSB, EB
208 > rnum [0x01, 0x30-1, 0x60-1] map MFG_CODE, FAM_CODE, PROD_NAME, PROD_VER
209 > 8051 only: rnum [0x02, 0x00] fuse HWB
210 > optionally get status: 0 OK, 0xb chip is protected
211 > on OK send upload request to get value
212 > on errVENDOR (0xb) send DFU_CLRSTATUS
213
214 // ---- A D C 8 0 5 1 O N L Y -------------------------------
215 ERASE_BLOCK - [ 0x04, 0x00, block ]
216 > send the command
217 > block0 0x00 is memory from 0x0 to 0x1FFF
218 > block1 0x20 is memory from 0x2000 to 0x3FFF
219 > block2 0x20 is memory from 0x4000 to 0x7FFF
220 > optionally request status: 0 OK, 3 chip is protected
221 > on errWRITE (3) and all other error DFU_CLRSTATUS
222
223 SET_CONFIG - [0x04, rnum0, rnum1, value]
224 > send command
225 > rnum [0x01, 0x00-0x06] enum BSB, SBV, P1_CF, P3_CF, P4_CF, SSB, EB
226 > rnum [0x02, 0x00] for Fuse HSB
227 > value is the value to set
228 > optionally request DFU_GETSTATUS: 0 OK, 3 chip is protected
229 > on errWRITE (3) or any other error must DFU_CLRSTATUS
230
231 Notes:
232 - AT90/ATmega the first command must be a full chip erase, everything else
233 is locked until this command is issued
234 - GRP_AVR provide entry points for low level flash drivers like page erase,
235 page write, load word in the temporary page buffer (see doc7618)
236 - AT89 when starting application through reset, if an external reset chip is
237 make sure the reset pulse is correct (see doc4287)
238 - alignment of program with page boundaries: In order to be in accordance
239 with the Flash page size (128 bytes), X non-significant bytes are added
240 before the first byte to program. The X number is calculated to align the
241 beginning of the firmware with the Flash page. X = start_address[32]. For
242 example, if the start address is 00AFh (175d), X = 175 [32] = 15.
243
244 - see doc32131: AVR32 UC3 USB DFU Bootloader Protocol (AVR32)
245 - see doc7618: USB DFU Bootloader Datasheet (AT90/ATmega)
246 - see doc4287: AT89C5131A USB Bootloader (AT89)
247 - see doc8457: FLIP USB DFU Protocol (xmega)
248
249 **************************************************************************/