fix issue with missing data
authorGreg Davill <greg.davill@gmail.com>
Sun, 19 Apr 2020 03:05:35 +0000 (12:35 +0930)
committerGreg Davill <greg.davill@gmail.com>
Sun, 19 Apr 2020 03:05:35 +0000 (12:35 +0930)
ecpprog.c
jtag_tap.c
mpsse.c

index 759a4abcce88a55e81e15cf446d65b3a204798a4..5483a0c93f23619aabb87b8ccbbd1b4d694603e3 100644 (file)
--- a/ecpprog.c
+++ b/ecpprog.c
@@ -934,11 +934,7 @@ int main(int argc, char **argv)
                /* Put device into SPI bypass mode */
                enter_spi_background_mode();
 
-               //usleep(20000);
                flash_reset();
-
-               //usleep(20000);
-
                flash_read_id();
        }
        else if (prog_sram)
index e45851ca99e0db041ef78d68d8d38c9a778a5519..90593feec137579e6e459c412cd239de23983665 100644 (file)
@@ -138,16 +138,6 @@ void jtag_init(void)
     jtag_go_to_state(STATE_TEST_LOGIC_RESET);
 }
 
-
-static inline void jtag_pulse_clock(void)
-{
-       //mpsse_send_byte(MC_CLK_N);
-       //mpsse_send_byte(0x00);
-    mpsse_send_byte(MC_DATA_TMS | MC_DATA_IN | MC_DATA_LSB | MC_DATA_BITS);
-       mpsse_send_byte(0);
-    mpsse_send_byte(0);
-}
-
 uint8_t data[512];
 uint8_t* ptr;
 uint8_t rx_cnt;
@@ -168,9 +158,6 @@ static inline uint8_t jtag_pulse_clock_and_read_tdo(bool tms, bool tdi)
         
     *ptr++ = data0;
        rx_cnt++;
-       //ret = mpsse_recv_byte();
-    
-       //return (ret >> 7) & 1;
 }
 
 
@@ -181,8 +168,7 @@ void jtag_tap_shift(
        bool must_end)
 {
 
-
-       mpsse_purge();
+       printf("Remain: %u \n", mpsse_ftdic.readbuffer_remaining);
 
        uint32_t bit_count = data_bits;
        uint32_t byte_count = (data_bits + 7) / 8;
@@ -194,36 +180,23 @@ void jtag_tap_shift(
                uint8_t tdo_byte = 0;
                for (int j = 0; j < 8 && bit_count-- > 0; ++j) {
             bool tms = false;
-            bool tdi = false;
                        if (bit_count == 0 && must_end) {
                 tms = true;
                                jtag_state_ack(1);
                        }
-                       if (byte_out & 1) {
-                               tdi = true;
-                       } else {
-                               tdi = false;
-                       }
+                       jtag_pulse_clock_and_read_tdo(tms, byte_out & 1);
                        byte_out >>= 1;
-                       bool tdo = jtag_pulse_clock_and_read_tdo(tms, tdi);
-                       tdo_byte |= tdo << j;
                }
        }
-       printf(" Tx: %u, Rx: %u\n", ptr-data, rx_cnt);
-       int rc = ftdi_write_data(&mpsse_ftdic, &data, ptr-data);
-       if (rc != (ptr-data)) {
-               fprintf(stderr, "Write error (single byte, rc=%d, expected %d).\n", rc, 1);
-               mpsse_error(2);
-       }
 
-
-       rc = ftdi_read_data(&mpsse_ftdic, &data, rx_cnt);
-       if (rc < 0) {
-               fprintf(stderr, "Read error.\n");
-               mpsse_error(2);
-       }
+       mpsse_xfer(data, ptr-data, rx_cnt);
+       
        for(int i = 0; i < rx_cnt/8; i++)
                output_data[i] = data[7+i*8];
+
+
+       printf("finish: %u - %u \n", rx_cnt, mpsse_ftdic.readbuffer_remaining);
+
 }
 
 void jtag_state_ack(bool tms)
@@ -235,53 +208,61 @@ void jtag_state_ack(bool tms)
        }
 }
 
