Skip to content

Commit

Permalink
Enable FT230X support, set hwspi pinout for it
Browse files Browse the repository at this point in the history
  • Loading branch information
lorf committed Mar 27, 2018
1 parent 0397cd4 commit 20bfd00
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 51 deletions.
2 changes: 2 additions & 0 deletions Makefile.mingw
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ LIBUSB ?= ./libusb
CPPFLAGS += -DSPI_STATS
# Enable LEDs
CPPFLAGS += -DENABLE_LEDS
# FT230X support is available since libftdi-1.1
CPPFLAGS += -DENABLE_FT230X

# Fixes compilation on Debian/Ubuntu with mingw-w64 < 3.0.
# See: http://sourceforge.net/p/mingw-w64/mailman/mingw-w64-public/thread/[email protected]/
Expand Down
2 changes: 2 additions & 0 deletions Makefile.wine
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ STDCXX_LIB ?= /usr/lib/gcc/i686-linux-gnu/$(WINEGCC_VER)
CPPFLAGS += -DSPI_STATS
# Enable LEDs
CPPFLAGS += -DENABLE_LEDS
# FT230X support is available since libftdi-1.1
CPPFLAGS += -DENABLE_FT230X
CPPFLAGS += $(shell libftdi1-config --cflags) $(shell env PKG_CONFIG_PATH=$(I386_LIB)/pkgconfig pkg-config --cflags libusb-1.0)

