@@ -172,7 +172,7 @@ typedef struct _CH347_SWD_CONTEXT {
172
172
uint8_t * ch347_cmd_buf ;
173
173
} CH347_SWD_CONTEXT ;
174
174
static CH347_SWD_CONTEXT ch347_swd_context ;
175
- static bool swd_mode ;
175
+ static bool swd_mode = false ;
176
176
#pragma pack()
177
177
178
178
#ifdef _WIN32
@@ -234,8 +234,8 @@ bool ugOpen;
234
234
unsigned long ugIndex ;
235
235
struct libusb_device_handle * ch347_handle ;
236
236
237
- static const uint16_t ch347_vids [] = {0x1a86 , 0x1a86 };
238
- static const uint16_t ch347_pids [] = {0x55dd , 0x55de };
237
+ static const uint16_t ch347_vids [] = {0x1a86 , 0x1a86 , 0x1a86 };
238
+ static const uint16_t ch347_pids [] = {0x55dd , 0x55de , 0x55e7 };
239
239
240
240
static uint32_t CH347OpenDevice (uint64_t iIndex )
241
241
{
@@ -447,17 +447,16 @@ static void CH347_Read_Scan(UCHAR *pBuffer, uint32_t length)
447
447
} else if (read_buf [index ] == CH347_CMD_JTAG_BIT_OP_RD ) {
448
448
dataLen = read_buf [++ index ] & 0xFF ;
449
449
dataLen += (read_buf [++ index ] & 0xFF ) << 8 ;
450
-
451
450
for (i = 0 ; i < dataLen ; i ++ ) {
452
- if (read_buf [index + 1 + i ] & 1 )
451
+ if (read_buf [index + 1 + i ] == 0x01 )
453
452
* (pBuffer + read_buf_index ) |= (1 << i );
454
453
else
455
454
* (pBuffer + read_buf_index ) &= ~(1 << i );
456
455
}
457
456
read_buf_index += 1 ;
458
457
index += dataLen + 1 ;
459
458
} else {
460
- LOG_ERROR ("readbuf read_commend error" );
459
+ // LOG_ERROR("readbuf read_commend error");
461
460
* (pBuffer + read_buf_index ) = read_buf [index ];
462
461
read_buf_index ++ ;
463
462
index ++ ;
@@ -649,58 +648,31 @@ static void CH347_TMS(struct tms_command *cmd)
649
648
static int ch347_reset (int trst , int srst )
650
649
{
651
650
LOG_DEBUG_IO ("reset trst: %i srst %i" , trst , srst );
652
- #if 1
653
651
unsigned char BitBang [512 ] = "" , BII , i ;
654
652
unsigned long TxLen ;
655
-
656
- BII = CH347_CMD_HEADER ;
657
- for (i = 0 ; i < 7 ; i ++ ) {
653
+ if (!swd_mode ){
654
+ BII = CH347_CMD_HEADER ;
655
+ for (i = 0 ; i < 7 ; i ++ ) {
656
+ BitBang [BII ++ ] = TMS_H | TDI_L | TCK_L ;
657
+ BitBang [BII ++ ] = TMS_H | TDI_L | TCK_H ;
658
+ }
658
659
BitBang [BII ++ ] = TMS_H | TDI_L | TCK_L ;
659
- BitBang [BII ++ ] = TMS_H | TDI_L | TCK_H ;
660
- }
661
- BitBang [BII ++ ] = TMS_H | TDI_L | TCK_L ;
662
-
663
- ch347 .TCK = TCK_L ;
664
- ch347 .TDI = TDI_L ;
665
- ch347 .TMS = 0 ;
666
-
667
- BitBang [0 ] = CH347_CMD_JTAG_BIT_OP ;
668
- BitBang [1 ] = BII - CH347_CMD_HEADER ;
669
- BitBang [2 ] = 0 ;
670
-
671
- TxLen = BII ;
672
-
673
- if (!CH347_Write (BitBang , & TxLen ) && (TxLen != BII )) {
674
- LOG_ERROR ("JTAG_Init send usb data failure." );
675
- return false;
676
- }
677
- #else
678
- if (!swd_mode && trst == 0 ) {
679
-
680
- unsigned long int BI = 0 ;
681
-
682
- CH347_In_Buffer (CH347_CMD_JTAG_BIT_OP );
683
- CH347_In_Buffer (0x01 );
684
- CH347_In_Buffer (0 );
685
660
686
- ch347 .TRST = 0 ;
687
- CH347_IdleClock (BI );
688
-
689
- CH347_Flush_Buffer ();
690
-
691
- Sleep (50 );
661
+ ch347 .TCK = TCK_L ;
662
+ ch347 .TDI = TDI_L ;
663
+ ch347 .TMS = 0 ;
692
664
693
- CH347_In_Buffer ( CH347_CMD_JTAG_BIT_OP ) ;
694
- CH347_In_Buffer ( 0x01 ) ;
695
- CH347_In_Buffer ( 0 ) ;
665
+ BitBang [ 0 ] = CH347_CMD_JTAG_BIT_OP ;
666
+ BitBang [ 1 ] = BII - CH347_CMD_HEADER ;
667
+ BitBang [ 2 ] = 0 ;
696
668
697
- ch347 .TRST = 1 ;
698
- CH347_IdleClock (BI );
669
+ TxLen = BII ;
699
670
700
- CH347_Flush_Buffer ();
701
- return ERROR_OK ;
671
+ if (!CH347_Write (BitBang , & TxLen ) && (TxLen != BII )) {
672
+ LOG_ERROR ("JTAG_Init send usb data failure." );
673
+ return false;
674
+ }
702
675
}
703
- #endif
704
676
return ERROR_OK ;
705
677
}
706
678
@@ -781,7 +753,7 @@ static void CH347_WriteRead(struct scan_command *cmd, uint8_t *bits,
781
753
unsigned long BI = 0 , DI , DII , PktDataLen , DLen = 0 , tempIndex ,
782
754
totalReadLength = 0 , tempLength = 0 ;
783
755
if (ch347 .pack_size == LARGER_PACK ) {
784
- if ((ch347 .read_count >= (USBC_PACKET_USBHS_SINGLE * 1 )))
756
+ if ((ch347 .read_count >= (510 * 1 )))
785
757
CH347_Flush_Buffer ();
786
758
} else {
787
759
CH347_Flush_Buffer ();
@@ -890,14 +862,15 @@ static void CH347_WriteRead(struct scan_command *cmd, uint8_t *bits,
890
862
CH347_Flush_Buffer ();
891
863
CH347_Read_Scan (readData , readLen );
892
864
}
893
-
894
865
for (i = 0 ; i < cmd -> num_fields ; i ++ ) {
895
866
/* if neither in_value nor in_handler
896
867
* are specified we don't have to examine this field
897
868
*/
898
869
LOG_DEBUG ("fields[%i].in_value[%i], offset: %d" ,
899
870
i , cmd -> fields [i ].num_bits , offset );
900
871
num_bits = cmd -> fields [i ].num_bits ;
872
+ if (ch347 .pack_size == LARGER_PACK )
873
+ bit_count += num_bits ;
901
874
if (cmd -> fields [i ].in_value ) {
902
875
if (ch347 .pack_size == LARGER_PACK ) {
903
876
bit_copy_queued (
@@ -906,7 +879,10 @@ static void CH347_WriteRead(struct scan_command *cmd, uint8_t *bits,
906
879
0 ,
907
880
& ch347 .read_buffer [ch347 .read_idx ],
908
881
offset , num_bits );
882
+ if (num_bits > 7 )
883
+ ch347 .read_idx += DIV_ROUND_UP (bit_count , 8 );
909
884
} else {
885
+ num_bits = cmd -> fields [i ].num_bits ;
910
886
uint8_t * captured = buf_set_buf (
911
887
readData , bit_count ,
912
888
malloc (DIV_ROUND_UP (num_bits ,
@@ -930,11 +906,10 @@ static void CH347_WriteRead(struct scan_command *cmd, uint8_t *bits,
930
906
}else {
931
907
LOG_DEBUG_IO ("cmd->fields with no data" );
932
908
}
933
- bit_count += cmd -> fields [i ].num_bits ;
934
909
if (ch347 .pack_size == LARGER_PACK ) {
935
- if (num_bits > 7 )
936
- ch347 .read_idx += DIV_ROUND_UP (bit_count , 8 );
937
910
offset += num_bits ;
911
+ }else {
912
+ bit_count += cmd -> fields [i ].num_bits ;
938
913
}
939
914
}
940
915
}
@@ -1207,7 +1182,6 @@ static bool CH347Jtag_INIT(uint64_t iIndex, uint8_t iClockRate)
1207
1182
else
1208
1183
return Check_Speed (iIndex , iClockRate - 2 );
1209
1184
}
1210
-
1211
1185
return Check_Speed (iIndex , iClockRate );
1212
1186
}
1213
1187
@@ -1831,4 +1805,4 @@ struct adapter_driver ch347_adapter_driver = {
1831
1805
1832
1806
.jtag_ops = & ch347_interface ,
1833
1807
.swd_ops = & ch347_swd ,
1834
- };
1808
+ };
0 commit comments