1 /*
2 * avrdude - A Downloader/Uploader for AVR device programmers
3 * Copyright (C) 2002-2004 Brian S. Dean <bsd@bsdhome.com>
4 * Copyright (C) 2008,2014 Joerg Wunsch
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 /* $Id$ */
21
22 /*
23 * avrdude interface for Atmel STK500 programmer
24 *
25 * Note: most commands use the "universal command" feature of the
26 * programmer in a "pass through" mode, exceptions are "program
27 * enable", "paged read", and "paged write".
28 *
29 */
30
31 #include "ac_cfg.h"
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <errno.h>
37 #include <unistd.h>
38
39 #include "avrdude.h"
40 #include "libavrdude.h"
41
42 #include "stk500.h"
43 #include "stk500_private.h"
44
45 #define STK500_XTAL 7372800U
46 #define MAX_SYNC_ATTEMPTS 10
47
48 struct pdata
49 {
50 unsigned char ext_addr_byte; /* Record ext-addr byte set in the
51 * target device (if used) */
52 };
53
54 #define PDATA(pgm) ((struct pdata *)(pgm->cookie))
55
56
57 static int stk500_getparm(PROGRAMMER * pgm, unsigned parm, unsigned * value);
58 static int stk500_setparm(PROGRAMMER * pgm, unsigned parm, unsigned value);
59 static void stk500_print_parms1(PROGRAMMER * pgm, const char * p);
60
61
stk500_send(PROGRAMMER * pgm,unsigned char * buf,size_t len)62 static int stk500_send(PROGRAMMER * pgm, unsigned char * buf, size_t len)
63 {
64 return serial_send(&pgm->fd, buf, len);
65 }
66
67
stk500_recv(PROGRAMMER * pgm,unsigned char * buf,size_t len)68 static int stk500_recv(PROGRAMMER * pgm, unsigned char * buf, size_t len)
69 {
70 int rv;
71
72 rv = serial_recv(&pgm->fd, buf, len);
73 if (rv < 0) {
74 avrdude_message(MSG_INFO, "%s: stk500_recv(): programmer is not responding\n",
75 progname);
76 return -1;
77 }
78 return 0;
79 }
80
81
stk500_drain(PROGRAMMER * pgm,int display)82 int stk500_drain(PROGRAMMER * pgm, int display)
83 {
84 return serial_drain(&pgm->fd, display);
85 }
86
87
stk500_getsync(PROGRAMMER * pgm)88 int stk500_getsync(PROGRAMMER * pgm)
89 {
90 unsigned char buf[32], resp[32];
91 int attempt;
92
93 /*
94 * get in sync */
95 buf[0] = Cmnd_STK_GET_SYNC;
96 buf[1] = Sync_CRC_EOP;
97
98 /*
99 * First send and drain a few times to get rid of line noise
100 */
101
102 stk500_send(pgm, buf, 2);
103 stk500_drain(pgm, 0);
104 stk500_send(pgm, buf, 2);
105 stk500_drain(pgm, 0);
106
107 for (attempt = 0; attempt < MAX_SYNC_ATTEMPTS; attempt++) {
108 stk500_send(pgm, buf, 2);
109 stk500_recv(pgm, resp, 1);
110 if (resp[0] == Resp_STK_INSYNC){
111 break;
112 }
113 avrdude_message(MSG_INFO, "%s: stk500_getsync() attempt %d of %d: not in sync: resp=0x%02x\n",
114 progname, attempt + 1, MAX_SYNC_ATTEMPTS, resp[0]);
115 }
116 if (attempt == MAX_SYNC_ATTEMPTS) {
117 stk500_drain(pgm, 0);
118 return -1;
119 }
120
121 if (stk500_recv(pgm, resp, 1) < 0)
122 return -1;
123 if (resp[0] != Resp_STK_OK) {
124 avrdude_message(MSG_INFO, "%s: stk500_getsync(): can't communicate with device: "
125 "resp=0x%02x\n",
126 progname, resp[0]);
127 return -1;
128 }
129
130 return 0;
131 }
132
133
134 /*
135 * transmit an AVR device command and return the results; 'cmd' and
136 * 'res' must point to at least a 4 byte data buffer
137 */
stk500_cmd(PROGRAMMER * pgm,const unsigned char * cmd,unsigned char * res)138 static int stk500_cmd(PROGRAMMER * pgm, const unsigned char *cmd,
139 unsigned char *res)
140 {
141 unsigned char buf[32];
142
143 buf[0] = Cmnd_STK_UNIVERSAL;
144 buf[1] = cmd[0];
145 buf[2] = cmd[1];
146 buf[3] = cmd[2];
147 buf[4] = cmd[3];
148 buf[5] = Sync_CRC_EOP;
149
150 stk500_send(pgm, buf, 6);
151
152 if (stk500_recv(pgm, buf, 1) < 0)
153 return -1;
154 if (buf[0] != Resp_STK_INSYNC) {
155 avrdude_message(MSG_INFO, "%s: stk500_cmd(): programmer is out of sync\n", progname);
156 return -1;
157 }
158
159 res[0] = cmd[1];
160 res[1] = cmd[2];
161 res[2] = cmd[3];
162 if (stk500_recv(pgm, &res[3], 1) < 0)
163 return -1;
164
165 if (stk500_recv(pgm, buf, 1) < 0)
166 return -1;
167 if (buf[0] != Resp_STK_OK) {
168 avrdude_message(MSG_INFO, "%s: stk500_cmd(): protocol error\n", progname);
169 return -1;
170 }
171
172 return 0;
173 }
174
175
176
177 /*
178 * issue the 'chip erase' command to the AVR device
179 */
stk500_chip_erase(PROGRAMMER * pgm,AVRPART * p)180 static int stk500_chip_erase(PROGRAMMER * pgm, AVRPART * p)
181 {
182 unsigned char cmd[4];
183 unsigned char res[4];
184
185 if (pgm->cmd == NULL) {
186 avrdude_message(MSG_INFO, "%s: Error: %s programmer uses stk500_chip_erase() but does not\n"
187 "provide a cmd() method.\n",
188 progname, pgm->type);
189 return -1;
190 }
191
192 if (p->op[AVR_OP_CHIP_ERASE] == NULL) {
193 avrdude_message(MSG_INFO, "chip erase instruction not defined for part \"%s\"\n",
194 p->desc);
195 return -1;
196 }
197
198 pgm->pgm_led(pgm, ON);
199
200 memset(cmd, 0, sizeof(cmd));
201
202 avr_set_bits(p->op[AVR_OP_CHIP_ERASE], cmd);
203 pgm->cmd(pgm, cmd, res);
204 usleep(p->chip_erase_delay);
205 pgm->initialize(pgm, p);
206
207 pgm->pgm_led(pgm, OFF);
208
209 return 0;
210 }
211
212 /*
213 * issue the 'program enable' command to the AVR device
214 */
stk500_program_enable(PROGRAMMER * pgm,AVRPART * p)215 static int stk500_program_enable(PROGRAMMER * pgm, AVRPART * p)
216 {
217 unsigned char buf[16];
218 int tries=0;
219
220 retry:
221
222 tries++;
223
224 buf[0] = Cmnd_STK_ENTER_PROGMODE;
225 buf[1] = Sync_CRC_EOP;
226
227 stk500_send(pgm, buf, 2);
228 if (stk500_recv(pgm, buf, 1) < 0)
229 return -1;
230 if (buf[0] == Resp_STK_NOSYNC) {
231 if (tries > 33) {
232 avrdude_message(MSG_INFO, "%s: stk500_program_enable(): can't get into sync\n",
233 progname);
234 return -1;
235 }
236 if (stk500_getsync(pgm) < 0)
237 return -1;
238 goto retry;
239 }
240 else if (buf[0] != Resp_STK_INSYNC) {
241 avrdude_message(MSG_INFO, "%s: stk500_program_enable(): protocol error, "
242 "expect=0x%02x, resp=0x%02x\n",
243 progname, Resp_STK_INSYNC, buf[0]);
244 return -1;
245 }
246
247 if (stk500_recv(pgm, buf, 1) < 0)
248 return -1;
249 if (buf[0] == Resp_STK_OK) {
250 return 0;
251 }
252 else if (buf[0] == Resp_STK_NODEVICE) {
253 avrdude_message(MSG_INFO, "%s: stk500_program_enable(): no device\n",
254 progname);
255 return -1;
256 }
257
258 if(buf[0] == Resp_STK_FAILED)
259 {
260 avrdude_message(MSG_INFO, "%s: stk500_program_enable(): failed to enter programming mode\n",
261 progname);
262 return -1;
263 }
264
265
266 avrdude_message(MSG_INFO, "%s: stk500_program_enable(): unknown response=0x%02x\n",
267 progname, buf[0]);
268
269 return -1;
270 }
271
272
273
stk500_set_extended_parms(PROGRAMMER * pgm,int n,unsigned char * cmd)274 static int stk500_set_extended_parms(PROGRAMMER * pgm, int n,
275 unsigned char * cmd)
276 {
277 unsigned char buf[16];
278 int tries=0;
279 int i;
280
281 retry:
282
283 tries++;
284
285 buf[0] = Cmnd_STK_SET_DEVICE_EXT;
286 for (i=0; i<n; i++) {
287 buf[1+i] = cmd[i];
288 }
289 i++;
290 buf[i] = Sync_CRC_EOP;
291
292 stk500_send(pgm, buf, i+1);
293 if (stk500_recv(pgm, buf, 1) < 0)
294 return -1;
295 if (buf[0] == Resp_STK_NOSYNC) {
296 if (tries > 33) {
297 avrdude_message(MSG_INFO, "%s: stk500_set_extended_parms(): can't get into sync\n",
298 progname);
299 return -1;
300 }
301 if (stk500_getsync(pgm) < 0)
302 return -1;
303 goto retry;
304 }
305 else if (buf[0] != Resp_STK_INSYNC) {
306 avrdude_message(MSG_INFO, "%s: stk500_set_extended_parms(): protocol error, "
307 "expect=0x%02x, resp=0x%02x\n",
308 progname, Resp_STK_INSYNC, buf[0]);
309 return -1;
310 }
311
312 if (stk500_recv(pgm, buf, 1) < 0)
313 return -1;
314 if (buf[0] == Resp_STK_OK) {
315 return 0;
316 }
317 else if (buf[0] == Resp_STK_NODEVICE) {
318 avrdude_message(MSG_INFO, "%s: stk500_set_extended_parms(): no device\n",
319 progname);
320 return -1;
321 }
322
323 if(buf[0] == Resp_STK_FAILED)
324 {
325 avrdude_message(MSG_INFO, "%s: stk500_set_extended_parms(): failed to set extended "
326 "device programming parameters\n",
327 progname);
328 return -1;
329 }
330
331
332 avrdude_message(MSG_INFO, "%s: stk500_set_extended_parms(): unknown response=0x%02x\n",
333 progname, buf[0]);
334
335 return -1;
336 }
337
338 /*
339 * Crossbow MIB510 initialization and shutdown. Use cmd = 1 to
340 * initialize, cmd = 0 to close.
341 */
mib510_isp(PROGRAMMER * pgm,unsigned char cmd)342 static int mib510_isp(PROGRAMMER * pgm, unsigned char cmd)
343 {
344 unsigned char buf[9];
345 int tries = 0;
346
347 buf[0] = 0xaa;
348 buf[1] = 0x55;
349 buf[2] = 0x55;
350 buf[3] = 0xaa;
351 buf[4] = 0x17;
352 buf[5] = 0x51;
353 buf[6] = 0x31;
354 buf[7] = 0x13;
355 buf[8] = cmd;
356
357
358 retry:
359
360 tries++;
361
362 stk500_send(pgm, buf, 9);
363 if (stk500_recv(pgm, buf, 1) < 0)
364 return -1;
365 if (buf[0] == Resp_STK_NOSYNC) {
366 if (tries > 33) {
367 avrdude_message(MSG_INFO, "%s: mib510_isp(): can't get into sync\n",
368 progname);
369 return -1;
370 }
371 if (stk500_getsync(pgm) < 0)
372 return -1;
373 goto retry;
374 }
375 else if (buf[0] != Resp_STK_INSYNC) {
376 avrdude_message(MSG_INFO, "%s: mib510_isp(): protocol error, "
377 "expect=0x%02x, resp=0x%02x\n",
378 progname, Resp_STK_INSYNC, buf[0]);
379 return -1;
380 }
381
382 if (stk500_recv(pgm, buf, 1) < 0)
383 return -1;
384 if (buf[0] == Resp_STK_OK) {
385 return 0;
386 }
387 else if (buf[0] == Resp_STK_NODEVICE) {
388 avrdude_message(MSG_INFO, "%s: mib510_isp(): no device\n",
389 progname);
390 return -1;
391 }
392
393 if (buf[0] == Resp_STK_FAILED)
394 {
395 avrdude_message(MSG_INFO, "%s: mib510_isp(): command %d failed\n",
396 progname, cmd);
397 return -1;
398 }
399
400
401 avrdude_message(MSG_INFO, "%s: mib510_isp(): unknown response=0x%02x\n",
402 progname, buf[0]);
403
404 return -1;
405 }
406
407
408 /*
409 * initialize the AVR device and prepare it to accept commands
410 */
stk500_initialize(PROGRAMMER * pgm,AVRPART * p)411 static int stk500_initialize(PROGRAMMER * pgm, AVRPART * p)
412 {
413 unsigned char buf[32];
414 AVRMEM * m;
415 int tries;
416 unsigned maj, min;
417 int rc;
418 int n_extparms;
419
420 stk500_getparm(pgm, Parm_STK_SW_MAJOR, &maj);
421 stk500_getparm(pgm, Parm_STK_SW_MINOR, &min);
422
423 // MIB510 does not need extparams
424 if (strcmp(ldata(lfirst(pgm->id)), "mib510") == 0)
425 n_extparms = 0;
426 else if ((maj > 1) || ((maj == 1) && (min > 10)))
427 n_extparms = 4;
428 else
429 n_extparms = 3;
430
431 tries = 0;
432
433 retry:
434 tries++;
435
436 memset(buf, 0, sizeof(buf));
437
438 /*
439 * set device programming parameters
440 */
441 buf[0] = Cmnd_STK_SET_DEVICE;
442
443 buf[1] = p->stk500_devcode;
444 buf[2] = 0; /* device revision */
445
446 if ((p->flags & AVRPART_SERIALOK) && (p->flags & AVRPART_PARALLELOK))
447 buf[3] = 0; /* device supports parallel and serial programming */
448 else
449 buf[3] = 1; /* device supports parallel only */
450
451 if (p->flags & AVRPART_PARALLELOK) {
452 if (p->flags & AVRPART_PSEUDOPARALLEL) {
453 buf[4] = 0; /* pseudo parallel interface */
454 n_extparms = 0;
455 }
456 else {
457 buf[4] = 1; /* full parallel interface */
458 }
459 }
460
461 #if 0
462 avrdude_message(MSG_INFO, "%s: stk500_initialize(): n_extparms = %d\n",
463 progname, n_extparms);
464 #endif
465
466 buf[5] = 1; /* polling supported - XXX need this in config file */
467 buf[6] = 1; /* programming is self-timed - XXX need in config file */
468
469 m = avr_locate_mem(p, "lock");
470 if (m)
471 buf[7] = m->size;
472 else
473 buf[7] = 0;
474
475 /*
476 * number of fuse bytes
477 */
478 buf[8] = 0;
479 m = avr_locate_mem(p, "fuse");
480 if (m)
481 buf[8] += m->size;
482 m = avr_locate_mem(p, "lfuse");
483 if (m)
484 buf[8] += m->size;
485 m = avr_locate_mem(p, "hfuse");
486 if (m)
487 buf[8] += m->size;
488 m = avr_locate_mem(p, "efuse");
489 if (m)
490 buf[8] += m->size;
491
492 m = avr_locate_mem(p, "flash");
493 if (m) {
494 buf[9] = m->readback[0];
495 buf[10] = m->readback[1];
496 if (m->paged) {
497 buf[13] = (m->page_size >> 8) & 0x00ff;
498 buf[14] = m->page_size & 0x00ff;
499 }
500 buf[17] = (m->size >> 24) & 0xff;
501 buf[18] = (m->size >> 16) & 0xff;
502 buf[19] = (m->size >> 8) & 0xff;
503 buf[20] = m->size & 0xff;
504 }
505 else {
506 buf[9] = 0xff;
507 buf[10] = 0xff;
508 buf[13] = 0;
509 buf[14] = 0;
510 buf[17] = 0;
511 buf[18] = 0;
512 buf[19] = 0;
513 buf[20] = 0;
514 }
515
516 m = avr_locate_mem(p, "eeprom");
517 if (m) {
518 buf[11] = m->readback[0];
519 buf[12] = m->readback[1];
520 buf[15] = (m->size >> 8) & 0x00ff;
521 buf[16] = m->size & 0x00ff;
522 }
523 else {
524 buf[11] = 0xff;
525 buf[12] = 0xff;
526 buf[15] = 0;
527 buf[16] = 0;
528 }
529
530 buf[21] = Sync_CRC_EOP;
531
532 stk500_send(pgm, buf, 22);
533 if (stk500_recv(pgm, buf, 1) < 0)
534 return -1;
535 if (buf[0] == Resp_STK_NOSYNC) {
536 avrdude_message(MSG_INFO, "%s: stk500_initialize(): programmer not in sync, resp=0x%02x\n",
537 progname, buf[0]);
538 if (tries > 33)
539 return -1;
540 if (stk500_getsync(pgm) < 0)
541 return -1;
542 goto retry;
543 }
544 else if (buf[0] != Resp_STK_INSYNC) {
545 avrdude_message(MSG_INFO, "%s: stk500_initialize(): (a) protocol error, "
546 "expect=0x%02x, resp=0x%02x\n",
547 progname, Resp_STK_INSYNC, buf[0]);
548 return -1;
549 }
550
551 if (stk500_recv(pgm, buf, 1) < 0)
552 return -1;
553 if (buf[0] != Resp_STK_OK) {
554 avrdude_message(MSG_INFO, "%s: stk500_initialize(): (b) protocol error, "
555 "expect=0x%02x, resp=0x%02x\n",
556 progname, Resp_STK_OK, buf[0]);
557 return -1;
558 }
559
560 if (n_extparms) {
561 if ((p->pagel == 0) || (p->bs2 == 0)) {
562 avrdude_message(MSG_NOTICE2, "%s: PAGEL and BS2 signals not defined in the configuration "
563 "file for part %s, using dummy values\n",
564 progname, p->desc);
565 buf[2] = 0xD7; /* they look somehow possible, */
566 buf[3] = 0xA0; /* don't they? ;) */
567 }
568 else {
569 buf[2] = p->pagel;
570 buf[3] = p->bs2;
571 }
572 buf[0] = n_extparms+1;
573
574 /*
575 * m is currently pointing to eeprom memory if the part has it
576 */
577 if (m)
578 buf[1] = m->page_size;
579 else
580 buf[1] = 0;
581
582
583 if (n_extparms == 4) {
584 if (p->reset_disposition == RESET_DEDICATED)
585 buf[4] = 0;
586 else
587 buf[4] = 1;
588 }
589
590 rc = stk500_set_extended_parms(pgm, n_extparms+1, buf);
591 if (rc) {
592 avrdude_message(MSG_INFO, "%s: stk500_initialize(): failed\n", progname);
593 return -1;
594 }
595 }
596
597 return pgm->program_enable(pgm, p);
598 }
599
600
stk500_disable(PROGRAMMER * pgm)601 static void stk500_disable(PROGRAMMER * pgm)
602 {
603 unsigned char buf[16];
604 int tries=0;
605
606 retry:
607
608 tries++;
609
610 buf[0] = Cmnd_STK_LEAVE_PROGMODE;
611 buf[1] = Sync_CRC_EOP;
612
613 stk500_send(pgm, buf, 2);
614 if (stk500_recv(pgm, buf, 1) < 0)
615 return;
616 if (buf[0] == Resp_STK_NOSYNC) {
617 if (tries > 33) {
618 avrdude_message(MSG_INFO, "%s: stk500_disable(): can't get into sync\n",
619 progname);
620 return;
621 }
622 if (stk500_getsync(pgm) < 0)
623 return;
624 goto retry;
625 }
626 else if (buf[0] != Resp_STK_INSYNC) {
627 avrdude_message(MSG_INFO, "%s: stk500_disable(): protocol error, expect=0x%02x, "
628 "resp=0x%02x\n",
629 progname, Resp_STK_INSYNC, buf[0]);
630 return;
631 }
632
633 if (stk500_recv(pgm, buf, 1) < 0)
634 return;
635 if (buf[0] == Resp_STK_OK) {
636 return;
637 }
638 else if (buf[0] == Resp_STK_NODEVICE) {
639 avrdude_message(MSG_INFO, "%s: stk500_disable(): no device\n",
640 progname);
641 return;
642 }
643
644 avrdude_message(MSG_INFO, "%s: stk500_disable(): unknown response=0x%02x\n",
645 progname, buf[0]);
646
647 return;
648 }
649
stk500_enable(PROGRAMMER * pgm)650 static void stk500_enable(PROGRAMMER * pgm)
651 {
652 return;
653 }
654
655
stk500_open(PROGRAMMER * pgm,char * port)656 static int stk500_open(PROGRAMMER * pgm, char * port)
657 {
658 union pinfo pinfo;
659 strcpy(pgm->port, port);
660 pinfo.baud = pgm->baudrate? pgm->baudrate: 115200;
661 if (serial_open(port, pinfo, &pgm->fd)==-1) {
662 return -1;
663 }
664
665 /*
666 * drain any extraneous input
667 */
668 stk500_drain(pgm, 0);
669
670 // MIB510 init
671 if (strcmp(ldata(lfirst(pgm->id)), "mib510") == 0 &&
672 mib510_isp(pgm, 1) != 0) {
673 serial_close(&pgm->fd);
674 return -1;
675 }
676
677 if (stk500_getsync(pgm) < 0) {
678 serial_close(&pgm->fd);
679 return -1;
680 }
681
682 return 0;
683 }
684
685
stk500_close(PROGRAMMER * pgm)686 static void stk500_close(PROGRAMMER * pgm)
687 {
688 // MIB510 close
689 if (strcmp(ldata(lfirst(pgm->id)), "mib510") == 0)
690 (void)mib510_isp(pgm, 0);
691
692 serial_close(&pgm->fd);
693 pgm->fd.ifd = -1;
694 }
695
696
stk500_loadaddr(PROGRAMMER * pgm,AVRMEM * mem,unsigned int addr)697 static int stk500_loadaddr(PROGRAMMER * pgm, AVRMEM * mem, unsigned int addr)
698 {
699 unsigned char buf[16];
700 int tries;
701 unsigned char ext_byte;
702 OPCODE * lext;
703
704 tries = 0;
705 retry:
706 tries++;
707
708 /* To support flash > 64K words the correct Extended Address Byte is needed */
709 lext = mem->op[AVR_OP_LOAD_EXT_ADDR];
710 if (lext != NULL) {
711 ext_byte = (addr >> 16) & 0xff;
712 if (ext_byte != PDATA(pgm)->ext_addr_byte) {
713 /* Either this is the first addr load, or a 64K word boundary is
714 * crossed, so set the ext addr byte */
715 avr_set_bits(lext, buf);
716 avr_set_addr(lext, buf, addr);
717 stk500_cmd(pgm, buf, buf);
718 PDATA(pgm)->ext_addr_byte = ext_byte;
719 }
720 }
721
722 buf[0] = Cmnd_STK_LOAD_ADDRESS;
723 // Workaround for the infamous ';' bug in the Prusa3D usb to serial converter.
724 // Send the binary data by nibbles to avoid transmitting the ';' character.
725 buf[1] = addr & 0x0f;
726 buf[2] = addr & 0xf0;
727 buf[3] = (addr >> 8) & 0x0f;
728 buf[4] = (addr >> 8) & 0xf0;
729 buf[5] = Sync_CRC_EOP;
730 stk500_send(pgm, buf, 6);
731
732 if (stk500_recv(pgm, buf, 1) < 0)
733 return -1;
734 if (buf[0] == Resp_STK_NOSYNC) {
735 if (tries > 33) {
736 avrdude_message(MSG_INFO, "%s: stk500_loadaddr(): can't get into sync\n",
737 progname);
738 return -1;
739 }
740 if (stk500_getsync(pgm) < 0)
741 return -1;
742 goto retry;
743 }
744 else if (buf[0] != Resp_STK_INSYNC) {
745 avrdude_message(MSG_INFO, "%s: stk500_loadaddr(): (a) protocol error, "
746 "expect=0x%02x, resp=0x%02x\n",
747 progname, Resp_STK_INSYNC, buf[0]);
748 return -1;
749 }
750
751 if (stk500_recv(pgm, buf, 1) < 0)
752 return -1;
753 if (buf[0] == Resp_STK_OK) {
754 return 0;
755 }
756
757 avrdude_message(MSG_INFO, "%s: loadaddr(): (b) protocol error, "
758 "expect=0x%02x, resp=0x%02x\n",
759 progname, Resp_STK_INSYNC, buf[0]);
760
761 return -1;
762 }
763
764
stk500_paged_write(PROGRAMMER * pgm,AVRPART * p,AVRMEM * m,unsigned int page_size,unsigned int addr,unsigned int n_bytes)765 static int stk500_paged_write(PROGRAMMER * pgm, AVRPART * p, AVRMEM * m,
766 unsigned int page_size,
767 unsigned int addr, unsigned int n_bytes)
768 {
769 unsigned char *buf = alloca(page_size + 16);
770 int memtype;
771 int a_div;
772 int block_size;
773 int tries;
774 unsigned int n;
775 unsigned int i, j;
776 unsigned int prusa3d_semicolon_workaround_round = 0;
777 bool has_semicolon = false;
778
779 if (strcmp(m->desc, "flash") == 0) {
780 memtype = 'F';
781 }
782 else if (strcmp(m->desc, "eeprom") == 0) {
783 memtype = 'E';
784 }
785 else {
786 return -2;
787 }
788
789 if ((m->op[AVR_OP_LOADPAGE_LO]) || (m->op[AVR_OP_READ_LO]))
790 a_div = 2;
791 else
792 a_div = 1;
793
794 n = addr + n_bytes;
795 #if 0
796 avrdude_message(MSG_INFO, "n_bytes = %d\n"
797 "n = %u\n"
798 "a_div = %d\n"
799 "page_size = %d\n",
800 n_bytes, n, a_div, page_size);
801 #endif
802
803 for (; addr < n; addr += block_size) {
804 // MIB510 uses fixed blocks size of 256 bytes
805 if (strcmp(ldata(lfirst(pgm->id)), "mib510") == 0) {
806 block_size = 256;
807 } else {
808 if (n - addr < page_size)
809 block_size = n - addr;
810 else
811 block_size = page_size;
812 }
813 tries = 0;
814 retry:
815 tries++;
816 stk500_loadaddr(pgm, m, addr/a_div);
817
818 for (i = 0; i < n_bytes; ++ i)
819 if (m->buf[addr + i] == ';') {
820 has_semicolon = true;
821 break;
822 }
823
824 for (prusa3d_semicolon_workaround_round = 0; prusa3d_semicolon_workaround_round < (has_semicolon ? 2u : 1u); prusa3d_semicolon_workaround_round++) {
825 /* build command block and avoid multiple send commands as it leads to a crash
826 of the silabs usb serial driver on mac os x */
827 i = 0;
828 buf[i++] = Cmnd_STK_PROG_PAGE;
829 // Workaround for the infamous ';' bug in the Prusa3D usb to serial converter.
830 // Send the binary data by nibbles to avoid transmitting the ';' character.
831 buf[i++] = (block_size >> 8) & 0xf0;
832 buf[i++] = (block_size >> 8) & 0x0f;
833 buf[i++] = block_size & 0xf0;
834 buf[i++] = block_size & 0x0f;
835 buf[i++] = memtype;
836 if (has_semicolon) {
837 for (j = 0; j < (unsigned)block_size; ++i, ++ j) {
838 buf[i] = m->buf[addr + j];
839 if (buf[i] == ';')
840 buf[i] |= (prusa3d_semicolon_workaround_round ? 0xf0 : 0x0f);
841 }
842 } else {
843 memcpy(&buf[i], &m->buf[addr], block_size);
844 i += block_size;
845 }
846 buf[i++] = Sync_CRC_EOP;
847 stk500_send( pgm, buf, i);
848
849 if (stk500_recv(pgm, buf, 1) < 0)
850 return -1;
851 if (buf[0] == Resp_STK_NOSYNC) {
852 if (tries > 33) {
853 avrdude_message(MSG_INFO, "\n%s: stk500_paged_write(): can't get into sync\n",
854 progname);
855 return -3;
856 }
857 if (stk500_getsync(pgm) < 0)
858 return -1;
859 goto retry;
860 }
861 else if (buf[0] != Resp_STK_INSYNC) {
862 avrdude_message(MSG_INFO, "\n%s: stk500_paged_write(): (a) protocol error, "
863 "expect=0x%02x, resp=0x%02x\n",
864 progname, Resp_STK_INSYNC, buf[0]);
865 return -4;
866 }
867
868 if (stk500_recv(pgm, buf, 1) < 0)
869 return -1;
870 if (buf[0] != Resp_STK_OK) {
871 avrdude_message(MSG_INFO, "\n%s: stk500_paged_write(): (a) protocol error, "
872 "expect=0x%02x, resp=0x%02x\n",
873 progname, Resp_STK_INSYNC, buf[0]);
874 return -5;
875 }
876 }
877 }
878
879 return n_bytes;
880 }
881
stk500_paged_load(PROGRAMMER * pgm,AVRPART * p,AVRMEM * m,unsigned int page_size,unsigned int addr,unsigned int n_bytes)882 static int stk500_paged_load(PROGRAMMER * pgm, AVRPART * p, AVRMEM * m,
883 unsigned int page_size,
884 unsigned int addr, unsigned int n_bytes)
885 {
886 unsigned char buf[16];
887 int memtype;
888 int a_div;
889 int tries;
890 unsigned int n;
891 int block_size;
892
893 if (strcmp(m->desc, "flash") == 0) {
894 memtype = 'F';
895 }
896 else if (strcmp(m->desc, "eeprom") == 0) {
897 memtype = 'E';
898 }
899 else {
900 return -2;
901 }
902
903 if ((m->op[AVR_OP_LOADPAGE_LO]) || (m->op[AVR_OP_READ_LO]))
904 a_div = 2;
905 else
906 a_div = 1;
907
908 n = addr + n_bytes;
909 for (; addr < n; addr += block_size) {
910 // MIB510 uses fixed blocks size of 256 bytes
911 if (strcmp(ldata(lfirst(pgm->id)), "mib510") == 0) {
912 block_size = 256;
913 } else {
914 if (n - addr < page_size)
915 block_size = n - addr;
916 else
917 block_size = page_size;
918 }
919
920 tries = 0;
921 retry:
922 tries++;
923 stk500_loadaddr(pgm, m, addr/a_div);
924 buf[0] = Cmnd_STK_READ_PAGE;
925 // Workaround for the infamous ';' bug in the Prusa3D usb to serial converter.
926 // Send the binary data by nibbles to avoid transmitting the ';' character.
927 buf[1] = (block_size >> 8) & 0xf0;
928 buf[2] = (block_size >> 8) & 0x0f;
929 buf[3] = block_size & 0xf0;
930 buf[4] = block_size & 0x0f;
931 buf[5] = memtype;
932 buf[6] = Sync_CRC_EOP;
933 stk500_send(pgm, buf, 7);
934
935 if (stk500_recv(pgm, buf, 1) < 0)
936 return -1;
937 if (buf[0] == Resp_STK_NOSYNC) {
938 if (tries > 33) {
939 avrdude_message(MSG_INFO, "\n%s: stk500_paged_load(): can't get into sync\n",
940 progname);
941 return -3;
942 }
943 if (stk500_getsync(pgm) < 0)
944 return -1;
945 goto retry;
946 }
947 else if (buf[0] != Resp_STK_INSYNC) {
948 avrdude_message(MSG_INFO, "\n%s: stk500_paged_load(): (a) protocol error, "
949 "expect=0x%02x, resp=0x%02x\n",
950 progname, Resp_STK_INSYNC, buf[0]);
951 return -4;
952 }
953
954 if (stk500_recv(pgm, &m->buf[addr], block_size) < 0)
955 return -1;
956
957 if (stk500_recv(pgm, buf, 1) < 0)
958 return -1;
959
960 if(strcmp(ldata(lfirst(pgm->id)), "mib510") == 0) {
961 if (buf[0] != Resp_STK_INSYNC) {
962 avrdude_message(MSG_INFO, "\n%s: stk500_paged_load(): (a) protocol error, "
963 "expect=0x%02x, resp=0x%02x\n",
964 progname, Resp_STK_INSYNC, buf[0]);
965 return -5;
966 }
967 }
968 else {
969 if (buf[0] != Resp_STK_OK) {
970 avrdude_message(MSG_INFO, "\n%s: stk500_paged_load(): (a) protocol error, "
971 "expect=0x%02x, resp=0x%02x\n",
972 progname, Resp_STK_OK, buf[0]);
973 return -5;
974 }
975 }
976 }
977
978 return n_bytes;
979 }
980
981
stk500_set_vtarget(PROGRAMMER * pgm,double v)982 static int stk500_set_vtarget(PROGRAMMER * pgm, double v)
983 {
984 unsigned uaref, utarg;
985
986 utarg = (unsigned)((v + 0.049) * 10);
987
988 if (stk500_getparm(pgm, Parm_STK_VADJUST, &uaref) != 0) {
989 avrdude_message(MSG_INFO, "%s: stk500_set_vtarget(): cannot obtain V[aref]\n",
990 progname);
991 return -1;
992 }
993
994 if (uaref > utarg) {
995 avrdude_message(MSG_INFO, "%s: stk500_set_vtarget(): reducing V[aref] from %.1f to %.1f\n",
996 progname, uaref / 10.0, v);
997 if (stk500_setparm(pgm, Parm_STK_VADJUST, utarg)
998 != 0)
999 return -1;
1000 }
1001 return stk500_setparm(pgm, Parm_STK_VTARGET, utarg);
1002 }
1003
1004
stk500_set_varef(PROGRAMMER * pgm,unsigned int chan,double v)1005 static int stk500_set_varef(PROGRAMMER * pgm, unsigned int chan /* unused */,
1006 double v)
1007 {
1008 unsigned uaref, utarg;
1009
1010 uaref = (unsigned)((v + 0.049) * 10);
1011
1012 if (stk500_getparm(pgm, Parm_STK_VTARGET, &utarg) != 0) {
1013 avrdude_message(MSG_INFO, "%s: stk500_set_varef(): cannot obtain V[target]\n",
1014 progname);
1015 return -1;
1016 }
1017
1018 if (uaref > utarg) {
1019 avrdude_message(MSG_INFO, "%s: stk500_set_varef(): V[aref] must not be greater than "
1020 "V[target] = %.1f\n",
1021 progname, utarg / 10.0);
1022 return -1;
1023 }
1024 return stk500_setparm(pgm, Parm_STK_VADJUST, uaref);
1025 }
1026
1027
stk500_set_fosc(PROGRAMMER * pgm,double v)1028 static int stk500_set_fosc(PROGRAMMER * pgm, double v)
1029 {
1030 unsigned prescale, cmatch, fosc;
1031 static unsigned ps[] = {
1032 1, 8, 32, 64, 128, 256, 1024
1033 };
1034 int idx, rc;
1035
1036 prescale = cmatch = 0;
1037 if (v > 0.0) {
1038 if (v > STK500_XTAL / 2) {
1039 const char *unit;
1040 if (v > 1e6) {
1041 v /= 1e6;
1042 unit = "MHz";
1043 } else if (v > 1e3) {
1044 v /= 1e3;
1045 unit = "kHz";
1046 } else
1047 unit = "Hz";
1048 avrdude_message(MSG_INFO, "%s: stk500_set_fosc(): f = %.3f %s too high, using %.3f MHz\n",
1049 progname, v, unit, STK500_XTAL / 2e6);
1050 fosc = STK500_XTAL / 2;
1051 } else
1052 fosc = (unsigned)v;
1053
1054 for (idx = 0; idx < sizeof(ps) / sizeof(ps[0]); idx++) {
1055 if (fosc >= STK500_XTAL / (256 * ps[idx] * 2)) {
1056 /* this prescaler value can handle our frequency */
1057 prescale = idx + 1;
1058 cmatch = (unsigned)(STK500_XTAL / (2 * fosc * ps[idx])) - 1;
1059 break;
1060 }
1061 }
1062 if (idx == sizeof(ps) / sizeof(ps[0])) {
1063 avrdude_message(MSG_INFO, "%s: stk500_set_fosc(): f = %u Hz too low, %u Hz min\n",
1064 progname, fosc, STK500_XTAL / (256 * 1024 * 2));
1065 return -1;
1066 }
1067 }
1068
1069 if ((rc = stk500_setparm(pgm, Parm_STK_OSC_PSCALE, prescale)) != 0
1070 || (rc = stk500_setparm(pgm, Parm_STK_OSC_CMATCH, cmatch)) != 0)
1071 return rc;
1072
1073 return 0;
1074 }
1075
1076
1077 /* This code assumes that each count of the SCK duration parameter
1078 represents 8/f, where f is the clock frequency of the STK500 master
1079 processors (not the target). This number comes from Atmel
1080 application note AVR061. It appears that the STK500 bit bangs SCK.
1081 For small duration values, the actual SCK width is larger than
1082 expected. As the duration value increases, the SCK width error
1083 diminishes. */
stk500_set_sck_period(PROGRAMMER * pgm,double v)1084 static int stk500_set_sck_period(PROGRAMMER * pgm, double v)
1085 {
1086 int dur;
1087 double min, max;
1088
1089 min = 8.0 / STK500_XTAL;
1090 max = 255 * min;
1091 dur = (int)(v / min + 0.5);
1092
1093 if (v < min) {
1094 dur = 1;
1095 avrdude_message(MSG_INFO, "%s: stk500_set_sck_period(): p = %.1f us too small, using %.1f us\n",
1096 progname, v / 1e-6, dur * min / 1e-6);
1097 } else if (v > max) {
1098 dur = 255;
1099 avrdude_message(MSG_INFO, "%s: stk500_set_sck_period(): p = %.1f us too large, using %.1f us\n",
1100 progname, v / 1e-6, dur * min / 1e-6);
1101 }
1102
1103 return stk500_setparm(pgm, Parm_STK_SCK_DURATION, dur);
1104 }
1105
1106
stk500_getparm(PROGRAMMER * pgm,unsigned parm,unsigned * value)1107 static int stk500_getparm(PROGRAMMER * pgm, unsigned parm, unsigned * value)
1108 {
1109 unsigned char buf[16];
1110 unsigned v;
1111 int tries = 0;
1112
1113 retry:
1114 tries++;
1115 buf[0] = Cmnd_STK_GET_PARAMETER;
1116 buf[1] = parm;
1117 buf[2] = Sync_CRC_EOP;
1118
1119 stk500_send(pgm, buf, 3);
1120
1121 if (stk500_recv(pgm, buf, 1) < 0)
1122 return -1;
1123 if (buf[0] == Resp_STK_NOSYNC) {
1124 if (tries > 33) {
1125 avrdude_message(MSG_INFO, "\n%s: stk500_getparm(): can't get into sync\n",
1126 progname);
1127 return -1;
1128 }
1129 if (stk500_getsync(pgm) < 0)
1130 return -1;
1131 goto retry;
1132 }
1133 else if (buf[0] != Resp_STK_INSYNC) {
1134 avrdude_message(MSG_INFO, "\n%s: stk500_getparm(): (a) protocol error, "
1135 "expect=0x%02x, resp=0x%02x\n",
1136 progname, Resp_STK_INSYNC, buf[0]);
1137 return -2;
1138 }
1139
1140 if (stk500_recv(pgm, buf, 1) < 0)
1141 return -1;
1142 v = buf[0];
1143
1144 if (stk500_recv(pgm, buf, 1) < 0)
1145 return -1;
1146 if (buf[0] == Resp_STK_FAILED) {
1147 avrdude_message(MSG_INFO, "\n%s: stk500_getparm(): parameter 0x%02x failed\n",
1148 progname, v);
1149 return -3;
1150 }
1151 else if (buf[0] != Resp_STK_OK) {
1152 avrdude_message(MSG_INFO, "\n%s: stk500_getparm(): (a) protocol error, "
1153 "expect=0x%02x, resp=0x%02x\n",
1154 progname, Resp_STK_INSYNC, buf[0]);
1155 return -3;
1156 }
1157
1158 *value = v;
1159
1160 return 0;
1161 }
1162
1163
stk500_setparm(PROGRAMMER * pgm,unsigned parm,unsigned value)1164 static int stk500_setparm(PROGRAMMER * pgm, unsigned parm, unsigned value)
1165 {
1166 unsigned char buf[16];
1167 int tries = 0;
1168
1169 retry:
1170 tries++;
1171 buf[0] = Cmnd_STK_SET_PARAMETER;
1172 buf[1] = parm;
1173 buf[2] = value;
1174 buf[3] = Sync_CRC_EOP;
1175
1176 stk500_send(pgm, buf, 4);
1177
1178 if (stk500_recv(pgm, buf, 1) < 0)
1179 return -1;
1180 if (buf[0] == Resp_STK_NOSYNC) {
1181 if (tries > 33) {
1182 avrdude_message(MSG_INFO, "\n%s: stk500_setparm(): can't get into sync\n",
1183 progname);
1184 return -1;
1185 }
1186 if (stk500_getsync(pgm) < 0)
1187 return -1;
1188 goto retry;
1189 }
1190 else if (buf[0] != Resp_STK_INSYNC) {
1191 avrdude_message(MSG_INFO, "\n%s: stk500_setparm(): (a) protocol error, "
1192 "expect=0x%02x, resp=0x%02x\n",
1193 progname, Resp_STK_INSYNC, buf[0]);
1194 return -2;
1195 }
1196
1197 if (stk500_recv(pgm, buf, 1) < 0)
1198 return -1;
1199 if (buf[0] == Resp_STK_OK)
1200 return 0;
1201
1202 parm = buf[0]; /* if not STK_OK, we've been echoed parm here */
1203 if (stk500_recv(pgm, buf, 1) < 0)
1204 return -1;
1205 if (buf[0] == Resp_STK_FAILED) {
1206 avrdude_message(MSG_INFO, "\n%s: stk500_setparm(): parameter 0x%02x failed\n",
1207 progname, parm);
1208 return -3;
1209 }
1210 else {
1211 avrdude_message(MSG_INFO, "\n%s: stk500_setparm(): (a) protocol error, "
1212 "expect=0x%02x, resp=0x%02x\n",
1213 progname, Resp_STK_INSYNC, buf[0]);
1214 return -3;
1215 }
1216 }
1217
1218
stk500_display(PROGRAMMER * pgm,const char * p)1219 static void stk500_display(PROGRAMMER * pgm, const char * p)
1220 {
1221 unsigned maj, min, hdw, topcard;
1222
1223 stk500_getparm(pgm, Parm_STK_HW_VER, &hdw);
1224 stk500_getparm(pgm, Parm_STK_SW_MAJOR, &maj);
1225 stk500_getparm(pgm, Parm_STK_SW_MINOR, &min);
1226 stk500_getparm(pgm, Param_STK500_TOPCARD_DETECT, &topcard);
1227
1228 avrdude_message(MSG_INFO, "%sHardware Version: %d\n", p, hdw);
1229 avrdude_message(MSG_INFO, "%sFirmware Version: %d.%d\n", p, maj, min);
1230 if (topcard < 3) {
1231 const char *n = "Unknown";
1232
1233 switch (topcard) {
1234 case 1:
1235 n = "STK502";
1236 break;
1237
1238 case 2:
1239 n = "STK501";
1240 break;
1241 }
1242 avrdude_message(MSG_INFO, "%sTopcard : %s\n", p, n);
1243 }
1244 stk500_print_parms1(pgm, p);
1245
1246 return;
1247 }
1248
1249
stk500_print_parms1(PROGRAMMER * pgm,const char * p)1250 static void stk500_print_parms1(PROGRAMMER * pgm, const char * p)
1251 {
1252 unsigned vtarget, vadjust, osc_pscale, osc_cmatch, sck_duration;
1253
1254 stk500_getparm(pgm, Parm_STK_VTARGET, &vtarget);
1255 stk500_getparm(pgm, Parm_STK_VADJUST, &vadjust);
1256 stk500_getparm(pgm, Parm_STK_OSC_PSCALE, &osc_pscale);
1257 stk500_getparm(pgm, Parm_STK_OSC_CMATCH, &osc_cmatch);
1258 stk500_getparm(pgm, Parm_STK_SCK_DURATION, &sck_duration);
1259
1260 avrdude_message(MSG_INFO, "%sVtarget : %.1f V\n", p, vtarget / 10.0);
1261 avrdude_message(MSG_INFO, "%sVaref : %.1f V\n", p, vadjust / 10.0);
1262 avrdude_message(MSG_INFO, "%sOscillator : ", p);
1263 if (osc_pscale == 0)
1264 avrdude_message(MSG_INFO, "Off\n");
1265 else {
1266 int prescale = 1;
1267 double f = STK500_XTAL / 2;
1268 const char *unit;
1269
1270 switch (osc_pscale) {
1271 case 2: prescale = 8; break;
1272 case 3: prescale = 32; break;
1273 case 4: prescale = 64; break;
1274 case 5: prescale = 128; break;
1275 case 6: prescale = 256; break;
1276 case 7: prescale = 1024; break;
1277 }
1278 f /= prescale;
1279 f /= (osc_cmatch + 1);
1280 if (f > 1e6) {
1281 f /= 1e6;
1282 unit = "MHz";
1283 } else if (f > 1e3) {
1284 f /= 1000;
1285 unit = "kHz";
1286 } else
1287 unit = "Hz";
1288 avrdude_message(MSG_INFO, "%.3f %s\n", f, unit);
1289 }
1290 avrdude_message(MSG_INFO, "%sSCK period : %.1f us\n", p,
1291 sck_duration * 8.0e6 / STK500_XTAL + 0.05);
1292
1293 return;
1294 }
1295
1296
stk500_print_parms(PROGRAMMER * pgm)1297 static void stk500_print_parms(PROGRAMMER * pgm)
1298 {
1299 stk500_print_parms1(pgm, "");
1300 }
1301
stk500_setup(PROGRAMMER * pgm)1302 static void stk500_setup(PROGRAMMER * pgm)
1303 {
1304 if ((pgm->cookie = malloc(sizeof(struct pdata))) == 0) {
1305 avrdude_message(MSG_INFO, "%s: stk500_setup(): Out of memory allocating private data\n",
1306 progname);
1307 return;
1308 }
1309 memset(pgm->cookie, 0, sizeof(struct pdata));
1310 PDATA(pgm)->ext_addr_byte = 0xff; /* Ensures it is programmed before
1311 * first memory address */
1312 }
1313
stk500_teardown(PROGRAMMER * pgm)1314 static void stk500_teardown(PROGRAMMER * pgm)
1315 {
1316 free(pgm->cookie);
1317 }
1318
1319 const char stk500_desc[] = "Atmel STK500 Version 1.x firmware";
1320
stk500_initpgm(PROGRAMMER * pgm)1321 void stk500_initpgm(PROGRAMMER * pgm)
1322 {
1323 strcpy(pgm->type, "STK500");
1324
1325 /*
1326 * mandatory functions
1327 */
1328 pgm->initialize = stk500_initialize;
1329 pgm->display = stk500_display;
1330 pgm->enable = stk500_enable;
1331 pgm->disable = stk500_disable;
1332 pgm->program_enable = stk500_program_enable;
1333 pgm->chip_erase = stk500_chip_erase;
1334 pgm->cmd = stk500_cmd;
1335 pgm->open = stk500_open;
1336 pgm->close = stk500_close;
1337 pgm->read_byte = avr_read_byte_default;
1338 pgm->write_byte = avr_write_byte_default;
1339
1340 /*
1341 * optional functions
1342 */
1343 pgm->paged_write = stk500_paged_write;
1344 pgm->paged_load = stk500_paged_load;
1345 pgm->print_parms = stk500_print_parms;
1346 pgm->set_vtarget = stk500_set_vtarget;
1347 pgm->set_varef = stk500_set_varef;
1348 pgm->set_fosc = stk500_set_fosc;
1349 pgm->set_sck_period = stk500_set_sck_period;
1350 pgm->setup = stk500_setup;
1351 pgm->teardown = stk500_teardown;
1352 pgm->page_size = 256;
1353 }
1354