-void jtag_state_step(bool tms)
-{
-       jtag_state_ack(tms);
-}
-
-
 void jtag_go_to_state(unsigned state)
 {
 
        if (state == STATE_TEST_LOGIC_RESET) {
                for (int i = 0; i < 5; ++i) {
-                       jtag_state_step(true);
+                       jtag_state_ack(true);
                }
-               mpsse_send_byte(MC_DATA_TMS | MC_DATA_LSB | MC_DATA_BITS);
-               mpsse_send_byte(5 - 1);
-               mpsse_send_byte(0b11111);
+
+               uint8_t data[3] = { 
+                       MC_DATA_TMS | MC_DATA_LSB | MC_DATA_BITS,
+                       5 - 1,
+                       0b11111
+               };
+               mpsse_xfer(data, 3, 0);
                
        } else {
                uint8_t d = 0;
                uint8_t count = 0;
-               while (jtag_current_state() != state) {
-                       d = (d >> 1) & ~0x80;
-                       if((tms_map[jtag_current_state()] >> state) & 1){
-                               d |= 0x80;
-                       }
-                       count++;
 
-                       jtag_state_step((tms_map[jtag_current_state()] >> state) & 1);
+               uint8_t* ptr = data;
+
+               while (jtag_current_state() != state) {
+                       uint8_t data[3] = {
+                               MC_DATA_TMS | MC_DATA_LSB | MC_DATA_ICN | MC_DATA_BITS,
+                               0,
+                               (tms_map[jtag_current_state()] >> state) & 1
+                       };
+
+                       jtag_state_ack((tms_map[jtag_current_state()] >> state) & 1);
+                       mpsse_xfer(data, 3, 0);
                }
-               mpsse_send_byte(MC_DATA_TMS | MC_DATA_LSB | MC_DATA_BITS);
-               mpsse_send_byte(count - 1);
-               mpsse_send_byte(d >> (8-count));
-               
        }
+
+       printf(" - Remain: %u \n", mpsse_ftdic.readbuffer_remaining);
 }
 
 void jtag_wait_time(uint32_t microseconds)
 {
        uint16_t bytes = microseconds / 8;
        uint8_t remain = microseconds % 8;
-       mpsse_send_byte( MC_CLK_N8 );
-       mpsse_send_byte(bytes & 0xFF);
-       mpsse_send_byte((bytes >> 8) & 0xFF);
+
+       uint8_t data[3] = {
+               MC_CLK_N8,
+               bytes & 0xFF,
+               (bytes >> 8) & 0xFF
+       };
+       mpsse_xfer(data, 3, 0);
 
        if(remain){
-               mpsse_send_byte( MC_CLK_N );
-               mpsse_send_byte( remain );
+               data[0] = MC_CLK_N;
+               data[1] = remain;
+               mpsse_xfer(data, 2, 0);
        }
+
+
+       printf(" -- Remain: %u \n", mpsse_ftdic.readbuffer_remaining);
 }
 
diff --git a/mpsse.c b/mpsse.c
index 43e91b679018142f3e063a593d5baf42dce9b9cc..203bac3b1cadcbf8df359c01cc849a1b687f135f 100644 (file)
--- a/mpsse.c
+++ b/mpsse.c
@@ -121,6 +121,32 @@ void mpsse_purge(void){
        }
 }
 
+void mpsse_xfer(uint8_t* data_buffer, uint16_t send_length, uint16_t receive_length)
+{
+       if(send_length){
+               int rc = ftdi_write_data(&mpsse_ftdic, data_buffer, send_length);
+               if (rc != send_length) {
+                       fprintf(stderr, "Write error (rc=%d, expected %d)[%s]\n", rc, 1, ftdi_get_error_string(&mpsse_ftdic));
+                       mpsse_error(2);
+               }
+       }
+
+       if(receive_length){
+               /* Calls to ftdi_read_data may return with less data than requested if it wasn't ready. 
+                * We stay in this while loop to collect all the data that we expect. */
+               uint16_t rx_len = 0;
+               while(rx_len != receive_length){
+                       int rc = ftdi_read_data(&mpsse_ftdic, data_buffer + rx_len, receive_length - rx_len);
+                       if (rc < 0) {
+                               fprintf(stderr, "Read error (rc=%d)[%s]\n", rc, ftdi_get_error_string(&mpsse_ftdic));
+                               mpsse_error(2);
+                       }else{
+                               rx_len += rc;
+                       }
+               }
+       }
+}
+
 void mpsse_send_spi(uint8_t *data, int n)
 {
        if (n < 1)
@@ -299,9 +325,6 @@ void mpsse_init(int ifnum, const char *devstr, bool slow_clock)
                mpsse_error(2);
        }
 
-       // enable clock divide by 5
-       //mpsse_send_byte(MC_TCK_D5);
-
        if (slow_clock) {
                // set 50 kHz clock
                mpsse_send_byte(MC_SET_CLK_DIV);
@@ -313,7 +336,6 @@ void mpsse_init(int ifnum, const char *devstr, bool slow_clock)
                mpsse_send_byte(5);
                mpsse_send_byte(0x00);
        }
-
 }
 
 void mpsse_close(void)