@@ -694,7 +694,8 @@ int drv_FTDI_DeInit(jtag_core * jc)
694
694
int drv_FTDI_TDOTDI_xfer (jtag_core * jc , unsigned char * str_out , unsigned char * str_in , int size )
695
695
{
696
696
int wr_bit_index ,rd_bit_index ;
697
- int j ,l ,payloadsize ;
697
+ int i ,payloadsize ;
698
+ unsigned char bitscnt ;
698
699
int rounded_size ;
699
700
DWORD nbRead ,nbtosend ;
700
701
FT_STATUS status ;
@@ -708,7 +709,7 @@ int drv_FTDI_TDOTDI_xfer(jtag_core * jc, unsigned char * str_out, unsigned char
708
709
709
710
if (size )
710
711
{
711
- // Set the first TMS/DOUT
712
+ // Set the first TMS/DOUT bit
712
713
if ( str_out [wr_bit_index ] & JTAG_STR_TMS )
713
714
{
714
715
if ( str_in )
@@ -721,13 +722,10 @@ int drv_FTDI_TDOTDI_xfer(jtag_core * jc, unsigned char * str_out, unsigned char
721
722
ftdi_out_buf [nbtosend ++ ] = opcode ;
722
723
ftdi_out_buf [nbtosend ++ ] = 0x00 ; // Size field : 1 Bit
723
724
724
- data = 0x00 ;
725
+ data = 0x7F ; // TMS state set
725
726
726
727
if (str_out [wr_bit_index ] & JTAG_STR_DOUT )
727
- data = 0x80 ; // Bit 7: TDI/DO pin state
728
-
729
- if (str_out [wr_bit_index ] & JTAG_STR_TMS )
730
- data |= 0x3F ; // TMS state
728
+ data |= 0x80 ; // Bit 7: TDI/DO pin state
731
729
732
730
wr_bit_index ++ ;
733
731
@@ -746,7 +744,8 @@ int drv_FTDI_TDOTDI_xfer(jtag_core * jc, unsigned char * str_out, unsigned char
746
744
747
745
status = pFT_Read (ftdih , & ftdi_in_buf , nbRead , & nbRead );
748
746
749
- if ( ftdi_in_buf [0 ] & 0x01 )
747
+ // bits are shifted in from MSB to LSB ...
748
+ if ( ftdi_in_buf [0 ] & 0x80 )
750
749
{
751
750
str_in [rd_bit_index ++ ] = JTAG_STR_DOUT ;
752
751
}
@@ -778,18 +777,18 @@ int drv_FTDI_TDOTDI_xfer(jtag_core * jc, unsigned char * str_out, unsigned char
778
777
779
778
ftdi_out_buf [nbtosend ] = 0x00 ;
780
779
781
- j = 0 ;
780
+ i = 0 ;
782
781
payloadsize = 0 ;
783
782
while (payloadsize < rounded_size )
784
783
{
785
784
if (str_out [wr_bit_index ] & JTAG_STR_DOUT )
786
785
{
787
- ftdi_out_buf [nbtosend ] |= (0x01 << (j & 0x7 ));
786
+ ftdi_out_buf [nbtosend ] |= (0x01 << (i & 0x7 ));
788
787
}
789
788
790
- j ++ ;
789
+ i ++ ;
791
790
792
- if (!(j & 0x7 ))
791
+ if (!(i & 0x7 ))
793
792
{
794
793
nbtosend ++ ;
795
794
ftdi_out_buf [nbtosend ] = 0x00 ;
@@ -811,9 +810,9 @@ int drv_FTDI_TDOTDI_xfer(jtag_core * jc, unsigned char * str_out, unsigned char
811
810
812
811
status = pFT_Read (ftdih , & ftdi_in_buf , nbRead , & nbRead );
813
812
814
- for (l = 0 ; l < rounded_size ; l ++ )
813
+ for (i = 0 ; i < rounded_size ; i ++ )
815
814
{
816
- if (ftdi_in_buf [l >> 3 ] & (0x01 << (l & 7 )))
815
+ if (ftdi_in_buf [i >> 3 ] & (0x01 << (i & 7 )))
817
816
{
818
817
str_in [rd_bit_index ++ ] = JTAG_STR_DOUT ;
819
818
}
@@ -825,35 +824,34 @@ int drv_FTDI_TDOTDI_xfer(jtag_core * jc, unsigned char * str_out, unsigned char
825
824
}
826
825
}
827
826
828
- if ( wr_bit_index < size )
827
+ // Send the remaining bits...
828
+ while ( wr_bit_index < size )
829
829
{
830
- // Send the remaining bits...
831
-
832
830
if ( str_in )
833
831
opcode = ( OP_WR_TDI | OP_LSB_FIRST | OP_BIT_MODE | OP_FEDGE_WR | OP_RD_TDO ); //bit mode with TDO read back
834
832
else
835
833
opcode = ( OP_WR_TDI | OP_LSB_FIRST | OP_BIT_MODE | OP_FEDGE_WR ); //bit mode
836
834
837
835
nbtosend = 0 ;
838
836
837
+ bitscnt = (size - wr_bit_index );
838
+ if ( bitscnt > 8 )
839
+ {
840
+ bitscnt = 8 ;
841
+ }
842
+
839
843
ftdi_out_buf [nbtosend ++ ] = opcode ;
840
- ftdi_out_buf [nbtosend ++ ] = ((( size - wr_bit_index ) - 1 ) & 0x7 ); // Size field
841
- ftdi_out_buf [nbtosend ] = 0x00 ; // Data field
844
+ ftdi_out_buf [nbtosend ++ ] = (bitscnt - 1 ) & 7 ; // Size field
845
+ ftdi_out_buf [nbtosend ] = 0x00 ;
842
846
843
- j = 0 ;
844
- payloadsize = 0 ;
845
- while (wr_bit_index < size ) // Should left less than 8 bits.
847
+ i = 0 ;
848
+ while ( i < bitscnt )
846
849
{
847
850
if (str_out [wr_bit_index ++ ] & JTAG_STR_DOUT )
848
- {
849
- ftdi_out_buf [nbtosend ] |= (0x01 << (j & 0x7 ));
850
- }
851
+ ftdi_out_buf [nbtosend ] |= 0x01 << i ; // Data field
851
852
852
- j ++ ;
853
-
854
- payloadsize ++ ;
853
+ i ++ ;
855
854
}
856
-
857
855
nbtosend ++ ;
858
856
859
857
status = pFT_Write (ftdih , ftdi_out_buf , nbtosend , & nbtosend );
@@ -868,16 +866,19 @@ int drv_FTDI_TDOTDI_xfer(jtag_core * jc, unsigned char * str_out, unsigned char
868
866
869
867
status = pFT_Read (ftdih , & ftdi_in_buf , nbRead , & nbRead );
870
868
871
- for (l = 0 ; l < payloadsize ; l ++ )
869
+ // bits are shifted in from MSB to LSB ...
870
+ i = 0 ;
871
+ while ( i < bitscnt )
872
872
{
873
- if (ftdi_in_buf [l >> 3 ] & (0x01 << (l & 7 )) )
873
+ if ( ftdi_in_buf [0 ] & (0x01 << ( ( 8 - bitscnt ) + i ) ) )
874
874
{
875
875
str_in [rd_bit_index ++ ] = JTAG_STR_DOUT ;
876
876
}
877
877
else
878
878
{
879
879
str_in [rd_bit_index ++ ] = 0x00 ;
880
880
}
881
+ i ++ ;
881
882
}
882
883
}
883
884
}
@@ -889,61 +890,45 @@ int drv_FTDI_TDOTDI_xfer(jtag_core * jc, unsigned char * str_out, unsigned char
889
890
int drv_FTDI_TMS_xfer (jtag_core * jc , unsigned char * str_out , int size )
890
891
{
891
892
int i ;
893
+ int wr_bit_index ;
892
894
DWORD nbtosend ;
893
895
FT_STATUS status ;
894
- unsigned char databyte ;
896
+ unsigned char bitscnt ;
897
+ unsigned char opcode ;
895
898
896
899
status = FT_OK ;
897
900
898
901
memset (ftdi_out_buf , 0 , sizeof (ftdi_out_buf ));
899
902
memset (ftdi_in_buf , 0 , sizeof (ftdi_in_buf ));
900
903
901
- if (size )
904
+ wr_bit_index = 0 ;
905
+ while ( wr_bit_index < size )
902
906
{
903
- i = 0 ;
904
- databyte = 0x00 ;
905
- while (size )
906
- {
907
- if (str_out [i ] & JTAG_STR_TMS )
908
- databyte |= (0x01 << (i % 6 ));
909
-
910
- i ++ ;
911
- size -- ;
912
- if (!(i % 6 ))
913
- {
914
- nbtosend = 0 ;
915
- ftdi_out_buf [nbtosend ++ ] = (OP_WR_TMS | OP_LSB_FIRST | OP_BIT_MODE | OP_FEDGE_WR ); // cmd
916
- ftdi_out_buf [nbtosend ++ ] = 0x06 - 1 ; // 6 Bit
917
-
918
- if ((databyte & 0x20 ) && size )
919
- ftdi_out_buf [nbtosend ++ ] = databyte | 0x40 ;
920
- else
921
- ftdi_out_buf [nbtosend ++ ] = databyte ;
907
+ opcode = ( OP_WR_TMS | OP_LSB_FIRST | OP_BIT_MODE | OP_FEDGE_WR );
922
908
923
- status = pFT_Write ( ftdih , ftdi_out_buf , nbtosend , & nbtosend ) ;
909
+ nbtosend = 0 ;
924
910
925
- databyte = 0x00 ;
926
- }
927
- }
928
-
929
- if ((i % 6 ))
911
+ bitscnt = (size - wr_bit_index );
912
+ if ( bitscnt > 7 )
930
913
{
931
- nbtosend = 0 ;
932
- ftdi_out_buf [nbtosend ++ ] = (OP_WR_TMS | OP_LSB_FIRST | OP_BIT_MODE | OP_FEDGE_WR );
933
-
934
- ftdi_out_buf [nbtosend ++ ] = (i % 6 ) - 1 ; // 1 Bit
935
-
936
- ftdi_out_buf [nbtosend ++ ] = databyte ;
914
+ bitscnt = 7 ;
915
+ }
937
916
938
- status = pFT_Write (ftdih , ftdi_out_buf , nbtosend , & nbtosend );
917
+ ftdi_out_buf [nbtosend ++ ] = opcode ;
918
+ ftdi_out_buf [nbtosend ++ ] = (bitscnt - 1 ); // Size field
919
+ ftdi_out_buf [nbtosend ] = 0x00 ;
939
920
940
- databyte = 0x00 ;
941
- }
942
- else
921
+ i = 0 ;
922
+ while ( i < bitscnt )
943
923
{
924
+ if (str_out [wr_bit_index ++ ] & JTAG_STR_TMS )
925
+ ftdi_out_buf [nbtosend ] |= 0x01 << i ; // Data field
944
926
927
+ i ++ ;
945
928
}
929
+ nbtosend ++ ;
946
930
931
+ status = pFT_Write (ftdih , ftdi_out_buf , nbtosend , & nbtosend );
947
932
}
948
933
949
934
if (status != FT_OK )
0 commit comments