This repository has been archived by the owner on Jul 2, 2024. It is now read-only.
forked from avrdudes/avrdude
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pickit2.c
1340 lines (1100 loc) · 40.6 KB
/
pickit2.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 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: pickit2.c 2010-05-03 dbrown$ */
/* Based on Id: stk500v2.c 836 2009-07-10 22:39:37Z joerg_wunsch */
/*
* avrdude interface for PicKit2 programmer
*
* The PicKit2 programmer is a cheap device capable
* of 2 (bidirectional data line), 3, 4 wire SPI comms
*
* The PICkit2 software license doesn't allow the source to be
* modified to program other devices - nor can we distribute
* their source code. This program is not derived from nor does it
* contain any of the pickit2 source and should be exempt from any
* licensing issues.
*
* ISP Pinout (AVR - PICKit2 (pin)):
* RST - VPP/MCLR (1)
* VDD - VDD Target (2) -- possibly optional if AVR self powered
* GND - GND (3)
* MISO - PGD (4)
* SCLK - PDC (5)
* MOSI - AUX (6)
*/
#include "ac_cfg.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <unistd.h>
#include "avrdude.h"
#include "libavrdude.h"
#if defined(HAVE_LIBUSB) || (defined(WIN32NATIVE) && defined(HAVE_LIBHID))
#if (defined(WIN32NATIVE) && defined(HAVE_LIBHID))
#include <windows.h>
#if defined(HAVE_DDK_HIDSDI_H)
# include <ddk/hidsdi.h>
#else
# include "my_ddk_hidsdi.h"
#endif
#include <setupapi.h>
#else
#if defined(HAVE_USB_H)
# include <usb.h>
#elif defined(HAVE_LUSB0_USB_H)
# include <lusb0_usb.h>
#else
# error "libusb needs either <usb.h> or <lusb0_usb.h>"
#endif
#endif
#if 0
#define DEBUG(...) do { avrdude_message(MSG_DEBUG, __VA_ARGS__); } while(0)
#else
#define DEBUG(...) ((void)0)
#endif
#if 0
#define DEBUGRECV(...) do { avrdude_message(MSG_DEBUG, __VA_ARGS__); } while(0)
#else
#define DEBUGRECV(...) ((void)0)
#endif
#define PICKIT2_VID 0x04d8
#define PICKIT2_PID 0x0033
#define SPI_MAX_CHUNK (64 - 10) // max packet size less the command overhead
// win32native only:
#if (defined(WIN32NATIVE) && defined(HAVE_LIBHID))
static HANDLE open_hid(unsigned short vid, unsigned short pid);
const char *usb_strerror()
{
return "";
}
#else
static int usb_open_device(struct usb_dev_handle **dev, int vid, int pid);
//#define INVALID_HANDLE_VALUE NULL
#define USB_ERROR_NONE 0
#define USB_ERROR_ACCESS 1
#define USB_ERROR_NOTFOUND 2
#define USB_ERROR_BUSY 16
#define USB_ERROR_IO 5
#endif // WIN32NATIVE
static int pickit2_write_report(PROGRAMMER *pgm, const unsigned char report[65]);
static int pickit2_read_report(PROGRAMMER *pgm, unsigned char report[65]);
#ifndef MIN
#define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
#endif
/*
* Private data for this programmer.
*/
struct pdata
{
#if (defined(WIN32NATIVE) && defined(HAVE_LIBHID))
HANDLE usb_handle, write_event, read_event;
#else
struct usb_dev_handle *usb_handle; // LIBUSB STUFF
#endif
uint8_t clock_period; // SPI clock period in us
int transaction_timeout; // usb trans timeout in ms
};
#define PDATA(pgm) ((struct pdata *)(pgm->cookie))
#define CMD_NOP 0x5A
#define CMD_GET_VERSION 0x76
#define CMD_SET_VDD_4(v) 0xA0, (uint8_t)((v)*2048+672), (uint8_t)(((v)*2048+672)/256), (uint8_t)((v)*36)
#define CMD_SET_VPP_4(v) 0xA1, 0x40, (uint8_t)((v)*18.61), (uint8_t)((v)*13)
#define CMD_READ_VDD_VPP 0xA3
#define CMD_EXEC_SCRIPT_2(len) 0xA6, (len)
#define CMD_CLR_DLOAD_BUFF 0xA7
#define CMD_DOWNLOAD_DATA_2(len) 0xA8, (len)
#define CMD_CLR_ULOAD_BUFF 0xA9
#define CMD_UPLOAD_DATA 0xAA
#define CMD_UPLOAD_DATA_NO_LEN 0xAC
#define CMD_END_OF_BUFFER 0xAD
#define SCR_VDD_ON 0xFF
#define SCR_VDD_OFF 0xFE
#define SCR_VPP_ON 0xFB
#define SCR_VPP_OFF 0xFA
#define SCR_VPP_PWM_ON 0xF9
#define SCR_VPP_PWM_OFF 0xF8
#define SCR_MCLR_GND_ON 0xF7
#define SCR_MCLR_GND_OFF 0xF6
#define SCR_BUSY_LED_ON 0xF5
#define SCR_BUSY_LED_OFF 0xF4
#define SCR_SET_ICSP_DELAY_2(us) 0xEA,(us)
#define SCR_SET_PINS_2(dd, cd, dv, cv) 0xF3, (((cd)!=0) | (((dd)!=0)<<1) | (((cv)!=0)<<2) | (((dv)!=0)<<3))
#define SCR_GET_PINS 0xDC
#define SCR_LOOP_3(rel, cnt) 0xE9, rel, cnt
#define SCR_DELAY_2(sec) ((sec)>0.0054528?0xE8:0xE7), (uint8_t)((sec)>0.0054528?(.999+(sec)/.00546):(.999+(sec)/.0000213))
#define SCR_SET_AUX_2(ad, av) 0xCF, (((ad)!=0) | (((av)!=0)<<1))
#define SCR_SPI_SETUP_PINS_4 SCR_SET_PINS_2(1,0,0,0), SCR_SET_AUX_2(0,0)
#define SCR_SPI 0xC3
#define SCR_SPI_LIT_2(v) 0xC7,(v)
static void pickit2_setup(PROGRAMMER * pgm)
{
if ((pgm->cookie = malloc(sizeof(struct pdata))) == 0)
{
avrdude_message(MSG_INFO, "%s: pickit2_setup(): Out of memory allocating private data\n",
progname);
exit(1);
}
memset(pgm->cookie, 0, sizeof(struct pdata));
PDATA(pgm)->transaction_timeout = 1500; // default value, may be overridden with -x timeout=ms
PDATA(pgm)->clock_period = 10; // default value, may be overridden with -x clockrate=us or -B or -i
}
static void pickit2_teardown(PROGRAMMER * pgm)
{
free(pgm->cookie);
}
static int pickit2_open(PROGRAMMER * pgm, char * port)
{
#if (defined(WIN32NATIVE) && defined(HAVE_LIBHID))
PDATA(pgm)->usb_handle = open_hid(PICKIT2_VID, PICKIT2_PID);
if (PDATA(pgm)->usb_handle == INVALID_HANDLE_VALUE)
{
/* no PICkit2 found */
avrdude_message(MSG_INFO, "%s: error: could not find PICkit2 with vid=0x%x pid=0x%x\n",
progname, PICKIT2_VID, PICKIT2_PID);
return -1;
}
else
{
// get the device description while we're at it
short buff[PGM_DESCLEN-1], i;
HidD_GetProductString(PDATA(pgm)->usb_handle, buff, PGM_DESCLEN-1);
// convert from wide chars, but do not overwrite trailing '\0'
memset(&(pgm->type), 0, PGM_DESCLEN);
for (i = 0; i < (PGM_DESCLEN-1) && buff[i]; i++)
{
pgm->type[i] = (char)buff[i]; // TODO what about little/big endian???
}
}
#else
if (usb_open_device(&(PDATA(pgm)->usb_handle), PICKIT2_VID, PICKIT2_PID) < 0)
{
/* no PICkit2 found */
avrdude_message(MSG_INFO, "%s: error: could not find PICkit2 with vid=0x%x pid=0x%x\n",
progname, PICKIT2_VID, PICKIT2_PID);
return -1;
}
#endif
if (pgm->ispdelay > 0)
{
PDATA(pgm)->clock_period = MIN(pgm->ispdelay, 255);
}
else if (pgm->bitclock > 0.0)
{
PDATA(pgm)->clock_period = MIN(pgm->bitclock * 1e6, 255);
}
return 0;
}
static void pickit2_close(PROGRAMMER * pgm)
{
#if (defined(WIN32NATIVE) && defined(HAVE_LIBHID))
CloseHandle(PDATA(pgm)->usb_handle);
CloseHandle(PDATA(pgm)->read_event);
CloseHandle(PDATA(pgm)->write_event);
#else
usb_close(PDATA(pgm)->usb_handle);
#endif // WIN32NATIVE
}
static int pickit2_initialize(PROGRAMMER * pgm, AVRPART * p)
{
unsigned char temp[4];
memset(temp, 0, sizeof(temp));
int errorCode = 0;
/* set sck period */
if (pgm->set_sck_period)
pgm->set_sck_period(pgm, pgm->bitclock);
/* connect to target device -- we'll just ask for the firmware version */
static const unsigned char report[65] = {0, CMD_GET_VERSION, CMD_END_OF_BUFFER};
if ((errorCode = pickit2_write_report(pgm, report)) > 0)
{
unsigned char report[65] = {0};
//memset(report, 0, sizeof(report));
if ((errorCode = pickit2_read_report(pgm, report)) >= 4)
{
avrdude_message(MSG_NOTICE, "%s: %s firmware version %d.%d.%d\n", progname, pgm->desc, (int)report[1], (int)report[2], (int)report[3]);
// set the pins, apply reset,
// TO DO: apply vtarget (if requested though -x option)
unsigned char report[65] =
{
0, CMD_SET_VDD_4(5),
CMD_SET_VPP_4(5),
CMD_EXEC_SCRIPT_2(24),
SCR_SPI_SETUP_PINS_4, // SDO, SDI, SCK
SCR_SET_ICSP_DELAY_2(PDATA(pgm)->clock_period), // slow down the SPI
SCR_VDD_ON,
SCR_MCLR_GND_OFF, // let reset float high
SCR_VPP_PWM_ON,
SCR_DELAY_2(.1),
SCR_VPP_ON,
SCR_DELAY_2(.1),
SCR_VPP_OFF,
SCR_DELAY_2(.01),
SCR_MCLR_GND_ON, // reset low - programming mode
SCR_DELAY_2(.1),
SCR_BUSY_LED_ON,
SCR_DELAY_2(.3),
SCR_BUSY_LED_OFF,
CMD_CLR_DLOAD_BUFF,
CMD_CLR_ULOAD_BUFF,
CMD_END_OF_BUFFER
};
if (pickit2_write_report(pgm, report) < 0)
{
avrdude_message(MSG_INFO, "pickit2_read_report failed (ec %d). %s\n", errorCode, usb_strerror());
return -1;
}
}
else
{
avrdude_message(MSG_INFO, "pickit2_read_report failed (ec %d). %s\n", errorCode, usb_strerror());
return -1;
}
}
else
{
avrdude_message(MSG_INFO, "pickit2_write_report failed (ec %d). %s\n", errorCode, usb_strerror());
return -1;
}
if (pgm->program_enable)
return pgm->program_enable(pgm, p);
else
return -1;
}
static void pickit2_disable(PROGRAMMER * pgm)
{
/* make sure all pins are floating & all voltages are off */
static const unsigned char report[65] =
{
0, CMD_EXEC_SCRIPT_2(8),
SCR_SET_PINS_2(1,1,0,0),
SCR_SET_AUX_2(1,0),
SCR_MCLR_GND_OFF,
SCR_VPP_OFF,
SCR_VDD_OFF,
SCR_BUSY_LED_OFF,
CMD_END_OF_BUFFER
};
pickit2_write_report(pgm, report);
return;
}
static void pickit2_enable(PROGRAMMER * pgm)
{
/* Do nothing. */
return;
}
static void pickit2_display(PROGRAMMER * pgm, const char * p)
{
DEBUG( "%s: Found \"%s\" version %d.%d.%d\n", progname, p, 1, 1, 1);
return;
}
#define sendReport(x)
#define readReport(x) 0
#if 0
static int pickit2_rdy_led (struct programmer_t * pgm, int value)
{
// no rdy led
return 0;
}
static int pickit2_err_led(struct programmer_t * pgm, int value)
{
// there is no error led, so just flash the busy led a few times
uint8_t report[65] =
{
0, CMD_EXEC_SCRIPT_2(9),
SCR_BUSY_LED_ON,
SCR_DELAY_2(.2),
SCR_BUSY_LED_OFF,
SCR_DELAY_2(.2),
SCR_LOOP_3(6, 9),
CMD_END_OF_BUFFER
};
// busy stops flashing by itself, so just return
if (!value)
{
return 0;
}
return pickit2_write_report(pgm, report) != -1;
}
#endif
static int pickit2_pgm_led (struct programmer_t * pgm, int value)
{
// script to set busy led appropriately
uint8_t report[65] = {0, CMD_EXEC_SCRIPT_2(1),
value ? SCR_BUSY_LED_ON : SCR_BUSY_LED_OFF,
CMD_END_OF_BUFFER
};
return pickit2_write_report(pgm, report) != -1;
}
static int pickit2_vfy_led (struct programmer_t * pgm, int value)
{
// no such thing - maybe just call pgm_led
return pgm->pgm_led(pgm, value);
}
static void pickit2_powerup(struct programmer_t * pgm)
{
// turn vdd on?
}
static void pickit2_powerdown(struct programmer_t * pgm)
{
// do what?
pgm->disable(pgm);
}
static int pickit2_program_enable(struct programmer_t * pgm, AVRPART * p)
{
unsigned char cmd[4];
unsigned char res[4];
if (p->op[AVR_OP_PGM_ENABLE] == NULL)
{
avrdude_message(MSG_INFO, "program enable instruction not defined for part \"%s\"\n",
p->desc);
return -1;
}
memset(cmd, 0, sizeof(cmd));
avr_set_bits(p->op[AVR_OP_PGM_ENABLE], cmd);
pgm->cmd(pgm, cmd, res);
{
int i;
avrdude_message(MSG_DEBUG, "program_enable(): sending command. Resp = ");
for (i = 0; i < 4; i++)
{
avrdude_message(MSG_DEBUG, "%x ", (int)res[i]);
}
avrdude_message(MSG_DEBUG, "\n");
}
// check for sync character
if (res[2] != cmd[1])
return -2;
return 0;
}
static int pickit2_chip_erase(struct programmer_t * pgm, AVRPART * p)
{
unsigned char cmd[4];
unsigned char res[4];
if (p->op[AVR_OP_CHIP_ERASE] == NULL)
{
avrdude_message(MSG_INFO, "chip erase instruction not defined for part \"%s\"\n",
p->desc);
return -1;
}
pgm->pgm_led(pgm, ON);
memset(cmd, 0, sizeof(cmd));
avr_set_bits(p->op[AVR_OP_CHIP_ERASE], cmd);
pgm->cmd(pgm, cmd, res);
usleep(p->chip_erase_delay);
pgm->initialize(pgm, p);
pgm->pgm_led(pgm, OFF);
return 0;
}
static int pickit2_paged_load(PROGRAMMER * pgm, AVRPART * p, AVRMEM * mem,
unsigned int page_size, unsigned int addr, unsigned int n_bytes)
{
// only supporting flash & eeprom page reads
if ((!mem->paged || page_size <= 1) || (strcmp(mem->desc, "flash") != 0 && strcmp(mem->desc, "eeprom") != 0))
{
return -1;
}
DEBUG( "paged read ps %d, mem %s\n", page_size, mem->desc);
OPCODE *readop = 0, *lext = mem->op[AVR_OP_LOAD_EXT_ADDR];
uint8_t data = 0, cmd[SPI_MAX_CHUNK], res[SPI_MAX_CHUNK];
unsigned int addr_base;
unsigned int max_addr = addr + n_bytes;
pgm->pgm_led(pgm, ON);
for (addr_base = addr; addr_base < max_addr; )
{
if ((addr_base == 0 || (addr_base % /*ext_address_boundary*/ 65536) == 0)
&& lext != NULL)
{
memset(cmd, 0, sizeof(cmd));
avr_set_bits(lext, cmd);
avr_set_addr(lext, cmd, addr_base);
pgm->cmd(pgm, cmd, res);
}
// bytes to send in the next packet -- not necessary as pickit2_spi() handles breaking up
// the data into packets -- but we need to keep transfers frequent so that we can update the
// status indicator bar
uint32_t blockSize = MIN(65536 - (addr_base % 65536), MIN(max_addr - addr_base, SPI_MAX_CHUNK / 4));
memset(cmd, 0, sizeof(cmd));
memset(res, 0, sizeof(res));
uint8_t addr_off;
for (addr_off = 0; addr_off < blockSize; addr_off++)
{
int addr = addr_base + addr_off, caddr = addr;
if (mem->op[AVR_OP_READ_LO] != NULL && mem->op[AVR_OP_READ_HI] != NULL)
{
if (addr & 0x00000001)
readop = mem->op[AVR_OP_READ_HI];
else
readop = mem->op[AVR_OP_READ_LO];
caddr /= 2;
}
else if (mem->op[AVR_OP_READ] != NULL)
{
readop = mem->op[AVR_OP_READ];
}
else
{
avrdude_message(MSG_INFO, "no read command specified\n");
return -1;
}
avr_set_bits(readop, &cmd[addr_off*4]);
avr_set_addr(readop, &cmd[addr_off*4], caddr);
}
int bytes_read = pgm->spi(pgm, cmd, res, blockSize*4);
if (bytes_read < 0)
{
avrdude_message(MSG_INFO, "Failed @ pgm->spi()\n");
pgm->err_led(pgm, ON);
return -1;
}
DEBUG( "\npaged_load @ %X, wrote: %d, read: %d bytes\n", addr_base, blockSize*4, bytes_read);
for (addr_off = 0; addr_off < bytes_read / 4; addr_off++)
{
data = 0;
avr_get_output(readop, &res[addr_off*4], &data);
mem->buf[addr_base + addr_off] = data;
DEBUG( "%2X(%c)", (int)data, data<0x20?'.':data);
}
DEBUG( "\n");
addr_base += blockSize;
}
pgm->pgm_led(pgm, OFF);
return n_bytes;
}
static int pickit2_commit_page(PROGRAMMER * pgm, AVRPART * p, AVRMEM * mem,
unsigned long addr)
{
OPCODE * wp, * lext;
wp = mem->op[AVR_OP_WRITEPAGE];
if (wp == NULL)
{
avrdude_message(MSG_INFO, "pickit2_commit_page(): memory \"%s\" not configured for page writes\n",
mem->desc);
return -1;
}
// adjust the address if this memory is word-addressable
if ((mem->op[AVR_OP_LOADPAGE_LO]) || (mem->op[AVR_OP_READ_LO]))
addr /= 2;
unsigned char cmd[8];
memset(cmd, 0, sizeof(cmd));
// use the "load extended address" command, if available
lext = mem->op[AVR_OP_LOAD_EXT_ADDR];
if (lext != NULL)
{
avr_set_bits(lext, cmd);
avr_set_addr(lext, cmd, addr);
}
// make up the write page command in the 2nd cmd position
avr_set_bits(wp, &cmd[4]);
avr_set_addr(wp, &cmd[4], addr);
if (lext != NULL)
{
// write the load extended address cmd && the write_page cmd
pgm->spi(pgm, cmd, NULL, 8);
}
else
{
// write just the write_page cmd
pgm->spi(pgm, &cmd[4], NULL, 4);
}
// just delay the max (we could do the delay in the PICkit2 if we wanted)
usleep(mem->max_write_delay);
return 0;
}
// not actually a paged write, but a bulk/batch write
static int pickit2_paged_write(PROGRAMMER * pgm, AVRPART * p, AVRMEM * mem,
unsigned int page_size, unsigned int addr, unsigned int n_bytes)
{
// only paged write for flash implemented
if (strcmp(mem->desc, "flash") != 0 && strcmp(mem->desc, "eeprom") != 0)
{
avrdude_message(MSG_INFO, "Part does not support %d paged write of %s\n", page_size, mem->desc);
return -1;
}
DEBUG( "page size %d mem %s supported: %d\n", page_size, mem->desc, mem->paged);
DEBUG( "loadpagehi %x, loadpagelow %x, writepage %x\n", (int)mem->op[AVR_OP_LOADPAGE_HI], (int)mem->op[AVR_OP_LOADPAGE_LO], (int)mem->op[AVR_OP_WRITEPAGE]);
OPCODE *writeop;
uint8_t cmd[SPI_MAX_CHUNK], res[SPI_MAX_CHUNK];
unsigned int addr_base;
unsigned int max_addr = addr + n_bytes;
pgm->pgm_led(pgm, ON);
for (addr_base = addr; addr_base < max_addr; )
{
uint32_t blockSize;
if (mem->paged)
{
blockSize = MIN(page_size - (addr_base % page_size), MIN(max_addr - addr_base, SPI_MAX_CHUNK/4) ); // bytes remaining in page
}
else
{
blockSize = 1;
}
memset(cmd, 0, sizeof(cmd));
memset(res, 0, sizeof(res));
uint8_t addr_off;
for (addr_off = 0; addr_off < blockSize; addr_off++)
{
int addr = addr_base + addr_off;
int caddr = 0;
/*
* determine which memory opcode to use
*/
if (mem->paged && mem->op[AVR_OP_LOADPAGE_HI] && mem->op[AVR_OP_LOADPAGE_LO])
{
if (addr & 0x01)
writeop = mem->op[AVR_OP_LOADPAGE_HI];
else
writeop = mem->op[AVR_OP_LOADPAGE_LO];
caddr = addr / 2;
}
else if (mem->paged && mem->op[AVR_OP_LOADPAGE_LO])
{
writeop = mem->op[AVR_OP_LOADPAGE_LO];
caddr = addr;
}
else if (mem->op[AVR_OP_WRITE_LO])
{
writeop = mem->op[AVR_OP_WRITE_LO];
caddr = addr; // maybe this should divide by 2 & use the write_high opcode also
avrdude_message(MSG_INFO, "Error AVR_OP_WRITE_LO defined only (where's the HIGH command?)\n");
return -1;
}
else
{
writeop = mem->op[AVR_OP_WRITE];
caddr = addr;
}
if (writeop == NULL)
{
pgm->err_led(pgm, ON);
// not supported!
return -1;
}
avr_set_bits(writeop, &cmd[addr_off*4]);
avr_set_addr(writeop, &cmd[addr_off*4], caddr);
avr_set_input(writeop, &cmd[addr_off*4], mem->buf[addr]);
}
int bytes_read = pgm->spi(pgm, cmd, res, blockSize*4);
if (bytes_read < 0)
{
avrdude_message(MSG_INFO, "Failed @ pgm->spi()\n");
pgm->err_led(pgm, ON);
return -1;
}
addr_base += blockSize;
// write the page - this function looks after extended address also
if (mem->paged && (((addr_base % page_size) == 0) || (addr_base == max_addr)))
{
DEBUG( "Calling pickit2_commit_page()\n");
pickit2_commit_page(pgm, p, mem, addr_base-1);
}
else if (!mem->paged)
{
usleep(mem->max_write_delay);
}
}
pgm->pgm_led(pgm, OFF);
return n_bytes;
}
static int pickit2_cmd(struct programmer_t * pgm, const unsigned char *cmd,
unsigned char *res)
{
return pgm->spi(pgm, cmd, res, 4);
}
// breaks up the cmd[] data into packets & sends to the pickit2. Data shifted in is stored in res[].
static int pickit2_spi(struct programmer_t * pgm, const unsigned char *cmd,
unsigned char *res, int n_bytes)
{
int retval = 0, temp1 = 0, temp2 = 0, count = n_bytes;
while (count > 0)
{
uint8_t i, blockSize = MIN(count, SPI_MAX_CHUNK);
uint8_t report[65] = {0, CMD_DOWNLOAD_DATA_2(blockSize)};
uint8_t *repptr = report + 3;
memset(report + 3, CMD_END_OF_BUFFER, sizeof(report) - 3);
// append some data to write to SPI
for (i = 0; i < blockSize; i++)
{
*repptr++ = *cmd++;
count--; // 1 less byte to pack
}
if (blockSize == 1)
{
*repptr++ = 0xa6; //CMD_EXECUTE_SCRIPT;
*repptr++ = 1;
*repptr++ = SCR_SPI;
}
else
{
*repptr++ = 0xa6; //CMD_EXECUTE_SCRIPT_2;
*repptr++ = 4;
*repptr++ = SCR_SPI;
*repptr++ = 0xe9; //SCR_LOOP_3;
*repptr++ = 1;
*repptr++ = blockSize - 1;
}
// request the data read to be sent to us
*repptr++ = CMD_UPLOAD_DATA;
// check return values
if ((temp1=pickit2_write_report(pgm, report)) < 0 ||
(temp2=pickit2_read_report(pgm, report)) < 0)
{
return -1;
}/*
else
{
int i;
DEBUG( "in spi. wrote %d, read %d\n", temp1, temp2);
for (i = 0; i < temp2; i++)
{
DEBUG( "%2.2x ", report[i]);
}
DEBUG( "\n");
}*/
retval = report[1]; // upload-length field
repptr = &report[2]; // actual data starts here
if (res) // copy data if user has specified a storage location
{
memcpy(res, repptr, retval);
res += retval;
}
}
return n_bytes;
}
#if (defined(WIN32NATIVE) && defined(HAVE_LIBHID))
/*
Func: open_hid()
Desc: finds & opens device having specified VID & PID.
Retn: Handle of open device or INVALID_HANDLE_VALUE on fail
Note this routine is a modified function from:
usbhidiocDlg.cpp : implementation file
Project: usbhidioc.cpp
Version: 3.0
Date: 7/18/05
by Jan Axelson (jan@Lvr.com)
*/
static HANDLE open_hid(unsigned short vid, unsigned short pid)
{
//Use a series of API calls to find a HID with a specified Vendor IF and Product ID.
HANDLE returnHandle = INVALID_HANDLE_VALUE;
HIDD_ATTRIBUTES Attributes;
// DWORD DeviceUsage;
SP_DEVICE_INTERFACE_DATA devInfoData;
BOOL LastDevice = FALSE;
int MemberIndex = 0;
LONG Result;
// were global, now just local scrap
DWORD Length = 0;
PSP_DEVICE_INTERFACE_DETAIL_DATA detailData = NULL;
HANDLE DeviceHandle=NULL;
GUID HidGuid;
HANDLE hDevInfo;
ULONG Required;
BOOL MyDeviceDetected = 0;
/*
API function: HidD_GetHidGuid
Get the GUID for all system HIDs.
Returns: the GUID in HidGuid.
*/
HidD_GetHidGuid(&HidGuid);
DEBUG("\nHidD_GetHidGuid returned.\n");
/*
API function: SetupDiGetClassDevs
Returns: a handle to a device information set for all installed devices.
Requires: the GUID returned by GetHidGuid.
*/
hDevInfo=SetupDiGetClassDevs
(&HidGuid,
NULL,
NULL,
DIGCF_PRESENT|DIGCF_INTERFACEDEVICE);
DEBUG("\nSetupDiGetClassDevs returned 0x%x\n", hDevInfo);
devInfoData.cbSize = sizeof(devInfoData);
//Step through the available devices looking for the one we want.
//Quit on detecting the desired device or checking all available devices without success.
MemberIndex = 0;
LastDevice = FALSE;
do
{
/*
API function: SetupDiEnumDeviceInterfaces
On return, MyDeviceInterfaceData contains the handle to a
SP_DEVICE_INTERFACE_DATA structure for a detected device.
Requires:
The DeviceInfoSet returned in SetupDiGetClassDevs.
The HidGuid returned in GetHidGuid.
An index to specify a device.
*/
Result=SetupDiEnumDeviceInterfaces
(hDevInfo,
0,
&HidGuid,
MemberIndex,
&devInfoData);
DEBUG("\nSetupDiEnumDeviceInterfaces returned 0x%x\n", Result);
if (Result != 0)
{
//A device has been detected, so get more information about it.
/*
API function: SetupDiGetDeviceInterfaceDetail
Returns: an SP_DEVICE_INTERFACE_DETAIL_DATA structure
containing information about a device.
To retrieve the information, call this function twice.
The first time returns the size of the structure in Length.
The second time returns a pointer to the data in DeviceInfoSet.
Requires:
A DeviceInfoSet returned by SetupDiGetClassDevs
The SP_DEVICE_INTERFACE_DATA structure returned by SetupDiEnumDeviceInterfaces.
The final parameter is an optional pointer to an SP_DEV_INFO_DATA structure.
This application doesn't retrieve or use the structure.
If retrieving the structure, set
MyDeviceInfoData.cbSize = length of MyDeviceInfoData.
and pass the structure's address.
*/
//Get the Length value.
//The call will return with a "buffer too small" error which can be ignored.
Result = SetupDiGetDeviceInterfaceDetail
(hDevInfo,
&devInfoData,
NULL,
0,
&Length,
NULL);
DEBUG("\nSetupDiGetDeviceInterfaceDetail returned 0x%x\n", Result);
//Allocate memory for the hDevInfo structure, using the returned Length.
detailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(Length);
//Set cbSize in the detailData structure.
detailData -> cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
//Call the function again, this time passing it the returned buffer size.
Result = SetupDiGetDeviceInterfaceDetail
(hDevInfo,
&devInfoData,
detailData,
Length,
&Required,
NULL);
// Open a handle to the device.
// To enable retrieving information about a system mouse or keyboard,
// don't request Read or Write access for this handle.
/*
API function: CreateFile
Returns: a handle that enables reading and writing to the device.
Requires:
The DevicePath in the detailData structure
returned by SetupDiGetDeviceInterfaceDetail.
*/
DeviceHandle=CreateFile
(detailData->DevicePath,
0,
FILE_SHARE_READ|FILE_SHARE_WRITE,
(LPSECURITY_ATTRIBUTES)NULL,
OPEN_EXISTING,
0,
NULL);
DEBUG("CreateFile(): %s\n", detailData->DevicePath);
/*
API function: HidD_GetAttributes
Requests information from the device.
Requires: the handle returned by CreateFile.
Returns: a HIDD_ATTRIBUTES structure containing
the Vendor ID, Product ID, and Product Version Number.
Use this information to decide if the detected device is
the one we're looking for.
*/
//Set the Size to the number of bytes in the structure.
Attributes.Size = sizeof(Attributes);
Result = HidD_GetAttributes
(DeviceHandle,
&Attributes);
DEBUG("HidD_GetAttributes returned 0x%x\n", Result);
DEBUG("VID: %.4X PID: %.4X\n", Attributes.VendorID, Attributes.ProductID);
//Is it the desired device?
MyDeviceDetected = FALSE;
if (Attributes.VendorID == vid)
{
if (Attributes.ProductID == pid)
{