Select one of the symbols to view example projects that use it.
 
Outline
#include "config.h"
#include <stdint.h>
#include <hidapi.h>
#include <jtag/interface.h>
#include <jtag/swd.h>
#include <jtag/commands.h>
#include "libusb_helper.h"
#define VID
#define PID
#define BULK_EP_IN
#define BULK_EP_OUT
#define CONTROL_TYPE_READ
#define CONTROL_TYPE_WRITE
#define CONTROL_COMMAND_PROGRAM
#define CONTROL_MODE_POLL_PROGRAMMER_STATUS
#define CONTROL_MODE_RESET_TARGET
#define CONTROL_MODE_SET_PROGRAMMER_PROTOCOL
#define CONTROL_MODE_SYNCHRONIZE_TRANSFER
#define CONTROL_MODE_ACQUIRE_SWD_TARGET
#define CONTROL_MODE_SEND_SWD_SEQUENCE
#define PROTOCOL_JTAG
#define PROTOCOL_SWD
#define DEVICE_PSOC4
#define DEVICE_PSOC3
#define DEVICE_UNKNOWN
#define DEVICE_PSOC5
#define ACQUIRE_MODE_RESET
#define ACQUIRE_MODE_POWER_CYCLE
#define SEQUENCE_LINE_RESET
#define SEQUENCE_JTAG_TO_SWD
#define PROGRAMMER_NOK_NACK
#define PROGRAMMER_OK_ACK
#define HID_TYPE_WRITE
#define HID_TYPE_READ
#define HID_TYPE_START
#define HID_COMMAND_POWER
#define HID_COMMAND_VERSION
#define HID_COMMAND_RESET
#define HID_COMMAND_CONFIGURE
#define HID_COMMAND_BOOTLOADER
#define SWD_MAX_BUFFER_LENGTH
kitprog
pending_transfer_result
kitprog_init_acquire_psoc
pending_transfer_count
pending_queue_len
pending_transfers
queued_retval
kitprog_handle
mm_to_version(uint8_t, uint8_t)
kitprog_init()
kitprog_quit()
kitprog usb functions
kitprog_get_usb_serial()
kitprog_usb_open()
kitprog_usb_close()
kitprog lowlevel functions
kitprog_hid_command(uint8_t *, size_t, uint8_t *, size_t)
kitprog_get_version()
kitprog_get_millivolts()
kitprog_get_info()
kitprog_set_protocol(uint8_t)
kitprog_get_status()
kitprog_set_unknown()
kitprog_acquire_psoc(uint8_t, uint8_t, uint8_t)
kitprog_reset_target()
kitprog_swd_sync()
kitprog_swd_seq(uint8_t)
kitprog_generic_acquire()
swd wrapper functions
kitprog_swd_init()
kitprog_swd_write_reg(uint8_t, uint32_t, uint32_t)
kitprog_swd_read_reg(uint8_t, uint32_t *, uint32_t)
swd lowlevel functions
kitprog_swd_switch_seq(enum swd_special_seq)
kitprog_swd_run_queue()
kitprog_swd_queue_cmd(uint8_t, uint32_t *, uint32_t)
kitprog_reset(int, int)
kitprog_handle_info_command(struct command_invocation *)
kitprog_handle_acquire_psoc_command(struct command_invocation *)
kitprog_handle_init_acquire_psoc_command(struct command_invocation *)
kitprog_subcommand_handlers
kitprog_command_handlers
kitprog_swd
kitprog_transports
kitprog_adapter_driver
Files
loading...
SourceVuDevelopment ToolsOpenOCDsrc/jtag/drivers/kitprog.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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
// SPDX-License-Identifier: GPL-2.0-or-later /*************************************************************************** * Copyright (C) 2007 by Juergen Stuber <juergen@jstuber.net> * * based on Dominic Rath's and Benedikt Sauter's usbprog.c * * * * Copyright (C) 2008 by Spencer Oliver * * spen@spen-soft.co.uk * * * * Copyright (C) 2011 by Jean-Christophe PLAGNIOL-VIILARD * * plagnioj@jcrosoft.com * * * * Copyright (C) 2015 by Marc Schink * * openocd-dev@marcschink.de * * * * Copyright (C) 2015 by Paul Fertser * * fercerpav@gmail.com * * * * Copyright (C) 2015-2017 by Forest Crossman * * cyrozap@gmail.com * ***************************************************************************//* ... */ #ifdef HAVE_CONFIG_H #include "config.h" #endif #include <stdint.h> #include <hidapi.h> #include <jtag/interface.h> #include <jtag/swd.h> #include <jtag/commands.h> #include "libusb_helper.h" 6 includes #define VID 0x04b4 #define PID 0xf139 #define BULK_EP_IN 1 #define BULK_EP_OUT 2 #define CONTROL_TYPE_READ 0x01 #define CONTROL_TYPE_WRITE 0x02 #define CONTROL_COMMAND_PROGRAM 0x07 #define CONTROL_MODE_POLL_PROGRAMMER_STATUS 0x01 #define CONTROL_MODE_RESET_TARGET 0x04 #define CONTROL_MODE_SET_PROGRAMMER_PROTOCOL 0x40 #define CONTROL_MODE_SYNCHRONIZE_TRANSFER 0x41 #define CONTROL_MODE_ACQUIRE_SWD_TARGET 0x42 #define CONTROL_MODE_SEND_SWD_SEQUENCE 0x43 #define PROTOCOL_JTAG 0x00 #define PROTOCOL_SWD 0x01 #define DEVICE_PSOC4 0x00 #define DEVICE_PSOC3 0x01 #define DEVICE_UNKNOWN 0x02 #define DEVICE_PSOC5 0x03 #define ACQUIRE_MODE_RESET 0x00 #define ACQUIRE_MODE_POWER_CYCLE 0x01 #define SEQUENCE_LINE_RESET 0x00 #define SEQUENCE_JTAG_TO_SWD 0x01 #define PROGRAMMER_NOK_NACK 0x00 #define PROGRAMMER_OK_ACK 0x01 #define HID_TYPE_WRITE 0x00 #define HID_TYPE_READ 0x01 #define HID_TYPE_START 0x02 #define HID_COMMAND_POWER 0x80 #define HID_COMMAND_VERSION 0x81 #define HID_COMMAND_RESET 0x82 #define HID_COMMAND_CONFIGURE 0x8f #define HID_COMMAND_BOOTLOADER 0xa0 /* 512 bytes seemed to work reliably. * It works with both full queue of mostly reads or mostly writes. * * Unfortunately the commit 88f429ead019fd6df96ec15f0d897385f3cef0d0 * 5321: target/cortex_m: faster reading of all CPU registers * revealed a serious Kitprog firmware problem: * If the queue contains more than 63 transactions in the repeated pattern * one write, two reads, the firmware fails badly. * Sending 64 transactions makes the adapter to loose the connection with the * device. Sending 65 or more transactions causes the adapter to stop * receiving USB HID commands, next kitprog_hid_command() stops in hid_write(). * * The problem was detected with KitProg v2.12 and v2.16. * We can guess the problem is something like a buffer or stack overflow. * * Use shorter buffer as a workaround. 300 bytes (= 60 transactions) works. *//* ... */ #define SWD_MAX_BUFFER_LENGTH 300 34 defines struct kitprog { hid_device *hid_handle; struct libusb_device_handle *usb_handle; uint16_t packet_size; uint16_t packet_index; uint8_t *packet_buffer; char *serial; uint8_t hardware_version; uint8_t minor_version; uint8_t major_version; uint16_t millivolts; bool supports_jtag_to_swd; ...}; struct pending_transfer_result { uint8_t cmd; uint32_t data; void *buffer; ...}; static bool kitprog_init_acquire_psoc; static int pending_transfer_count, pending_queue_len; static struct pending_transfer_result *pending_transfers; static int queued_retval; static struct kitprog *kitprog_handle; static int kitprog_usb_open(void); static void kitprog_usb_close(void); static int kitprog_hid_command(uint8_t *command, size_t command_length, uint8_t *data, size_t data_length); static int kitprog_get_version(void); static int kitprog_get_millivolts(void); static int kitprog_get_info(void); static int kitprog_set_protocol(uint8_t protocol); static int kitprog_get_status(void); static int kitprog_set_unknown(void); static int kitprog_acquire_psoc(uint8_t psoc_type, uint8_t acquire_mode, uint8_t max_attempts); static int kitprog_reset_target(void); static int kitprog_swd_sync(void); static int kitprog_swd_seq(uint8_t seq_type); static int kitprog_generic_acquire(void); static int kitprog_swd_run_queue(void); static void kitprog_swd_queue_cmd(uint8_t cmd, uint32_t *dst, uint32_t data); static int kitprog_swd_switch_seq(enum swd_special_seq seq); static inline int mm_to_version(uint8_t major, uint8_t minor) { return (major << 8) | minor; }{ ... } static int kitprog_init(void) { int retval; kitprog_handle = malloc(sizeof(struct kitprog)); if (!kitprog_handle) { LOG_ERROR("Failed to allocate memory"); return ERROR_FAIL; }if (!kitprog_handle) { ... } if (kitprog_usb_open() != ERROR_OK) { LOG_ERROR("Can't find a KitProg device! Please check device connections and permissions."); return ERROR_JTAG_INIT_FAILED; }if (kitprog_usb_open() != ERROR_OK) { ... } /* Get the current KitProg version and target voltage */ if (kitprog_get_info() != ERROR_OK) return ERROR_FAIL; /* Compatibility check */ kitprog_handle->supports_jtag_to_swd = true; int kitprog_version = mm_to_version(kitprog_handle->major_version, kitprog_handle->minor_version); if (kitprog_version < mm_to_version(2, 14)) { LOG_WARNING("KitProg firmware versions below v2.14 do not support sending JTAG to SWD sequences. These sequences will be substituted with SWD line resets."); kitprog_handle->supports_jtag_to_swd = false; }if (kitprog_version < mm_to_version(2, 14)) { ... } /* I have no idea what this does */ if (kitprog_set_unknown() != ERROR_OK) return ERROR_FAIL; /* SWD won't work unless we do this */ if (kitprog_swd_sync() != ERROR_OK) return ERROR_FAIL; /* Set the protocol to SWD */ if (kitprog_set_protocol(PROTOCOL_SWD) != ERROR_OK) return ERROR_FAIL; /* Reset the SWD bus */ if (kitprog_swd_seq(SEQUENCE_LINE_RESET) != ERROR_OK) return ERROR_FAIL; if (kitprog_init_acquire_psoc) { /* Try to acquire any device that will respond */ retval = kitprog_generic_acquire(); if (retval != ERROR_OK) { LOG_ERROR("No PSoC devices found"); return retval; }if (retval != ERROR_OK) { ... } }if (kitprog_init_acquire_psoc) { ... } /* Allocate packet buffers and queues */ kitprog_handle->packet_size = SWD_MAX_BUFFER_LENGTH; kitprog_handle->packet_buffer = malloc(SWD_MAX_BUFFER_LENGTH); if (!kitprog_handle->packet_buffer) { LOG_ERROR("Failed to allocate memory for the packet buffer"); return ERROR_FAIL; }if (!kitprog_handle->packet_buffer) { ... } pending_queue_len = SWD_MAX_BUFFER_LENGTH / 5; pending_transfers = malloc(pending_queue_len * sizeof(*pending_transfers)); if (!pending_transfers) { LOG_ERROR("Failed to allocate memory for the SWD transfer queue"); return ERROR_FAIL; }if (!pending_transfers) { ... } return ERROR_OK; }{ ... } static int kitprog_quit(void) { kitprog_usb_close(); free(kitprog_handle->packet_buffer); free(kitprog_handle->serial); free(kitprog_handle); free(pending_transfers); return ERROR_OK; }{ ... } /*************** kitprog usb functions *********************/ static int kitprog_get_usb_serial(void) { int retval; const uint8_t str_index = 128; /* This seems to be a constant */ char desc_string[256+1]; /* Max size of string descriptor */ retval = libusb_get_string_descriptor_ascii(kitprog_handle->usb_handle, str_index, (unsigned char *)desc_string, sizeof(desc_string)-1); if (retval < 0) { LOG_ERROR("libusb_get_string_descriptor_ascii() failed with %d", retval); return ERROR_FAIL; }if (retval < 0) { ... } /* Null terminate descriptor string */ desc_string[retval] = '\0'; /* Allocate memory for the serial number */ kitprog_handle->serial = calloc(retval + 1, sizeof(char)); if (!kitprog_handle->serial) { LOG_ERROR("Failed to allocate memory for the serial number"); return ERROR_FAIL; }if (!kitprog_handle->serial) { ... } /* Store the serial number */ strncpy(kitprog_handle->serial, desc_string, retval + 1); return ERROR_OK; }{ ... } static int kitprog_usb_open(void) { const uint16_t vids[] = { VID, 0 }; const uint16_t pids[] = { PID, 0 }; if (jtag_libusb_open(vids, pids, NULL, &kitprog_handle->usb_handle, NULL) != ERROR_OK) { LOG_ERROR("Failed to open or find the device"); return ERROR_FAIL; }if (jtag_libusb_open(vids, pids, NULL, &kitprog_handle->usb_handle, NULL) != ERROR_OK) { ... } /* Get the serial number for the device */ if (kitprog_get_usb_serial() != ERROR_OK) LOG_WARNING("Failed to get KitProg serial number"); /* Convert the ASCII serial number into a (wchar_t *) */ size_t len = strlen(kitprog_handle->serial); wchar_t *hid_serial = calloc(len + 1, sizeof(wchar_t)); if (!hid_serial) { LOG_ERROR("Failed to allocate memory for the serial number"); return ERROR_FAIL; }if (!hid_serial) { ... } if (mbstowcs(hid_serial, kitprog_handle->serial, len + 1) == (size_t)-1) { free(hid_serial); LOG_ERROR("Failed to convert serial number"); return ERROR_FAIL; }if (mbstowcs(hid_serial, kitprog_handle->serial, len + 1) == (size_t)-1) { ... } /* Use HID for the KitBridge interface */ kitprog_handle->hid_handle = hid_open(VID, PID, hid_serial); free(hid_serial); if (!kitprog_handle->hid_handle) { LOG_ERROR("Failed to open KitBridge (HID) interface"); return ERROR_FAIL; }if (!kitprog_handle->hid_handle) { ... } /* Claim the KitProg Programmer (bulk transfer) interface */ if (libusb_claim_interface(kitprog_handle->usb_handle, 1) != ERROR_OK) { LOG_ERROR("Failed to claim KitProg Programmer (bulk transfer) interface"); return ERROR_FAIL; }if (libusb_claim_interface(kitprog_handle->usb_handle, 1) != ERROR_OK) { ... } return ERROR_OK; }{ ... } static void kitprog_usb_close(void) { if (kitprog_handle->hid_handle) { hid_close(kitprog_handle->hid_handle); hid_exit(); }if (kitprog_handle->hid_handle) { ... } jtag_libusb_close(kitprog_handle->usb_handle); }{ ... } kitprog usb functions /*************** kitprog lowlevel functions *********************/ static int kitprog_hid_command(uint8_t *command, size_t command_length, uint8_t *data, size_t data_length) { int ret; ret = hid_write(kitprog_handle->hid_handle, command, command_length); if (ret < 0) { LOG_DEBUG("HID write returned %i", ret); return ERROR_FAIL; }if (ret < 0) { ... } ret = hid_read_timeout(kitprog_handle->hid_handle, data, data_length, LIBUSB_TIMEOUT_MS); if (ret == 0) { LOG_ERROR("HID read timed out"); return ERROR_TIMEOUT_REACHED; }if (ret == 0) { ... } else if (ret < 0) { LOG_ERROR("HID read error %ls", hid_error(kitprog_handle->hid_handle)); return ERROR_FAIL; }else if (ret < 0) { ... } return ERROR_OK; }{ ... } static int kitprog_get_version(void) { int ret; unsigned char command[3] = {HID_TYPE_START | HID_TYPE_WRITE, 0x00, HID_COMMAND_VERSION}; unsigned char data[64]; ret = kitprog_hid_command(command, sizeof(command), data, sizeof(data)); if (ret != ERROR_OK) return ret; kitprog_handle->hardware_version = data[1]; kitprog_handle->minor_version = data[2]; kitprog_handle->major_version = data[3]; return ERROR_OK; }{ ... } static int kitprog_get_millivolts(void) { int ret; unsigned char command[3] = {HID_TYPE_START | HID_TYPE_READ, 0x00, HID_COMMAND_POWER}; unsigned char data[64]; ret = kitprog_hid_command(command, sizeof(command), data, sizeof(data)); if (ret != ERROR_OK) return ret; kitprog_handle->millivolts = (data[4] << 8) | data[3]; return ERROR_OK; }{ ... } static int kitprog_get_info(void) { /* Get the device version information */ if (kitprog_get_version() == ERROR_OK) { LOG_INFO("KitProg v%u.%02u", kitprog_handle->major_version, kitprog_handle->minor_version); LOG_INFO("Hardware version: %u", kitprog_handle->hardware_version); }if (kitprog_get_version() == ERROR_OK) { ... } else { LOG_ERROR("Failed to get KitProg version"); return ERROR_FAIL; }else { ... } /* Get the current reported target voltage */ if (kitprog_get_millivolts() == ERROR_OK) { LOG_INFO("VTARG = %u.%03u V", kitprog_handle->millivolts / 1000, kitprog_handle->millivolts % 1000); }if (kitprog_get_millivolts() == ERROR_OK) { ... } else { LOG_ERROR("Failed to get target voltage"); return ERROR_FAIL; }else { ... } return ERROR_OK; }{ ... } static int kitprog_set_protocol(uint8_t protocol) { int transferred; char status = PROGRAMMER_NOK_NACK; int retval = jtag_libusb_control_transfer(kitprog_handle->usb_handle, LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, CONTROL_TYPE_WRITE, (CONTROL_MODE_SET_PROGRAMMER_PROTOCOL << 8) | CONTROL_COMMAND_PROGRAM, protocol, &status, 1, 0, &transferred); if (retval != ERROR_OK || transferred == 0) { LOG_DEBUG("Zero bytes transferred"); return ERROR_FAIL; }if (retval != ERROR_OK || transferred == 0) { ... } if (status != PROGRAMMER_OK_ACK) { LOG_DEBUG("Programmer did not respond OK"); return ERROR_FAIL; }if (status != PROGRAMMER_OK_ACK) { ... } return ERROR_OK; }{ ... } static int kitprog_get_status(void) { int transferred = 0; char status = PROGRAMMER_NOK_NACK; /* Try a maximum of three times */ for (int i = 0; (i < 3) && (transferred == 0); i++) { jtag_libusb_control_transfer(kitprog_handle->usb_handle, LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, CONTROL_TYPE_READ, (CONTROL_MODE_POLL_PROGRAMMER_STATUS << 8) | CONTROL_COMMAND_PROGRAM, 0, &status, 1, 0, &transferred); jtag_sleep(1000); }for (int i = 0; (i < 3) && (transferred == 0); i++) { ... } if (transferred == 0) { LOG_DEBUG("Zero bytes transferred"); return ERROR_FAIL; }if (transferred == 0) { ... } if (status != PROGRAMMER_OK_ACK) { LOG_DEBUG("Programmer did not respond OK"); return ERROR_FAIL; }if (status != PROGRAMMER_OK_ACK) { ... } return ERROR_OK; }{ ... } static int kitprog_set_unknown(void) { int transferred; char status = PROGRAMMER_NOK_NACK; int retval = jtag_libusb_control_transfer(kitprog_handle->usb_handle, LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, CONTROL_TYPE_WRITE, (0x03 << 8) | 0x04, 0, &status, 1, 0, &transferred); if (retval != ERROR_OK || transferred == 0) { LOG_DEBUG("Zero bytes transferred"); return ERROR_FAIL; }if (retval != ERROR_OK || transferred == 0) { ... } if (status != PROGRAMMER_OK_ACK) { LOG_DEBUG("Programmer did not respond OK"); return ERROR_FAIL; }if (status != PROGRAMMER_OK_ACK) { ... } return ERROR_OK; }{ ... } static int kitprog_acquire_psoc(uint8_t psoc_type, uint8_t acquire_mode, uint8_t max_attempts) { int transferred; char status = PROGRAMMER_NOK_NACK; int retval = jtag_libusb_control_transfer(kitprog_handle->usb_handle, LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, CONTROL_TYPE_WRITE, (CONTROL_MODE_ACQUIRE_SWD_TARGET << 8) | CONTROL_COMMAND_PROGRAM, (max_attempts << 8) | (acquire_mode << 4) | psoc_type, &status, 1, 0, &transferred); if (retval != ERROR_OK || transferred == 0) { LOG_DEBUG("Zero bytes transferred"); return ERROR_FAIL; }if (retval != ERROR_OK || transferred == 0) { ... } if (status != PROGRAMMER_OK_ACK) { LOG_DEBUG("Programmer did not respond OK"); return ERROR_FAIL; }if (status != PROGRAMMER_OK_ACK) { ... } return ERROR_OK; }{ ... } static int kitprog_reset_target(void) { int transferred; char status = PROGRAMMER_NOK_NACK; int retval = jtag_libusb_control_transfer(kitprog_handle->usb_handle, LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, CONTROL_TYPE_WRITE, (CONTROL_MODE_RESET_TARGET << 8) | CONTROL_COMMAND_PROGRAM, 0, &status, 1, 0, &transferred); if (retval != ERROR_OK || transferred == 0) { LOG_DEBUG("Zero bytes transferred"); return ERROR_FAIL; }if (retval != ERROR_OK || transferred == 0) { ... } if (status != PROGRAMMER_OK_ACK) { LOG_DEBUG("Programmer did not respond OK"); return ERROR_FAIL; }if (status != PROGRAMMER_OK_ACK) { ... } return ERROR_OK; }{ ... } static int kitprog_swd_sync(void) { int transferred; char status = PROGRAMMER_NOK_NACK; int retval = jtag_libusb_control_transfer(kitprog_handle->usb_handle, LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, CONTROL_TYPE_WRITE, (CONTROL_MODE_SYNCHRONIZE_TRANSFER << 8) | CONTROL_COMMAND_PROGRAM, 0, &status, 1, 0, &transferred); if (retval != ERROR_OK || transferred == 0) { LOG_DEBUG("Zero bytes transferred"); return ERROR_FAIL; }if (retval != ERROR_OK || transferred == 0) { ... } if (status != PROGRAMMER_OK_ACK) { LOG_DEBUG("Programmer did not respond OK"); return ERROR_FAIL; }if (status != PROGRAMMER_OK_ACK) { ... } return ERROR_OK; }{ ... } static int kitprog_swd_seq(uint8_t seq_type) { int transferred; char status = PROGRAMMER_NOK_NACK; int retval = jtag_libusb_control_transfer(kitprog_handle->usb_handle, LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, CONTROL_TYPE_WRITE, (CONTROL_MODE_SEND_SWD_SEQUENCE << 8) | CONTROL_COMMAND_PROGRAM, seq_type, &status, 1, 0, &transferred); if (retval != ERROR_OK || transferred == 0) { LOG_DEBUG("Zero bytes transferred"); return ERROR_FAIL; }if (retval != ERROR_OK || transferred == 0) { ... } if (status != PROGRAMMER_OK_ACK) { LOG_DEBUG("Programmer did not respond OK"); return ERROR_FAIL; }if (status != PROGRAMMER_OK_ACK) { ... } return ERROR_OK; }{ ... } static int kitprog_generic_acquire(void) { const uint8_t devices[] = {DEVICE_PSOC4, DEVICE_PSOC3, DEVICE_PSOC5}; int retval; int acquire_count = 0; /* Due to the way the SWD port is shared between the Test Controller (TC) * and the Cortex-M3 DAP on the PSoC 5LP, the TC is the default SWD target * after power is applied. To access the DAP, the PSoC 5LP requires at least * one acquisition sequence to be run (which switches the SWD mux from the * TC to the DAP). However, after the mux is switched, the Cortex-M3 will be * held in reset until a series of registers are written to (see section 5.2 * of the PSoC 5LP Device Programming Specifications for details). * * Instead of writing the registers in this function, we just do what the * Cypress tools do and run the acquisition sequence a second time. This * will take the Cortex-M3 out of reset and enable debugging. *//* ... */ for (int i = 0; i < 2; i++) { for (uint8_t j = 0; j < sizeof(devices) && acquire_count == i; j++) { retval = kitprog_acquire_psoc(devices[j], ACQUIRE_MODE_RESET, 3); if (retval != ERROR_OK) { LOG_DEBUG("Acquisition function failed for device 0x%02x.", devices[j]); return retval; }if (retval != ERROR_OK) { ... } if (kitprog_get_status() == ERROR_OK) acquire_count++; }for (uint8_t j = 0; j < sizeof(devices) && acquire_count == i; j++) { ... } jtag_sleep(10); }for (int i = 0; i < 2; i++) { ... } if (acquire_count < 2) return ERROR_FAIL; return ERROR_OK; }{ ... } kitprog lowlevel functions /*************** swd wrapper functions *********************/ static int kitprog_swd_init(void) { return ERROR_OK; }{ ... } static void kitprog_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk) { assert(!(cmd & SWD_CMD_RNW)); kitprog_swd_queue_cmd(cmd, NULL, value); }{ ... } static void kitprog_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk) { assert(cmd & SWD_CMD_RNW); kitprog_swd_queue_cmd(cmd, value, 0); }{ ... } swd wrapper functions /*************** swd lowlevel functions ********************/ static int kitprog_swd_switch_seq(enum swd_special_seq seq) { switch (seq) { case JTAG_TO_SWD: if (kitprog_handle->supports_jtag_to_swd) { LOG_DEBUG("JTAG to SWD"); if (kitprog_swd_seq(SEQUENCE_JTAG_TO_SWD) != ERROR_OK) return ERROR_FAIL; break; }if (kitprog_handle->supports_jtag_to_swd) { ... } else { LOG_DEBUG("JTAG to SWD not supported"); /* Fall through to fix target reset issue */ }else { ... } /* fallthrough */case JTAG_TO_SWD: case LINE_RESET: LOG_DEBUG("SWD line reset"); if (kitprog_swd_seq(SEQUENCE_LINE_RESET) != ERROR_OK) return ERROR_FAIL; break;case LINE_RESET: default: LOG_ERROR("Sequence %d not supported.", seq); return ERROR_FAIL;default }switch (seq) { ... } return ERROR_OK; }{ ... } static int kitprog_swd_run_queue(void) { int ret; size_t read_count = 0; size_t read_index = 0; size_t write_count = 0; uint8_t *buffer = kitprog_handle->packet_buffer; do { LOG_DEBUG_IO("Executing %d queued transactions", pending_transfer_count); if (queued_retval != ERROR_OK) { LOG_DEBUG("Skipping due to previous errors: %d", queued_retval); break; }if (queued_retval != ERROR_OK) { ... } if (!pending_transfer_count) break; for (int i = 0; i < pending_transfer_count; i++) { uint8_t cmd = pending_transfers[i].cmd; uint32_t data = pending_transfers[i].data; /* When proper WAIT handling is implemented in the * common SWD framework, this kludge can be * removed. However, this might lead to minor * performance degradation as the adapter wouldn't be * able to automatically retry anything (because ARM * has forgotten to implement sticky error flags * clearing). See also comments regarding * cmsis_dap_cmd_DAP_TFER_Configure() and * cmsis_dap_cmd_DAP_SWD_Configure() in * cmsis_dap_init(). *//* ... */ if (!(cmd & SWD_CMD_RNW) && !(cmd & SWD_CMD_APNDP) && (cmd & SWD_CMD_A32) >> 1 == DP_CTRL_STAT && (data & CORUNDETECT)) { LOG_DEBUG("refusing to enable sticky overrun detection"); data &= ~CORUNDETECT; }if (!(cmd & SWD_CMD_RNW) && !(cmd & SWD_CMD_APNDP) && (cmd & SWD_CMD_A32) >> 1 == DP_CTRL_STAT && (data & CORUNDETECT)) { ... } LOG_DEBUG_IO("%s %s reg %x %"PRIx32, cmd & SWD_CMD_APNDP ? "AP" : "DP", cmd & SWD_CMD_RNW ? "read" : "write", (cmd & SWD_CMD_A32) >> 1, data); buffer[write_count++] = (cmd | SWD_CMD_START | SWD_CMD_PARK) & ~SWD_CMD_STOP; read_count++; if (!(cmd & SWD_CMD_RNW)) { buffer[write_count++] = (data) & 0xff; buffer[write_count++] = (data >> 8) & 0xff; buffer[write_count++] = (data >> 16) & 0xff; buffer[write_count++] = (data >> 24) & 0xff; }if (!(cmd & SWD_CMD_RNW)) { ... } else { read_count += 4; }else { ... } }for (int i = 0; i < pending_transfer_count; i++) { ... } if (jtag_libusb_bulk_write(kitprog_handle->usb_handle, BULK_EP_OUT, (char *)buffer, write_count, 0, &ret)) { LOG_ERROR("Bulk write failed"); queued_retval = ERROR_FAIL; break; }if (jtag_libusb_bulk_write(kitprog_handle->usb_handle, BULK_EP_OUT, (char *)buffer, write_count, 0, &ret)) { ... } else { queued_retval = ERROR_OK; }else { ... } /* KitProg firmware does not send a zero length packet * after the bulk-in transmission of a length divisible by bulk packet * size (64 bytes) as required by the USB specification. * Therefore libusb would wait for continuation of transmission. * Workaround: Limit bulk read size to expected number of bytes * for problematic transfer sizes. Otherwise use the maximum buffer * size here because the KitProg sometimes doesn't like bulk reads * of fewer than 62 bytes. (?!?!) *//* ... */ size_t read_count_workaround = SWD_MAX_BUFFER_LENGTH; if (read_count % 64 == 0) read_count_workaround = read_count; if (jtag_libusb_bulk_read(kitprog_handle->usb_handle, BULK_EP_IN | LIBUSB_ENDPOINT_IN, (char *)buffer, read_count_workaround, 1000, &ret)) { LOG_ERROR("Bulk read failed"); queued_retval = ERROR_FAIL; break; }if (jtag_libusb_bulk_read(kitprog_handle->usb_handle, BULK_EP_IN | LIBUSB_ENDPOINT_IN, (char *)buffer, read_count_workaround, 1000, &ret)) { ... } else { /* Handle garbage data by offsetting the initial read index */ if ((unsigned int)ret > read_count) read_index = ret - read_count; queued_retval = ERROR_OK; }else { ... } for (int i = 0; i < pending_transfer_count; i++) { if (pending_transfers[i].cmd & SWD_CMD_RNW) { uint32_t data = le_to_h_u32(&buffer[read_index]); LOG_DEBUG_IO("Read result: %"PRIx32, data); if (pending_transfers[i].buffer) *(uint32_t *)pending_transfers[i].buffer = data; read_index += 4; }if (pending_transfers[i].cmd & SWD_CMD_RNW) { ... } uint8_t ack = buffer[read_index] & 0x07; if (ack != SWD_ACK_OK || (buffer[read_index] & 0x08)) { LOG_DEBUG("SWD ack not OK: %d %s", i, ack == SWD_ACK_WAIT ? "WAIT" : ack == SWD_ACK_FAULT ? "FAULT" : "JUNK"); queued_retval = swd_ack_to_error_code(ack); break; }if (ack != SWD_ACK_OK || (buffer[read_index] & 0x08)) { ... } read_index++; }for (int i = 0; i < pending_transfer_count; i++) { ... } ...} while (0); pending_transfer_count = 0; int retval = queued_retval; queued_retval = ERROR_OK; return retval; }{ ... } static void kitprog_swd_queue_cmd(uint8_t cmd, uint32_t *dst, uint32_t data) { if (pending_transfer_count == pending_queue_len) { /* Not enough room in the queue. Run the queue. */ queued_retval = kitprog_swd_run_queue(); }if (pending_transfer_count == pending_queue_len) { ... } if (queued_retval != ERROR_OK) return; pending_transfers[pending_transfer_count].data = data; pending_transfers[pending_transfer_count].cmd = cmd; if (cmd & SWD_CMD_RNW) { /* Queue a read transaction */ pending_transfers[pending_transfer_count].buffer = dst; }if (cmd & SWD_CMD_RNW) { ... } pending_transfer_count++; }{ ... } swd lowlevel functions /*************** jtag lowlevel functions ********************/ static int kitprog_reset(int trst, int srst) { int retval = ERROR_OK; if (trst == 1) { LOG_ERROR("KitProg: Interface has no TRST"); return ERROR_FAIL; }if (trst == 1) { ... } if (srst == 1) { retval = kitprog_reset_target(); /* Since the previous command also disables SWCLK output, we need to send an * SWD bus reset command to re-enable it. For some reason, running * kitprog_swd_seq() immediately after kitprog_reset_target() won't * actually fix this. Instead, kitprog_swd_seq() will be run once OpenOCD * tries to send a JTAG-to-SWD sequence, which should happen during * swd_check_reconnect (see the JTAG_TO_SWD case in kitprog_swd_switch_seq). *//* ... */ }if (srst == 1) { ... } if (retval != ERROR_OK) LOG_ERROR("KitProg: Interface reset failed"); return retval; }{ ... } COMMAND_HANDLER(kitprog_handle_info_command) { int retval = kitprog_get_info(); return retval; }{ ... } COMMAND_HANDLER(kitprog_handle_acquire_psoc_command) { int retval = kitprog_generic_acquire(); return retval; }{ ... } COMMAND_HANDLER(kitprog_handle_init_acquire_psoc_command) { kitprog_init_acquire_psoc = true; return ERROR_OK; }{ ... } static const struct command_registration kitprog_subcommand_handlers[] = { { .name = "info", .handler = &kitprog_handle_info_command, .mode = COMMAND_EXEC, .usage = "", .help = "show KitProg info", ...}, { .name = "acquire_psoc", .handler = &kitprog_handle_acquire_psoc_command, .mode = COMMAND_EXEC, .usage = "", .help = "try to acquire a PSoC", ...}, { .name = "init_acquire_psoc", .handler = &kitprog_handle_init_acquire_psoc_command, .mode = COMMAND_CONFIG, .help = "try to acquire a PSoC during init", .usage = "", ...}, COMMAND_REGISTRATION_DONE ...}; static const struct command_registration kitprog_command_handlers[] = { { .name = "kitprog", .mode = COMMAND_ANY, .help = "perform KitProg management", .usage = "<cmd>", .chain = kitprog_subcommand_handlers, ...}, COMMAND_REGISTRATION_DONE ...}; static const struct swd_driver kitprog_swd = { .init = kitprog_swd_init, .switch_seq = kitprog_swd_switch_seq, .read_reg = kitprog_swd_read_reg, .write_reg = kitprog_swd_write_reg, .run = kitprog_swd_run_queue, ...}; static const char * const kitprog_transports[] = { "swd", NULL }; struct adapter_driver kitprog_adapter_driver = { .name = "kitprog", .transports = kitprog_transports, .commands = kitprog_command_handlers, .init = kitprog_init, .quit = kitprog_quit, .reset = kitprog_reset, .swd_ops = &kitprog_swd, ...};
Details
Show:
from
Types: Columns:
Click anywhere in the source to view detailed information here...