-
Notifications
You must be signed in to change notification settings - Fork 36
/
ch347.c
1808 lines (1610 loc) · 49.8 KB
/
ch347.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
/***************************************************************************
* Driver for CH347-JTAG interface V1.1 *
* *
* Copyright (C) 2022 by oidcat. *
* Author: oidcatiot@163.com *
* *
* CH347 is a high-speed USB bus converter chip that provides UART, I2C *
* and SPI synchronous serial ports and JTAG interface through USB bus. *
* *
* The Jtag interface by CH347 can supports transmission frequency *
* configuration up to 60MHz. *
* *
* The USB2.0 to JTAG scheme based on CH347 can be used to build *
* customized USB high-speed JTAG debugger and other products. *
* *
* 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. *
* *
* _____________ *
* | |____JTAG/SWD (TDO,TDI,TMS,TCK,TRST) *
* USB__| CH347T/F | *
* |_____________|____UART(TXD1,RXD1,RTS1,CTS1,DTR1) *
* ______|______ *
* | | *
* | 8 MHz XTAL | *
* |_____________| *
* *
* 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/>. *
***************************************************************************/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#if IS_CYGWIN == 1
#include "windows.h"
#undef LOG_ERROR
#endif
/* project specific includes */
#include <jtag/interface.h>
#include <jtag/commands.h>
#include <jtag/swd.h>
#include <helper/time_support.h>
#include <helper/replacements.h>
#include <helper/list.h>
#include <helper/binarybuffer.h>
#include "libusb_helper.h"
/* system includes */
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#define JTAGIO_STA_OUT_TDI (0x10)
#define JTAGIO_STA_OUT_TMS (0x02)
#define JTAGIO_STA_OUT_TCK (0x01)
#define JTAGIO_STA_OUT_TRST (0x20)
#define TDI_H JTAGIO_STA_OUT_TDI
#define TDI_L 0
#define TMS_H JTAGIO_STA_OUT_TMS
#define TMS_L 0
#define TCK_H JTAGIO_STA_OUT_TCK
#define TCK_L 0
#define TRST_H JTAGIO_STA_OUT_TRST
#define TRST_L 0
#define KHZ(n) ((n)*UINT64_C(1000))
#define MHZ(n) ((n)*UINT64_C(1000000))
#define GHZ(n) ((n)*UINT64_C(1000000000))
#define HW_TDO_BUF_SIZE 4096
#define SF_PACKET_BUF_SIZE 51200 /* Command packet length */
#define UCMDPKT_DATA_MAX_BYTES_USBHS 507 /* The data length contained in each
command packet during USB
high-speed operation */
#define USBC_PACKET_USBHS 512 /* Maximum data length per packet at
USB high speed */
#define USBC_PACKET_USBHS_SINGLE 510 /* usb high speed max
package length */
#define CH347_CMD_HEADER 3 /* Protocol header length */
#define CH347_CMD_HEADER 3 /* 协议包头长度 */
/* Protocol transmission format: CMD (1 byte)+Length (2 bytes)+Data */
#define CH347_CMD_INFO_RD 0xCA /* Parameter acquisition, used to
obtain firmware version,
JTAG interface related parameters,
etc */
#define CH347_CMD_JTAG_INIT 0xD0 /* JTAG Interface Initialization
Command */
#define CH347_CMD_JTAG_BIT_OP 0xD1 /* JTAG interface pin bit control
command */
#define CH347_CMD_JTAG_BIT_OP_RD 0xD2 /* JTAG interface pin bit control and
read commands */
#define CH347_CMD_JTAG_DATA_SHIFT 0xD3 /* JTAG interface data shift
command */
#define CH347_CMD_JTAG_DATA_SHIFT_RD 0xD4 /* JTAG interface data shift and read
command */
/* SWD */
#define CH347_CMD_SWD_INIT 0xE5 /* SWD Interface Initialization Command */
#define CH347_CMD_SWD 0xE8
#define CH347_CMD_SWD_REG_W 0xA0 /* SWD Interface write reg */
#define CH347_CMD_SWD_SEQ_W 0xA1 /* SWD Interface write spec seq */
#define CH347_CMD_SWD_REG_R 0xA2 /* SWD Interface read reg */
#define CH347_MAX_SEND_CMD 0X20 /* max send cmd number */
#define CH347_MAX_SEND_BUF 0X200
#define CH347_MAX_RECV_BUF 0X200
#define BUILD_UINT16(loByte, hiByte) \
((uint16_t)(((loByte)&0x00FF) + (((hiByte)&0x00FF) << 8)))
#pragma pack(1)
typedef unsigned char UCHAR;
typedef enum pack {
STANDARD_PACK = 0,
LARGER_PACK = 1,
} PACK_SIZE;
typedef struct _CH347_info /* Record the CH347 pin status */
{
int TMS;
int TDI;
int TCK;
int TRST;
int buffer_idx;
uint8_t buffer[SF_PACKET_BUF_SIZE];
int len_idx;
int len_value;
uint8_t lastCmd;
uint8_t read_buffer[SF_PACKET_BUF_SIZE];
uint32_t read_idx;
uint32_t read_count;
struct bit_copy_queue read_queue;
PACK_SIZE pack_size;
} _CH347_Info;
int DevIsOpened; /* Whether the device is turned on */
bool UsbHighDev = true;
unsigned long USBC_PACKET;
typedef struct _CH347_SWD_IO {
uint8_t usbcmd; /* 0xA0、0xA1、0xA2 */
uint8_t cmd;
uint32_t *dst;
uint32_t value;
struct list_head list_entry;
} CH347_SWD_IO, *PCH347_SWD_IO;
typedef struct _CH347_SWD_CONTEXT {
uint8_t send_buf[CH347_MAX_SEND_BUF];
uint8_t recv_buf[CH347_MAX_RECV_BUF];
uint32_t send_len;
uint32_t recv_len;
uint32_t need_recv_len;
int queued_retval;
uint8_t sent_cmd_count;
struct list_head send_cmd_head;
struct list_head free_cmd_head;
uint8_t *ch347_cmd_buf;
} CH347_SWD_CONTEXT;
static CH347_SWD_CONTEXT ch347_swd_context;
static bool swd_mode = false;
#pragma pack()
#ifdef _WIN32
#include <windows.h>
typedef int(__stdcall * pCH347OpenDevice)(unsigned long iIndex);
typedef int(__stdcall * pCH347CloseDevice)(unsigned long iIndex);
typedef unsigned long(__stdcall * pCH347SetTimeout)(
unsigned long iIndex, /* Specify equipment serial number */
unsigned long iWriteTimeout, /* Specifies the timeout period for USB
write out data blocks, in milliseconds
mS, and 0xFFFFFFFF specifies no timeout
(default) */
unsigned long iReadTimeout); /* Specifies the timeout period for USB
reading data blocks, in milliseconds mS,
and 0xFFFFFFFF specifies no timeout
(default) */
typedef unsigned long(__stdcall * pCH347WriteData)(
unsigned long iIndex, /* Specify equipment serial number */
void *oBuffer, /* Point to a buffer large enough to hold
the descriptor */
unsigned long *ioLength); /* Pointing to the length unit, the input
is the length to be read, and the
return is the actual read length */
typedef unsigned long(__stdcall * pCH347ReadData)(
unsigned long iIndex, /* Specify equipment serial number */
void *oBuffer, /* Point to a buffer large enough to
hold the descriptor */
unsigned long *ioLength); /* Pointing to the length unit, the input
is the length to be read, and the
return is the actual read length */
typedef unsigned long(__stdcall * pCH347Jtag_INIT)(
unsigned long iIndex, /* Specify equipment serial number */
unsigned char iClockRate); /* Pointing to the length unit, the input
is the length to be read, and the
return is the actual read length */
HMODULE uhModule;
BOOL ugOpen;
unsigned long ugIndex;
pCH347OpenDevice CH347OpenDevice;
pCH347CloseDevice CH347CloseDevice;
pCH347SetTimeout CH347SetTimeout;
pCH347ReadData CH347ReadData;
pCH347WriteData CH347WriteData;
/* pCH347Jtag_INIT CH347Jtag_INIT; */
#elif defined(__linux__)
#include <jtag/drivers/libusb_helper.h>
#define CH347_EPOUT 0x06u
#define CH347_EPIN 0x86u
bool ugOpen;
unsigned long ugIndex;
struct libusb_device_handle *ch347_handle;
static const uint16_t ch347_vids[] = {0x1a86, 0x1a86, 0x1a86};
static const uint16_t ch347_pids[] = {0x55dd, 0x55de, 0x55e7};
static uint32_t CH347OpenDevice(uint64_t iIndex)
{
if (jtag_libusb_open(ch347_vids, ch347_pids,
&ch347_handle, NULL) != ERROR_OK) {
return false;
} else {
return true;
}
}
static bool CH347WriteData(uint64_t iIndex, uint8_t *data, uint64_t *length)
{
int ret, tmp = 0;
ret = jtag_libusb_bulk_write(ch347_handle,
CH347_EPOUT,
(char *)data,
*length,
100, &tmp);
*length = tmp;
if (!ret)
return true;
else
return false;
}
static bool CH347ReadData(uint64_t iIndex, uint8_t *data, uint64_t *length)
{
int ret, tmp = 0;
int size = *length;
ret = jtag_libusb_bulk_read(ch347_handle,
CH347_EPIN,
(char *)data,
size,
100, &tmp);
*length = tmp;
if (!ret)
return true;
else
return false;
}
static bool CH347CloseDevice(uint64_t iIndex)
{
jtag_libusb_close(ch347_handle);
return true;
}
#endif
_CH347_Info ch347;
static int ch347_swd_run_queue(void);
/* swd init func */
static bool CH347SWD_INIT(uint64_t iIndex, uint8_t iClockRate)
{
uint8_t cmdBuf[128] = "";
unsigned long i = 0;
cmdBuf[i++] = CH347_CMD_SWD_INIT;
cmdBuf[i++] = 8; /* Data length is 6 */
cmdBuf[i++] = 0;
cmdBuf[i++] = 0x40;
cmdBuf[i++] = 0x42;
cmdBuf[i++] = 0x0f; /* Reserved Bytes */
cmdBuf[i++] = 0x00; /* Reserved Bytes */
cmdBuf[i++] = iClockRate; /* JTAG clock speed */
i += 3; /* Reserved Bytes */
unsigned long mLength = i;
if (!CH347WriteData(iIndex, cmdBuf, &mLength) || (mLength != i))
return false;
mLength = 4;
memset(cmdBuf, 0, sizeof(cmdBuf));
if (!CH347ReadData(iIndex, cmdBuf, &mLength) || (mLength != 4))
return false;
return true;
}
/**
* HexToString - Hex Conversion String Function
* @param buf Point to a buffer to place the Hex data to be converted
* @param size Pointing to the length unit where data needs to be converted
*
* @return Returns a converted string
*/
static char *HexToString(uint8_t *buf, uint32_t size)
{
uint32_t i;
if (buf == NULL)
return "NULL";
char *str = calloc(size * 2 + 1, 1);
for (i = 0; i < size; i++)
sprintf(str + 2 * i, "%02x ", buf[i]);
return str;
}
/**
* CH347_Write - CH347 Write
* @param oBuffer Point to a buffer to place the data to be written out
* @param ioLength Pointing to the length unit, the input is the length to be
* written out, and the return is the actual written length
*
* @return Write success returns 1, failure returns 0
*/
static int CH347_Write(void *oBuffer, unsigned long *ioLength)
{
int ret = -1;
unsigned long wlength = *ioLength, WI;
if (*ioLength >= HW_TDO_BUF_SIZE)
wlength = HW_TDO_BUF_SIZE;
WI = 0;
while (1) {
ret = CH347WriteData(ugIndex, (uint8_t *)oBuffer + WI,
&wlength);
if (!ret) {
*ioLength = 0;
return false;
}
LOG_DEBUG_IO("(size=%lu, buf=[%s]) -> %" PRIu32, wlength,
HexToString((uint8_t *)oBuffer, wlength),
(uint32_t)wlength);
WI += wlength;
if (WI >= *ioLength)
break;
if ((*ioLength - WI) > HW_TDO_BUF_SIZE)
wlength = HW_TDO_BUF_SIZE;
else
wlength = *ioLength - WI;
}
*ioLength = WI;
return true;
}
/**
* CH347_Read - CH347 Read
* @param oBuffer Point to a buffer to place the data to be read in
* @param ioLength Pointing to the length unit, the input is the length to
* be read, and the return is the actual read length
*
* @return Write success returns 1, failure returns 0
*/
static int CH347_Read(void *oBuffer, unsigned long *ioLength)
{
unsigned long rlength = *ioLength, WI;
/* The maximum allowable reading for a single read is 4096B of data.
If it exceeds the allowable reading limit, it will be calculated as
4096B */
if (rlength > HW_TDO_BUF_SIZE)
rlength = HW_TDO_BUF_SIZE;
WI = 0;
while (1) {
if (!CH347ReadData(ugIndex, (uint8_t *)oBuffer + WI,
&rlength)) {
LOG_ERROR("CH347_Read read data failure.");
return false;
}
WI += rlength;
if (WI >= *ioLength)
break;
if ((*ioLength - WI) > HW_TDO_BUF_SIZE)
rlength = HW_TDO_BUF_SIZE;
else
rlength = *ioLength - WI;
}
LOG_DEBUG_IO("(size=%lu, buf=[%s]) -> %" PRIu32, WI,
HexToString((uint8_t *)oBuffer, WI), (uint32_t)WI);
*ioLength = WI;
return true;
}
static void CH347_Read_Scan(UCHAR *pBuffer, uint32_t length)
{
unsigned long read_size = 0;
unsigned long RxLen = 0;
unsigned long index = 0;
unsigned long read_buf_index = 0;
unsigned char *read_buf = NULL;
int dataLen = 0, i = 0; /*, this_bits = 0; */
read_size = length;
RxLen = read_size;
index = 0;
read_buf_index = 0;
read_buf = calloc(sizeof(unsigned char), read_size);
if (!CH347_Read(read_buf, &RxLen)) {
LOG_ERROR("CH347_Read read data failure.");
return;
}
while (index < read_size) { /* deal with the CH347_CMD_JTAG_BIT_OP_RD or CH347_CMD_JTAG_DATA_SHIFT_RD */
if (read_buf[index] == CH347_CMD_JTAG_DATA_SHIFT_RD) {
dataLen = read_buf[++index] & 0xFF;
dataLen += (read_buf[++index] & 0xFF) << 8;
memcpy(pBuffer + read_buf_index, &read_buf[index + 1],
dataLen);
read_buf_index += dataLen;
index += dataLen + 1;
} else if (read_buf[index] == CH347_CMD_JTAG_BIT_OP_RD) {
dataLen = read_buf[++index] & 0xFF;
dataLen += (read_buf[++index] & 0xFF) << 8;
for (i = 0; i < dataLen; i++) {
if (read_buf[index + 1 + i] == 0x01)
*(pBuffer + read_buf_index) |= (1 << i);
else
*(pBuffer + read_buf_index) &= ~(1 << i);
}
read_buf_index += 1;
index += dataLen + 1;
} else {
// LOG_ERROR("readbuf read_commend error");
*(pBuffer + read_buf_index) = read_buf[index];
read_buf_index++;
index++;
}
}
if (read_buf) {
free(read_buf);
read_buf = NULL;
}
}
static void CH347_Flush_Buffer(void)
{
unsigned long retlen = ch347.buffer_idx;
int nb = ch347.buffer_idx, ret = ERROR_OK;
while (ret == ERROR_OK && nb > 0) {
ret = CH347_Write(ch347.buffer, &retlen);
nb -= retlen;
}
memset(&ch347.buffer, 0, sizeof(ch347.buffer));
ch347.buffer_idx = 0;
ch347.lastCmd = 0;
ch347.len_idx = 0;
ch347.len_value = 0;
if (ch347.read_count == 0)
return;
if (ch347.pack_size == LARGER_PACK) {
CH347_Read_Scan(&ch347.read_buffer[0], ch347.read_count);
bit_copy_execute(&ch347.read_queue);
memset(ch347.read_buffer, 0, SF_PACKET_BUF_SIZE);
ch347.read_count = 0;
ch347.read_idx = 0;
}
}
static void CH347_In_Buffer(uint8_t byte)
{
if ((SF_PACKET_BUF_SIZE - ch347.buffer_idx) < 1)
CH347_Flush_Buffer();
ch347.buffer[ch347.buffer_idx] = byte;
ch347.buffer_idx++;
if ((SF_PACKET_BUF_SIZE - ch347.buffer_idx) == 0)
CH347_Flush_Buffer();
}
static void CH347_In_Buffer_bytes(uint8_t *bytes, unsigned long bytes_length)
{
if ((ch347.buffer_idx + bytes_length) > SF_PACKET_BUF_SIZE)
CH347_Flush_Buffer();
memcpy(&ch347.buffer[ch347.buffer_idx], bytes, bytes_length);
ch347.buffer_idx += bytes_length;
if ((SF_PACKET_BUF_SIZE - ch347.buffer_idx) < 1)
CH347_Flush_Buffer();
}
static void combinePackets(uint8_t cmd, int cur_idx, unsigned long int len)
{
if (cmd != ch347.lastCmd) {
ch347.buffer[cur_idx] = cmd;
ch347.buffer[cur_idx + 1] =
(uint8_t)(((len - CH347_CMD_HEADER) >> 0) & 0xFF);
ch347.buffer[cur_idx + 2] =
(uint8_t)(((len - CH347_CMD_HEADER) >> 8) & 0xFF);
/* update the ch347 struct */
ch347.lastCmd = cmd;
ch347.len_idx = cur_idx + 1;
ch347.len_value = (len - CH347_CMD_HEADER);
} else {
/* update the ch347 struct cmd data leng */
ch347.len_value += (len - CH347_CMD_HEADER);
/* update the cmd packet valid leng */
ch347.buffer[ch347.len_idx] = (uint8_t)((ch347.len_value >> 0) \
& 0xFF);
ch347.buffer[ch347.len_idx + 1] = (uint8_t)(
(ch347.len_value >> 8) & 0xFF);
/* update the buffer data leng */
memcpy(&ch347.buffer[cur_idx],
&ch347.buffer[cur_idx + CH347_CMD_HEADER],
(len - CH347_CMD_HEADER));
/* update the ch347 buffer index */
ch347.buffer_idx -= CH347_CMD_HEADER;
}
}
/**
* CH347_ClockTms - Function function used to change the TMS value at the
* rising edge of TCK to switch its Tap state
* @param BitBangPkt Protocol package
* @param tms TMS value to be changed
* @param BI Protocol packet length
*
* @return Return protocol packet length
*/
static unsigned long CH347_ClockTms(int tms, unsigned long BI)
{
uint8_t data = 0;
unsigned char cmd = 0;
if (tms == 1)
cmd = TMS_H;
else
cmd = TMS_L;
BI += 2;
data = cmd | TDI_L | TCK_L | TRST_H;
CH347_In_Buffer(data);
data = cmd | TDI_L | TCK_H | TRST_H;
CH347_In_Buffer(data);
ch347.TMS = cmd;
ch347.TDI = TDI_L;
ch347.TCK = TCK_H;
ch347.TRST = TRST_H;
return BI;
}
/**
* CH347_IdleClock - Function function to ensure that the clock is in a low state
* @param BitBangPkt Protocol package
* @param BI Protocol packet length
*
* @return Return protocol packet length
*/
static unsigned long CH347_IdleClock(unsigned long BI)
{
unsigned char byte = 0;
byte |= ch347.TMS ? TMS_H : TMS_L;
byte |= ch347.TDI ? TDI_H : TDI_L;
byte |= ch347.TRST ? TRST_H : TRST_L;
BI++;
CH347_In_Buffer(byte);
return BI;
}
/**
* CH347_TmsChange - Function function that performs state switching by changing the value of TMS
* @param tmsValue The TMS values that need to be switched form one byte of data in the switching order
* @param step The number of bit values that need to be read from the tmsValue value
* @param skip Count from the skip bit of tmsValue to step
*
*/
static void CH347_TmsChange(const unsigned char *tmsValue, int step, int skip)
{
int i;
int index = ch347.buffer_idx;
unsigned long BI, retlen, cmdLen;
BI = CH347_CMD_HEADER;
retlen = CH347_CMD_HEADER;
LOG_DEBUG_IO("(TMS Value: %02x..., step = %d, skip = %d)", tmsValue[0],
step, skip);
for (i = 0; i < 3; i++)
CH347_In_Buffer(0);
for (i = skip; i < step; i++) {
retlen = CH347_ClockTms((tmsValue[i / 8] >> (i % 8)) & 0x01, BI);
BI = retlen;
}
cmdLen = CH347_IdleClock(BI);
combinePackets(CH347_CMD_JTAG_BIT_OP, index, cmdLen);
}
/**
* CH347_TMS - By ch347_ execute_ Queue call
* @param cmd Upper layer transfer command parameters
*
*/
static void CH347_TMS(struct tms_command *cmd)
{
LOG_DEBUG_IO("(step: %d)", cmd->num_bits);
CH347_TmsChange(cmd->bits, cmd->num_bits, 0);
}
/**
* CH347_Reset - CH347 Reset Tap Status Function
* @brief If there are more than six consecutive TCKs and TMS is high, the state
* machine can be set to a Test-Logic-Reset state
*
*/
static int ch347_reset(int trst, int srst)
{
LOG_DEBUG_IO("reset trst: %i srst %i", trst, srst);
unsigned char BitBang[512] = "", BII, i;
unsigned long TxLen;
if (!swd_mode){
BII = CH347_CMD_HEADER;
for (i = 0; i < 7; i++) {
BitBang[BII++] = TMS_H | TDI_L | TCK_L;
BitBang[BII++] = TMS_H | TDI_L | TCK_H;
}
BitBang[BII++] = TMS_H | TDI_L | TCK_L;
ch347.TCK = TCK_L;
ch347.TDI = TDI_L;
ch347.TMS = 0;
BitBang[0] = CH347_CMD_JTAG_BIT_OP;
BitBang[1] = BII - CH347_CMD_HEADER;
BitBang[2] = 0;
TxLen = BII;
if (!CH347_Write(BitBang, &TxLen) && (TxLen != BII)) {
LOG_ERROR("JTAG_Init send usb data failure.");
return false;
}
}
return ERROR_OK;
}
/**
* CH347_MovePath - Obtain the current Tap status and switch to the status TMS
* value passed down by cmd
* @param cmd Upper layer transfer command parameters
*
*/
static void CH347_MovePath(struct pathmove_command *cmd)
{
int i;
int index = ch347.buffer_idx;
unsigned long BI, retlen = 0, cmdLen;
BI = CH347_CMD_HEADER;
for (i = 0; i < 3; i++)
CH347_In_Buffer(0);
LOG_DEBUG_IO("(num_states=%d, last_state=%d)",
cmd->num_states, cmd->path[cmd->num_states - 1]);
for (i = 0; i < cmd->num_states; i++) {
if (tap_state_transition(tap_get_state(), false) ==
cmd->path[i])
retlen = CH347_ClockTms(0, BI);
BI = retlen;
if (tap_state_transition(tap_get_state(), true) == cmd->path[i])
retlen = CH347_ClockTms(1, BI);
BI = retlen;
tap_set_state(cmd->path[i]);
}
cmdLen = CH347_IdleClock(BI);
combinePackets(CH347_CMD_JTAG_BIT_OP, index, cmdLen);
}
/**
* CH347_MoveState - Toggle the Tap state to the Target state stat
* @param stat Pre switch target path
* @param skip Number of digits to skip
*
*/
static void CH347_MoveState(tap_state_t state, int skip)
{
uint8_t tms_scan;
int tms_len;
LOG_DEBUG_IO("(from %s to %s)", tap_state_name(tap_get_state()),
tap_state_name(state));
if (tap_get_state() == state)
return;
tms_scan = tap_get_tms_path(tap_get_state(), state);
tms_len = tap_get_tms_path_len(tap_get_state(), state);
CH347_TmsChange(&tms_scan, tms_len, skip);
tap_set_state(state);
}
/**
* CH347_WriteRead - CH347 Batch read/write function
* @param bits Read and write data this time
* @param nb_bits Incoming data length
* @param scan The transmission method of incoming data to determine whether
* to perform data reading
*/
static void CH347_WriteRead(struct scan_command *cmd, uint8_t *bits,
int nb_bits, enum scan_type scan)
{
int nb8 = nb_bits / 8;
int nb1 = nb_bits % 8;
int i, num_bits = 0;
bool IsRead = false; /*, isLastByte = false */
uint8_t TMS_Bit = 0, TDI_Bit = 0, CMD_Bit;
static uint8_t byte0[SF_PACKET_BUF_SIZE];
uint8_t *readData = calloc(SF_PACKET_BUF_SIZE, 1);
unsigned long readLen = 0;
unsigned long BI = 0, DI, DII, PktDataLen, DLen = 0, tempIndex,
totalReadLength = 0, tempLength = 0;
if (ch347.pack_size == LARGER_PACK) {
if ((ch347.read_count >= (510 * 1)))
CH347_Flush_Buffer();
} else {
CH347_Flush_Buffer();
}
if (nb8 > 0 && nb1 == 0) {
nb8--;
nb1 = 8;
}
IsRead = (scan == SCAN_IN || scan == SCAN_IO);
DI = BI = 0;
while (DI < (unsigned long)nb8) {
if ((nb8 - DI) > UCMDPKT_DATA_MAX_BYTES_USBHS)
PktDataLen = UCMDPKT_DATA_MAX_BYTES_USBHS;
else
PktDataLen = nb8 - DI;
DII = PktDataLen;
if (IsRead)
CH347_In_Buffer(CH347_CMD_JTAG_DATA_SHIFT_RD);
else
CH347_In_Buffer(CH347_CMD_JTAG_DATA_SHIFT);
/* packet data don't deal D3 & D4 */
if ((CH347_CMD_JTAG_DATA_SHIFT_RD != ch347.lastCmd) |
(CH347_CMD_JTAG_DATA_SHIFT != ch347.lastCmd)) {
/* update the ch347 struct */
ch347.lastCmd = 0;
ch347.len_idx = 0;
ch347.len_value = 0;
}
CH347_In_Buffer((uint8_t)(PktDataLen >> 0) & 0xFF);
CH347_In_Buffer((uint8_t)(PktDataLen >> 8) & 0xFF);
if (bits)
CH347_In_Buffer_bytes(&bits[DI], PktDataLen);
else
CH347_In_Buffer_bytes(byte0, PktDataLen);
DI += DII;
tempLength += (DII + CH347_CMD_HEADER);
}
totalReadLength += tempLength;
if (IsRead) {
ch347.read_count += tempLength;
readLen += tempLength;
}
if (bits) {
CMD_Bit = IsRead ? CH347_CMD_JTAG_BIT_OP_RD : \
CH347_CMD_JTAG_BIT_OP;
DLen = (nb1 * 2) + 1;
if (CMD_Bit != ch347.lastCmd) {
CH347_In_Buffer(CMD_Bit);
CH347_In_Buffer((uint8_t)(DLen >> 0) & 0xFF);
CH347_In_Buffer((uint8_t)(DLen >> 8) & 0xFF);
ch347.lastCmd = CMD_Bit;
ch347.len_idx = ch347.buffer_idx - 2;
ch347.len_value = DLen;
} else {
/* update the ch347 struct cmd data leng */
ch347.len_value += DLen;
/* update the cmd packet valid leng */
ch347.buffer[ch347.len_idx] =
(uint8_t)(ch347.len_value >> 0) & 0xFF;
ch347.buffer[ch347.len_idx + 1] =
(uint8_t)(ch347.len_value >> 8) & 0xFF;
}
TMS_Bit = TMS_L;
for (i = 0; i < nb1; i++) {
if ((bits[nb8] >> i) & 0x01)
TDI_Bit = TDI_H;
else
TDI_Bit = TDI_L;
if ((i + 1) == nb1)
TMS_Bit = TMS_H;
CH347_In_Buffer(TMS_Bit | TDI_Bit | TCK_L | TRST_H);
CH347_In_Buffer(TMS_Bit | TDI_Bit | TCK_H | TRST_H);
}
CH347_In_Buffer(TMS_Bit | TDI_Bit | TCK_L | TRST_H);
}
ch347.TMS = TMS_Bit;
ch347.TDI = TDI_Bit;
ch347.TCK = TCK_L;
if (IsRead) {
tempLength = ((DLen / 2) + CH347_CMD_HEADER);
totalReadLength += tempLength;
ch347.read_count += tempLength;
readLen += tempLength;
DI = BI = 0;
}
int offset = 0, bit_count = 0;
if (IsRead && totalReadLength > 0) {
if (ch347.pack_size == STANDARD_PACK && bits && cmd) {
CH347_Flush_Buffer();
CH347_Read_Scan(readData, readLen);
}
for (i = 0; i < cmd->num_fields; i++) {
/* if neither in_value nor in_handler
* are specified we don't have to examine this field
*/
LOG_DEBUG("fields[%i].in_value[%i], offset: %d",
i, cmd->fields[i].num_bits, offset);
num_bits = cmd->fields[i].num_bits;
if (ch347.pack_size == LARGER_PACK)
bit_count += num_bits;
if (cmd->fields[i].in_value) {
if (ch347.pack_size == LARGER_PACK) {
bit_copy_queued(
&ch347.read_queue,
cmd->fields[i].in_value,
0,
&ch347.read_buffer[ch347.read_idx],
offset, num_bits);
if (num_bits > 7)
ch347.read_idx += DIV_ROUND_UP(bit_count, 8);
} else {
num_bits = cmd->fields[i].num_bits;
uint8_t *captured = buf_set_buf(
readData, bit_count,
malloc(DIV_ROUND_UP(num_bits,
8)), 0,
num_bits);
if (LOG_LEVEL_IS(LOG_LVL_DEBUG_IO)) {
char *char_buf =
buf_to_hex_str(
captured,
(num_bits >
DEBUG_JTAG_IOZ)
? DEBUG_JTAG_IOZ
: num_bits);
free(char_buf);
}
buf_cpy(captured,
cmd->fields[i].in_value,
num_bits);
free(captured);
}
}else {
LOG_DEBUG_IO("cmd->fields with no data");
}
if (ch347.pack_size == LARGER_PACK) {
offset += num_bits;
}else{
bit_count += cmd->fields[i].num_bits;
}
}
}
tempIndex = ch347.buffer_idx;
for (i = 0; i < CH347_CMD_HEADER; i++)
CH347_In_Buffer(0);
BI = CH347_CMD_HEADER;
BI = CH347_IdleClock(BI);
combinePackets(CH347_CMD_JTAG_BIT_OP, tempIndex, BI);
if (readData) {
free(readData);
readData = NULL;
}
}
static void CH347_RunTest(int cycles, tap_state_t state)
{
LOG_DEBUG_IO("%s(cycles=%i, end_state=%d)", __func__, cycles, state);
if (tap_get_state() != TAP_IDLE)
CH347_MoveState(TAP_IDLE, 0);
uint8_t tmsValue = 0;
CH347_TmsChange(&tmsValue, 7, 1);
CH347_WriteRead(NULL, NULL, cycles, SCAN_OUT);
CH347_MoveState(state, 0);
}
static void CH347_TableClocks(int cycles)
{
LOG_DEBUG_IO("%s(cycles=%i)", __func__, cycles);
CH347_WriteRead(NULL, NULL, cycles, SCAN_OUT);
}
/**
* CH347_Scan - Switch to SHIFT-DR or SHIFT-IR status for scanning
* @param cmd Upper layer transfer command parameters
*
* @return Success returns ERROR_OK
*/
static int CH347_Scan(struct scan_command *cmd)
{
int scan_bits;
uint8_t *buf = NULL;
enum scan_type type;
int ret = ERROR_OK;
static const char *const type2str[] = {
"", "SCAN_IN", "SCAN_OUT", "SCAN_IO"
};
char *log_buf = NULL;
type = jtag_scan_type(cmd);
scan_bits = jtag_build_buffer(cmd, &buf);
if (cmd->ir_scan)
CH347_MoveState(TAP_IRSHIFT, 0);
else
CH347_MoveState(TAP_DRSHIFT, 0);
log_buf = HexToString(buf, DIV_ROUND_UP(scan_bits, 8));
LOG_DEBUG_IO("Scan");
LOG_DEBUG_IO("%s(scan=%s, type=%s, bits=%d, buf=[%s], end_state=%d)",
__func__,
cmd->ir_scan ? "IRSCAN" : "DRSCAN",
type2str[type],
scan_bits, log_buf, cmd->end_state);
free(log_buf);
CH347_WriteRead(cmd, buf, scan_bits, type);
free(buf);
CH347_MoveState(cmd->end_state, 1);
return ret;
}
static void CH347_Sleep(int us)
{
LOG_DEBUG_IO("%s(us=%d)", __func__, us);
jtag_sleep(us);
}
static int ch347_execute_queue(void)
{