From: Greg Davill Date: Sun, 19 Apr 2020 03:53:21 +0000 (+0930) Subject: cleanup warnings and errors X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=6f3c631c5d16812a79903f0d78a8886bc29e0082;p=ecpprog.git cleanup warnings and errors --- diff --git a/ecpprog.c b/ecpprog.c index c7bf38e..e058a38 100644 --- a/ecpprog.c +++ b/ecpprog.c @@ -42,7 +42,6 @@ #include /* _O_BINARY */ #endif -#include "mpsse.h" #include "jtag.h" #include "lattice_cmds.h" @@ -93,6 +92,59 @@ enum flash_cmd { FC_RESET = 0x99, /* Reset Device */ }; + +// --------------------------------------------------------- +// JTAG -> SPI functions +// --------------------------------------------------------- + +/* + * JTAG performrs all shifts LSB first, our FLSAH is expeting bytes MSB first, + * There are a few ways to fix this, for now we just bit-reverse all the input data to the JTAG core + */ +uint8_t bit_reverse(uint8_t in){ + + uint8_t out = (in & 0x01) ? 0x80 : 0x00; + out |= (in & 0x02) ? 0x40 : 0x00; + out |= (in & 0x04) ? 0x20 : 0x00; + out |= (in & 0x08) ? 0x10 : 0x00; + out |= (in & 0x10) ? 0x08 : 0x00; + out |= (in & 0x20) ? 0x04 : 0x00; + out |= (in & 0x40) ? 0x02 : 0x00; + out |= (in & 0x80) ? 0x01 : 0x00; + + return out; +} + +void xfer_spi(uint8_t* data, uint32_t len){ + /* Reverse bit order of all bytes */ + for(int i = 0; i < len; i++){ + data[i] = bit_reverse(data[i]); + } + + /* 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); +} + // --------------------------------------------------------- // FLASH function implementations // --------------------------------------------------------- @@ -142,18 +194,6 @@ static void flash_reset() xfer_spi(data, 8); } -static void flash_power_up() -{ - uint8_t data_rpd[1] = { FC_RPD }; - xfer_spi(data_rpd, 1); -} - -static void flash_power_down() -{ - uint8_t data[1] = { FC_PD }; - xfer_spi(data, 1); -} - static uint8_t flash_read_status() { uint8_t data[2] = { FC_RSR1 }; @@ -353,58 +393,6 @@ static void flash_disable_protection() } -// --------------------------------------------------------- -// JTAG -> SPI functions -// --------------------------------------------------------- - -/* - * JTAG performrs all shifts LSB first, our FLSAH is expeting bytes MSB first, - * There are a few ways to fix this, for now we just bit-reverse all the input data to the JTAG core - */ -uint8_t bit_reverse(uint8_t in){ - - uint8_t out = (in & 0x01) ? 0x80 : 0x00; - out |= (in & 0x02) ? 0x40 : 0x00; - out |= (in & 0x04) ? 0x20 : 0x00; - out |= (in & 0x08) ? 0x10 : 0x00; - out |= (in & 0x10) ? 0x08 : 0x00; - out |= (in & 0x20) ? 0x04 : 0x00; - out |= (in & 0x40) ? 0x02 : 0x00; - out |= (in & 0x80) ? 0x01 : 0x00; - - return out; -} - -void xfer_spi(uint8_t* data, uint32_t len){ - /* Reverse bit order of all bytes */ - for(int i = 0; i < len; i++){ - data[i] = bit_reverse(data[i]); - } - - /* 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); -} - // --------------------------------------------------------- // ECP5 specific JTAG functions // --------------------------------------------------------- @@ -895,10 +883,7 @@ int main(int argc, char **argv) // --------------------------------------------------------- fprintf(stderr, "init.."); - mpsse_init(ifnum, devstr, slow_clock); - - fprintf(stderr, "jtag..\n"); - mpsse_jtag_init(); + jtag_init(ifnum, devstr, slow_clock); fprintf(stderr, "idcode..\n"); read_idcode(); @@ -973,7 +958,6 @@ int main(int argc, char **argv) flash_reset(); - flash_power_up(); flash_read_id(); @@ -1070,7 +1054,7 @@ int main(int argc, char **argv) flash_read(rw_offset + addr, buffer_flash, rc); if (memcmp(buffer_file, buffer_flash, rc)) { fprintf(stderr, "Found difference between flash and file!\n"); - mpsse_error(3); + jtag_error(3); } } @@ -1086,6 +1070,6 @@ int main(int argc, char **argv) // --------------------------------------------------------- fprintf(stderr, "Bye.\n"); - mpsse_close(); + jtag_deinit(); return 0; } diff --git a/jtag.h b/jtag.h index a752f66..51caa9d 100644 --- a/jtag.h +++ b/jtag.h @@ -34,7 +34,7 @@ typedef enum e_TAPState /** * Performs the start-of-day tasks necessary to talk JTAG to our FPGA. */ -void jtag_init(void); +void jtag_init(int ifnum, const char *devstr, bool slow_clock); /** @@ -58,6 +58,7 @@ void jtag_tap_shift( uint32_t data_bits, bool must_end); +void jtag_error(int status); void jtag_wait_time(uint32_t microseconds); diff --git a/jtag_tap.c b/jtag_tap.c index 3402c5a..44781f5 100644 --- a/jtag_tap.c +++ b/jtag_tap.c @@ -118,22 +118,21 @@ void jtag_set_current_state(uint8_t state) current_state = state; } - -/** - * Hook for any per-platform initialization that needs to occur. - */ -__attribute__((weak)) void jtag_platform_init(void) -{ - +void jtag_error(int status){ + mpsse_error(status); } +void jtag_deinit(){ + mpsse_close(); +} /** * Performs any start-of-day tasks necessary to talk JTAG to our FPGA. */ -void jtag_init(void) +void jtag_init(int ifnum, const char *devstr, bool slow_clock) { - jtag_platform_init(); + mpsse_init(ifnum, devstr, slow_clock); + jtag_set_current_state(STATE_TEST_LOGIC_RESET); jtag_go_to_state(STATE_TEST_LOGIC_RESET); } @@ -144,9 +143,8 @@ uint16_t rx_cnt; extern struct ftdi_context mpsse_ftdic; -static inline uint8_t jtag_pulse_clock_and_read_tdo(bool tms, bool tdi) +static inline void jtag_pulse_clock_and_read_tdo(bool tms, bool tdi) { - uint8_t ret; *ptr++ = MC_DATA_TMS | MC_DATA_IN | MC_DATA_LSB | MC_DATA_BITS; *ptr++ = 0; *ptr++ = (tdi ? 0x80 : 0) | (tms ? 0x01 : 0); @@ -168,7 +166,6 @@ static void _jtag_tap_shift( for (uint32_t i = 0; i < byte_count; ++i) { uint8_t byte_out = input_data[i]; - uint8_t tdo_byte = 0; for (int j = 0; j < 8 && bit_count-- > 0; ++j) { bool tms = false; if (bit_count == 0 && must_end) { @@ -237,11 +234,6 @@ void jtag_go_to_state(unsigned state) mpsse_xfer(data, 3, 0); } else { - uint8_t d = 0; - uint8_t count = 0; - - 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, diff --git a/mpsse.c b/mpsse.c index 1a942ec..5833c65 100644 --- a/mpsse.c +++ b/mpsse.c @@ -77,7 +77,7 @@ void mpsse_check_rx() void mpsse_error(int status) { - mpsse_check_rx(); + //mpsse_check_rx(); fprintf(stderr, "ABORT.\n"); if (mpsse_ftdic_open) { if (mpsse_ftdic_latency_set) @@ -147,21 +147,6 @@ void mpsse_xfer(uint8_t* data_buffer, uint16_t send_length, uint16_t receive_len } } -void mpsse_jtag_init(){ - mpsse_send_byte(MC_SETB_LOW); - mpsse_send_byte(0x08); /* Value */ - mpsse_send_byte(0x0B); /* Direction */ - - /* Reset JTAG State machine */ - jtag_init(); -} - -void mpsse_jtag_tms(uint8_t bits, uint8_t pattern){ - mpsse_send_byte(MC_DATA_TMS | MC_DATA_LSB | MC_DATA_BITS); - mpsse_send_byte(bits-1); - mpsse_send_byte(pattern); -} - void mpsse_init(int ifnum, const char *devstr, bool slow_clock) { enum ftdi_interface ftdi_ifnum = INTERFACE_A; @@ -247,6 +232,10 @@ void mpsse_init(int ifnum, const char *devstr, bool slow_clock) mpsse_send_byte(4); mpsse_send_byte(0x00); } + + mpsse_send_byte(MC_SETB_LOW); + mpsse_send_byte(0x08); /* Value */ + mpsse_send_byte(0x0B); /* Direction */ } void mpsse_close(void) diff --git a/mpsse.h b/mpsse.h index d71761c..e10601a 100644 --- a/mpsse.h +++ b/mpsse.h @@ -103,6 +103,7 @@ enum mpsse_cmd void mpsse_check_rx(void); void mpsse_error(int status); uint8_t mpsse_recv_byte(void); +void mpsse_xfer(uint8_t* data_buffer, uint16_t send_length, uint16_t receive_length); void mpsse_send_byte(uint8_t data); void mpsse_send_spi(uint8_t *data, int n); void mpsse_xfer_spi(uint8_t *data, int n);