From: Greg Davill Date: Sun, 19 Apr 2020 03:05:35 +0000 (+0930) Subject: fix issue with missing data X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=f12b3e58e2cd3469c047735543917f03ad583b76;p=ecpprog.git fix issue with missing data --- diff --git a/ecpprog.c b/ecpprog.c index 759a4ab..5483a0c 100644 --- 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) diff --git a/jtag_tap.c b/jtag_tap.c index e45851c..90593fe 100644 --- a/jtag_tap.c +++ b/jtag_tap.c @@ -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 43e91b6..203bac3 100644 --- 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)