-
Notifications
You must be signed in to change notification settings - Fork 52
/
adapter-stk500v2.c
640 lines (558 loc) · 18 KB
/
adapter-stk500v2.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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
/*
* Interface to PIC32 bootloader with serial STK500v2 protocol.
*
* Copyright (C) 2014-2015 Serge Vakulenko
*
* This file is part of PIC32PROG project, which is distributed
* under the terms of the GNU General Public License (GPL).
* See the accompanying file "COPYING" for more details.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "adapter.h"
#include "pic32.h"
#include "serial.h"
/*
* AVR068 - STK500 Communication Protocol
*/
/* STK message constants */
#define MESSAGE_START 0x1B /* ESC = 27 decimal */
#define TOKEN 0x0E
/* STK command constants */
#define CMD_SIGN_ON 0x01
#define CMD_SET_PARAMETER 0x02
#define CMD_GET_PARAMETER 0x03
#define CMD_LOAD_ADDRESS 0x06
#define CMD_ENTER_PROGMODE_ISP 0x10
#define CMD_LEAVE_PROGMODE_ISP 0x11
#define CMD_CHIP_ERASE_ISP 0x12
#define CMD_PROGRAM_FLASH_ISP 0x13
#define CMD_READ_FLASH_ISP 0x14
#define CMD_SPI_MULTI 0x1D
#define CMD_SET_BAUD 0x48
#define PARAM_CK_VEND_LOW 0x40
#define PARAM_CK_VEND_HIGH 0x41
#define PARAM_CK_PROD_LOW 0x42
#define PARAM_CK_PROD_HIGH 0x43
#define PARAM_CK_DEVID_LOW 0x44
#define PARAM_CK_DEVID_MID 0x45
#define PARAM_CK_DEVID_HIGH 0x46
#define PARAM_CK_DEVID_TOP 0x47
#define PARAM_CK_DEVID_REV 0x48
/* STK status constants */
#define STATUS_CMD_OK 0x00 /* Success */
#define PAGE_NBYTES 256 /* Write packet sise */
#define READ_NBYTES 256 /* Read packet size */
extern unsigned int alternate_speed;
typedef struct {
/* Common part */
adapter_t adapter;
int first_time;
int timeout_msec;
unsigned baud;
unsigned char sequence_number;
unsigned char page_addr_fetched;
unsigned page_addr;
unsigned last_load_addr;
unsigned char page [PAGE_NBYTES];
} stk_adapter_t;
/*
* Send the command sequence and get back a response.
*/
static int send_receive(stk_adapter_t *a, unsigned char *cmd, int cmdlen,
unsigned char *response, int reply_len)
{
unsigned char *p, sum, hdr [5];
int len, i, got, rlen, retry = 0;
again:
/*
* Prepare header and checksum.
*/
hdr[0] = MESSAGE_START;
hdr[1] = ++a->sequence_number;
hdr[2] = cmdlen >> 8;
hdr[3] = cmdlen;
hdr[4] = TOKEN;
sum = hdr[0] ^ hdr[1] ^ hdr[2] ^ hdr[3] ^ hdr[4];
for (i=0; i<cmdlen; ++i)
sum ^= cmd[i];
/*
* Send command.
*/
if (debug_level > 1) {
printf("send [%d] %x-%x-%x-%x-%x",
5 + cmdlen + 1, hdr[0], hdr[1], hdr[2], hdr[3], hdr[4]);
for (i=0; i<cmdlen; ++i)
printf("-%x", cmd[i]);
printf("-%x\n", sum);
}
if (serial_write(hdr, 5) < 0 ||
serial_write(cmd, cmdlen) < 0 ||
serial_write(&sum, 1) < 0) {
fprintf(stderr, "stk-send: write error\n");
exit(-1);
}
/*
* Get header.
*/
p = hdr;
len = 0;
while (len < 5) {
got = serial_read(p, 5 - len, a->timeout_msec);
if (! got)
return 0;
p += got;
len += got;
}
if (hdr[0] != MESSAGE_START || hdr[1] != a->sequence_number ||
hdr[4] != TOKEN) {
/* Skip all incoming data. */
unsigned char buf [300];
if (retry)
printf("got invalid header: %x-%x-%x-%x-%x\n",
hdr[0], hdr[1], hdr[2], hdr[3], hdr[4]);
flush_input:
serial_read(buf, sizeof(buf), a->timeout_msec);
if (retry) {
retry = 1;
goto again;
}
return 0;
}
rlen = hdr[2] << 8 | hdr[3];
if (rlen == 0 || rlen > reply_len) {
printf("invalid reply length=%d, expecting %d bytes\n",
rlen, reply_len);
goto flush_input;
}
/*
* Get response.
*/
p = response;
len = 0;
while (len < rlen) {
got = serial_read(p, rlen - len, a->timeout_msec);
if (! got)
return 0;
p += got;
len += got;
}
/*
* Get sum.
*/
p = ∑
len = 0;
while (len < 1) {
got = serial_read(p, 1, a->timeout_msec);
if (! got)
return 0;
++len;
}
if (debug_level > 1) {
printf(" got [%d] %x-%x-%x-%x-%x",
5 + rlen + 1, hdr[0], hdr[1], hdr[2], hdr[3], hdr[4]);
for (i=0; i<rlen; ++i)
printf("-%x", response[i]);
printf("-%x\n", sum);
}
/* Check sum. */
sum ^= hdr[0] ^ hdr[1] ^ hdr[2] ^ hdr[3] ^ hdr[4];
for (i=0; i<rlen; ++i)
sum ^= response[i];
if (sum != 0) {
printf("invalid reply checksum\n");
goto flush_input;
}
return 1;
}
static void switch_baud(stk_adapter_t *a)
{
unsigned char cmd [5] = { CMD_SET_BAUD,
(alternate_speed) & 0xFF,
(alternate_speed >> 8) & 0xFF,
(alternate_speed >> 16) & 0xFF,
(alternate_speed >> 24) & 0xFF,
};
unsigned char response [6];
if (alternate_speed != a->baud) {
if (send_receive(a, cmd, 5, response, 6) &&
response[0] == cmd[0] &&
response[1] == STATUS_CMD_OK &&
response[2] == cmd[1] &&
response[3] == cmd[2] &&
response[4] == cmd[3] &&
response[5] == cmd[4])
{
serial_baud(alternate_speed);
printf(" Baud rate: %d bps\n", alternate_speed);
} else {
printf(" Baud rate: %d bps\n", a->baud);
}
}
}
static unsigned char get_parameter(stk_adapter_t *a, unsigned char param) {
unsigned char cmd [2] = { CMD_GET_PARAMETER, param };
unsigned char response [3];
if (debug_level > 1)
printf("Get parameter %x\n", param);
if (! send_receive(a, cmd, 2, response, 3) || response[0] != cmd[0] || response[1] != STATUS_CMD_OK) {
fprintf(stderr, "Error fetching parameter %d\n", param);
exit(-1);
}
if (debug_level > 1)
printf("Value %x\n", response[2]);
return response[2];
}
static void set_parameter(stk_adapter_t *a, unsigned char param, int val) {
unsigned char cmd [3] = { CMD_SET_PARAMETER, param, val };
unsigned char response [2];
if (debug_level > 1)
printf("Set parameter %x\n", param);
if (! send_receive(a, cmd, 3, response, 2) || response[0] != cmd[0] || response[1] != STATUS_CMD_OK) {
fprintf(stderr, "Error setting parameter %d\n", param);
exit(-1);
}
}
static void prog_enable(stk_adapter_t *a)
{
unsigned char cmd [12] = { CMD_ENTER_PROGMODE_ISP,
200, /* timeout in msec */
100, /* pin stabilization delay in msec */
25, /* command execution delay in msec */
32, /* number of synchronization loops */
0, /* per byte delay */
0x53, /* poll value, 53h for AVR, 69h for AT89xx */
3, /* poll index, 3 for AVR, 4 for AT89xx */
0xAC, 0x53, 0x00, 0x00
};
unsigned char response [2];
if (! send_receive(a, cmd, 12, response, 2) || response[0] != cmd[0] ||
response[1] != STATUS_CMD_OK) {
fprintf(stderr, "Cannot enter programming mode.\n");
exit(-1);
}
}
/*
* This function does not actually do anything on the PIC32 side for
* any known STK500 bootloader on the PIC32 (i.e. chipKIT). In addition,
* a bug in an early bootloader version does not respond when this
* command is sent. Thus PIC32Prog will fail to erase (and thus fail
* to program) any board with this early bootloader version.
* By simply retruning, PIC32Prog can be made to work with the early
* bootloader.
*/
static void chip_erase(stk_adapter_t *a)
{
/* Empty. */
}
static void prog_disable(stk_adapter_t *a)
{
unsigned char cmd [3] = { CMD_LEAVE_PROGMODE_ISP,
1, /* pre-delay in msec */
1, /* post-delay in msec */
};
unsigned char response [2];
/* Skip all incoming data. */
unsigned char buf [300];
serial_read(buf, sizeof(buf), a->timeout_msec);
/* Leave programming mode; ignore errors. */
send_receive(a, cmd, 3, response, 2);
}
static void load_address(stk_adapter_t *a, unsigned addr)
{
// Convert an absolute address into a flash relative address
if (addr >= (0x1D000000 >> 1)) {
if (debug_level > 2)
printf("Adjusting address 0x%08x to ", addr << 1);
addr -= (0x1D000000 >> 1);
if (debug_level > 2)
printf("0x%08x\n", addr << 1);
}
unsigned char cmd [5] = { CMD_LOAD_ADDRESS,
(addr >> 24), (addr >> 16), addr >> 8, addr, };
unsigned char response [2];
if (a->last_load_addr == addr)
return;
if (debug_level > 1)
printf("Load address: %#x\n", addr << 1); // & 0x7f0000);
if (! send_receive(a, cmd, 5, response, 2) || response[0] != cmd[0] ||
response[1] != STATUS_CMD_OK) {
fprintf(stderr, "Load address failed.\n");
exit(-1);
}
a->last_load_addr = addr;
}
static void flush_write_buffer(stk_adapter_t *a)
{
unsigned char cmd [10+PAGE_NBYTES] = { CMD_PROGRAM_FLASH_ISP,
PAGE_NBYTES >> 8, PAGE_NBYTES & 0xff, 0, 0, 0, 0, 0, 0, 0 };
unsigned char response [2];
if (! a->page_addr_fetched)
return;
load_address(a, a->page_addr >> 1);
/*
* An early chipKIT bootloader version does a whole-chip erase
* after the first CMD_PROGRAM_FLASH_ISP command, which can
* take up to 4 seconds to complete. Thus the timeout needs
* to be at least this long so that PIC32Prog will not timeout
* during the erase cycle on that version of bootloader.
*/
if (a->first_time) {
a->timeout_msec = 5000;
a->first_time = 0;
} else {
a->timeout_msec = 1000;
}
if (debug_level > 1)
printf("Programming page: %#x\n", a->page_addr);
memcpy(cmd+10, a->page, PAGE_NBYTES);
if (! send_receive(a, cmd, 10+PAGE_NBYTES, response, 2) ||
response[0] != cmd[0]) {
fprintf(stderr, "Program flash failed.\n");
exit(-1);
}
if (response[1] != STATUS_CMD_OK)
printf("Programming flash: timeout at %#x\n", a->page_addr);
a->page_addr_fetched = 0;
a->last_load_addr += PAGE_NBYTES / 2;
}
/*
* PAGE MODE PROGRAMMING:
* Cache page address. When current address is out of the page address,
* flush page buffer and continue programming.
*/
static void write_byte(stk_adapter_t *a, unsigned addr, unsigned char byte)
{
if (debug_level > 2)
printf("Loading to address: %#x (page_addr_fetched=%s)\n",
addr, a->page_addr_fetched ? "Yes" : "No");
if (a->page_addr / PAGE_NBYTES != addr / PAGE_NBYTES)
flush_write_buffer(a);
if (! a->page_addr_fetched) {
a->page_addr = addr / PAGE_NBYTES * PAGE_NBYTES;
a->page_addr_fetched = 1;
}
a->page [addr % PAGE_NBYTES] = byte;
}
/*
* Read 256 bytes from the flash memory.
* For some reason, the chipKIT bootloader fails to read blocks
* shorter that 256 bytes.
*/
static void read_page(stk_adapter_t *a, unsigned addr,
unsigned char *buf)
{
unsigned char cmd [4] = { CMD_READ_FLASH_ISP,
READ_NBYTES >> 8, READ_NBYTES & 0xff, 0x20 };
unsigned char response [3+READ_NBYTES];
load_address(a, addr >> 1);
if (debug_level > 1)
printf("Read page: %#x\n", addr);
if (! send_receive(a, cmd, 4, response, 3+READ_NBYTES) ||
response[0] != cmd[0] ||
response[1] != STATUS_CMD_OK ||
response[2+READ_NBYTES] != STATUS_CMD_OK) {
fprintf(stderr, "Read page failed.\n");
exit(-1);
}
memcpy(buf, response+2, READ_NBYTES);
a->last_load_addr += READ_NBYTES / 2;
}
static void stk_close(adapter_t *adapter, int power_on)
{
stk_adapter_t *a = (stk_adapter_t*) adapter;
prog_disable(a);
/* restore and close serial port */
serial_close();
free(a);
}
/*
* Return the Device Identification code
*/
static unsigned stk_get_idcode(adapter_t *adapter)
{
unsigned int i = 0;
// A non-DEVID aware bootloader will just store
// the parameters we send in the slot we specify. So
// let's set the DEAFBOOB ID into the parameter
// slots that correspond to the device ID. A DEVID
// aware bootloader will overwrite these with the
// real device ID, so we can know if it's supported
// or not.
set_parameter((stk_adapter_t *)adapter, PARAM_CK_DEVID_LOW, 0x0B);
set_parameter((stk_adapter_t *)adapter, PARAM_CK_DEVID_MID, 0xB0);
set_parameter((stk_adapter_t *)adapter, PARAM_CK_DEVID_HIGH, 0xAF);
set_parameter((stk_adapter_t *)adapter, PARAM_CK_DEVID_TOP, 0xDE);
i = get_parameter((stk_adapter_t *)adapter, PARAM_CK_DEVID_LOW);
i |= (get_parameter((stk_adapter_t *)adapter, PARAM_CK_DEVID_MID) << 8);
i |= (get_parameter((stk_adapter_t *)adapter, PARAM_CK_DEVID_HIGH) << 16);
i |= (get_parameter((stk_adapter_t *)adapter, PARAM_CK_DEVID_TOP) << 24);
if (i == 0) {
/* Bootloader does not allow to get cpu ID code. */
if (debug_level > 1)
printf("Cannot get the DEVID for the target\n");
return 0xDEAFB00B;
}
return i;
}
/*
* Read a configuration word from memory.
*/
static unsigned stk_read_word(adapter_t *adapter, unsigned addr)
{
if (debug_level > 1)
printf("Reading word from %x\n", addr);
stk_adapter_t *a = (stk_adapter_t*) adapter;
unsigned char cmd [4] = { CMD_READ_FLASH_ISP, 0, 4, 0x20 };
unsigned char response [7];
load_address(a, addr >> 1);
if (debug_level > 1)
printf("Sending read request\n");
if (! send_receive(a, cmd, 4, response, 7) || response[0] != cmd[0] ||
response[1] != STATUS_CMD_OK || response[6] != STATUS_CMD_OK) {
fprintf(stderr, "Read word failed.\n");
exit(-1);
}
if (debug_level > 1)
printf("Read request done\n");
return response[2] | response[3] << 8 |
response[4] << 16 | response[5] << 24;
}
/*
* Write a configuration word to flash memory.
*/
static void stk_program_word(adapter_t *adapter,
unsigned addr, unsigned word)
{
/* This function is needed for programming DEVCFG fuses.
* Not supported by booloader. */
if (debug_level > 0)
fprintf(stderr, "stk: program word at %08x: %08x\n", addr, word);
}
/*
* Erase all flash memory.
*/
static void stk_erase_chip(adapter_t *adapter)
{
stk_adapter_t *a = (stk_adapter_t*) adapter;
chip_erase(a);
prog_enable(a);
}
/*
* Verify a block of memory (1024 bytes).
*/
static void stk_verify_data(adapter_t *adapter,
unsigned addr, unsigned nwords, unsigned *data)
{
stk_adapter_t *a = (stk_adapter_t*) adapter;
unsigned block [1024/4], i, expected, word;
/* Read block of data. */
for (i=0; i<1024; i+=READ_NBYTES)
read_page(a, addr+i, i + (unsigned char*) block);
/* Compare. */
for (i=0; i<nwords; i++) {
expected = data [i];
word = block [i];
if (word != expected) {
printf("\nerror at address %08X: file=%08X, mem=%08X\n",
addr + i*4, expected, word);
exit(1);
}
}
}
/*
* Flash write, 1-kbyte blocks.
*/
static void stk_program_block(adapter_t *adapter,
unsigned addr, unsigned *data)
{
stk_adapter_t *a = (stk_adapter_t*) adapter;
unsigned i;
for (i=0; i<1024/4; ++i) {
write_byte(a, addr + i*4, data[i]);
write_byte(a, addr + i*4 + 1, data[i] >> 8);
write_byte(a, addr + i*4 + 2, data[i] >> 16);
write_byte(a, addr + i*4 + 3, data[i] >> 24);
}
flush_write_buffer(a);
}
adapter_t *adapter_open_stk500v2(const char *port, int baud_rate)
{
stk_adapter_t *a;
a = calloc(1, sizeof(*a));
if (! a) {
fprintf(stderr, "Out of memory\n");
return 0;
}
a->baud = baud_rate;
a->first_time = 1;
a->timeout_msec = 1000;
/* Open serial port */
if (serial_open(port, baud_rate) < 0) {
/* failed to open serial port */
free(a);
return 0;
}
usleep(200000);
/* Detect the device. */
int retry_count;
unsigned char response [12];
/* Synchronize */
retry_count = 0;
int outer_retry = 0;
for (;;) {
/* Send CMD_SIGN_ON. */
if (send_receive(a, (unsigned char*)"\1", 1, response, 11) &&
((memcmp(response, "\1\0\10STK500_2", 11) == 0)
||
(memcmp(response, "\1\0\10AVRISP_2", 11) == 0))) {
if (debug_level > 1)
printf("stk-probe: OK\n");
break;
}
++retry_count;
if (debug_level > 1) {
printf("stk-probe: retry %d: "
"%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x\n",
retry_count, response[0], response[1], response[2],
response[3], response[4], response[5], response[6],
response[7], response[8], response[9], response[10]);
}
if (retry_count >= 3) {
/* Bad reply or no device connected */
retry_count = 0;
serial_close();
usleep(200000);
serial_open(port, baud_rate);
outer_retry++;
}
if (outer_retry >= 2) {
free(a);
serial_close();
return 0;
}
}
switch_baud(a);
prog_enable(a);
a->last_load_addr = -1;
/* Identify device. */
printf(" Adapter: STK500v2 Bootloader\n");
a->adapter.user_start = 0x1d000000;
a->adapter.user_nbytes = 2048 * 1024;
a->adapter.boot_nbytes = 80 * 1024;
a->adapter.block_override = 1024;
a->adapter.flags = (AD_PROBE | AD_ERASE | AD_READ | AD_WRITE);
printf(" Program area: %08x-%08x\n", a->adapter.user_start,
a->adapter.user_start + a->adapter.user_nbytes - 1);
/* User functions. */
a->adapter.close = stk_close;
a->adapter.get_idcode = stk_get_idcode;
a->adapter.read_word = stk_read_word;
a->adapter.verify_data = stk_verify_data;
a->adapter.program_block = stk_program_block;
a->adapter.program_word = stk_program_word;
a->adapter.erase_chip = stk_erase_chip;
return &a->adapter;
}