-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstk500v2.c
4723 lines (4090 loc) · 136 KB
/
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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* avrdude - A Downloader/Uploader for AVR device programmers
* Copyright (C) 2005 Erik Walthinsen
* Copyright (C) 2002-2004 Brian S. Dean <bsd@bsdhome.com>
* Copyright (C) 2006 David Moore
* Copyright (C) 2006,2007,2010 Joerg Wunsch <j@uriah.heep.sax.de>
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/* $Id: stk500v2.c 1342 2014-11-19 17:23:14Z joerg_wunsch $ */
/* Based on Id: stk500.c,v 1.46 2004/12/22 01:52:45 bdean Exp */
/*
* avrdude interface for Atmel STK500V2 programmer
*
* As the AVRISP mkII device is basically an STK500v2 one that can
* only talk across USB, and that misses any kind of framing protocol,
* this is handled here as well.
*
* Note: most commands use the "universal command" feature of the
* programmer in a "pass through" mode, exceptions are "program
* enable", "paged read", and "paged write".
*
*/
#include "ac_cfg.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
#include <unistd.h>
#include <sys/time.h>
#include <time.h>
#include "avrdude.h"
#include "libavrdude.h"
#include "stk500_private.h" // temp until all code converted
#include "stk500v2.h"
#include "stk500v2_private.h"
#include "usbdevs.h"
/*
* We need to import enough from the JTAG ICE mkII definitions to be
* able to talk to the ICE, query some parameters etc. The macro
* JTAGMKII_PRIVATE_EXPORTED limits the amount of definitions that
* jtagmkII_private.h will export, so to avoid conflicts with those
* names that are identical to the STK500v2 ones.
*/
#include "jtagmkII.h" // public interfaces from jtagmkII.c
#define JTAGMKII_PRIVATE_EXPORTED
#include "jtagmkII_private.h"
#include "jtag3.h" // public interfaces from jtagmkII.c
#define JTAG3_PRIVATE_EXPORTED
#include "jtag3_private.h"
#define STK500V2_XTAL 7372800U
// Timeout (in seconds) for waiting for serial response
#define SERIAL_TIMEOUT 2
// Retry count
#define RETRIES 5
#if 0
#define DEBUG(...) avrdude_message(MSG_INFO, __VA_ARGS__)
#else
#define DEBUG(...)
#endif
#if 0
#define DEBUGRECV(...) avrdude_message(MSG_INFO, __VA_ARGS__)
#else
#define DEBUGRECV(...)
#endif
enum hvmode
{
PPMODE, HVSPMODE
};
#define PDATA(pgm) ((struct pdata *)(pgm->cookie))
/*
* Data structure for displaying STK600 routing and socket cards.
*/
struct carddata
{
int id;
const char *name;
};
static const char *pgmname[] =
{
"unknown",
"STK500",
"AVRISP",
"AVRISP mkII",
"JTAG ICE mkII",
"STK600",
"JTAGICE3",
};
struct jtagispentry
{
unsigned char cmd;
unsigned short size;
#define SZ_READ_FLASH_EE USHRT_MAX
#define SZ_SPI_MULTI (USHRT_MAX - 1)
};
static const struct jtagispentry jtagispcmds[] = {
/* generic */
{ CMD_SET_PARAMETER, 2 },
{ CMD_GET_PARAMETER, 3 },
{ CMD_OSCCAL, 2 },
{ CMD_LOAD_ADDRESS, 2 },
/* ISP mode */
{ CMD_ENTER_PROGMODE_ISP, 2 },
{ CMD_LEAVE_PROGMODE_ISP, 2 },
{ CMD_CHIP_ERASE_ISP, 2 },
{ CMD_PROGRAM_FLASH_ISP, 2 },
{ CMD_READ_FLASH_ISP, SZ_READ_FLASH_EE },
{ CMD_PROGRAM_EEPROM_ISP, 2 },
{ CMD_READ_EEPROM_ISP, SZ_READ_FLASH_EE },
{ CMD_PROGRAM_FUSE_ISP, 3 },
{ CMD_READ_FUSE_ISP, 4 },
{ CMD_PROGRAM_LOCK_ISP, 3 },
{ CMD_READ_LOCK_ISP, 4 },
{ CMD_READ_SIGNATURE_ISP, 4 },
{ CMD_READ_OSCCAL_ISP, 4 },
{ CMD_SPI_MULTI, SZ_SPI_MULTI },
/* all HV modes */
{ CMD_SET_CONTROL_STACK, 2 },
/* HVSP mode */
{ CMD_ENTER_PROGMODE_HVSP, 2 },
{ CMD_LEAVE_PROGMODE_HVSP, 2 },
{ CMD_CHIP_ERASE_HVSP, 2 },
{ CMD_PROGRAM_FLASH_HVSP, 2 },
{ CMD_READ_FLASH_HVSP, SZ_READ_FLASH_EE },
{ CMD_PROGRAM_EEPROM_HVSP, 2 },
{ CMD_READ_EEPROM_HVSP, SZ_READ_FLASH_EE },
{ CMD_PROGRAM_FUSE_HVSP, 2 },
{ CMD_READ_FUSE_HVSP, 3 },
{ CMD_PROGRAM_LOCK_HVSP, 2 },
{ CMD_READ_LOCK_HVSP, 3 },
{ CMD_READ_SIGNATURE_HVSP, 3 },
{ CMD_READ_OSCCAL_HVSP, 3 },
/* PP mode */
{ CMD_ENTER_PROGMODE_PP, 2 },
{ CMD_LEAVE_PROGMODE_PP, 2 },
{ CMD_CHIP_ERASE_PP, 2 },
{ CMD_PROGRAM_FLASH_PP, 2 },
{ CMD_READ_FLASH_PP, SZ_READ_FLASH_EE },
{ CMD_PROGRAM_EEPROM_PP, 2 },
{ CMD_READ_EEPROM_PP, SZ_READ_FLASH_EE },
{ CMD_PROGRAM_FUSE_PP, 2 },
{ CMD_READ_FUSE_PP, 3 },
{ CMD_PROGRAM_LOCK_PP, 2 },
{ CMD_READ_LOCK_PP, 3 },
{ CMD_READ_SIGNATURE_PP, 3 },
{ CMD_READ_OSCCAL_PP, 3 },
};
/*
* From XML file:
<REVISION>
<RC_ID_MAJOR>0</RC_ID_MAJOR>
<RC_ID_MINOR>56</RC_ID_MINOR>
<EC_ID_MAJOR>0</EC_ID_MAJOR>
<EC_ID_MINOR>1</EC_ID_MINOR>
</REVISION>
*/
/*
* These two tables can be semi-automatically updated from
* targetboards.xml using tools/get-stk600-cards.xsl.
*/
static const struct carddata routing_cards[] =
{
{ 0x01, "STK600-RC020T-1" },
{ 0x03, "STK600-RC028T-3" },
{ 0x05, "STK600-RC040M-5" },
{ 0x08, "STK600-RC020T-8" },
{ 0x0A, "STK600-RC040M-4" },
{ 0x0C, "STK600-RC008T-2" },
{ 0x0D, "STK600-RC028M-6" },
{ 0x10, "STK600-RC064M-10" },
{ 0x11, "STK600-RC100M-11" },
{ 0x13, "STK600-RC100X-13" },
{ 0x15, "STK600-RC044X-15" },
{ 0x18, "STK600-RC100M-18" },
{ 0x19, "STK600-RCPWM-19" },
{ 0x1A, "STK600-RC064X-14" },
{ 0x1B, "STK600-RC032U-20" },
{ 0x1C, "STK600-RC014T-12" },
{ 0x1E, "STK600-RC064U-17" },
{ 0x1F, "STK600-RCuC3B0-21" },
{ 0x20, "STK600-RCPWM-22" },
{ 0x21, "STK600-RC020T-23" },
{ 0x22, "STK600-RC044M-24" },
{ 0x23, "STK600-RC044U-25" },
{ 0x24, "STK600-RCPWM-26" },
{ 0x25, "STK600-RCuC3B48-27" },
{ 0x27, "STK600-RC032M-29" },
{ 0x28, "STK600-RC044M-30" },
{ 0x29, "STK600-RC044M-31" },
{ 0x2A, "STK600-RC014T-42" },
{ 0x2B, "STK600-RC020T-43" },
{ 0x30, "STK600-RCUC3A144-32" },
{ 0x34, "STK600-RCUC3L0-34" },
{ 0x38, "STK600-RCUC3C0-36" },
{ 0x3B, "STK600-RCUC3C0-37" },
{ 0x3E, "STK600-RCUC3A144-33" },
{ 0x46, "STK600-RCuC3A100-28" },
{ 0x55, "STK600-RC064M-9" },
{ 0x88, "STK600-RCUC3C1-38" },
{ 0x8B, "STK600-RCUC3C1-39" },
{ 0xA0, "STK600-RC008T-7" },
{ 0xB8, "STK600-RCUC3C2-40" },
{ 0xBB, "STK600-RCUC3C2-41" },
};
static const struct carddata socket_cards[] =
{
{ 0x01, "STK600-TQFP48" },
{ 0x02, "STK600-TQFP32" },
{ 0x03, "STK600-TQFP100" },
{ 0x04, "STK600-SOIC" },
{ 0x06, "STK600-TQFP144" },
{ 0x09, "STK600-TinyX3U" },
{ 0x0C, "STK600-TSSOP44" },
{ 0x0D, "STK600-TQFP44" },
{ 0x0E, "STK600-TQFP64-2" },
{ 0x0F, "STK600-ATMEGA2560" },
{ 0x15, "STK600-MLF64" },
{ 0x16, "STK600-ATXMEGAT0" },
{ 0x18, "QT600-ATMEGA324-QM64" },
{ 0x19, "STK600-ATMEGA128RFA1" },
{ 0x1A, "QT600-ATTINY88-QT8" },
{ 0x1B, "QT600-ATXMEGA128A1-QT16" },
{ 0x1C, "QT600-AT32UC3L-QM64" },
{ 0x1D, "STK600-HVE2" },
{ 0x1E, "STK600-ATTINY10" },
{ 0x55, "STK600-TQFP64" },
{ 0x69, "STK600-uC3-144" },
{ 0xF0, "STK600-ATXMEGA1281A1" },
{ 0xF1, "STK600-DIP" },
};
static int stk500v2_getparm(PROGRAMMER * pgm, unsigned char parm, unsigned char * value);
static int stk500v2_setparm(PROGRAMMER * pgm, unsigned char parm, unsigned char value);
static int stk500v2_getparm2(PROGRAMMER * pgm, unsigned char parm, unsigned int * value);
static int stk500v2_setparm2(PROGRAMMER * pgm, unsigned char parm, unsigned int value);
static int stk500v2_setparm_real(PROGRAMMER * pgm, unsigned char parm, unsigned char value);
static void stk500v2_print_parms1(PROGRAMMER * pgm, const char * p);
static int stk500v2_paged_load(PROGRAMMER * pgm, AVRPART * p, AVRMEM * m,
unsigned int page_size,
unsigned int addr, unsigned int n_bytes);
static int stk500v2_paged_write(PROGRAMMER * pgm, AVRPART * p, AVRMEM * m,
unsigned int page_size,
unsigned int addr, unsigned int n_bytes);
static unsigned int stk500v2_mode_for_pagesize(unsigned int pagesize);
static double stk500v2_sck_to_us(PROGRAMMER * pgm, unsigned char dur);
static int stk500v2_set_sck_period_mk2(PROGRAMMER * pgm, double v);
static int stk600_set_sck_period(PROGRAMMER * pgm, double v);
static void stk600_setup_xprog(PROGRAMMER * pgm);
static void stk600_setup_isp(PROGRAMMER * pgm);
static int stk600_xprog_program_enable(PROGRAMMER * pgm, AVRPART * p);
void stk500v2_setup(PROGRAMMER * pgm)
{
if ((pgm->cookie = malloc(sizeof(struct pdata))) == 0) {
avrdude_message(MSG_INFO, "%s: stk500v2_setup(): Out of memory allocating private data\n",
progname);
exit(1);
}
memset(pgm->cookie, 0, sizeof(struct pdata));
PDATA(pgm)->command_sequence = 1;
PDATA(pgm)->boot_start = ULONG_MAX;
}
static void stk500v2_jtagmkII_setup(PROGRAMMER * pgm)
{
void *mycookie, *theircookie;
if ((pgm->cookie = malloc(sizeof(struct pdata))) == 0) {
avrdude_message(MSG_INFO, "%s: stk500v2_setup(): Out of memory allocating private data\n",
progname);
exit(1);
}
memset(pgm->cookie, 0, sizeof(struct pdata));
PDATA(pgm)->command_sequence = 1;
/*
* Now, have the JTAG ICE mkII backend allocate its own private
* data. Store our own cookie in a safe place for the time being.
*/
mycookie = pgm->cookie;
jtagmkII_setup(pgm);
theircookie = pgm->cookie;
pgm->cookie = mycookie;
PDATA(pgm)->chained_pdata = theircookie;
}
static void stk500v2_jtag3_setup(PROGRAMMER * pgm)
{
void *mycookie, *theircookie;
if ((pgm->cookie = malloc(sizeof(struct pdata))) == 0) {
avrdude_message(MSG_INFO, "%s: stk500v2_setup(): Out of memory allocating private data\n",
progname);
exit(1);
}
memset(pgm->cookie, 0, sizeof(struct pdata));
PDATA(pgm)->command_sequence = 1;
/*
* Now, have the JTAGICE3 backend allocate its own private
* data. Store our own cookie in a safe place for the time being.
*/
mycookie = pgm->cookie;
jtag3_setup(pgm);
theircookie = pgm->cookie;
pgm->cookie = mycookie;
PDATA(pgm)->chained_pdata = theircookie;
}
void stk500v2_teardown(PROGRAMMER * pgm)
{
free(pgm->cookie);
}
static void stk500v2_jtagmkII_teardown(PROGRAMMER * pgm)
{
void *mycookie;
mycookie = pgm->cookie;
pgm->cookie = PDATA(pgm)->chained_pdata;
jtagmkII_teardown(pgm);
free(mycookie);
}
static void stk500v2_jtag3_teardown(PROGRAMMER * pgm)
{
void *mycookie;
mycookie = pgm->cookie;
pgm->cookie = PDATA(pgm)->chained_pdata;
jtag3_teardown(pgm);
free(mycookie);
}
static unsigned short
b2_to_u16(unsigned char *b)
{
unsigned short l;
l = b[0];
l += (unsigned)b[1] << 8;
return l;
}
static int stk500v2_send_mk2(PROGRAMMER * pgm, unsigned char * data, size_t len)
{
if (serial_send(&pgm->fd, data, len) != 0) {
avrdude_message(MSG_INFO, "%s: stk500_send_mk2(): failed to send command to serial port\n",progname);
return -1;
}
return 0;
}
static unsigned short get_jtagisp_return_size(unsigned char cmd)
{
int i;
for (i = 0; i < sizeof jtagispcmds / sizeof jtagispcmds[0]; i++)
if (jtagispcmds[i].cmd == cmd)
return jtagispcmds[i].size;
return 0;
}
/*
* Send the data as a JTAG ICE mkII encapsulated ISP packet.
* Unlike what AVR067 says, the packet gets a length of our
* response buffer prepended, and replies with RSP_SPI_DATA
* if successful.
*/
static int stk500v2_jtagmkII_send(PROGRAMMER * pgm, unsigned char * data, size_t len)
{
unsigned char *cmdbuf;
int rv;
unsigned short sz;
void *mycookie;
sz = get_jtagisp_return_size(data[0]);
if (sz == 0) {
avrdude_message(MSG_INFO, "%s: unsupported encapsulated ISP command: %#x\n",
progname, data[0]);
return -1;
}
if (sz == SZ_READ_FLASH_EE) {
/*
* For CMND_READ_FLASH_ISP and CMND_READ_EEPROM_ISP, extract the
* size of the return data from the request. Note that the
* request itself has the size in big endian format, while we are
* supposed to deliver it in little endian.
*/
sz = 3 + (data[1] << 8) + data[2];
} else if (sz == SZ_SPI_MULTI) {
/*
* CMND_SPI_MULTI has the Rx size encoded in its 3rd byte.
*/
sz = 3 + data[2];
}
if ((cmdbuf = malloc(len + 3)) == NULL) {
avrdude_message(MSG_INFO, "%s: out of memory for command packet\n",
progname);
exit(1);
}
mycookie = pgm->cookie;
pgm->cookie = PDATA(pgm)->chained_pdata;
cmdbuf[0] = CMND_ISP_PACKET;
cmdbuf[1] = sz & 0xff;
cmdbuf[2] = (sz >> 8) & 0xff;
memcpy(cmdbuf + 3, data, len);
rv = jtagmkII_send(pgm, cmdbuf, len + 3);
free(cmdbuf);
pgm->cookie = mycookie;
return rv;
}
/*
* Send the data as a JTAGICE3 encapsulated ISP packet.
*/
static int stk500v2_jtag3_send(PROGRAMMER * pgm, unsigned char * data, size_t len)
{
unsigned char *cmdbuf;
int rv;
void *mycookie;
if ((cmdbuf = malloc(len + 1)) == NULL) {
avrdude_message(MSG_INFO, "%s: out of memory for command packet\n",
progname);
exit(1);
}
mycookie = pgm->cookie;
pgm->cookie = PDATA(pgm)->chained_pdata;
cmdbuf[0] = SCOPE_AVR_ISP;
memcpy(cmdbuf + 1, data, len);
rv = jtag3_send(pgm, cmdbuf, len + 1);
free(cmdbuf);
pgm->cookie = mycookie;
return rv;
}
static int stk500v2_send(PROGRAMMER * pgm, unsigned char * data, size_t len)
{
unsigned char buf[275 + 6]; // max MESSAGE_BODY of 275 bytes, 6 bytes overhead
int i;
if (PDATA(pgm)->pgmtype == PGMTYPE_AVRISP_MKII ||
PDATA(pgm)->pgmtype == PGMTYPE_STK600)
return stk500v2_send_mk2(pgm, data, len);
else if (PDATA(pgm)->pgmtype == PGMTYPE_JTAGICE_MKII)
return stk500v2_jtagmkII_send(pgm, data, len);
else if (PDATA(pgm)->pgmtype == PGMTYPE_JTAGICE3)
return stk500v2_jtag3_send(pgm, data, len);
buf[0] = MESSAGE_START;
buf[1] = PDATA(pgm)->command_sequence;
buf[2] = len / 256;
buf[3] = len % 256;
buf[4] = TOKEN;
memcpy(buf+5, data, len);
// calculate the XOR checksum
buf[5+len] = 0;
for (i=0;i<5+len;i++)
buf[5+len] ^= buf[i];
DEBUG("STK500V2: stk500v2_send(");
for (i=0;i<len+6;i++) DEBUG("0x%02x ",buf[i]);
DEBUG(", %d)\n",len+6);
if (serial_send(&pgm->fd, buf, len+6) != 0) {
avrdude_message(MSG_INFO, "%s: stk500_send(): failed to send command to serial port\n",progname);
return -1;
}
return 0;
}
int stk500v2_drain(PROGRAMMER * pgm, int display)
{
return serial_drain(&pgm->fd, display);
}
static int stk500v2_recv_mk2(PROGRAMMER * pgm, unsigned char *msg,
size_t maxsize)
{
int rv;
rv = serial_recv(&pgm->fd, msg, maxsize);
if (rv < 0) {
avrdude_message(MSG_INFO, "%s: stk500v2_recv_mk2: error in USB receive\n", progname);
return -1;
}
return rv;
}
static int stk500v2_jtagmkII_recv(PROGRAMMER * pgm, unsigned char *msg,
size_t maxsize)
{
int rv;
unsigned char *jtagmsg;
void *mycookie;
mycookie = pgm->cookie;
pgm->cookie = PDATA(pgm)->chained_pdata;
rv = jtagmkII_recv(pgm, &jtagmsg);
pgm->cookie = mycookie;
if (rv <= 0) {
avrdude_message(MSG_INFO, "%s: stk500v2_jtagmkII_recv(): error in jtagmkII_recv()\n",
progname);
return -1;
}
if (rv - 1 > maxsize) {
avrdude_message(MSG_INFO, "%s: stk500v2_jtagmkII_recv(): got %u bytes, have only room for %u bytes\n",
progname, (unsigned)rv - 1, (unsigned)maxsize);
rv = maxsize;
}
switch (jtagmsg[0]) {
case RSP_SPI_DATA:
break;
case RSP_FAILED:
avrdude_message(MSG_INFO, "%s: stk500v2_jtagmkII_recv(): failed\n",
progname);
return -1;
case RSP_ILLEGAL_MCU_STATE:
avrdude_message(MSG_INFO, "%s: stk500v2_jtagmkII_recv(): illegal MCU state\n",
progname);
return -1;
default:
avrdude_message(MSG_INFO, "%s: stk500v2_jtagmkII_recv(): unknown status %d\n",
progname, jtagmsg[0]);
return -1;
}
memcpy(msg, jtagmsg + 1, rv - 1);
return rv;
}
static int stk500v2_jtag3_recv(PROGRAMMER * pgm, unsigned char *msg,
size_t maxsize)
{
int rv;
unsigned char *jtagmsg;
void *mycookie;
mycookie = pgm->cookie;
pgm->cookie = PDATA(pgm)->chained_pdata;
rv = jtag3_recv(pgm, &jtagmsg);
pgm->cookie = mycookie;
if (rv <= 0) {
avrdude_message(MSG_INFO, "%s: stk500v2_jtag3_recv(): error in jtagmkII_recv()\n",
progname);
return -1;
}
/* Getting more data than expected is a normal case for the EDBG
implementation of JTAGICE3, as they always request a full 512
octets from the ICE. Thus, only complain at high verbose
levels. */
if (rv - 1 > maxsize) {
avrdude_message(MSG_DEBUG, "%s: stk500v2_jtag3_recv(): got %u bytes, have only room for %u bytes\n",
progname, (unsigned)rv - 1, (unsigned)maxsize);
rv = maxsize;
}
if (jtagmsg[0] != SCOPE_AVR_ISP) {
avrdude_message(MSG_INFO, "%s: stk500v2_jtag3_recv(): message is not AVR ISP: 0x%02x\n",
progname, jtagmsg[0]);
free(jtagmsg);
return -1;
}
memcpy(msg, jtagmsg + 1, rv - 1);
free(jtagmsg);
return rv;
}
static int stk500v2_recv(PROGRAMMER * pgm, unsigned char *msg, size_t maxsize) {
enum states { sINIT, sSTART, sSEQNUM, sSIZE1, sSIZE2, sTOKEN, sDATA, sCSUM, sDONE } state = sSTART;
unsigned int msglen = 0;
unsigned int curlen = 0;
int timeout = 0;
unsigned char c, checksum = 0;
/*
* The entire timeout handling here is not very consistent, see
*
* https://savannah.nongnu.org/bugs/index.php?43626
*/
long timeoutval = SERIAL_TIMEOUT; // seconds
struct timeval tv;
double tstart, tnow;
if (PDATA(pgm)->pgmtype == PGMTYPE_AVRISP_MKII ||
PDATA(pgm)->pgmtype == PGMTYPE_STK600)
return stk500v2_recv_mk2(pgm, msg, maxsize);
else if (PDATA(pgm)->pgmtype == PGMTYPE_JTAGICE_MKII)
return stk500v2_jtagmkII_recv(pgm, msg, maxsize);
else if (PDATA(pgm)->pgmtype == PGMTYPE_JTAGICE3)
return stk500v2_jtag3_recv(pgm, msg, maxsize);
DEBUG("STK500V2: stk500v2_recv(): ");
gettimeofday(&tv, NULL);
tstart = tv.tv_sec;
while ( (state != sDONE ) && (!timeout) ) {
if (serial_recv(&pgm->fd, &c, 1) < 0)
goto timedout;
DEBUG("0x%02x ",c);
checksum ^= c;
switch (state) {
case sSTART:
DEBUGRECV("hoping for start token...");
if (c == MESSAGE_START) {
DEBUGRECV("got it\n");
checksum = MESSAGE_START;
state = sSEQNUM;
} else
DEBUGRECV("sorry\n");
break;
case sSEQNUM:
DEBUGRECV("hoping for sequence...\n");
if (c == PDATA(pgm)->command_sequence) {
DEBUGRECV("got it, incrementing\n");
state = sSIZE1;
PDATA(pgm)->command_sequence++;
} else {
DEBUGRECV("sorry\n");
state = sSTART;
}
break;
case sSIZE1:
DEBUGRECV("hoping for size LSB\n");
msglen = (unsigned)c * 256;
state = sSIZE2;
break;
case sSIZE2:
DEBUGRECV("hoping for size MSB...");
msglen += (unsigned)c;
DEBUG(" msg is %u bytes\n",msglen);
state = sTOKEN;
break;
case sTOKEN:
if (c == TOKEN) state = sDATA;
else state = sSTART;
break;
case sDATA:
if (curlen < maxsize) {
msg[curlen] = c;
} else {
avrdude_message(MSG_INFO, "%s: stk500v2_recv(): buffer too small, received %d byte into %u byte buffer\n",
progname,curlen,(unsigned int)maxsize);
return -2;
}
if ((curlen == 0) && (msg[0] == ANSWER_CKSUM_ERROR)) {
avrdude_message(MSG_INFO, "%s: stk500v2_recv(): previous packet sent with wrong checksum\n",
progname);
return -3;
}
curlen++;
if (curlen == msglen) state = sCSUM;
break;
case sCSUM:
if (checksum == 0) {
state = sDONE;
} else {
state = sSTART;
avrdude_message(MSG_INFO, "%s: stk500v2_recv(): checksum error\n",
progname);
return -4;
}
break;
default:
avrdude_message(MSG_INFO, "%s: stk500v2_recv(): unknown state\n",
progname);
return -5;
} /* switch */
gettimeofday(&tv, NULL);
tnow = tv.tv_sec;
if (tnow-tstart > timeoutval) { // wuff - signed/unsigned/overflow
timedout:
avrdude_message(MSG_INFO, "%s: stk500v2_ReceiveMessage(): timeout\n",
progname);
return -1;
}
} /* while */
DEBUG("\n");
return (int)(msglen+6);
}
int stk500v2_getsync(PROGRAMMER * pgm) {
int tries = 0;
unsigned char buf[1], resp[32];
int status;
DEBUG("STK500V2: stk500v2_getsync()\n");
if (PDATA(pgm)->pgmtype == PGMTYPE_JTAGICE_MKII ||
PDATA(pgm)->pgmtype == PGMTYPE_JTAGICE3)
return 0;
retry:
tries++;
// send the sync command and see if we can get there
buf[0] = CMD_SIGN_ON;
stk500v2_send(pgm, buf, 1);
// try to get the response back and see where we got
status = stk500v2_recv(pgm, resp, sizeof(resp));
// if we got bytes returned, check to see what came back
if (status > 0) {
if ((resp[0] == CMD_SIGN_ON) && (resp[1] == STATUS_CMD_OK) &&
(status > 3)) {
// success!
unsigned int siglen = resp[2];
if (siglen >= strlen("STK500_2") &&
memcmp(resp + 3, "STK500_2", strlen("STK500_2")) == 0) {
PDATA(pgm)->pgmtype = PGMTYPE_STK500;
} else if (siglen >= strlen("AVRISP_2") &&
memcmp(resp + 3, "AVRISP_2", strlen("AVRISP_2")) == 0) {
PDATA(pgm)->pgmtype = PGMTYPE_AVRISP;
} else if (siglen >= strlen("AVRISP_MK2") &&
memcmp(resp + 3, "AVRISP_MK2", strlen("AVRISP_MK2")) == 0) {
PDATA(pgm)->pgmtype = PGMTYPE_AVRISP_MKII;
} else if (siglen >= strlen("STK600") &&
memcmp(resp + 3, "STK600", strlen("STK600")) == 0) {
PDATA(pgm)->pgmtype = PGMTYPE_STK600;
} else {
resp[siglen + 3] = 0;
avrdude_message(MSG_NOTICE, "%s: stk500v2_getsync(): got response from unknown "
"programmer %s, assuming STK500\n",
progname, resp + 3);
PDATA(pgm)->pgmtype = PGMTYPE_STK500;
}
avrdude_message(MSG_DEBUG, "%s: stk500v2_getsync(): found %s programmer\n",
progname, pgmname[PDATA(pgm)->pgmtype]);
return 0;
} else {
if (tries > RETRIES) {
avrdude_message(MSG_INFO, "%s: stk500v2_getsync(): can't communicate with device: resp=0x%02x\n",
progname, resp[0]);
return -6;
} else
goto retry;
}
// or if we got a timeout
} else if (status == -1) {
if (tries > RETRIES) {
avrdude_message(MSG_INFO, "%s: stk500v2_getsync(): timeout communicating with programmer\n",
progname);
return -1;
} else
goto retry;
// or any other error
} else {
if (tries > RETRIES) {
avrdude_message(MSG_INFO, "%s: stk500v2_getsync(): error communicating with programmer: (%d)\n",
progname,status);
} else
goto retry;
}
return 0;
}
static int stk500v2_command(PROGRAMMER * pgm, unsigned char * buf,
size_t len, size_t maxlen) {
int i;
int tries = 0;
int status;
DEBUG("STK500V2: stk500v2_command(");
for (i=0;i<len;i++) DEBUG("0x%02x ",buf[i]);
DEBUG(", %d)\n",len);
retry:
tries++;
// send the command to the programmer
stk500v2_send(pgm,buf,len);
// attempt to read the status back
status = stk500v2_recv(pgm,buf,maxlen);
// if we got a successful readback, return
if (status > 0) {
DEBUG(" = %d\n",status);
if (status < 2) {
avrdude_message(MSG_INFO, "%s: stk500v2_command(): short reply\n", progname);
return -1;
}
if (buf[0] == CMD_XPROG_SETMODE || buf[0] == CMD_XPROG) {
/*
* Decode XPROG wrapper errors.
*/
const char *msg;
int i;
/*
* For CMD_XPROG_SETMODE, the status is returned in buf[1].
* For CMD_XPROG, buf[1] contains the XPRG_CMD_* command, and
* buf[2] contains the status.
*/
i = buf[0] == CMD_XPROG_SETMODE? 1: 2;
if (buf[i] != XPRG_ERR_OK) {
switch (buf[i]) {
case XPRG_ERR_FAILED: msg = "Failed"; break;
case XPRG_ERR_COLLISION: msg = "Collision"; break;
case XPRG_ERR_TIMEOUT: msg = "Timeout"; break;
default: msg = "Unknown"; break;
}
avrdude_message(MSG_INFO, "%s: stk500v2_command(): error in %s: %s\n",
progname,
(buf[0] == CMD_XPROG_SETMODE? "CMD_XPROG_SETMODE": "CMD_XPROG"),
msg);
return -1;
}
return 0;
} else {
/*
* Decode STK500v2 errors.
*/
if (buf[1] >= STATUS_CMD_TOUT && buf[1] < 0xa0) {
const char *msg;
char msgbuf[30];
switch (buf[1]) {
case STATUS_CMD_TOUT:
msg = "Command timed out";
break;
case STATUS_RDY_BSY_TOUT:
msg = "Sampling of the RDY/nBSY pin timed out";
break;
case STATUS_SET_PARAM_MISSING:
msg = "The `Set Device Parameters' have not been "
"executed in advance of this command";
default:
sprintf(msgbuf, "unknown, code 0x%02x", buf[1]);
msg = msgbuf;
break;
}
if (quell_progress < 2) {
avrdude_message(MSG_INFO, "%s: stk500v2_command(): warning: %s\n",
progname, msg);
}
} else if (buf[1] == STATUS_CMD_OK) {
return status;
} else if (buf[1] == STATUS_CMD_FAILED) {
avrdude_message(MSG_INFO, "%s: stk500v2_command(): command failed\n",
progname);
} else if (buf[1] == STATUS_CMD_UNKNOWN) {
avrdude_message(MSG_INFO, "%s: stk500v2_command(): unknown command\n",
progname);
} else {
avrdude_message(MSG_INFO, "%s: stk500v2_command(): unknown status 0x%02x\n",
progname, buf[1]);
}
return -1;
}
}
// otherwise try to sync up again
status = stk500v2_getsync(pgm);
if (status != 0) {
if (tries > RETRIES) {
avrdude_message(MSG_INFO, "%s: stk500v2_command(): failed miserably to execute command 0x%02x\n",
progname,buf[0]);
return -1;
} else
goto retry;
}
DEBUG(" = 0\n");
return 0;
}
static int stk500v2_cmd(PROGRAMMER * pgm, const unsigned char *cmd,
unsigned char *res)
{
unsigned char buf[8];
int result;
DEBUG("STK500V2: stk500v2_cmd(%02x,%02x,%02x,%02x)\n",cmd[0],cmd[1],cmd[2],cmd[3]);
buf[0] = CMD_SPI_MULTI;
buf[1] = 4;
buf[2] = 4;
buf[3] = 0;
buf[4] = cmd[0];
buf[5] = cmd[1];
buf[6] = cmd[2];
buf[7] = cmd[3];
result = stk500v2_command(pgm, buf, 8, sizeof(buf));
if (result < 0) {
avrdude_message(MSG_INFO, "%s: stk500v2_cmd(): failed to send command\n",
progname);
return -1;
} else if (result < 6) {
avrdude_message(MSG_INFO, "%s: stk500v2_cmd(): short reply, len = %d\n",
progname, result);
return -1;
}
res[0] = buf[2];
res[1] = buf[3];
res[2] = buf[4];
res[3] = buf[5];
return 0;
}
static int stk500v2_jtag3_cmd(PROGRAMMER * pgm, const unsigned char *cmd,
unsigned char *res)
{
avrdude_message(MSG_INFO, "%s: stk500v2_jtag3_cmd(): Not available in JTAGICE3\n",
progname);
return -1;
}
/*
* issue the 'chip erase' command to the AVR device
*/
static int stk500v2_chip_erase(PROGRAMMER * pgm, AVRPART * p)
{
int result;
unsigned char buf[16];
if (p->op[AVR_OP_CHIP_ERASE] == NULL) {
avrdude_message(MSG_INFO, "%s: stk500v2_chip_erase: chip erase instruction not defined for part \"%s\"\n",
progname, p->desc);
return -1;
}
pgm->pgm_led(pgm, ON);
buf[0] = CMD_CHIP_ERASE_ISP;
buf[1] = p->chip_erase_delay / 1000;
buf[2] = 0; // use delay (?)
avr_set_bits(p->op[AVR_OP_CHIP_ERASE], buf+3);
result = stk500v2_command(pgm, buf, 7, sizeof(buf));
usleep(p->chip_erase_delay);