# Compile with debugging
Expand Down
24 changes: 18 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* [Programmer hardware](#programmer-hardware)
* [Using FT232RL breakout board as a programmer](#using-ft232rl-breakout-board-as-a-programmer)
* [Dedicated programmer](#dedicated-programmer)
* [Supported FTDI chips](#supported-ftdi-chips)
* [Counterfeit FT232RL chips](#counterfeit-ft232rl-chips)
* [Software](#software)
* [CSR software](#csr-software)
Expand Down Expand Up @@ -71,9 +72,8 @@ Programmer was tested with the following chips:

## Programmer hardware

Programmer hardware is based on FT232R chip. It is possible that later
generation FTDI chips, such as FT2232C/D/H or FT232H, will also work, but this
was not tested.
Programmer hardware is based on FT232R chip. It is possible to use later
generation FTDI chips, see [Supported FTDI chips](#supported-ftdi-chips).

### Using FT232RL breakout board as a programmer

Expand Down Expand Up @@ -119,6 +119,18 @@ Also see notes on [Counterfeit FT232RL chips](#counterfeit-ft232rl-chips).
KiCad schematic for a dedicated programmer can be found in
[hardware/](hardware/) subdirectory.

### Supported FTDI chips

The following FTDI chips are reported working with programmer driver: FT232R
(including counterfeits, even "bricked" ones), FT2232H, FT4232H. There is
experimental support for the following chips: FT2232C, FT2232D, FT232H, FT230X.
Please report the working status via
[GitHub issues](https://github.com/lorf/csr-spi-ftdi/issues).

FT230X has only four primary GPIO pins and does not support default pinout. The
pinout for FT230X is set to `hwspi` on initialization, see `FTDI_PINOUT`
[option](#options).

### Counterfeit FT232RL chips

Cheap (< $5) FT232RL modules usually contain counterfeit FT232RL chips
Expand Down Expand Up @@ -243,10 +255,10 @@ variables or using the -TRANS option to most CSR commandline apps.
used, just the same pinout is used for convenience. This pinout can be used
with adapters like [TIAO
TUMPA](http://www.tiaowiki.com/w/TIAO_USB_Multi_Protocol_Adapter_User's_Manual).
The pinout is as follows: `CS` - `DBUS3`, `CLK` - `DBUS0`, `MOSI` -
`DBUS1`, `MISO` - `DBUS2`.
The pinout is as follows: `CS` - `CTS#` (`D3`), `CLK` - `TXD` (`D0`), `MOSI` -
`RXD` (`D1`), `MISO` - `RTS#` (`D2`).
* `hwspi+leds` - this is the same as `hwspi` but adds read and write LEDs on
`DBUS4` and `DBUS5` pins respectively.
`DTR#` (`D4`) and `DSR#` (`D5`) pins respectively.

For other options see [misc/transport-options.md](misc/transport-options.md).

Expand Down
100 changes: 55 additions & 45 deletions spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,18 @@ static struct ftdi_device_ids ftdi_device_ids[] = {
{ 0x0403, 0x6010, "FT2232" }, /* FT2232H/C/D */
{ 0x0403, 0x6011, "FT4232" }, /* FT4232H */
{ 0x0403, 0x6014, "FT232H" }, /* FT232H */
/*{ 0x0403, 0x6015, "FT230X" },*/ /* FT230X, only since libftdi1-1.2 */
#ifdef ENABLE_FT230X
/* FT230X support is available since libftdi-1.1 */
{ 0x0403, 0x6015, "FT230X" }, /* FT230X */
#endif
};

static char *spi_err_buf = NULL;
static size_t spi_err_buf_sz = 0;

static struct spi_pins *spi_pins;
static struct spi_pins spi_pin_presets[] = SPI_PIN_PRESETS;
static enum spi_pinouts spi_pinout = SPI_PINOUT_DEFAULT;
static int spi_pinout_set = 0;

void spi_set_err_buf(char *buf, size_t sz)
{
Expand Down Expand Up @@ -519,7 +522,8 @@ static int spi_enumerate_ports(void)

void spi_set_pinout(enum spi_pinouts pinout)
{
spi_pinout = pinout;
spi_pins = &spi_pin_presets[pinout];
spi_pinout_set = 1;
}

int spi_set_interface(const char *intf)
Expand Down Expand Up @@ -568,8 +572,6 @@ int spi_init(void)
return -1;
}

spi_pins = &spi_pin_presets[spi_pinout];

return 0;
}

Expand Down Expand Up @@ -733,43 +735,6 @@ int spi_open(int nport)

LOG(INFO, "FTDI: using FTDI device: \"%s\"", spi_ports[nport].name);

rc = ftdi_usb_reset(&ftdic);
if (rc < 0) {
SPI_ERR("FTDI: reset failed: [%d] %s", rc, ftdi_get_error_string(&ftdic));
goto open_err;
}

rc = ftdi_usb_purge_buffers(&ftdic);
if (rc < 0) {
SPI_ERR("FTDI: purge buffers failed: [%d] %s", rc, ftdi_get_error_string(&ftdic));
goto open_err;
}

/* Set 1 ms latency timer, see FTDI AN232B-04 */
rc = ftdi_set_latency_timer(&ftdic, 1);
if (rc < 0) {
SPI_ERR("FTDI: setting latency timer failed: [%d] %s", rc, ftdi_get_error_string(&ftdic));
goto open_err;
}

rc = ftdi_set_bitmode(&ftdic, 0, BITMODE_RESET);
if (rc < 0) {
SPI_ERR("FTDI: reset bitmode failed: [%d] %s", rc, ftdi_get_error_string(&ftdic));
goto open_err;
}

/* Set pins direction */
output_pins = spi_pins->mosi | spi_pins->clk | spi_pins->ncs;
if (spi_pins->nledr)
output_pins |= spi_pins->nledr;
if (spi_pins->nledw)
output_pins |= spi_pins->nledw;
rc = ftdi_set_bitmode(&ftdic, output_pins, BITMODE_SYNCBB);
if (rc < 0) {
SPI_ERR("FTDI: set synchronous bitbang mode failed: [%d] %s", rc, ftdi_get_error_string(&ftdic));
goto open_err;
}

/*
* Note on buffer sizes:
*
Expand Down Expand Up @@ -816,12 +781,16 @@ int spi_open(int nport)
ftdi_type_str = "FT232H";
ftdi_buf_size = 2048;
break;
/* TYPE_230X is supported since libftdi1-1.2 */
/*case TYPE_230X:
#ifdef ENABLE_FT230X
/* FT230X support is available since libftdi-1.1 */
case TYPE_230X:
ftdi_type_str = "FT230X";
ftdi_buf_size = 1024;
/* If pinout was not set via spi_set_pinout() */
if (!spi_pinout_set)
spi_set_pinout(SPI_PINOUT_HWSPI);
break;
*/
#endif
default:
LOG(WARN, "Unknown FTDI chip type, assuming FT232R");
ftdi_type_str = "Unknown";
Expand All @@ -832,6 +801,47 @@ int spi_open(int nport)
LOG(INFO, "Detected %s type programmer chip, buffer size: %u",
ftdi_type_str, ftdi_buf_size);

rc = ftdi_usb_reset(&ftdic);
if (rc < 0) {
SPI_ERR("FTDI: reset failed: [%d] %s", rc, ftdi_get_error_string(&ftdic));
goto open_err;
}

rc = ftdi_usb_purge_buffers(&ftdic);
if (rc < 0) {
SPI_ERR("FTDI: purge buffers failed: [%d] %s", rc, ftdi_get_error_string(&ftdic));
goto open_err;
}

/* Set 1 ms latency timer, see FTDI AN232B-04 */
rc = ftdi_set_latency_timer(&ftdic, 1);
if (rc < 0) {
SPI_ERR("FTDI: setting latency timer failed: [%d] %s", rc, ftdi_get_error_string(&ftdic));
goto open_err;
}

rc = ftdi_set_bitmode(&ftdic, 0, BITMODE_RESET);
if (rc < 0) {
SPI_ERR("FTDI: reset bitmode failed: [%d] %s", rc, ftdi_get_error_string(&ftdic));
goto open_err;
}

/* If pinout was not set via spi_set_pinout() */
if (!spi_pinout_set)
spi_set_pinout(SPI_PINOUT_HWSPI);

/* Set pins direction */
output_pins = spi_pins->mosi | spi_pins->clk | spi_pins->ncs;
if (spi_pins->nledr)
output_pins |= spi_pins->nledr;
if (spi_pins->nledw)
output_pins |= spi_pins->nledw;
rc = ftdi_set_bitmode(&ftdic, output_pins, BITMODE_SYNCBB);
if (rc < 0) {
SPI_ERR("FTDI: set synchronous bitbang mode failed: [%d] %s", rc, ftdi_get_error_string(&ftdic));
goto open_err;
}

/* Initialize xfer buffers */
ftdi_out_buf = malloc(ftdi_buf_size);
ftdi_in_buf = malloc(ftdi_buf_size);
Expand Down

0 comments on commit 20bfd00

Please sign in to comment.