Merge branch 'iceprog-pipe' of https://github.com/jhol/icestorm

This commit is contained in:
Clifford Wolf 2015-10-21 00:51:29 +02:00
commit c373976bc1
1 changed files with 118 additions and 106 deletions

View File

@ -44,14 +44,14 @@ void check_rx()
uint8_t data; uint8_t data;
int rc = ftdi_read_data(&ftdic, &data, 1); int rc = ftdi_read_data(&ftdic, &data, 1);
if (rc <= 0) break; if (rc <= 0) break;
printf("unexpected rx byte: %02X\n", data); fprintf(stderr, "unexpected rx byte: %02X\n", data);
} }
} }
void error() void error()
{ {
check_rx(); check_rx();
printf("ABORT.\n"); fprintf(stderr, "ABORT.\n");
if (ftdic_open) if (ftdic_open)
ftdi_usb_close(&ftdic); ftdi_usb_close(&ftdic);
ftdi_deinit(&ftdic); ftdi_deinit(&ftdic);
@ -64,7 +64,7 @@ uint8_t recv_byte()
while (1) { while (1) {
int rc = ftdi_read_data(&ftdic, &data, 1); int rc = ftdi_read_data(&ftdic, &data, 1);
if (rc < 0) { if (rc < 0) {
printf("Read error.\n"); fprintf(stderr, "Read error.\n");
error(); error();
} }
if (rc == 1) if (rc == 1)
@ -78,7 +78,7 @@ void send_byte(uint8_t data)
{ {
int rc = ftdi_write_data(&ftdic, &data, 1); int rc = ftdi_write_data(&ftdic, &data, 1);
if (rc != 1) { if (rc != 1) {
printf("Write error (single byte, rc=%d, expected %d).\n", rc, 1); fprintf(stderr, "Write error (single byte, rc=%d, expected %d).\n", rc, 1);
error(); error();
} }
} }
@ -94,7 +94,7 @@ void send_spi(uint8_t *data, int n)
int rc = ftdi_write_data(&ftdic, data, n); int rc = ftdi_write_data(&ftdic, data, n);
if (rc != n) { if (rc != n) {
printf("Write error (chunk, rc=%d, expected %d).\n", rc, n); fprintf(stderr, "Write error (chunk, rc=%d, expected %d).\n", rc, n);
error(); error();
} }
} }
@ -110,7 +110,7 @@ void xfer_spi(uint8_t *data, int n)
int rc = ftdi_write_data(&ftdic, data, n); int rc = ftdi_write_data(&ftdic, data, n);
if (rc != n) { if (rc != n) {
printf("Write error (chunk, rc=%d, expected %d).\n", rc, n); fprintf(stderr, "Write error (chunk, rc=%d, expected %d).\n", rc, n);
error(); error();
} }
@ -148,17 +148,17 @@ int get_cdone()
void flash_read_id() void flash_read_id()
{ {
// printf("read flash ID..\n"); // fprintf(stderr, "read flash ID..\n");
uint8_t data[21] = { 0x9F }; uint8_t data[21] = { 0x9F };
set_gpio(0, 0); set_gpio(0, 0);
xfer_spi(data, 21); xfer_spi(data, 21);
set_gpio(1, 0); set_gpio(1, 0);
printf("flash ID:"); fprintf(stderr, "flash ID:");
for (int i = 1; i < 21; i++) for (int i = 1; i < 21; i++)
printf(" 0x%02X", data[i]); fprintf(stderr, " 0x%02X", data[i]);
printf("\n"); fprintf(stderr, "\n");
} }
void flash_power_up() void flash_power_up()
@ -180,7 +180,7 @@ void flash_power_down()
void flash_write_enable() void flash_write_enable()
{ {
if (verbose) if (verbose)
printf("write enable..\n"); fprintf(stderr, "write enable..\n");
uint8_t data[1] = { 0x06 }; uint8_t data[1] = { 0x06 };
set_gpio(0, 0); set_gpio(0, 0);
@ -190,7 +190,7 @@ void flash_write_enable()
void flash_bulk_erase() void flash_bulk_erase()
{ {
printf("bulk erase..\n"); fprintf(stderr, "bulk erase..\n");
uint8_t data[1] = { 0xc7 }; uint8_t data[1] = { 0xc7 };
set_gpio(0, 0); set_gpio(0, 0);
@ -200,7 +200,7 @@ void flash_bulk_erase()
void flash_64kB_sector_erase(int addr) void flash_64kB_sector_erase(int addr)
{ {
printf("erase 64kB sector at 0x%06X..\n", addr); fprintf(stderr, "erase 64kB sector at 0x%06X..\n", addr);
uint8_t command[4] = { 0xd8, addr >> 16, addr >> 8, addr }; uint8_t command[4] = { 0xd8, addr >> 16, addr >> 8, addr };
set_gpio(0, 0); set_gpio(0, 0);
@ -211,7 +211,7 @@ void flash_64kB_sector_erase(int addr)
void flash_prog(int addr, uint8_t *data, int n) void flash_prog(int addr, uint8_t *data, int n)
{ {
if (verbose) if (verbose)
printf("prog 0x%06X +0x%03X..\n", addr, n); fprintf(stderr, "prog 0x%06X +0x%03X..\n", addr, n);
uint8_t command[4] = { 0x02, addr >> 16, addr >> 8, addr }; uint8_t command[4] = { 0x02, addr >> 16, addr >> 8, addr };
set_gpio(0, 0); set_gpio(0, 0);
@ -221,13 +221,13 @@ void flash_prog(int addr, uint8_t *data, int n)
if (verbose) if (verbose)
for (int i = 0; i < n; i++) for (int i = 0; i < n; i++)
printf("%02x%c", data[i], i == n-1 || i % 32 == 31 ? '\n' : ' '); fprintf(stderr, "%02x%c", data[i], i == n-1 || i % 32 == 31 ? '\n' : ' ');
} }
void flash_read(int addr, uint8_t *data, int n) void flash_read(int addr, uint8_t *data, int n)
{ {
if (verbose) if (verbose)
printf("read 0x%06X +0x%03X..\n", addr, n); fprintf(stderr, "read 0x%06X +0x%03X..\n", addr, n);
uint8_t command[4] = { 0x03, addr >> 16, addr >> 8, addr }; uint8_t command[4] = { 0x03, addr >> 16, addr >> 8, addr };
set_gpio(0, 0); set_gpio(0, 0);
@ -238,13 +238,13 @@ void flash_read(int addr, uint8_t *data, int n)
if (verbose) if (verbose)
for (int i = 0; i < n; i++) for (int i = 0; i < n; i++)
printf("%02x%c", data[i], i == n-1 || i % 32 == 31 ? '\n' : ' '); fprintf(stderr, "%02x%c", data[i], i == n-1 || i % 32 == 31 ? '\n' : ' ');
} }
void flash_wait() void flash_wait()
{ {
if (verbose) if (verbose)
printf("waiting.."); fprintf(stderr, "waiting..");
while (1) while (1)
{ {
@ -258,72 +258,76 @@ void flash_wait()
break; break;
if (verbose) { if (verbose) {
printf("."); fprintf(stderr, ".");
fflush(stdout); fflush(stdout);
} }
usleep(250000); usleep(250000);
} }
if (verbose) if (verbose)
printf("\n"); fprintf(stderr, "\n");
} }
void help(const char *progname) void help(const char *progname)
{ {
fprintf(stderr, "\n"); fprintf(stderr, "\n"
fprintf(stderr, "iceprog -- simple programming tool for FTDI-based Lattice iCE programmers\n"); "iceprog -- simple programming tool for FTDI-based Lattice iCE programmers\n"
fprintf(stderr, "\n"); "\n"
fprintf(stderr, "\n"); "\n"
fprintf(stderr, "Notes for iCEstick (iCE40HX-1k devel board):\n"); "Notes for iCEstick (iCE40HX-1k devel board):\n"
fprintf(stderr, " An unmodified iCEstick can only be programmed via the serial flash.\n"); " An unmodified iCEstick can only be programmed via the serial flash.\n"
fprintf(stderr, " Direct programming of the SRAM is not supported. For direct SRAM\n"); " Direct programming of the SRAM is not supported. For direct SRAM\n"
fprintf(stderr, " programming the flash chip and one zero ohm resistor must be desoldered\n"); " programming the flash chip and one zero ohm resistor must be desoldered\n"
fprintf(stderr, " and the FT2232H SI pin must be connected to the iCE SPI_SI pin, as shown\n"); " and the FT2232H SI pin must be connected to the iCE SPI_SI pin, as shown\n"
fprintf(stderr, " in this picture: http://www.clifford.at/gallery/2014-elektronik/IMG_20141115_183838\n"); " in this picture: http://www.clifford.at/gallery/2014-elektronik/IMG_20141115_183838\n"
fprintf(stderr, "\n"); "\n"
fprintf(stderr, "\n"); "\n"
fprintf(stderr, "Notes for the iCE40-HX8K Breakout Board:\n"); "Notes for the iCE40-HX8K Breakout Board:\n"
fprintf(stderr, " Make sure that the jumper settings on the board match the selected\n"); " Make sure that the jumper settings on the board match the selected\n"
fprintf(stderr, " mode (SRAM or FLASH). See the iCE40-HX8K user manual for details.\n"); " mode (SRAM or FLASH). See the iCE40-HX8K user manual for details.\n"
fprintf(stderr, "\n"); "\n"
fprintf(stderr, "\n"); "\n"
fprintf(stderr, "Usage: %s [options] <filename>\n", progname); "Usage: %s [options] <filename>\n"
fprintf(stderr, "\n"); "\n"
fprintf(stderr, " -d <device-string>\n"); " <filename>\n"
fprintf(stderr, " use the specified USB device:\n"); " the path to the file to read or write from, or - to read\n"
fprintf(stderr, "\n"); " from stdin and write to stdout\n"
fprintf(stderr, " d:<devicenode> (e.g. d:002/005)\n"); "\n"
fprintf(stderr, " i:<vendor>:<product> (e.g. i:0x0403:0x6010)\n"); " -d <device-string>\n"
fprintf(stderr, " i:<vendor>:<product>:<index> (e.g. i:0x0403:0x6010:0)\n"); " use the specified USB device:\n"
fprintf(stderr, " s:<vendor>:<product>:<serial-string>\n"); "\n"
fprintf(stderr, "\n"); " d:<devicenode> (e.g. d:002/005)\n"
fprintf(stderr, " -I [ABCD]\n"); " i:<vendor>:<product> (e.g. i:0x0403:0x6010)\n"
fprintf(stderr, " connect to the specified interface on the FTDI chip\n"); " i:<vendor>:<product>:<index> (e.g. i:0x0403:0x6010:0)\n"
fprintf(stderr, "\n"); " s:<vendor>:<product>:<serial-string>\n"
fprintf(stderr, " -r\n"); "\n"
fprintf(stderr, " read entire flash (32Mb / 4MB) and write to file\n"); " -I [ABCD]\n"
fprintf(stderr, "\n"); " connect to the specified interface on the FTDI chip\n"
fprintf(stderr, " -R\n"); "\n"
fprintf(stderr, " read first 256 kB from flash and write to file\n"); " -r\n"
fprintf(stderr, "\n"); " read entire flash (32Mb / 4MB) and write to file\n"
fprintf(stderr, " -c\n"); "\n"
fprintf(stderr, " do not write flash, only verify (check)\n"); " -R\n"
fprintf(stderr, "\n"); " read first 256 kB from flash and write to file\n"
fprintf(stderr, " -b\n"); "\n"
fprintf(stderr, " bulk erase entire flash before writing\n"); " -c\n"
fprintf(stderr, "\n"); " do not write flash, only verify (check)\n"
fprintf(stderr, " -n\n"); "\n"
fprintf(stderr, " do not erase flash before writing\n"); " -b\n"
fprintf(stderr, "\n"); " bulk erase entire flash before writing\n"
fprintf(stderr, " -S\n"); "\n"
fprintf(stderr, " perform SRAM programming\n"); " -n\n"
fprintf(stderr, "\n"); " do not erase flash before writing\n"
fprintf(stderr, " -t\n"); "\n"
fprintf(stderr, " just read the flash ID sequence\n"); " -S\n"
fprintf(stderr, "\n"); " perform SRAM programming\n"
fprintf(stderr, " -v\n"); "\n"
fprintf(stderr, " verbose output\n"); " -t\n"
fprintf(stderr, "\n"); " just read the flash ID sequence\n"
"\n"
" -v\n"
" verbose output\n"
"\n", progname);
exit(1); exit(1);
} }
@ -400,19 +404,19 @@ int main(int argc, char **argv)
// Initialize USB connection to FT2232H // Initialize USB connection to FT2232H
// --------------------------------------------------------- // ---------------------------------------------------------
printf("init..\n"); fprintf(stderr, "init..\n");
ftdi_init(&ftdic); ftdi_init(&ftdic);
ftdi_set_interface(&ftdic, ifnum); ftdi_set_interface(&ftdic, ifnum);
if (devstr != NULL) { if (devstr != NULL) {
if (ftdi_usb_open_string(&ftdic, devstr)) { if (ftdi_usb_open_string(&ftdic, devstr)) {
printf("Can't find iCE FTDI USB device (device string %s).\n", devstr); fprintf(stderr, "Can't find iCE FTDI USB device (device string %s).\n", devstr);
error(); error();
} }
} else { } else {
if (ftdi_usb_open(&ftdic, 0x0403, 0x6010)) { if (ftdi_usb_open(&ftdic, 0x0403, 0x6010)) {
printf("Can't find iCE FTDI USB device (vedor_id 0x0403, device_id 0x6010).\n"); fprintf(stderr, "Can't find iCE FTDI USB device (vedor_id 0x0403, device_id 0x6010).\n");
error(); error();
} }
} }
@ -420,17 +424,17 @@ int main(int argc, char **argv)
ftdic_open = true; ftdic_open = true;
if (ftdi_usb_reset(&ftdic)) { if (ftdi_usb_reset(&ftdic)) {
printf("Failed to reset iCE FTDI USB device.\n"); fprintf(stderr, "Failed to reset iCE FTDI USB device.\n");
error(); error();
} }
if (ftdi_usb_purge_buffers(&ftdic)) { if (ftdi_usb_purge_buffers(&ftdic)) {
printf("Failed to purge buffers on iCE FTDI USB device.\n"); fprintf(stderr, "Failed to purge buffers on iCE FTDI USB device.\n");
error(); error();
} }
if (ftdi_set_bitmode(&ftdic, 0xff, BITMODE_MPSSE) < 0) { if (ftdi_set_bitmode(&ftdic, 0xff, BITMODE_MPSSE) < 0) {
printf("Failed set BITMODE_MPSSE on iCE FTDI USB device.\n"); fprintf(stderr, "Failed set BITMODE_MPSSE on iCE FTDI USB device.\n");
error(); error();
} }
@ -442,7 +446,7 @@ int main(int argc, char **argv)
send_byte(0x00); send_byte(0x00);
send_byte(0x00); send_byte(0x00);
printf("cdone: %s\n", get_cdone() ? "high" : "low"); fprintf(stderr, "cdone: %s\n", get_cdone() ? "high" : "low");
set_gpio(1, 1); set_gpio(1, 1);
usleep(100000); usleep(100000);
@ -450,12 +454,12 @@ int main(int argc, char **argv)
if (test_mode) if (test_mode)
{ {
printf("reset..\n"); fprintf(stderr, "reset..\n");
set_gpio(1, 0); set_gpio(1, 0);
usleep(250000); usleep(250000);
printf("cdone: %s\n", get_cdone() ? "high" : "low"); fprintf(stderr, "cdone: %s\n", get_cdone() ? "high" : "low");
flash_power_up(); flash_power_up();
@ -466,7 +470,7 @@ int main(int argc, char **argv)
set_gpio(1, 1); set_gpio(1, 1);
usleep(250000); usleep(250000);
printf("cdone: %s\n", get_cdone() ? "high" : "low"); fprintf(stderr, "cdone: %s\n", get_cdone() ? "high" : "low");
} }
else if (prog_sram) else if (prog_sram)
{ {
@ -474,7 +478,7 @@ int main(int argc, char **argv)
// Reset // Reset
// --------------------------------------------------------- // ---------------------------------------------------------
printf("reset..\n"); fprintf(stderr, "reset..\n");
set_gpio(0, 0); set_gpio(0, 0);
usleep(100); usleep(100);
@ -482,31 +486,33 @@ int main(int argc, char **argv)
set_gpio(0, 1); set_gpio(0, 1);
usleep(2000); usleep(2000);
printf("cdone: %s\n", get_cdone() ? "high" : "low"); fprintf(stderr, "cdone: %s\n", get_cdone() ? "high" : "low");
// --------------------------------------------------------- // ---------------------------------------------------------
// Program // Program
// --------------------------------------------------------- // ---------------------------------------------------------
FILE *f = fopen(filename, "r"); FILE *f = (strcmp(filename, "-") == 0) ? stdin :
fopen(filename, "r");
if (f == NULL) { if (f == NULL) {
fprintf(stderr, "Error: Can't open '%s' for reading: %s\n", filename, strerror(errno)); fprintf(stderr, "Error: Can't open '%s' for reading: %s\n", filename, strerror(errno));
error(); error();
} }
printf("programming..\n"); fprintf(stderr, "programming..\n");
while (1) while (1)
{ {
static unsigned char buffer[4096]; static unsigned char buffer[4096];
int rc = fread(buffer, 1, 4096, f); int rc = fread(buffer, 1, 4096, f);
if (rc <= 0) break; if (rc <= 0) break;
if (verbose) if (verbose)
printf("sending %d bytes.\n", rc); fprintf(stderr, "sending %d bytes.\n", rc);
send_spi(buffer, rc); send_spi(buffer, rc);
} }
fclose(f); if (f != stdin)
fclose(f);
// add 48 dummy bits // add 48 dummy bits
send_byte(0x8f); send_byte(0x8f);
@ -517,7 +523,7 @@ int main(int argc, char **argv)
send_byte(0x8e); send_byte(0x8e);
send_byte(0x00); send_byte(0x00);
printf("cdone: %s\n", get_cdone() ? "high" : "low"); fprintf(stderr, "cdone: %s\n", get_cdone() ? "high" : "low");
} }
else else
{ {
@ -525,12 +531,12 @@ int main(int argc, char **argv)
// Reset // Reset
// --------------------------------------------------------- // ---------------------------------------------------------
printf("reset..\n"); fprintf(stderr, "reset..\n");
set_gpio(1, 0); set_gpio(1, 0);
usleep(250000); usleep(250000);
printf("cdone: %s\n", get_cdone() ? "high" : "low"); fprintf(stderr, "cdone: %s\n", get_cdone() ? "high" : "low");
flash_power_up(); flash_power_up();
@ -543,7 +549,8 @@ int main(int argc, char **argv)
if (!read_mode && !check_mode) if (!read_mode && !check_mode)
{ {
FILE *f = fopen(filename, "r"); FILE *f = (strcmp(filename, "-") == 0) ? stdin :
fopen(filename, "r");
if (f == NULL) { if (f == NULL) {
fprintf(stderr, "Error: Can't open '%s' for reading: %s\n", filename, strerror(errno)); fprintf(stderr, "Error: Can't open '%s' for reading: %s\n", filename, strerror(errno));
error(); error();
@ -565,7 +572,7 @@ int main(int argc, char **argv)
error(); error();
} }
printf("file size: %d\n", (int)st_buf.st_size); fprintf(stderr, "file size: %d\n", (int)st_buf.st_size);
for (int addr = 0; addr < st_buf.st_size; addr += 0x10000) { for (int addr = 0; addr < st_buf.st_size; addr += 0x10000) {
flash_write_enable(); flash_write_enable();
flash_64kB_sector_erase(addr); flash_64kB_sector_erase(addr);
@ -574,7 +581,7 @@ int main(int argc, char **argv)
} }
} }
printf("programming..\n"); fprintf(stderr, "programming..\n");
for (int addr = 0; true; addr += 256) { for (int addr = 0; true; addr += 256) {
uint8_t buffer[256]; uint8_t buffer[256];
int rc = fread(buffer, 1, 256, f); int rc = fread(buffer, 1, 256, f);
@ -584,7 +591,8 @@ int main(int argc, char **argv)
flash_wait(); flash_wait();
} }
fclose(f); if (f != stdin)
fclose(f);
} }
@ -594,30 +602,33 @@ int main(int argc, char **argv)
if (read_mode) if (read_mode)
{ {
FILE *f = fopen(filename, "w"); FILE *f = (strcmp(filename, "-") == 0) ? stdout :
fopen(filename, "w");
if (f == NULL) { if (f == NULL) {
fprintf(stderr, "Error: Can't open '%s' for writing: %s\n", filename, strerror(errno)); fprintf(stderr, "Error: Can't open '%s' for writing: %s\n", filename, strerror(errno));
error(); error();
} }
printf("reading..\n"); fprintf(stderr, "reading..\n");
for (int addr = 0; addr < max_read_size; addr += 256) { for (int addr = 0; addr < max_read_size; addr += 256) {
uint8_t buffer[256]; uint8_t buffer[256];
flash_read(addr, buffer, 256); flash_read(addr, buffer, 256);
fwrite(buffer, 256, 1, f); fwrite(buffer, 256, 1, f);
} }
fclose(f); if (f != stdout)
fclose(f);
} }
else else
{ {
FILE *f = fopen(filename, "r"); FILE *f = (strcmp(filename, "-") == 0) ? stdin :
fopen(filename, "r");
if (f == NULL) { if (f == NULL) {
fprintf(stderr, "Error: Can't open '%s' for reading: %s\n", filename, strerror(errno)); fprintf(stderr, "Error: Can't open '%s' for reading: %s\n", filename, strerror(errno));
error(); error();
} }
printf("reading..\n"); fprintf(stderr, "reading..\n");
for (int addr = 0; true; addr += 256) { for (int addr = 0; true; addr += 256) {
uint8_t buffer_flash[256], buffer_file[256]; uint8_t buffer_flash[256], buffer_file[256];
int rc = fread(buffer_file, 1, 256, f); int rc = fread(buffer_file, 1, 256, f);
@ -629,9 +640,10 @@ int main(int argc, char **argv)
} }
} }
printf("VERIFY OK\n"); fprintf(stderr, "VERIFY OK\n");
fclose(f); if (f != stdin)
fclose(f);
} }
@ -644,7 +656,7 @@ int main(int argc, char **argv)
set_gpio(1, 1); set_gpio(1, 1);
usleep(250000); usleep(250000);
printf("cdone: %s\n", get_cdone() ? "high" : "low"); fprintf(stderr, "cdone: %s\n", get_cdone() ? "high" : "low");
} }
@ -652,7 +664,7 @@ int main(int argc, char **argv)
// Exit // Exit
// --------------------------------------------------------- // ---------------------------------------------------------
printf("Bye.\n"); fprintf(stderr, "Bye.\n");
ftdi_disable_bitbang(&ftdic); ftdi_disable_bitbang(&ftdic);
ftdi_usb_close(&ftdic); ftdi_usb_close(&ftdic);
ftdi_deinit(&ftdic); ftdi_deinit(&ftdic);