-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathSTM32_ISP.ino
317 lines (290 loc) · 7.12 KB
/
STM32_ISP.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#include <SoftwareSerial.h>
#include "FS.h"
SoftwareSerial debugSerial(13, 12, false, 256); //rx tx inverse buffer
#define ispSerial Serial
#define BLOCK_SIZE 128
#define START_KEY 0
#define BOOT0_PIN 14
#define RESET_PIN 16
#define BEGIN_ADDRESS 0x08000000
uint8_t memory_buffer[BLOCK_SIZE];
uint8_t start_key_pressed() {
if(digitalRead(START_KEY) == LOW) {
return 1;
} else {
return 0;
}
}
uint8_t reset_stm32_to_isp_mode() {
pinMode(RESET_PIN,OUTPUT);
pinMode(BOOT0_PIN,OUTPUT);
digitalWrite(BOOT0_PIN,HIGH);
delay(50);
digitalWrite(RESET_PIN,LOW);
delay(50);
digitalWrite(RESET_PIN,HIGH);
delay(50);
digitalWrite(BOOT0_PIN,LOW);
pinMode(BOOT0_PIN,INPUT);
pinMode(RESET_PIN,INPUT);
}
uint8_t reset_stm32_to_app_mode() {
pinMode(RESET_PIN,OUTPUT);
pinMode(BOOT0_PIN,OUTPUT);
digitalWrite(BOOT0_PIN,LOW);
delay(50);
digitalWrite(RESET_PIN,LOW);
delay(50);
digitalWrite(RESET_PIN,HIGH);
pinMode(BOOT0_PIN,INPUT);
pinMode(RESET_PIN,INPUT);
}
void hardware_init() {
pinMode(RESET_PIN,INPUT);
pinMode(BOOT0_PIN,INPUT);
pinMode(START_KEY,INPUT);
ispSerial.begin(57600,SERIAL_8E1);
ispSerial.setTimeout(5000);
debugSerial.begin(57600);
}
char log_buffer[128];
void debug_log() {
debugSerial.print(log_buffer);
}
uint8_t isp_serial_write(uint8_t *buffer,uint8_t length) {
return ispSerial.write(buffer,length);
}
uint8_t isp_serial_read(uint8_t *buffer,uint8_t length) {
return ispSerial.readBytes(buffer,length);
}
uint8_t isp_serial_flush() {
while(ispSerial.available() > 0) {
ispSerial.read();
}
return 1;
}
uint8_t wait_for_ack(char *when) {
uint8_t cmd[1] = {0};
uint8_t nread = isp_serial_read(cmd,1);
if(nread == 1) {
if(cmd[0] == 0x79) { // ack
return 1;
}
if(cmd[0] == 0x1F) { // nack
sprintf(log_buffer,"got nack when: %s\n",when);
debug_log();
return 2;
}
sprintf(log_buffer,"got unknown response: %d when: %s.\n",cmd[0],when);
debug_log();
}
sprintf(log_buffer,"no response when: %s.\n",when);
debug_log();
return 0;
}
uint8_t init_chip() {
uint8_t cmd[1];
cmd[0] = 0x7F;
for(int i=0;i<100;i++){
sprintf(log_buffer,"trying to init chip...\n");
debug_log();
isp_serial_write(cmd,1);
if(wait_for_ack("init_chip_write_cmd") > 0) { // ack or nack
sprintf(log_buffer,"init chip successed.\n");
debug_log();
isp_serial_flush();
return 1;
}
delay(50);
}
sprintf(log_buffer,"init chip failed.\n");
debug_log();
return 0;
}
uint8_t cmd_generic(uint8_t command) {
isp_serial_flush();
uint8_t cmd[2];
cmd[0] = command;
cmd[1] = command ^ 0xFF;
isp_serial_write(cmd,2);
return wait_for_ack("cmd_generic");
}
uint8_t cmd_get() {
if(cmd_generic(0x00)) {
uint8_t buffer[8];
isp_serial_read(buffer,2);
uint8_t len = buffer[0];
uint8_t ver = buffer[1];
sprintf(log_buffer,"version: %d\n",ver);
debug_log();
isp_serial_read(buffer,len);
return wait_for_ack("cmd_get");
}
return 0;
}
void encode_address(uint32_t address, uint8_t *result) {
uint8_t b3 = (uint8_t)((address >> 0) & 0xFF);
uint8_t b2 = (uint8_t)((address >> 8) & 0xFF);
uint8_t b1 = (uint8_t)((address >> 16) & 0xFF);
uint8_t b0 = (uint8_t)((address >> 24) & 0xFF);
uint8_t crc = (uint8_t)(b0 ^ b1 ^ b2 ^ b3);
result[0] = b0;
result[1] = b1;
result[2] = b2;
result[3] = b3;
result[4] = crc;
}
uint8_t cmd_read_memory(uint32_t address, uint8_t length) {
if(cmd_generic(0x11) == 1) {
uint8_t enc_address[5] = {0};
encode_address(address,enc_address);
isp_serial_write(enc_address,5);
if(wait_for_ack("write_address") != 1) {
return 0;
}
uint8_t nread = length - 1;
uint8_t crc = nread ^ 0xFF;
uint8_t buffer[2];
buffer[0] = nread;
buffer[1] = crc;
isp_serial_write(buffer,2);
if(wait_for_ack("write_address") != 1) {
return 0;
}
uint8_t nreaded = 0;
while(nreaded < length) {
nreaded += isp_serial_read(memory_buffer+nreaded, length - nreaded);
}
return 1;
}
return 0;
}
uint8_t cmd_write_memory(uint32_t address, uint8_t length) {
if(cmd_generic(0x31) == 1) {
uint8_t enc_address[5] = {0};
encode_address(address,enc_address);
isp_serial_write(enc_address,5);
if(wait_for_ack("write_address") != 1) {
return 0;
}
uint8_t nwrite = length - 1;
uint8_t buffer[1];
buffer[0] = nwrite;
isp_serial_write(buffer,1);
uint8_t crc = nwrite;
for(int i=0;i<length;i++){
crc = crc ^ memory_buffer[i];
}
isp_serial_write(memory_buffer,length);
buffer[0] = crc;
isp_serial_write(buffer,1);
return wait_for_ack("write_data");
}
return 0;
}
uint8_t cmd_erase_all_memory() {
if(cmd_generic(0x44) == 1) {
uint8_t cmd[3];
cmd[0] = 0xFF;
cmd[1] = 0xFF;
cmd[2] = 0x00;
isp_serial_write(cmd,3);
return wait_for_ack("erase_all_memory");
}
return 0;
}
uint8_t cmd_go(uint32_t address) {
if(cmd_generic(0x21) == 1) {
uint8_t enc_address[5] = {0};
encode_address(address,enc_address);
isp_serial_write(enc_address,5);
return wait_for_ack("cmd_go");
}
return 0;
}
uint8_t esp8266_spifs_write_file(char *filename) {
reset_stm32_to_isp_mode();
if(init_chip() != 1) {
return 0;
}
cmd_get();
sprintf(log_buffer,"erasing all memory...\n");
debug_log();
if(cmd_erase_all_memory() != 1) {
return 0;
}
SPIFFS.begin();
if(!SPIFFS.exists(filename)) {
sprintf(log_buffer,"file: %s not exists.\n",filename);
debug_log();
return 0;
}
File fp = SPIFFS.open(filename,"r");
uint32_t filesize = fp.size();
sprintf(log_buffer,"filesize: %d\n",filesize);
debug_log();
sprintf(log_buffer,"begin to write file.\n");
debug_log();
while(fp.position() < filesize) {
uint8_t nread = BLOCK_SIZE;
if(filesize - fp.position() < BLOCK_SIZE) {
nread = filesize - fp.position();
}
nread = fp.readBytes((char *)memory_buffer,nread);
sprintf(log_buffer,"read bytes: %d, file position: %d\n",nread, fp.position());
debug_log();
uint8_t result = cmd_write_memory(BEGIN_ADDRESS+fp.position() - nread,nread);
if(result != 1) {
sprintf(log_buffer,"file write failed.\n");
debug_log();
return 0;
}
}
sprintf(log_buffer,"file write successed.\n");
debug_log();
uint8_t file_buffer[BLOCK_SIZE];
fp.seek(0,SeekSet);
sprintf(log_buffer,"begin to verify file.\n");
debug_log();
while(fp.position() < filesize) {
uint8_t nread = BLOCK_SIZE;
if(filesize - fp.position() < BLOCK_SIZE) {
nread = filesize - fp.position();
}
nread = fp.readBytes((char *)file_buffer,nread);
sprintf(log_buffer,"read bytes: %d, file position: %d\n",nread, fp.position());
debug_log();
uint8_t result = cmd_read_memory(BEGIN_ADDRESS+fp.position() - nread, nread);
if(result != 1) {
sprintf(log_buffer,"read memory failed: %d\n",fp.position());
debug_log();
return 0;
}
result = memcmp(file_buffer,memory_buffer,nread);
if(result != 0) {
sprintf(log_buffer,"verify failed.\n");
debug_log();
return 0;
}
}
sprintf(log_buffer,"verify file successed.\n");
debug_log();
sprintf(log_buffer,"start application.\n");
debug_log();
//reset_stm32_to_app_mode();
cmd_go(BEGIN_ADDRESS);
return 1;
}
void setup() {
hardware_init();
delay(1000);
while(! start_key_pressed()) {
delay(1000);
sprintf(log_buffer,"press key to start.\n");
debug_log();
}
esp8266_spifs_write_file("/stm32f030c8_db_led.bin");
}
void loop() {
delay(100);
}