update jtag_shift
authorGreg Davill <greg.davill@gmail.com>
Sun, 19 Apr 2020 03:38:15 +0000 (13:08 +0930)
committerGreg Davill <greg.davill@gmail.com>
Sun, 19 Apr 2020 03:38:15 +0000 (13:08 +0930)
ecpprog.c
jtag_tap.c
mpsse.c

index 7c572094833613562f44fb81a84f84a629d4fc3f..72a9ce88721a30ed2d4fe1bf018c7f35f47fbdd7 100644 (file)
--- 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();
        }
index 3775ca0b50f1bc307457d1651c901b6951c148c1..3402c5abc2ae7ded2a5eb04dc1882ac0fcfeac8c 100644 (file)
@@ -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 269d0dad5221de78afc25facb3f47b2100703434..1a942ec4aa8b398692be114d3acfa4e03f69fbde 100644 (file)
--- 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);
        }
 }