-
Notifications
You must be signed in to change notification settings - Fork 15
/
stm32.c
611 lines (522 loc) · 12.7 KB
/
stm32.c
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
/*
* stm32.c - implementing STM32F10x-ISP functions
* part of STM32ISP
* copyright (c) 2012 Kai ([email protected])
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <assert.h>
#include <stdlib.h>
#include "serial.h"
#include "stm32.h"
#include "memmap.h"
#define STM32_ACK 0x79
#define STM32_NACK 0x1F
/**
* maxim length of block for read/write
*/
#define MAXLEN 256
#define MAXTRY 4
#define TM_BOOTP 2000 // delay after change bootp
#define TM_RESET 200000 // delay after RESET down
typedef struct
{
int serial_fd;
// int error;
uint8_t version;
uint8_t cmd[16];
uint8_t pid[8];
// int flash;
// int ram;
int readprotect;
int writeprotect;
int bootp;
int reset;
}Stm32Info;
static Stm32Info board;
static int sendCommand(unsigned char byte);
static int waitAck();
static unsigned char *addrPacket(unsigned int addr, unsigned char *buffer);
static unsigned char calcChecksum(unsigned char* data, unsigned int len);
static int stm32WriteBlock(unsigned char *data, unsigned int len, unsigned int addr);
/**
* Init serial port and sync with the board
* @param port the serial port, for example: /dev/ttyS0 or COM1
* @param baudrate the bardrate used during the burning
* @return STM_OK if the port is inited and the board is connected;
* STM_ERR if the port can not be opened, or the board can not be connected
*/
int stm32Init(const char* port, unsigned int baudrate, int bootp, int reset)
{
int ret = STM_ERR;
board.bootp = bootp;
board.reset = reset;
ret = serialOpen(port, baudrate, 8, 'E', 1);
if ( ret>0 ) {
board.serial_fd = ret;
printf("Serial port %s open.\n", port);
ret = stm32Reboot2isp();
// ret = stm32Sync();
if ( ret == STM_ERR ) {
serialClose(board.serial_fd);
}
} else {
printf("Serial port %s open failed.\n", port);
}
return ret;
}
/**
* close serial port
*/
void stm32Close()
{
serialClose(board.serial_fd);
}
int stm32Reboot2isp()
{
int ret = STM_ERR;
int i;
for ( i=0; ret == STM_ERR && i< MAXTRY; i++ ) {
if ( board.bootp == RTS ) {
serialSetPin(board.serial_fd, TIOCM_RTS); //RTS->bootp
} else if ( board.bootp == -RTS ) {
serialResetPin(board.serial_fd, TIOCM_RTS); //-RTS->bootp
} if ( board.bootp == DTR ) {
serialSetPin(board.serial_fd, TIOCM_DTR); //DTR->bootp
} else if ( board.bootp == -DTR ) {
serialResetPin(board.serial_fd, TIOCM_DTR); //-DTR->bootp
}
usleep(TM_BOOTP);
printf("Reboot board to isp. ");
stm32Reboot(board.serial_fd, board.reset);
ret = stm32Sync();
// printf("ret=%d\n",ret);
}
return ret;
}
void stm32Reboot(int fd, int reset)
{
printf("Booting...\n");
if ( reset == RTS ) {
serialSetPin(fd, TIOCM_RTS); //RTS
} else if ( reset == -RTS ) {
serialResetPin(fd, TIOCM_RTS); //-RTS
} if ( reset == DTR ) {
serialSetPin(fd, TIOCM_DTR); //DTR
} else if ( reset == -DTR ) {
serialResetPin(fd, TIOCM_DTR); //-DTR
}
// serialSetPin(board.serial_fd, TIOCM_DTR);
usleep(TM_RESET);
if ( reset == RTS ) {
serialResetPin(fd, TIOCM_RTS); //RTS
} else if ( reset == -RTS ) {
serialSetPin(fd, TIOCM_RTS); //-RTS
} if (reset == DTR ) {
serialResetPin(fd, TIOCM_DTR); //DTR
} else if ( reset == -DTR ) {
serialSetPin(fd, TIOCM_DTR); //-DTR
}
// serialResetPin(board.serial_fd, TIOCM_DTR);
usleep(TM_RESET);
}
int stm32Sync()
{
int ret = STM_ERR;
static unsigned char STM32_INIT = 0x7F;
unsigned char r;
int rr;
int i;
printf("Sync");
fflush(stdout);
for ( i=0; i<MAXTRY; i++ ) {
printf(".");
fflush(stdout);
rr = serialWrite(board.serial_fd, &STM32_INIT, 1);
if ( rr == SER_OK ) {
serialFlush(board.serial_fd);
rr = serialRead(board.serial_fd, &r, sizeof(r));
if ( rr == SER_OK ) {
if ( r==STM32_ACK || r==STM32_NACK) {
ret = STM_OK;
break;
}
}
}
}
printf("\n");
if ( ret == STM_OK ) {
printf("Connected to board.\n");
} else {
printf("Can not connect to board, ");
if ( rr == SER_ERR ) {
printf(" failed.\n");
} else {
printf(" timed-out.\n");
}
}
return ret;
}
int stm32CmdGet()
{
int ret = STM_ERR;
if ( sendCommand(0x00)==STM_OK ) {
int i;
unsigned char num;
serialRead(board.serial_fd, &num, 1); // number of bytes
serialRead(board.serial_fd, &board.version, 1);
serialRead(board.serial_fd, board.cmd, num);
if ( waitAck()==STM_OK ) {
printf("Bootloader version: %x.%x\nCommand set-[", board.version >> 4, board.version & 0x0F);
for ( i=0; i < num; i++ )
printf(" %02X", board.cmd[i]);
printf("]\n");
ret = STM_OK;
}
}
if ( ret == STM_ERR ) {
printf("STM32-Get: command error!\n");
}
return ret;
}
int stm32GetVersion()
{
int ret = STM_ERR;
if ( sendCommand(0x01) == STM_OK ) {
unsigned char buf[3];
serialRead(board.serial_fd, buf, 3);
printf("Bootloader version: %x.%x\n", buf[0] >> 4, buf[0] & 0x0F);
printf("%02X-%02X\n", buf[1], buf[2]);
if ( waitAck() == STM_OK ) {
ret = STM_OK;
}
}
if ( ret == STM_ERR ) {
printf("STM32-GetVersion: command error!\n");
}
return ret;
}
int stm32GetID()
{
int ret = STM_ERR;
if (sendCommand(0x02) == STM_OK ) {
unsigned char num;
int i;
serialRead(board.serial_fd, &num, 1);
serialRead(board.serial_fd, board.pid, num+1);
printf("Product ID=");
for ( i=0; i<num+1; i++ ) {
printf("%02X", board.pid[i]);
}
printf("\n");
if ( waitAck() == STM_OK ) {
ret = STM_OK;
}
}
if ( ret == STM_ERR ) {
printf("Command-getID: command error!\n");
}
return ret;
}
/**
* Read from the chip.
* It read data from anywhere in the chip: falsh or ram. The max length can be read is 256-byte.
*/
int stm32Read(unsigned char *data, unsigned int len, unsigned int addr)
{
int ret = STM_ERR;
unsigned char addr_buffer[5];
// printf("STM32-Read: %08X(%d)\n", addr, len);
if ( sendCommand(0x11) == STM_OK ) {
serialWrite(board.serial_fd, addrPacket(addr, addr_buffer), 5);
if ( waitAck() == STM_OK ) {
if ( sendCommand(len - 1) == STM_OK ) {
if ( serialRead(board.serial_fd, data, len) == STM_OK) {
ret = STM_OK;
} else {
printf("STM32-Read: data error!\n");
}
} else {
printf("STM32-Read: length error!\n");
}
} else {
printf("STM32-Read: address error!\n");
}
} else {
printf("STM32-Read: command error!\n");
}
return ret;
}
int stm32Verify(MemMap* mm)
{
int ret = STM_ERR;
MemBlock* pBlock;
printf("STM-32 Verifying\n");
for ( pBlock = mm->head; pBlock; pBlock = pBlock->next ) {
unsigned int address = pBlock->address;
unsigned int loc = 0;
unsigned char buf[MAXLEN];
printf("%08X:", address);
fflush(stdout);
while ( address+loc < pBlock->lastValid ) {
int size = pBlock->lastValid - (address + loc);
// printf("address=%08X, loc=%d\n", address, loc);
if ( size > MAXLEN)
size = MAXLEN;
if ( stm32Read(buf, size, address+loc) == STM_OK ) {
int i;
for ( i=0; i<size; i++ ) {
if ( pBlock->data[loc] != buf[i] ) {
printf("\nSTM32-Verify: not match at %08X:data=%02X,chip=%02X.\n", address+loc, pBlock->data[loc], buf[i]);
goto EXIT;
}
loc++;
}
printf(".");fflush(stdout);
} else {
printf("\nSTM32-Verify: can not read!\n");
goto EXIT;
}
}
printf("\n");
}
printf("STM32 Verity success.\n");
EXIT:
return ret;
}
int stm32Erase()
{
int ret = STM_ERR;
unsigned char buffer[] = {0xFF, 0xFF, 0x00};
printf("\nSTM32-Erase: %X\n", board.cmd[6]);
if ( board.cmd[6] == 0x43 ) {
if ( sendCommand(0x43) == STM_OK ) {
if ( sendCommand(0xFF) == STM_OK ) {
ret = STM_OK;
} else {
printf("STM32-GlobalErase: fail\n");
}
} else {
printf("STM32-Erase: fail\n");
}
} else {
if ( sendCommand(0x44) == STM_OK ) {
serialWrite(board.serial_fd, buffer, sizeof(buffer));
if ( waitAck() == STM_OK ) {
ret = STM_OK;
} else {
printf("STM32-GlobalExErase: fail\n");
}
} else {
printf("STM32-ExErase: fail\n");
}
}
if ( ret == STM_OK ) {
printf("STM32-Erase: OK\n");
}
return ret;
}
int stm32Write(MemMap* mp)
{
int ret = STM_ERR;
MemBlock *pBlock;
for ( pBlock = mp->head; pBlock; pBlock = pBlock->next ) {
unsigned int loc = 0;
printf("STM32Wirte: %08X", pBlock->address+loc);
while ( pBlock->address+loc < pBlock->lastValid ) {
int size = pBlock->lastValid - (pBlock->address + loc);
if ( size > MAXLEN) {
size = MAXLEN;
}
if ( size %4 ) {
size += 4-size%4;
}
printf(".");
fflush(stdout);
stm32WriteBlock(pBlock->data+loc, size, pBlock->address+loc);
loc+=size;
}
printf("...Done.\n");
}
printf("STM32Write: success.\n");
return ret;
}
int stm32Go(unsigned int addr)
{
int ret = STM_ERR;
unsigned char addr_buffer[5];
printf("STM32-Go: %08X\n", addr);
if ( sendCommand(0x21) == STM_OK ) {
serialWrite(board.serial_fd, addrPacket(addr, addr_buffer), 5);
if (waitAck() == STM_OK ) {
printf("STM32-Go: success\n");
ret = STM_OK;
} else {
printf("STM32-Go: address error!\n");
}
} else {
printf("STM32-Go: command error!\n");
}
return ret;
}
static int stm32WriteBlock(unsigned char *data, unsigned int len, unsigned int addr)
{
int ret = STM_ERR;
unsigned char addr_buffer[5];
unsigned char slen;
unsigned char cs;
assert(len%4 == 0);
assert(len<=MAXLEN);
slen = (unsigned char)(len-1);
cs = calcChecksum(data, len);
if ( sendCommand(0x31) == STM_OK ) {
serialWrite(board.serial_fd, addrPacket(addr, addr_buffer), 5);
if ( waitAck() == STM_OK ) {
serialWrite(board.serial_fd, &slen, 1);
serialWrite(board.serial_fd, data, len);
serialWrite(board.serial_fd, &cs, 1);
if ( waitAck() == STM_OK ) {
ret = STM_OK;
} else {
printf("STM32-Write: data error!\n");
}
} else {
printf("STM32-Write: address error!\n");
}
} else {
printf("STM32-Write: command error!\n");
}
return ret;
}
//---------------------------------------------------------------------
/**
* send a byte to the board and get the ACK
* @return STM_OK or STM_ERR if NAK received or communication failed
*/
static int sendCommand(unsigned char byte)
{
unsigned char xorbyte;
xorbyte = byte ^ 0xFF;
// printf("byte=%02X\n",byte);
serialFlush(board.serial_fd);
serialWrite(board.serial_fd,&byte, 1);
serialWrite(board.serial_fd,&xorbyte, 1);
return waitAck();
}
/**
* read and check one byte if it is ACK
* @return STM_OK or STM_ERR if NAK received or communication failed
*/
static int waitAck()
{
int ret = STM_ERR;
int rr;
unsigned char r;
rr = serialRead(board.serial_fd, &r, sizeof(r));
// printf("rr=%d, r=%d\n", rr, r);
if ( rr == SER_OK ) {
if ( r == STM32_ACK )
ret = STM_OK;
}
return ret;
}
static unsigned char *addrPacket(unsigned int addr, unsigned char *buffer)
{
buffer[0] = (addr >> 24) & 0xFF;
buffer[1] = (addr >> 16) & 0xFF;
buffer[2] = (addr >> 8) & 0xFF;
buffer[3] = (addr >> 0) & 0xFF;
buffer[4] = buffer[0] ^ buffer[1] ^ buffer[2] ^ buffer[3];
return buffer;
}
static unsigned char calcChecksum(unsigned char* data, unsigned int len)
{
int i;
unsigned char cs = (unsigned char)(len-1);
for ( i=0; i<len; i++ )
cs ^= data[i];
return cs;
}
#if 0
int stm32_readprotect(int x)
{
uint8_t command;
(x > 0)? (command = 0x82): (command = 0x92);
if (sendCommand(command))
{
printf("STM32-%x: command fail!\n", command);
return 1;
}
if (waitAck())
return 2;
stm32f10x.readprotect = x;
return 0;
}
int write_unprotect(void)
{
if (sendCommand(0x73))
{
printf("STM32-WriteUnprotect: command fail!\n");
return 1;
}
if (waitAck())
{
printf("STM32-WriteUnprotect: unprotect fail!\n");
return 2;
}
stm32f10x.writeprotect = 0;
return 0;
}
int write_protect(void)
{
uint8_t buffer[MAXLEN+2];
unsigned int i;
if (sendCommand(0x63))
{
printf("STM32-WriteProtect: command fail!\n");
return 1;
}
buffer[0] = MAXLEN - 1;
buffer[MAXLEN+1] = buffer[0];
for (i=1; i<=MAXLEN; i++)
{
buffer[i] = i - 1;
buffer[MAXLEN+1] ^= buffer[i];
}
serialWrite(buffer, MAXLEN+2);
if (waitAck())
{
printf("STM32-WriteProtect: protect fail!\n");
return 2;
}
stm32f10x.writeprotect = 0;
return 0;
}
int stm32_writeprotect(int x)
{
int ret;
if (x > 0)
ret = write_protect();
else
ret = write_unprotect();
return ret;
}
#endif