Skip to content

Commit 1d2fb55

Browse files
committed
FTDI probe : Last bits jtag chain shift-in issue fixed.
1 parent 89159e2 commit 1d2fb55

File tree

1 file changed

+53
-68
lines changed

1 file changed

+53
-68
lines changed

lib_jtag_core/src/drivers/ftdi_jtag/ftdi_jtag_drv.c

Lines changed: 53 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,8 @@ int drv_FTDI_DeInit(jtag_core * jc)
694694
int drv_FTDI_TDOTDI_xfer(jtag_core * jc, unsigned char * str_out, unsigned char * str_in, int size)
695695
{
696696
int wr_bit_index,rd_bit_index;
697-
int j,l,payloadsize;
697+
int i,payloadsize;
698+
unsigned char bitscnt;
698699
int rounded_size;
699700
DWORD nbRead,nbtosend;
700701
FT_STATUS status;
@@ -708,7 +709,7 @@ int drv_FTDI_TDOTDI_xfer(jtag_core * jc, unsigned char * str_out, unsigned char
708709

709710
if (size)
710711
{
711-
// Set the first TMS/DOUT
712+
// Set the first TMS/DOUT bit
712713
if ( str_out[wr_bit_index] & JTAG_STR_TMS )
713714
{
714715
if( str_in )
@@ -721,13 +722,10 @@ int drv_FTDI_TDOTDI_xfer(jtag_core * jc, unsigned char * str_out, unsigned char
721722
ftdi_out_buf[nbtosend++] = opcode;
722723
ftdi_out_buf[nbtosend++] = 0x00; // Size field : 1 Bit
723724

724-
data = 0x00;
725+
data = 0x7F; // TMS state set
725726

726727
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
731729

732730
wr_bit_index++;
733731

@@ -746,7 +744,8 @@ int drv_FTDI_TDOTDI_xfer(jtag_core * jc, unsigned char * str_out, unsigned char
746744

747745
status = pFT_Read(ftdih, &ftdi_in_buf, nbRead, &nbRead);
748746

749-
if ( ftdi_in_buf[0] & 0x01 )
747+
// bits are shifted in from MSB to LSB ...
748+
if ( ftdi_in_buf[0] & 0x80 )
750749
{
751750
str_in[rd_bit_index++] = JTAG_STR_DOUT;
752751
}
@@ -778,18 +777,18 @@ int drv_FTDI_TDOTDI_xfer(jtag_core * jc, unsigned char * str_out, unsigned char
778777

779778
ftdi_out_buf[nbtosend] = 0x00;
780779

781-
j = 0;
780+
i = 0;
782781
payloadsize = 0;
783782
while (payloadsize < rounded_size)
784783
{
785784
if (str_out[wr_bit_index] & JTAG_STR_DOUT)
786785
{
787-
ftdi_out_buf[nbtosend] |= (0x01 << (j & 0x7));
786+
ftdi_out_buf[nbtosend] |= (0x01 << (i & 0x7));
788787
}
789788

790-
j++;
789+
i++;
791790

792-
if (!(j & 0x7))
791+
if (!(i & 0x7))
793792
{
794793
nbtosend++;
795794
ftdi_out_buf[nbtosend] = 0x00;
@@ -811,9 +810,9 @@ int drv_FTDI_TDOTDI_xfer(jtag_core * jc, unsigned char * str_out, unsigned char
811810

812811
status = pFT_Read(ftdih, &ftdi_in_buf, nbRead, &nbRead);
813812

814-
for (l = 0; l < rounded_size; l++)
813+
for (i = 0; i < rounded_size; i++)
815814
{
816-
if (ftdi_in_buf[l >> 3] & (0x01 << (l & 7)))
815+
if (ftdi_in_buf[i >> 3] & (0x01 << (i & 7)))
817816
{
818817
str_in[rd_bit_index++] = JTAG_STR_DOUT;
819818
}
@@ -825,35 +824,34 @@ int drv_FTDI_TDOTDI_xfer(jtag_core * jc, unsigned char * str_out, unsigned char
825824
}
826825
}
827826

828-
if( wr_bit_index < size )
827+
// Send the remaining bits...
828+
while( wr_bit_index < size )
829829
{
830-
// Send the remaining bits...
831-
832830
if( str_in )
833831
opcode = ( OP_WR_TDI | OP_LSB_FIRST | OP_BIT_MODE | OP_FEDGE_WR | OP_RD_TDO ); //bit mode with TDO read back
834832
else
835833
opcode = ( OP_WR_TDI | OP_LSB_FIRST | OP_BIT_MODE | OP_FEDGE_WR ); //bit mode
836834

837835
nbtosend = 0;
838836

837+
bitscnt = (size - wr_bit_index);
838+
if( bitscnt > 8 )
839+
{
840+
bitscnt = 8;
841+
}
842+
839843
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;
842846

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 )
846849
{
847850
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
851852

852-
j++;
853-
854-
payloadsize++;
853+
i++;
855854
}
856-
857855
nbtosend++;
858856

859857
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
868866

869867
status = pFT_Read(ftdih, &ftdi_in_buf, nbRead, &nbRead);
870868

871-
for (l = 0; l < payloadsize; l++)
869+
// bits are shifted in from MSB to LSB ...
870+
i = 0;
871+
while( i < bitscnt )
872872
{
873-
if (ftdi_in_buf[l >> 3] & (0x01 << (l & 7)))
873+
if ( ftdi_in_buf[0] & (0x01 << ( (8 - bitscnt) + i ) ) )
874874
{
875875
str_in[rd_bit_index++] = JTAG_STR_DOUT;
876876
}
877877
else
878878
{
879879
str_in[rd_bit_index++] = 0x00;
880880
}
881+
i++;
881882
}
882883
}
883884
}
@@ -889,61 +890,45 @@ int drv_FTDI_TDOTDI_xfer(jtag_core * jc, unsigned char * str_out, unsigned char
889890
int drv_FTDI_TMS_xfer(jtag_core * jc, unsigned char * str_out, int size)
890891
{
891892
int i;
893+
int wr_bit_index;
892894
DWORD nbtosend;
893895
FT_STATUS status;
894-
unsigned char databyte;
896+
unsigned char bitscnt;
897+
unsigned char opcode;
895898

896899
status = FT_OK;
897900

898901
memset(ftdi_out_buf, 0, sizeof(ftdi_out_buf));
899902
memset(ftdi_in_buf, 0, sizeof(ftdi_in_buf));
900903

901-
if (size)
904+
wr_bit_index = 0;
905+
while( wr_bit_index < size )
902906
{
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 );
922908

923-
status = pFT_Write(ftdih, ftdi_out_buf, nbtosend, &nbtosend);
909+
nbtosend = 0;
924910

925-
databyte = 0x00;
926-
}
927-
}
928-
929-
if ((i % 6))
911+
bitscnt = (size - wr_bit_index);
912+
if( bitscnt > 7 )
930913
{
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+
}
937916

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;
939920

940-
databyte = 0x00;
941-
}
942-
else
921+
i = 0;
922+
while( i < bitscnt )
943923
{
924+
if (str_out[wr_bit_index++] & JTAG_STR_TMS)
925+
ftdi_out_buf[nbtosend] |= 0x01 << i; // Data field
944926

927+
i++;
945928
}
929+
nbtosend++;
946930

931+
status = pFT_Write(ftdih, ftdi_out_buf, nbtosend, &nbtosend);
947932
}
948933

949934
if (status != FT_OK)

0 commit comments

Comments
 (0)