From 97d343712bc7e8b228e2522982a6b59ecc7ccf2b Mon Sep 17 00:00:00 2001 From: Tomasz Wasilczyk Date: Fri, 16 Apr 2021 11:40:38 -0700 Subject: [PATCH 1/2] Implement get_bits_low. Verified with FT4232H Mini Module. --- src/support.c | 10 ++++++++++ src/support.h | 1 + 2 files changed, 11 insertions(+) diff --git a/src/support.c b/src/support.c index 0f8b8a8..3bc02a3 100644 --- a/src/support.c +++ b/src/support.c @@ -228,6 +228,16 @@ int set_bits_high(struct mpsse_context *mpsse, int port) return raw_write(mpsse, (unsigned char *) &buf, sizeof(buf)); } +int get_bits_low(struct mpsse_context *mpsse, uint8_t* value) { + unsigned char buf[] = { GET_BITS_LOW }; + + int res = raw_write(mpsse, buf, sizeof(buf)); + if (res != MPSSE_OK) return res; + + if (raw_read(mpsse, value, 1) != 1) return MPSSE_FAIL; + return MPSSE_OK; +} + int get_bits_high(struct mpsse_context *mpsse, uint8_t* value) { unsigned char buf[] = { GET_BITS_HIGH }; diff --git a/src/support.h b/src/support.h index 6cdbd65..1e77747 100644 --- a/src/support.h +++ b/src/support.h @@ -12,6 +12,7 @@ unsigned char *build_block_buffer(struct mpsse_context *mpsse, uint8_t cmd, cons int set_bits_high(struct mpsse_context *mpsse, int port); int set_bits_low(struct mpsse_context *mpsse, int port); int get_bits_high(struct mpsse_context *mpsse, uint8_t* value); +int get_bits_low(struct mpsse_context *mpsse, uint8_t* value); int gpio_write(struct mpsse_context *mpsse, int pin, int direction); int is_valid_context(struct mpsse_context *mpsse); From cc84c33b97e850f8ca8bbe2628e3518ac4e11df5 Mon Sep 17 00:00:00 2001 From: Tomasz Wasilczyk Date: Thu, 11 Mar 2021 15:22:14 -0800 Subject: [PATCH 2/2] Support multi-session GPIO. This allows implementing command-line tools that leave GPIO state after terminating themselves. This is a modified version of a change authored by Huihong Luo --- src/mpsse.c | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/src/mpsse.c b/src/mpsse.c index 58ae541..5ec41c2 100644 --- a/src/mpsse.c +++ b/src/mpsse.c @@ -175,11 +175,9 @@ static struct mpsse_context *OpenIndexInternal(int vid, int pid, enum modes mode mpsse->xsize = SPI_RW_SIZE; } - status |= ftdi_usb_reset(&mpsse->ftdi); status |= ftdi_set_latency_timer(&mpsse->ftdi, LATENCY_MS); status |= ftdi_write_data_set_chunksize(&mpsse->ftdi, CHUNK_SIZE); status |= ftdi_read_data_set_chunksize(&mpsse->ftdi, CHUNK_SIZE); - status |= ftdi_set_bitmode(&mpsse->ftdi, 0, BITMODE_RESET); if(status == 0) { @@ -239,7 +237,6 @@ void Close(struct mpsse_context *mpsse) { if(mpsse->open) { - ftdi_set_bitmode(&mpsse->ftdi, 0, BITMODE_RESET); ftdi_usb_close(&mpsse->ftdi); ftdi_deinit(&mpsse->ftdi); } @@ -286,8 +283,7 @@ void EnableBitmode(struct mpsse_context *mpsse, int tf) */ int SetMode(struct mpsse_context *mpsse, int endianess) { - int retval = MPSSE_OK, i = 0, setup_commands_size = 0; - unsigned char buf[CMD_SIZE] = { 0 }; + int retval = MPSSE_OK, setup_commands_size = 0; unsigned char setup_commands[CMD_SIZE*MAX_SETUP_COMMANDS] = { 0 }; /* Do not call is_valid_context() here, as the FTDI chip may not be completely configured when SetMode is called */ @@ -395,18 +391,16 @@ int SetMode(struct mpsse_context *mpsse, int endianess) if(retval == MPSSE_OK) { - /* Set the idle pin states */ - set_bits_low(mpsse, mpsse->pidle); - - /* All GPIO pins are outputs, set low */ - mpsse->trish = 0xFF; - mpsse->gpioh = 0x00; + uint8_t low_pins, high_pins; + if (get_bits_low(mpsse, &low_pins) != MPSSE_OK) low_pins = 0; + if (get_bits_high(mpsse, &high_pins) != MPSSE_OK) high_pins = 0; - buf[i++] = SET_BITS_HIGH; - buf[i++] = mpsse->gpioh; - buf[i++] = mpsse->trish; + mpsse->pstart = low_pins; + mpsse->gpioh = high_pins; - retval = raw_write(mpsse, buf, i); + /* All GPIO pins are outputs */ + mpsse->tris |= GPIO0 | GPIO1 | GPIO2 | GPIO3; + mpsse->trish = 0xFF; } } else