From: Greg Davill Date: Sun, 19 Apr 2020 03:38:15 +0000 (+0930) Subject: update jtag_shift X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=5c92bf5dbb99d273d2ed343dbf3424f642637341;p=ecpprog.git update jtag_shift --- diff --git a/ecpprog.c b/ecpprog.c index 7c57209..72a9ce8 100644 --- a/ecpprog.c +++ b/ecpprog.c @@ -305,8 +305,9 @@ static void flash_read(int addr, uint8_t *data, int n) //flash_chip_select(); //mpsse_send_spi(command, 4); + send_spi(command, 4); memset(data, 0, n); - //mpsse_xfer_spi(data, n); + xfer_spi(data, n); //flash_chip_deselect(); if (verbose) @@ -405,18 +406,33 @@ uint8_t bit_reverse(uint8_t in){ } void xfer_spi(uint8_t* data, uint32_t len){ - /* Flip bit order of all bytes */ + /* Reverse bit order of all bytes */ for(int i = 0; i < len; i++){ data[i] = bit_reverse(data[i]); } - jtag_go_to_state(STATE_SHIFT_DR); + /* Don't switch states if we're already in SHIFT-DR */ + if(jtag_current_state() != STATE_SHIFT_DR) + jtag_go_to_state(STATE_SHIFT_DR); jtag_tap_shift(data, data, len * 8, true); + /* Reverse bit order of all return bytes */ + for(int i = 0; i < len; i++){ + data[i] = bit_reverse(data[i]); + } +} + +void send_spi(uint8_t* data, uint32_t len){ + uint8_t unused[len]; + /* Flip bit order of all bytes */ for(int i = 0; i < len; i++){ data[i] = bit_reverse(data[i]); } + + jtag_go_to_state(STATE_SHIFT_DR); + /* Stay in SHIFT-DR state, this keep CS low */ + jtag_tap_shift(data, unused, len * 8, false); } // --------------------------------------------------------- @@ -920,20 +936,20 @@ int main(int argc, char **argv) fprintf(stderr, "status..\n"); read_status_register(); + + /* Reset ECP5 to release SPI interface */ + ecp_jtag_cmd(ISC_ENABLE); + ecp_jtag_cmd(ISC_ERASE); + ecp_jtag_cmd(ISC_DISABLE); + + /* Put device into SPI bypass mode */ + enter_spi_background_mode(); //usleep(20000); if (test_mode) { - /* Reset ECP5 to release SPI interface */ - ecp_jtag_cmd(ISC_ENABLE); - ecp_jtag_cmd(ISC_ERASE); - ecp_jtag_cmd(ISC_DISABLE); - - /* Put device into SPI bypass mode */ - enter_spi_background_mode(); - flash_reset(); flash_read_id(); } diff --git a/jtag_tap.c b/jtag_tap.c index 3775ca0..3402c5a 100644 --- a/jtag_tap.c +++ b/jtag_tap.c @@ -138,9 +138,9 @@ void jtag_init(void) jtag_go_to_state(STATE_TEST_LOGIC_RESET); } -uint8_t data[1024]; +uint8_t data[32*1024]; uint8_t* ptr; -uint8_t rx_cnt; +uint16_t rx_cnt; extern struct ftdi_context mpsse_ftdic; @@ -153,14 +153,14 @@ static inline uint8_t jtag_pulse_clock_and_read_tdo(bool tms, bool tdi) rx_cnt++; } - -void jtag_tap_shift( +static void _jtag_tap_shift( uint8_t *input_data, uint8_t *output_data, uint32_t data_bits, bool must_end) { + //printf("_jtag_tap_shift(0x%08x,0x%08x,%u,%s);\n",input_data, output_data, data_bits, must_end ? "true" : "false"); uint32_t bit_count = data_bits; uint32_t byte_count = (data_bits + 7) / 8; rx_cnt = 0; @@ -188,6 +188,30 @@ void jtag_tap_shift( output_data[i] = data[7+i*8]; } +#define MIN(a,b) (a < b) ? a : b + +void jtag_tap_shift( + uint8_t *input_data, + uint8_t *output_data, + uint32_t data_bits, + bool must_end) +{ + uint32_t data_bits_sent = 0; + while(data_bits_sent != data_bits){ + + uint32_t _data_bits = MIN(256, data_bits); + bool last = (data_bits_sent + _data_bits) == data_bits; + + _jtag_tap_shift( + input_data + data_bits_sent/8, + output_data + data_bits_sent/8, + _data_bits, + last & must_end + ); + data_bits_sent += _data_bits; + } +} + void jtag_state_ack(bool tms) { if (tms) { diff --git a/mpsse.c b/mpsse.c index 269d0da..1a942ec 100644 --- a/mpsse.c +++ b/mpsse.c @@ -244,7 +244,7 @@ void mpsse_init(int ifnum, const char *devstr, bool slow_clock) } else { // set 6 MHz clock mpsse_send_byte(MC_SET_CLK_DIV); - mpsse_send_byte(5); + mpsse_send_byte(4); mpsse_send_byte(0x00); } }