From 1fefe355459123f7c253a4ac649ccb584a080459 Mon Sep 17 00:00:00 2001 From: Roland Lutz Date: Wed, 7 Jun 2017 16:30:29 +0200 Subject: Fix I/O tile documentation --- docs/io_tile.html | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/io_tile.html b/docs/io_tile.html index c683ff7..82cf65b 100644 --- a/docs/io_tile.html +++ b/docs/io_tile.html @@ -47,9 +47,9 @@ to span12_horz_23.

-A top/bottom io cell has 16 connections named span4_vert_l_0 to span4_vert_l_15 on its top edge and -16 connections named span4_vert_r_0 to span4_vert_r_15 on its bottom edge. The nets span4_vert_l_0 -to span4_vert_l_11 are connected to span4_vert_r_4 to span4_vert_r_15. The span-4 and span-12 wires +A top/bottom io cell has 16 connections named span4_horz_l_0 to span4_horz_l_15 on its left edge and +16 connections named span4_horz_r_0 to span4_horz_r_15 on its right edge. The nets span4_horz_l_0 +to span4_horz_l_11 are connected to span4_horz_r_4 to span4_horz_r_15. The span-4 and span-12 wires of the adjacent logic cell are connected to the nets span4_vert_0 to span4_vert_47 and span12_vert_0 to span12_vert_23.

@@ -489,9 +489,9 @@ of the 1k chip: 1 0faboutBYPASS 2 0faboutRESETB 5 0faboutLATCHINPUTVALUE -12 1neigh_op_bnl_1SDO +12 1neigh_op_bnr_3SDO 4 0faboutSDI -5 0faboutSCLK +3 0faboutSCLK

-- cgit v1.2.3 From dbdc65b65b6862af3b7afdb4ef6c3d2a153541e5 Mon Sep 17 00:00:00 2001 From: Roland Lutz Date: Wed, 7 Jun 2017 19:40:54 +0200 Subject: iceprog: Give more information about invocation errors --- iceprog/iceprog.c | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/iceprog/iceprog.c b/iceprog/iceprog.c index 5db6dc9..8f93427 100644 --- a/iceprog/iceprog.c +++ b/iceprog/iceprog.c @@ -32,6 +32,7 @@ #include #include #include +#include #include #include @@ -373,7 +374,10 @@ int main(int argc, char **argv) else if (!strcmp(optarg, "B")) ifnum = INTERFACE_B; else if (!strcmp(optarg, "C")) ifnum = INTERFACE_C; else if (!strcmp(optarg, "D")) ifnum = INTERFACE_D; - else help(argv[0]); + else + errx(EXIT_FAILURE, + "`%s' is not a valid interface (must be " + "`A', `B', `C', or `D')", optarg); break; case 'r': read_mode = true; @@ -413,18 +417,29 @@ int main(int argc, char **argv) } if (read_mode + check_mode + prog_sram + test_mode > 1) - help(argv[0]); + errx(EXIT_FAILURE, + "options `-r'/`-R', `-c', `-S', and `-t' are mutually " + "exclusive"); if (bulk_erase && dont_erase) - help(argv[0]); + errx(EXIT_FAILURE, + "options `-b' and `-n' are mutually exclusive"); - if (optind+1 != argc && !test_mode) { - if (bulk_erase && optind == argc) - filename = "/dev/null"; - else - help(argv[0]); - } else + if (optind + 1 == argc) { filename = argv[optind]; + } else if (optind != argc) { + warnx("too many arguments"); + fprintf(stderr, "Try `%s --help' " + "for more information.\n", argv[0]); + return EXIT_FAILURE; + } else if (bulk_erase) { + filename = "/dev/null"; + } else if (!test_mode) { + warnx("missing argument"); + fprintf(stderr, "Try `%s --help' " + "for more information.\n", argv[0]); + return EXIT_FAILURE; + } // --------------------------------------------------------- // Initialize USB connection to FT2232H -- cgit v1.2.3 From 90381332e2063618431fa42cf5028870c5453cb4 Mon Sep 17 00:00:00 2001 From: Roland Lutz Date: Wed, 7 Jun 2017 20:11:43 +0200 Subject: iceprog: Check for invalid offset/size arguments --- iceprog/iceprog.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/iceprog/iceprog.c b/iceprog/iceprog.c index 8f93427..7886811 100644 --- a/iceprog/iceprog.c +++ b/iceprog/iceprog.c @@ -385,13 +385,21 @@ int main(int argc, char **argv) case 'R': read_mode = true; read_size = strtol(optarg, &endptr, 0); - if (!strcmp(endptr, "k")) read_size *= 1024; - if (!strcmp(endptr, "M")) read_size *= 1024 * 1024; + if (*endptr == '\0') /* ok */; + else if (!strcmp(endptr, "k")) read_size *= 1024; + else if (!strcmp(endptr, "M")) read_size *= 1024 * 1024; + else + errx(EXIT_FAILURE, + "`%s' is not a valid size", optarg); break; case 'o': rw_offset = strtol(optarg, &endptr, 0); - if (!strcmp(endptr, "k")) rw_offset *= 1024; - if (!strcmp(endptr, "M")) rw_offset *= 1024 * 1024; + if (*endptr == '\0') /* ok */; + else if (!strcmp(endptr, "k")) rw_offset *= 1024; + else if (!strcmp(endptr, "M")) rw_offset *= 1024 * 1024; + else + errx(EXIT_FAILURE, + "`%s' is not a valid offset", optarg); break; case 'c': check_mode = true; -- cgit v1.2.3 From 86af65cc32f2902e1f8b3bb5d8f2ce0412a94217 Mon Sep 17 00:00:00 2001 From: Roland Lutz Date: Wed, 7 Jun 2017 19:41:35 +0200 Subject: iceprog: Check for non-applicable options --- iceprog/iceprog.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/iceprog/iceprog.c b/iceprog/iceprog.c index 7886811..31b7a2f 100644 --- a/iceprog/iceprog.c +++ b/iceprog/iceprog.c @@ -433,7 +433,25 @@ int main(int argc, char **argv) errx(EXIT_FAILURE, "options `-b' and `-n' are mutually exclusive"); + if (bulk_erase && (read_mode || check_mode || prog_sram || test_mode)) + errx(EXIT_FAILURE, + "option `-b' only valid in programming mode"); + if (dont_erase && (read_mode || check_mode || prog_sram || test_mode)) + errx(EXIT_FAILURE, + "option `-n' only valid in programming mode"); + + if (rw_offset != 0 && prog_sram) + errx(EXIT_FAILURE, "option `-o' not supported in SRAM mode"); + if (rw_offset != 0 && test_mode) + errx(EXIT_FAILURE, "option `-o' not supported in test mode"); + if (optind + 1 == argc) { + if (test_mode) { + warnx("test mode doesn't take a file name"); + fprintf(stderr, "Try `%s --help' " + "for more information.\n", argv[0]); + return EXIT_FAILURE; + } filename = argv[optind]; } else if (optind != argc) { warnx("too many arguments"); -- cgit v1.2.3 From 703a913bd188f1338d3f2cb27428227006e916aa Mon Sep 17 00:00:00 2001 From: Roland Lutz Date: Wed, 7 Jun 2017 19:46:08 +0200 Subject: iceprog: Add option `--help' --- iceprog/iceprog.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/iceprog/iceprog.c b/iceprog/iceprog.c index 31b7a2f..7f0830c 100644 --- a/iceprog/iceprog.c +++ b/iceprog/iceprog.c @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -342,7 +343,6 @@ void help(const char *progname) fprintf(stderr, "This means that some data after the written data (or even before when -o is\n"); fprintf(stderr, "used) may be erased as well.\n"); fprintf(stderr, "\n"); - exit(1); } int main(int argc, char **argv) @@ -360,9 +360,15 @@ int main(int argc, char **argv) const char *devstr = NULL; enum ftdi_interface ifnum = INTERFACE_A; + static struct option long_options[] = { + {"help", no_argument, NULL, -2}, + {NULL, 0, NULL, 0} + }; + int opt; char *endptr; - while ((opt = getopt(argc, argv, "d:I:rR:o:cbnStv")) != -1) + while ((opt = getopt_long(argc, argv, "d:I:rR:o:cbnStv", + long_options, NULL)) != -1) { switch (opt) { @@ -419,8 +425,14 @@ int main(int argc, char **argv) case 'v': verbose = true; break; - default: + case -2: help(argv[0]); + return EXIT_SUCCESS; + default: + /* error message has already been printed */ + fprintf(stderr, "Try `%s --help' " + "for more information.\n", argv[0]); + return EXIT_FAILURE; } } -- cgit v1.2.3 From 6741d93245c3e080ef43d2c4c2ac525749c2eb76 Mon Sep 17 00:00:00 2001 From: Roland Lutz Date: Wed, 7 Jun 2017 17:33:59 +0200 Subject: iceprog: Overhaul `--help' text --- iceprog/iceprog.c | 93 ++++++++++++++++++++++++------------------------------- 1 file changed, 41 insertions(+), 52 deletions(-) diff --git a/iceprog/iceprog.c b/iceprog/iceprog.c index 7f0830c..a38e113 100644 --- a/iceprog/iceprog.c +++ b/iceprog/iceprog.c @@ -278,71 +278,60 @@ void flash_wait() void help(const char *progname) { + fprintf(stderr, "Simple programming tool for FTDI-based Lattice iCE programmers.\n"); + fprintf(stderr, "Usage: %s [-b|-n|-c] \n", progname); + fprintf(stderr, " %s -r|-R \n", progname); + fprintf(stderr, " %s -S \n", progname); + fprintf(stderr, " %s -t\n", progname); fprintf(stderr, "\n"); - fprintf(stderr, "iceprog -- simple programming tool for FTDI-based Lattice iCE programmers\n"); + fprintf(stderr, "General options:\n"); + fprintf(stderr, " -d use the specified USB device [default: i:0x0403:0x6010]\n"); + fprintf(stderr, " d: (e.g. d:002/005)\n"); + fprintf(stderr, " i:: (e.g. i:0x0403:0x6010)\n"); + fprintf(stderr, " i::: (e.g. i:0x0403:0x6010:0)\n"); + fprintf(stderr, " s:::\n"); + fprintf(stderr, " -I [ABCD] connect to the specified interface on the FTDI chip\n"); + fprintf(stderr, " [default: A]\n"); + fprintf(stderr, " -o start address for read/write [default: 0]\n"); + fprintf(stderr, " (append 'k' to the argument for size in kilobytes,\n"); + fprintf(stderr, " or 'M' for size in megabytes)\n"); + fprintf(stderr, " -v verbose output\n"); fprintf(stderr, "\n"); + fprintf(stderr, "Mode of operation:\n"); + fprintf(stderr, " [default] write file contents to flash, then verify\n"); + fprintf(stderr, " -r read first 256 kB from flash and write to file\n"); + fprintf(stderr, " -R read the specified number of bytes from flash\n"); + fprintf(stderr, " (append 'k' to the argument for size in kilobytes,\n"); + fprintf(stderr, " or 'M' for size in megabytes)\n"); + fprintf(stderr, " -c do not write flash, only verify (`check')\n"); + fprintf(stderr, " -S perform SRAM programming\n"); + fprintf(stderr, " -t just read the flash ID sequence\n"); + fprintf(stderr, "\n"); + fprintf(stderr, "Erase mode (only meaningful in default mode):\n"); + fprintf(stderr, " [default] erase aligned chunks of 64kB in write mode\n"); + fprintf(stderr, " This means that some data after the written data (or\n"); + fprintf(stderr, " even before when -o is used) may be erased as well.\n"); + fprintf(stderr, " -b bulk erase entire flash before writing\n"); + fprintf(stderr, " -n do not erase flash before writing\n"); + fprintf(stderr, "\n"); + fprintf(stderr, "Miscellaneous options:\n"); + fprintf(stderr, " --help display this help and exit\n"); + fprintf(stderr, " -- treat all remaining arguments as filenames\n"); fprintf(stderr, "\n"); fprintf(stderr, "Notes for iCEstick (iCE40HX-1k devel board):\n"); fprintf(stderr, " 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"); fprintf(stderr, " 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"); - fprintf(stderr, " in this picture: http://www.clifford.at/gallery/2014-elektronik/IMG_20141115_183838\n"); - fprintf(stderr, "\n"); + fprintf(stderr, " in this picture:\n"); + fprintf(stderr, " http://www.clifford.at/gallery/2014-elektronik/IMG_20141115_183838\n"); fprintf(stderr, "\n"); fprintf(stderr, "Notes for the iCE40-HX8K Breakout Board:\n"); fprintf(stderr, " 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"); fprintf(stderr, "\n"); - fprintf(stderr, "\n"); - fprintf(stderr, "Usage: %s [options] \n", progname); - fprintf(stderr, "\n"); - fprintf(stderr, " -d \n"); - fprintf(stderr, " use the specified USB device:\n"); - fprintf(stderr, "\n"); - fprintf(stderr, " d: (e.g. d:002/005)\n"); - fprintf(stderr, " i:: (e.g. i:0x0403:0x6010)\n"); - fprintf(stderr, " i::: (e.g. i:0x0403:0x6010:0)\n"); - fprintf(stderr, " s:::\n"); - fprintf(stderr, "\n"); - fprintf(stderr, " -I [ABCD]\n"); - fprintf(stderr, " connect to the specified interface on the FTDI chip\n"); - fprintf(stderr, "\n"); - fprintf(stderr, " -r\n"); - fprintf(stderr, " read first 256 kB from flash and write to file\n"); - fprintf(stderr, "\n"); - fprintf(stderr, " -R \n"); - fprintf(stderr, " read the specified number of bytes from flash\n"); - fprintf(stderr, " (append 'k' to the argument for size in kilobytes, or\n"); - fprintf(stderr, " 'M' for size in megabytes)\n"); - fprintf(stderr, "\n"); - fprintf(stderr, " -o \n"); - fprintf(stderr, " start address for read/write (instead of zero)\n"); - fprintf(stderr, " (append 'k' to the argument for size in kilobytes, or\n"); - fprintf(stderr, " 'M' for size in megabytes)\n"); - fprintf(stderr, "\n"); - fprintf(stderr, " -c\n"); - fprintf(stderr, " do not write flash, only verify (check)\n"); - fprintf(stderr, "\n"); - fprintf(stderr, " -b\n"); - fprintf(stderr, " bulk erase entire flash before writing\n"); - fprintf(stderr, "\n"); - fprintf(stderr, " -n\n"); - fprintf(stderr, " do not erase flash before writing\n"); - fprintf(stderr, "\n"); - fprintf(stderr, " -S\n"); - fprintf(stderr, " perform SRAM programming\n"); - fprintf(stderr, "\n"); - fprintf(stderr, " -t\n"); - fprintf(stderr, " just read the flash ID sequence\n"); - fprintf(stderr, "\n"); - fprintf(stderr, " -v\n"); - fprintf(stderr, " verbose output\n"); - fprintf(stderr, "\n"); - fprintf(stderr, "Without -b or -n, iceprog will erase aligned chunks of 64kB in write mode.\n"); - fprintf(stderr, "This means that some data after the written data (or even before when -o is\n"); - fprintf(stderr, "used) may be erased as well.\n"); - fprintf(stderr, "\n"); + fprintf(stderr, "If you have a bug report, please file an issue on github:\n"); + fprintf(stderr, " https://github.com/cliffordwolf/icestorm/issues\n"); } int main(int argc, char **argv) -- cgit v1.2.3 From 47c9cd4ac1335f04ad84feae45f493b7db46bde8 Mon Sep 17 00:00:00 2001 From: Roland Lutz Date: Thu, 8 Jun 2017 19:08:42 +0200 Subject: iceprog: Open input/output files before talking to hardware --- iceprog/iceprog.c | 82 ++++++++++++++++++++++++------------------------------- 1 file changed, 35 insertions(+), 47 deletions(-) diff --git a/iceprog/iceprog.c b/iceprog/iceprog.c index a38e113..d5356b9 100644 --- a/iceprog/iceprog.c +++ b/iceprog/iceprog.c @@ -468,6 +468,35 @@ int main(int argc, char **argv) return EXIT_FAILURE; } + /* open input/output file in advance + so we can fail before initializing the hardware */ + + FILE *f = NULL; + int file_size = -1; + + if (test_mode) + /* nop */; + else if (read_mode) { + f = (strcmp(filename, "-") == 0) ? stdout + : fopen(filename, "wb"); + if (f == NULL) + err(EXIT_FAILURE, + "can't open '%s' for writing", filename); + } else { + f = (strcmp(filename, "-") == 0) ? stdin + : fopen(filename, "rb"); + if (f == NULL) + err(EXIT_FAILURE, + "can't open '%s' for reading", filename); + + if (!prog_sram && !check_mode && !dont_erase && !bulk_erase) { + struct stat st_buf; + if (stat(filename, &st_buf) == -1) + err(EXIT_FAILURE, "can't stat '%s'", filename); + file_size = (int)st_buf.st_size; + } + } + // --------------------------------------------------------- // Initialize USB connection to FT2232H // --------------------------------------------------------- @@ -574,13 +603,6 @@ int main(int argc, char **argv) // Program // --------------------------------------------------------- - FILE *f = (strcmp(filename, "-") == 0) ? stdin : - fopen(filename, "rb"); - if (f == NULL) { - fprintf(stderr, "Error: Can't open '%s' for reading: %s\n", filename, strerror(errno)); - error(); - } - fprintf(stderr, "programming..\n"); while (1) { @@ -592,9 +614,6 @@ int main(int argc, char **argv) send_spi(buffer, rc); } - if (f != stdin) - fclose(f); - // add 48 dummy bits send_byte(0x8f); send_byte(0x05); @@ -630,13 +649,6 @@ int main(int argc, char **argv) if (!read_mode && !check_mode) { - FILE *f = (strcmp(filename, "-") == 0) ? stdin : - fopen(filename, "rb"); - if (f == NULL) { - fprintf(stderr, "Error: Can't open '%s' for reading: %s\n", filename, strerror(errno)); - error(); - } - if (!dont_erase) { if (bulk_erase) @@ -647,16 +659,10 @@ int main(int argc, char **argv) } else { - struct stat st_buf; - if (stat(filename, &st_buf)) { - fprintf(stderr, "Error: Can't stat '%s': %s\n", filename, strerror(errno)); - error(); - } - - fprintf(stderr, "file size: %d\n", (int)st_buf.st_size); + fprintf(stderr, "file size: %d\n", file_size); int begin_addr = rw_offset & ~0xffff; - int end_addr = (rw_offset + (int)st_buf.st_size + 0xffff) & ~0xffff; + int end_addr = (rw_offset + file_size + 0xffff) & ~0xffff; for (int addr = begin_addr; addr < end_addr; addr += 0x10000) { flash_write_enable(); @@ -678,8 +684,8 @@ int main(int argc, char **argv) flash_wait(); } - if (f != stdin) - fclose(f); + /* seek to the beginning for second pass */ + fseek(f, 0, SEEK_SET); } @@ -689,32 +695,15 @@ int main(int argc, char **argv) if (read_mode) { - FILE *f = (strcmp(filename, "-") == 0) ? stdout : - fopen(filename, "wb"); - if (f == NULL) { - fprintf(stderr, "Error: Can't open '%s' for writing: %s\n", filename, strerror(errno)); - error(); - } - fprintf(stderr, "reading..\n"); for (int addr = 0; addr < read_size; addr += 256) { uint8_t buffer[256]; flash_read(rw_offset + addr, buffer, 256); fwrite(buffer, 256, 1, f); } - - if (f != stdout) - fclose(f); } else { - FILE *f = (strcmp(filename, "-") == 0) ? stdin : - fopen(filename, "rb"); - if (f == NULL) { - fprintf(stderr, "Error: Can't open '%s' for reading: %s\n", filename, strerror(errno)); - error(); - } - fprintf(stderr, "reading..\n"); for (int addr = 0; true; addr += 256) { uint8_t buffer_flash[256], buffer_file[256]; @@ -728,9 +717,6 @@ int main(int argc, char **argv) } fprintf(stderr, "VERIFY OK\n"); - - if (f != stdin) - fclose(f); } @@ -746,6 +732,8 @@ int main(int argc, char **argv) fprintf(stderr, "cdone: %s\n", get_cdone() ? "high" : "low"); } + if (f != NULL && f != stdin && f != stdout) + fclose(f); // --------------------------------------------------------- // Exit -- cgit v1.2.3 From 0bd8876d7f5dad7abf8918b33d0d8e30d33b0aa0 Mon Sep 17 00:00:00 2001 From: Roland Lutz Date: Thu, 22 Jun 2017 21:36:11 +0200 Subject: iceprog: Allow programming from standard input --- iceprog/iceprog.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/iceprog/iceprog.c b/iceprog/iceprog.c index d5356b9..cd19ca0 100644 --- a/iceprog/iceprog.c +++ b/iceprog/iceprog.c @@ -472,7 +472,7 @@ int main(int argc, char **argv) so we can fail before initializing the hardware */ FILE *f = NULL; - int file_size = -1; + long file_size = -1; if (test_mode) /* nop */; @@ -490,10 +490,13 @@ int main(int argc, char **argv) "can't open '%s' for reading", filename); if (!prog_sram && !check_mode && !dont_erase && !bulk_erase) { - struct stat st_buf; - if (stat(filename, &st_buf) == -1) - err(EXIT_FAILURE, "can't stat '%s'", filename); - file_size = (int)st_buf.st_size; + if (fseek(f, 0L, SEEK_END) == -1) + err(EXIT_FAILURE, "%s: fseek", filename); + file_size = ftell(f); + if (file_size == -1) + err(EXIT_FAILURE, "%s: ftell", filename); + if (fseek(f, 0L, SEEK_SET) == -1) + err(EXIT_FAILURE, "%s: fseek", filename); } } @@ -659,7 +662,7 @@ int main(int argc, char **argv) } else { - fprintf(stderr, "file size: %d\n", file_size); + fprintf(stderr, "file size: %ld\n", file_size); int begin_addr = rw_offset & ~0xffff; int end_addr = (rw_offset + file_size + 0xffff) & ~0xffff; -- cgit v1.2.3 From 8413b2c68977b76cc17e01df299337d3f2aeba6f Mon Sep 17 00:00:00 2001 From: Roland Lutz Date: Thu, 8 Jun 2017 14:11:53 +0200 Subject: iceprog: Allow programming from pipe --- iceprog/iceprog.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 48 insertions(+), 8 deletions(-) diff --git a/iceprog/iceprog.c b/iceprog/iceprog.c index cd19ca0..e4e6f49 100644 --- a/iceprog/iceprog.c +++ b/iceprog/iceprog.c @@ -489,14 +489,54 @@ int main(int argc, char **argv) err(EXIT_FAILURE, "can't open '%s' for reading", filename); - if (!prog_sram && !check_mode && !dont_erase && !bulk_erase) { - if (fseek(f, 0L, SEEK_END) == -1) - err(EXIT_FAILURE, "%s: fseek", filename); - file_size = ftell(f); - if (file_size == -1) - err(EXIT_FAILURE, "%s: ftell", filename); - if (fseek(f, 0L, SEEK_SET) == -1) - err(EXIT_FAILURE, "%s: fseek", filename); + /* For regular programming, we need to read the file + twice--once for programming and once for verifying--and + need to know the file size in advance in order to erase + the correct amount of memory. + + See if we can seek on the input file. Checking for "-" + as an argument isn't enough as we might be reading from a + named pipe, or contrarily, the standard input may be an + ordinary file. */ + + if (!prog_sram && !check_mode) { + if (fseek(f, 0L, SEEK_END) != -1) { + file_size = ftell(f); + if (file_size == -1) + err(EXIT_FAILURE, + "%s: ftell", filename); + if (fseek(f, 0L, SEEK_SET) == -1) + err(EXIT_FAILURE, + "%s: fseek", filename); + } else { + FILE *pipe = f; + + f = tmpfile(); + if (f == NULL) + errx(EXIT_FAILURE, + "can't open temporary file"); + file_size = 0; + + while (true) { + static unsigned char buffer[4096]; + size_t rc = + fread(buffer, 1, 4096, pipe); + if (rc <= 0) + break; + size_t wc = + fwrite(buffer, 1, rc, f); + if (wc != rc) + errx(EXIT_FAILURE, + "can't write to " + "temporary file"); + file_size += rc; + } + fclose(pipe); + + /* now seek to the beginning so we can + start reading again */ + fseek(f, 0, SEEK_SET); + } } } -- cgit v1.2.3 From 7b97eb41774019443f552ca1d49766b9798fee32 Mon Sep 17 00:00:00 2001 From: Roland Lutz Date: Thu, 8 Jun 2017 19:40:08 +0200 Subject: iceprog: When reading, don't write more bytes than requested --- iceprog/iceprog.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/iceprog/iceprog.c b/iceprog/iceprog.c index e4e6f49..5233c46 100644 --- a/iceprog/iceprog.c +++ b/iceprog/iceprog.c @@ -742,7 +742,8 @@ int main(int argc, char **argv) for (int addr = 0; addr < read_size; addr += 256) { uint8_t buffer[256]; flash_read(rw_offset + addr, buffer, 256); - fwrite(buffer, 256, 1, f); + fwrite(buffer, read_size - addr > 256 ? 256 : + read_size - addr, 1, f); } } else -- cgit v1.2.3 From eef1731d2b114273f7b4d55475e5b2aa021d6308 Mon Sep 17 00:00:00 2001 From: Roland Lutz Date: Sun, 11 Jun 2017 14:38:43 +0200 Subject: iceprog: Return a meaningful exit status --- iceprog/iceprog.c | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/iceprog/iceprog.c b/iceprog/iceprog.c index 5233c46..430c58d 100644 --- a/iceprog/iceprog.c +++ b/iceprog/iceprog.c @@ -53,7 +53,7 @@ void check_rx() } } -void error() +void error(int status) { check_rx(); fprintf(stderr, "ABORT.\n"); @@ -63,7 +63,7 @@ void error() ftdi_usb_close(&ftdic); } ftdi_deinit(&ftdic); - exit(1); + exit(status); } uint8_t recv_byte() @@ -73,7 +73,7 @@ uint8_t recv_byte() int rc = ftdi_read_data(&ftdic, &data, 1); if (rc < 0) { fprintf(stderr, "Read error.\n"); - error(); + error(2); } if (rc == 1) break; @@ -87,7 +87,7 @@ void send_byte(uint8_t data) int rc = ftdi_write_data(&ftdic, &data, 1); if (rc != 1) { fprintf(stderr, "Write error (single byte, rc=%d, expected %d).\n", rc, 1); - error(); + error(2); } } @@ -103,7 +103,7 @@ void send_spi(uint8_t *data, int n) int rc = ftdi_write_data(&ftdic, data, n); if (rc != n) { fprintf(stderr, "Write error (chunk, rc=%d, expected %d).\n", rc, n); - error(); + error(2); } } @@ -119,7 +119,7 @@ void xfer_spi(uint8_t *data, int n) int rc = ftdi_write_data(&ftdic, data, n); if (rc != n) { fprintf(stderr, "Write error (chunk, rc=%d, expected %d).\n", rc, n); - error(); + error(2); } for (int i = 0; i < n; i++) @@ -318,6 +318,14 @@ void help(const char *progname) fprintf(stderr, " --help display this help and exit\n"); fprintf(stderr, " -- treat all remaining arguments as filenames\n"); fprintf(stderr, "\n"); + fprintf(stderr, "Exit status:\n"); + fprintf(stderr, " 0 on success,\n"); + fprintf(stderr, " 1 if a non-hardware error occurred (e.g., failure to read from or\n"); + fprintf(stderr, " write to a file, or invoked with invalid options),\n"); + fprintf(stderr, " 2 if communication with the hardware failed (e.g., cannot find the\n"); + fprintf(stderr, " iCE FTDI USB device),\n"); + fprintf(stderr, " 3 if verification of the data failed.\n"); + fprintf(stderr, "\n"); fprintf(stderr, "Notes for iCEstick (iCE40HX-1k devel board):\n"); fprintf(stderr, " 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"); @@ -552,12 +560,12 @@ int main(int argc, char **argv) if (devstr != NULL) { if (ftdi_usb_open_string(&ftdic, devstr)) { fprintf(stderr, "Can't find iCE FTDI USB device (device string %s).\n", devstr); - error(); + error(2); } } else { if (ftdi_usb_open(&ftdic, 0x0403, 0x6010)) { fprintf(stderr, "Can't find iCE FTDI USB device (vedor_id 0x0403, device_id 0x6010).\n"); - error(); + error(2); } } @@ -565,30 +573,30 @@ int main(int argc, char **argv) if (ftdi_usb_reset(&ftdic)) { fprintf(stderr, "Failed to reset iCE FTDI USB device.\n"); - error(); + error(2); } if (ftdi_usb_purge_buffers(&ftdic)) { fprintf(stderr, "Failed to purge buffers on iCE FTDI USB device.\n"); - error(); + error(2); } if (ftdi_get_latency_timer(&ftdic, &ftdi_latency) < 0) { fprintf(stderr, "Failed to get latency timer (%s).\n", ftdi_get_error_string(&ftdic)); - error(); + error(2); } /* 1 is the fastest polling, it means 1 kHz polling */ if (ftdi_set_latency_timer(&ftdic, 1) < 0) { fprintf(stderr, "Failed to set latency timer (%s).\n", ftdi_get_error_string(&ftdic)); - error(); + error(2); } ftdic_latency_set = true; if (ftdi_set_bitmode(&ftdic, 0xff, BITMODE_MPSSE) < 0) { fprintf(stderr, "Failed set BITMODE_MPSSE on iCE FTDI USB device.\n"); - error(); + error(2); } // enable clock divide by 5 @@ -756,7 +764,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"); - error(); + error(3); } } -- cgit v1.2.3 From b61cfb8a011330b3621734b134ce3b56d7507b2e Mon Sep 17 00:00:00 2001 From: Roland Lutz Date: Mon, 12 Jun 2017 20:02:34 +0200 Subject: iceprog: Add manpage --- iceprog/Makefile | 3 ++ iceprog/iceprog.1 | 155 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 158 insertions(+) create mode 100644 iceprog/iceprog.1 diff --git a/iceprog/Makefile b/iceprog/Makefile index 286460f..c61b470 100644 --- a/iceprog/Makefile +++ b/iceprog/Makefile @@ -24,9 +24,12 @@ iceprog$(EXE): iceprog.o install: all mkdir -p $(DESTDIR)$(PREFIX)/bin cp iceprog $(DESTDIR)$(PREFIX)/bin/iceprog + mkdir -p $(DESTDIR)$(PREFIX)/share/man/man1 + install -c -m 644 iceprog.1 $(DESTDIR)$(PREFIX)/share/man/man1 uninstall: rm -f $(DESTDIR)$(PREFIX)/bin/iceprog + rm -f $(DESTDIR)$(PREFIX)/share/man/man1/iceprog.1 clean: rm -f iceprog diff --git a/iceprog/iceprog.1 b/iceprog/iceprog.1 new file mode 100644 index 0000000..22d3882 --- /dev/null +++ b/iceprog/iceprog.1 @@ -0,0 +1,155 @@ +.\" Manpage for iceprog(1) +.\" Copyright (C) 2017 Roland Lutz +.\" +.\" Permission is granted to copy, distribute and/or modify this document +.\" under the terms of the GNU Free Documentation License, Version 1.3 or +.\" any later version published by the Free Software Foundation; with no +.\" Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. +.\" +.TH ICEPROG "1" "June 2017" "IceStorm" "User Commands" +.SH NAME +iceprog \- simple programming tool for FTDI\-based Lattice iCE programmers +.SH SYNOPSIS +.B iceprog +[\-b|\-n|\-c] \fIINPUT\-FILE\fR + +.B iceprog +\-r|\-R\fI\,BYTES\fR \fIOUTPUT\-FILE\fR + +.B iceprog +\-S \fIINPUT\-FILE\fR + +.B iceprog +\-t +.SH DESCRIPTION +The \fBiceprog\fR program is a simple programming tool for FTDI\-based +Lattice iCE programmers which can read, write and erase the flash and +write the SRAM of an FPGA. It is typically invoked after the +bitstream has been converted by \fBicepack\fR to the iCE40 \fB.bin\fR +format as the last step of the build process to transfer the bitstream +to the FPGA. +.SS Operation mode +When no special option is given, \fBiceprog\fR erases all 64kB sectors +which would be touched by the written data, writes the data to the +flash, and then reads it back and verifies it. + +\fIPlease note:\fR If the data is not aligned to 64kB, some data +before (if \fB\-o\fR is used) and after the written data may be erased +as well. + +The way the flash is erased can be changed with the following options: +.TP +\fB\-b\fR +Bulk erase the entire flash before writing. When using this option, +\fBiceprog\fR can be invoked without an \fIINPUT\-FILE\fR; in this +case, the flash is just bulk erased, and nothing is written. +.TP +\fB\-n\fR +Don't erase the flash before writing. +.PP +Instead of the default erase/write/verify, \fBiceprog\fR can perform +some other operations: +.TP +\fB\-c\fR +Just read the data which would have been written from the flash and +verify it (`check'). +.TP +\fB\-r\fR +Read the first 256 kB from flash and write them to a file. +.TP +\fB\-R\fR \fISIZE\-IN\-BYTES\fR +Read the specified number of bytes from the flash and write them to a +file. You can append `\fBk\fR' to the size to specify it in kilobytes +and `\fBM\fR' to specify it in megabytes. +.TP +\fB\-S\fR +Perform SRAM programming. +.TP +\fB\-t\fR +Just read the flash ID sequence. +.PP +All of the above options are mutually exclusive. +.SS General options +.TP +\fB\-d\fR \fIDEVICE\-STRING\fR +Use the specified USB device instead of the default one (which is +vendor ID 0x0403 and device ID 0x6010). The supported notations for +\fIDEVICE\-STRING\fR are: + +\fBd:\,\f(BIdevicenode\fR \- path of the bus and device node within +USB device tree (usually at /proc/bus/usb/); e.g., `d:002/005' + +\fBi:\,\f(BIvendor\/\fB:\,\f(BIproduct\fR \- first device with given +vendor and product ID. IDs can be decimal, octal (preceded by +`\fB0\fR'), or hex (preceded by `\fB0x\fR'); e.g., `i:0x0403:0x6010' + +\fBi:\,\f(BIvendor\/\fB:\,\f(BIproduct\/\fB:\,\f(BIindex\fR \- same as +above, with index being the number of the device (starting with 0) if +there is more than one device with this vendor and product ID; e.g., +`i:0x0403:0x6010:0' + +\fBs:\,\f(BIvendor\/\fB:\,\f(BIproduct\/\fB:\,\f(BIserial\-string\fR +\- first device with given vendor ID, product ID and serial string. +.TP +\fB\-I\fR A|B|C|D +Connect to the specified interface on the FTDI chip. If this option +is omitted, interface A is used. +.TP +\fB\-o\fR \fIOFFSET\-IN\-BYTES\fR +Start reading/writing at address \fIOFFSET\-IN\-BYTES\fR instead of the +beginning of the memory. You can append `\fBk\fR' to the offset to +specify it in kilobytes and `\fBM\fR' to specify it in megabytes. +.TP +\fB\-v\fR +Write more verbose messages. +.TP +\fB\-\-help\fR +Display a help text and exit. +.SS Exit status +.TP +.B 0 +on success, +.TP +.B 1 +if a non\-hardware error occurred (e.g., failure to read from or write +to a file, or invoked with invalid options), +.TP +.B 2 +if communication with the hardware failed (e.g., cannot find the iCE +FTDI USB device), +.TP +.B 3 +if verification of the data failed. +.SS Notes for iCEstick (iCE40HX\-1k devel board) +An unmodified iCEstick can only be programmed via the serial flash. +Direct programming of the SRAM is not supported. For direct SRAM +programming the flash chip and one zero ohm resistor must be +desoldered and the FT2232H SI pin must be connected to the iCE SPI_SI +pin, as shown in this picture: + +.SS Notes for the iCE40\-HX8K Breakout Board +Make sure that the jumper settings on the board match the selected +mode (SRAM or FLASH). See the iCE40\-HX8K user manual for details. +.SH AUTHOR +Written by Clifford Wolf. +.SH REPORTING BUGS +If you have a bug report, please file an issue on github: + +.SH COPYRIGHT +Most of Project IceStorm is licensed under the ISC license: + +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +.SH SEE ALSO +Full documentation at: +.br +or available locally via: info \(aq(icestorm) iceprog invocation\(aq -- cgit v1.2.3 From 0a8d98f36a19082eb303a1d42d951c82848b79bb Mon Sep 17 00:00:00 2001 From: Roland Lutz Date: Wed, 28 Jun 2017 22:18:10 +0200 Subject: iceprog: Fix error messages --- iceprog/iceprog.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/iceprog/iceprog.c b/iceprog/iceprog.c index 430c58d..1c2820c 100644 --- a/iceprog/iceprog.c +++ b/iceprog/iceprog.c @@ -564,7 +564,7 @@ int main(int argc, char **argv) } } else { if (ftdi_usb_open(&ftdic, 0x0403, 0x6010)) { - fprintf(stderr, "Can't find iCE FTDI USB device (vedor_id 0x0403, device_id 0x6010).\n"); + fprintf(stderr, "Can't find iCE FTDI USB device (vendor_id 0x0403, device_id 0x6010).\n"); error(2); } } @@ -595,7 +595,7 @@ int main(int argc, char **argv) ftdic_latency_set = true; if (ftdi_set_bitmode(&ftdic, 0xff, BITMODE_MPSSE) < 0) { - fprintf(stderr, "Failed set BITMODE_MPSSE on iCE FTDI USB device.\n"); + fprintf(stderr, "Failed to set BITMODE_MPSSE on iCE FTDI USB device.\n"); error(2); } -- cgit v1.2.3 From 64e129bee9da96738dad416787d543a9711ac6c3 Mon Sep 17 00:00:00 2001 From: Roland Lutz Date: Wed, 28 Jun 2017 16:53:45 +0200 Subject: iceprog: Fix coding style inconsistencies --- iceprog/iceprog.c | 98 ++++++++++++++++++++++++++----------------------------- 1 file changed, 47 insertions(+), 51 deletions(-) diff --git a/iceprog/iceprog.c b/iceprog/iceprog.c index 1c2820c..ec71fd0 100644 --- a/iceprog/iceprog.c +++ b/iceprog/iceprog.c @@ -48,7 +48,8 @@ void check_rx() while (1) { uint8_t data; int rc = ftdi_read_data(&ftdic, &data, 1); - if (rc <= 0) break; + if (rc <= 0) + break; fprintf(stderr, "unexpected rx byte: %02X\n", data); } } @@ -97,8 +98,8 @@ void send_spi(uint8_t *data, int n) return; send_byte(0x11); - send_byte(n-1); - send_byte((n-1) >> 8); + send_byte(n - 1); + send_byte((n - 1) >> 8); int rc = ftdi_write_data(&ftdic, data, n); if (rc != n) { @@ -113,8 +114,8 @@ void xfer_spi(uint8_t *data, int n) return; send_byte(0x31); - send_byte(n-1); - send_byte((n-1) >> 8); + send_byte(n - 1); + send_byte((n - 1) >> 8); int rc = ftdi_write_data(&ftdic, data, n); if (rc != n) { @@ -229,7 +230,7 @@ void flash_prog(int addr, uint8_t *data, int n) if (verbose) for (int i = 0; i < n; i++) - fprintf(stderr, "%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) @@ -246,7 +247,7 @@ void flash_read(int addr, uint8_t *data, int n) if (verbose) for (int i = 0; i < n; i++) - fprintf(stderr, "%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() @@ -254,8 +255,7 @@ void flash_wait() if (verbose) fprintf(stderr, "waiting.."); - while (1) - { + while (1) { uint8_t data[2] = { 0x05 }; set_gpio(0, 0); @@ -365,18 +365,20 @@ int main(int argc, char **argv) int opt; char *endptr; while ((opt = getopt_long(argc, argv, "d:I:rR:o:cbnStv", - long_options, NULL)) != -1) - { - switch (opt) - { + long_options, NULL)) != -1) { + switch (opt) { case 'd': devstr = optarg; break; case 'I': - if (!strcmp(optarg, "A")) ifnum = INTERFACE_A; - else if (!strcmp(optarg, "B")) ifnum = INTERFACE_B; - else if (!strcmp(optarg, "C")) ifnum = INTERFACE_C; - else if (!strcmp(optarg, "D")) ifnum = INTERFACE_D; + if (!strcmp(optarg, "A")) + ifnum = INTERFACE_A; + else if (!strcmp(optarg, "B")) + ifnum = INTERFACE_B; + else if (!strcmp(optarg, "C")) + ifnum = INTERFACE_C; + else if (!strcmp(optarg, "D")) + ifnum = INTERFACE_D; else errx(EXIT_FAILURE, "`%s' is not a valid interface (must be " @@ -388,18 +390,24 @@ int main(int argc, char **argv) case 'R': read_mode = true; read_size = strtol(optarg, &endptr, 0); - if (*endptr == '\0') /* ok */; - else if (!strcmp(endptr, "k")) read_size *= 1024; - else if (!strcmp(endptr, "M")) read_size *= 1024 * 1024; + if (*endptr == '\0') + /* ok */; + else if (!strcmp(endptr, "k")) + read_size *= 1024; + else if (!strcmp(endptr, "M")) + read_size *= 1024 * 1024; else errx(EXIT_FAILURE, "`%s' is not a valid size", optarg); break; case 'o': rw_offset = strtol(optarg, &endptr, 0); - if (*endptr == '\0') /* ok */; - else if (!strcmp(endptr, "k")) rw_offset *= 1024; - else if (!strcmp(endptr, "M")) rw_offset *= 1024 * 1024; + if (*endptr == '\0') + /* ok */; + else if (!strcmp(endptr, "k")) + rw_offset *= 1024; + else if (!strcmp(endptr, "M")) + rw_offset *= 1024 * 1024; else errx(EXIT_FAILURE, "`%s' is not a valid offset", optarg); @@ -613,8 +621,7 @@ int main(int argc, char **argv) usleep(100000); - if (test_mode) - { + if (test_mode) { fprintf(stderr, "reset..\n"); set_gpio(1, 0); @@ -632,9 +639,7 @@ int main(int argc, char **argv) usleep(250000); fprintf(stderr, "cdone: %s\n", get_cdone() ? "high" : "low"); - } - else if (prog_sram) - { + } else if (prog_sram) { // --------------------------------------------------------- // Reset // --------------------------------------------------------- @@ -655,11 +660,11 @@ int main(int argc, char **argv) // --------------------------------------------------------- fprintf(stderr, "programming..\n"); - while (1) - { + while (1) { static unsigned char buffer[4096]; int rc = fread(buffer, 1, 4096, f); - if (rc <= 0) break; + if (rc <= 0) + break; if (verbose) fprintf(stderr, "sending %d bytes.\n", rc); send_spi(buffer, rc); @@ -675,9 +680,7 @@ int main(int argc, char **argv) send_byte(0x00); fprintf(stderr, "cdone: %s\n", get_cdone() ? "high" : "low"); - } - else - { + } else { // --------------------------------------------------------- // Reset // --------------------------------------------------------- @@ -698,18 +701,13 @@ int main(int argc, char **argv) // Program // --------------------------------------------------------- - if (!read_mode && !check_mode) - { - if (!dont_erase) - { - if (bulk_erase) - { + if (!read_mode && !check_mode) { + if (!dont_erase) { + if (bulk_erase) { flash_write_enable(); flash_bulk_erase(); flash_wait(); - } - else - { + } else { fprintf(stderr, "file size: %ld\n", file_size); int begin_addr = rw_offset & ~0xffff; @@ -729,7 +727,8 @@ int main(int argc, char **argv) uint8_t buffer[256]; int page_size = 256 - (rw_offset + addr) % 256; rc = fread(buffer, 1, page_size, f); - if (rc <= 0) break; + if (rc <= 0) + break; flash_write_enable(); flash_prog(rw_offset + addr, buffer, rc); flash_wait(); @@ -739,13 +738,11 @@ int main(int argc, char **argv) fseek(f, 0, SEEK_SET); } - // --------------------------------------------------------- // Read/Verify // --------------------------------------------------------- - if (read_mode) - { + if (read_mode) { fprintf(stderr, "reading..\n"); for (int addr = 0; addr < read_size; addr += 256) { uint8_t buffer[256]; @@ -753,14 +750,13 @@ int main(int argc, char **argv) fwrite(buffer, read_size - addr > 256 ? 256 : read_size - addr, 1, f); } - } - else - { + } else { fprintf(stderr, "reading..\n"); for (int addr = 0; true; addr += 256) { uint8_t buffer_flash[256], buffer_file[256]; int rc = fread(buffer_file, 1, 256, f); - if (rc <= 0) break; + if (rc <= 0) + break; flash_read(rw_offset + addr, buffer_flash, rc); if (memcmp(buffer_file, buffer_flash, rc)) { fprintf(stderr, "Found difference between flash and file!\n"); -- cgit v1.2.3 From f36652689fd4cd5acc03b653175cc81c5e758baa Mon Sep 17 00:00:00 2001 From: Roland Lutz Date: Wed, 28 Jun 2017 16:57:45 +0200 Subject: iceprog: Break overlong lines --- iceprog/iceprog.c | 58 ++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 40 insertions(+), 18 deletions(-) diff --git a/iceprog/iceprog.c b/iceprog/iceprog.c index ec71fd0..8321a3d 100644 --- a/iceprog/iceprog.c +++ b/iceprog/iceprog.c @@ -87,7 +87,8 @@ void send_byte(uint8_t data) { int rc = ftdi_write_data(&ftdic, &data, 1); if (rc != 1) { - fprintf(stderr, "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(2); } } @@ -103,7 +104,8 @@ void send_spi(uint8_t *data, int n) int rc = ftdi_write_data(&ftdic, data, n); if (rc != n) { - fprintf(stderr, "Write error (chunk, rc=%d, expected %d).\n", rc, n); + fprintf(stderr, "Write error (chunk, " + "rc=%d, expected %d).\n", rc, n); error(2); } } @@ -119,7 +121,8 @@ void xfer_spi(uint8_t *data, int n) int rc = ftdi_write_data(&ftdic, data, n); if (rc != n) { - fprintf(stderr, "Write error (chunk, rc=%d, expected %d).\n", rc, n); + fprintf(stderr, "Write error (chunk, " + "rc=%d, expected %d).\n", rc, n); error(2); } @@ -211,7 +214,9 @@ void flash_64kB_sector_erase(int addr) { fprintf(stderr, "erase 64kB sector at 0x%06X..\n", addr); - uint8_t command[4] = { 0xd8, (uint8_t)(addr >> 16), (uint8_t)(addr >> 8), (uint8_t)addr }; + uint8_t command[4] = { 0xd8, (uint8_t)(addr >> 16), + (uint8_t)(addr >> 8), + (uint8_t)addr }; set_gpio(0, 0); send_spi(command, 4); set_gpio(1, 0); @@ -222,7 +227,9 @@ void flash_prog(int addr, uint8_t *data, int n) if (verbose) fprintf(stderr, "prog 0x%06X +0x%03X..\n", addr, n); - uint8_t command[4] = { 0x02, (uint8_t)(addr >> 16), (uint8_t)(addr >> 8), (uint8_t)addr }; + uint8_t command[4] = { 0x02, (uint8_t)(addr >> 16), + (uint8_t)(addr >> 8), + (uint8_t)addr }; set_gpio(0, 0); send_spi(command, 4); send_spi(data, n); @@ -230,7 +237,8 @@ void flash_prog(int addr, uint8_t *data, int n) if (verbose) for (int i = 0; i < n; i++) - fprintf(stderr, "%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) @@ -238,7 +246,9 @@ void flash_read(int addr, uint8_t *data, int n) if (verbose) fprintf(stderr, "read 0x%06X +0x%03X..\n", addr, n); - uint8_t command[4] = { 0x03, (uint8_t)(addr >> 16), (uint8_t)(addr >> 8), (uint8_t)addr }; + uint8_t command[4] = { 0x03, (uint8_t)(addr >> 16), + (uint8_t)(addr >> 8), + (uint8_t)addr }; set_gpio(0, 0); send_spi(command, 4); memset(data, 0, n); @@ -247,7 +257,8 @@ void flash_read(int addr, uint8_t *data, int n) if (verbose) for (int i = 0; i < n; i++) - fprintf(stderr, "%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() @@ -567,12 +578,15 @@ int main(int argc, char **argv) if (devstr != NULL) { if (ftdi_usb_open_string(&ftdic, devstr)) { - fprintf(stderr, "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(2); } } else { if (ftdi_usb_open(&ftdic, 0x0403, 0x6010)) { - fprintf(stderr, "Can't find iCE FTDI USB device (vendor_id 0x0403, device_id 0x6010).\n"); + fprintf(stderr, "Can't find iCE FTDI USB device " + "(vendor_id 0x0403, " + "device_id 0x6010).\n"); error(2); } } @@ -585,25 +599,29 @@ int main(int argc, char **argv) } if (ftdi_usb_purge_buffers(&ftdic)) { - fprintf(stderr, "Failed to purge buffers on iCE FTDI USB device.\n"); + fprintf(stderr, "Failed to purge buffers " + "on iCE FTDI USB device.\n"); error(2); } if (ftdi_get_latency_timer(&ftdic, &ftdi_latency) < 0) { - fprintf(stderr, "Failed to get latency timer (%s).\n", ftdi_get_error_string(&ftdic)); + fprintf(stderr, "Failed to get latency timer (%s).\n", + ftdi_get_error_string(&ftdic)); error(2); } /* 1 is the fastest polling, it means 1 kHz polling */ if (ftdi_set_latency_timer(&ftdic, 1) < 0) { - fprintf(stderr, "Failed to set latency timer (%s).\n", ftdi_get_error_string(&ftdic)); + fprintf(stderr, "Failed to set latency timer (%s).\n", + ftdi_get_error_string(&ftdic)); error(2); } ftdic_latency_set = true; if (ftdi_set_bitmode(&ftdic, 0xff, BITMODE_MPSSE) < 0) { - fprintf(stderr, "Failed to set BITMODE_MPSSE on iCE FTDI USB device.\n"); + fprintf(stderr, "Failed to set BITMODE_MPSSE " + "on iCE FTDI USB device.\n"); error(2); } @@ -708,12 +726,15 @@ int main(int argc, char **argv) flash_bulk_erase(); flash_wait(); } else { - fprintf(stderr, "file size: %ld\n", file_size); + fprintf(stderr, "file size: %ld\n", + file_size); int begin_addr = rw_offset & ~0xffff; - int end_addr = (rw_offset + file_size + 0xffff) & ~0xffff; + int end_addr = (rw_offset + file_size + + 0xffff) & ~0xffff; - for (int addr = begin_addr; addr < end_addr; addr += 0x10000) { + for (int addr = begin_addr; + addr < end_addr; addr += 0x10000) { flash_write_enable(); flash_64kB_sector_erase(addr); flash_wait(); @@ -759,7 +780,8 @@ int main(int argc, char **argv) break; flash_read(rw_offset + addr, buffer_flash, rc); if (memcmp(buffer_file, buffer_flash, rc)) { - fprintf(stderr, "Found difference between flash and file!\n"); + fprintf(stderr, "Found difference " + "between flash and file!\n"); error(3); } } -- cgit v1.2.3 From 62e70897102cbb07e2174658c6030d7403dc7121 Mon Sep 17 00:00:00 2001 From: Roland Lutz Date: Wed, 28 Jun 2017 16:59:53 +0200 Subject: iceprog: Remove trailing newline --- iceprog/iceprog.c | 1 - 1 file changed, 1 deletion(-) diff --git a/iceprog/iceprog.c b/iceprog/iceprog.c index 8321a3d..2234247 100644 --- a/iceprog/iceprog.c +++ b/iceprog/iceprog.c @@ -816,4 +816,3 @@ int main(int argc, char **argv) ftdi_deinit(&ftdic); return 0; } - -- cgit v1.2.3 From 53a8bcce3532bd93b2cbb54a83767df34a28a907 Mon Sep 17 00:00:00 2001 From: Roland Lutz Date: Wed, 28 Jun 2017 17:04:02 +0200 Subject: iceprog: Keep name space clean --- iceprog/iceprog.c | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/iceprog/iceprog.c b/iceprog/iceprog.c index 2234247..9dd9f17 100644 --- a/iceprog/iceprog.c +++ b/iceprog/iceprog.c @@ -37,13 +37,13 @@ #include #include -struct ftdi_context ftdic; -bool ftdic_open = false; -bool verbose = false; -bool ftdic_latency_set = false; -unsigned char ftdi_latency; +static struct ftdi_context ftdic; +static bool ftdic_open = false; +static bool verbose = false; +static bool ftdic_latency_set = false; +static unsigned char ftdi_latency; -void check_rx() +static void check_rx() { while (1) { uint8_t data; @@ -54,7 +54,7 @@ void check_rx() } } -void error(int status) +static void error(int status) { check_rx(); fprintf(stderr, "ABORT.\n"); @@ -67,7 +67,7 @@ void error(int status) exit(status); } -uint8_t recv_byte() +static uint8_t recv_byte() { uint8_t data; while (1) { @@ -83,7 +83,7 @@ uint8_t recv_byte() return data; } -void send_byte(uint8_t data) +static void send_byte(uint8_t data) { int rc = ftdi_write_data(&ftdic, &data, 1); if (rc != 1) { @@ -93,7 +93,7 @@ void send_byte(uint8_t data) } } -void send_spi(uint8_t *data, int n) +static void send_spi(uint8_t *data, int n) { if (n < 1) return; @@ -110,7 +110,7 @@ void send_spi(uint8_t *data, int n) } } -void xfer_spi(uint8_t *data, int n) +static void xfer_spi(uint8_t *data, int n) { if (n < 1) return; @@ -130,7 +130,7 @@ void xfer_spi(uint8_t *data, int n) data[i] = recv_byte(); } -void set_gpio(int slavesel_b, int creset_b) +static void set_gpio(int slavesel_b, int creset_b) { uint8_t gpio = 1; @@ -149,7 +149,7 @@ void set_gpio(int slavesel_b, int creset_b) send_byte(0x93); } -int get_cdone() +static int get_cdone() { uint8_t data; send_byte(0x81); @@ -158,7 +158,7 @@ int get_cdone() return (data & 0x40) != 0; } -void flash_read_id() +static void flash_read_id() { // fprintf(stderr, "read flash ID..\n"); @@ -173,7 +173,7 @@ void flash_read_id() fprintf(stderr, "\n"); } -void flash_power_up() +static void flash_power_up() { uint8_t data[1] = { 0xAB }; set_gpio(0, 0); @@ -181,7 +181,7 @@ void flash_power_up() set_gpio(1, 0); } -void flash_power_down() +static void flash_power_down() { uint8_t data[1] = { 0xB9 }; set_gpio(0, 0); @@ -189,7 +189,7 @@ void flash_power_down() set_gpio(1, 0); } -void flash_write_enable() +static void flash_write_enable() { if (verbose) fprintf(stderr, "write enable..\n"); @@ -200,7 +200,7 @@ void flash_write_enable() set_gpio(1, 0); } -void flash_bulk_erase() +static void flash_bulk_erase() { fprintf(stderr, "bulk erase..\n"); @@ -210,7 +210,7 @@ void flash_bulk_erase() set_gpio(1, 0); } -void flash_64kB_sector_erase(int addr) +static void flash_64kB_sector_erase(int addr) { fprintf(stderr, "erase 64kB sector at 0x%06X..\n", addr); @@ -222,7 +222,7 @@ void flash_64kB_sector_erase(int addr) set_gpio(1, 0); } -void flash_prog(int addr, uint8_t *data, int n) +static void flash_prog(int addr, uint8_t *data, int n) { if (verbose) fprintf(stderr, "prog 0x%06X +0x%03X..\n", addr, n); @@ -241,7 +241,7 @@ void flash_prog(int addr, uint8_t *data, int n) i == n - 1 || i % 32 == 31 ? '\n' : ' '); } -void flash_read(int addr, uint8_t *data, int n) +static void flash_read(int addr, uint8_t *data, int n) { if (verbose) fprintf(stderr, "read 0x%06X +0x%03X..\n", addr, n); @@ -261,7 +261,7 @@ void flash_read(int addr, uint8_t *data, int n) i == n - 1 || i % 32 == 31 ? '\n' : ' '); } -void flash_wait() +static void flash_wait() { if (verbose) fprintf(stderr, "waiting.."); @@ -287,7 +287,7 @@ void flash_wait() fprintf(stderr, "\n"); } -void help(const char *progname) +static void help(const char *progname) { fprintf(stderr, "Simple programming tool for FTDI-based Lattice iCE programmers.\n"); fprintf(stderr, "Usage: %s [-b|-n|-c] \n", progname); -- cgit v1.2.3 From 3c42bdbf666d155f8703eda8ebf6dbd20a4e771b Mon Sep 17 00:00:00 2001 From: Clifford Wolf Date: Wed, 5 Jul 2017 18:34:57 +0200 Subject: Fix coding style in iceprog.c (mostly line breaks and indenting) --- iceprog/iceprog.c | 162 ++++++++++++++++++++++-------------------------------- 1 file changed, 65 insertions(+), 97 deletions(-) diff --git a/iceprog/iceprog.c b/iceprog/iceprog.c index 9dd9f17..e7aab93 100644 --- a/iceprog/iceprog.c +++ b/iceprog/iceprog.c @@ -87,8 +87,7 @@ static void send_byte(uint8_t data) { int rc = ftdi_write_data(&ftdic, &data, 1); if (rc != 1) { - fprintf(stderr, "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(2); } } @@ -104,8 +103,7 @@ static void send_spi(uint8_t *data, int n) int rc = ftdi_write_data(&ftdic, data, n); if (rc != n) { - fprintf(stderr, "Write error (chunk, " - "rc=%d, expected %d).\n", rc, n); + fprintf(stderr, "Write error (chunk, rc=%d, expected %d).\n", rc, n); error(2); } } @@ -121,8 +119,7 @@ static void xfer_spi(uint8_t *data, int n) int rc = ftdi_write_data(&ftdic, data, n); if (rc != n) { - fprintf(stderr, "Write error (chunk, " - "rc=%d, expected %d).\n", rc, n); + fprintf(stderr, "Write error (chunk, rc=%d, expected %d).\n", rc, n); error(2); } @@ -214,9 +211,8 @@ static void flash_64kB_sector_erase(int addr) { fprintf(stderr, "erase 64kB sector at 0x%06X..\n", addr); - uint8_t command[4] = { 0xd8, (uint8_t)(addr >> 16), - (uint8_t)(addr >> 8), - (uint8_t)addr }; + uint8_t command[4] = { 0xd8, (uint8_t)(addr >> 16), (uint8_t)(addr >> 8), (uint8_t)addr }; + set_gpio(0, 0); send_spi(command, 4); set_gpio(1, 0); @@ -227,9 +223,8 @@ static void flash_prog(int addr, uint8_t *data, int n) if (verbose) fprintf(stderr, "prog 0x%06X +0x%03X..\n", addr, n); - uint8_t command[4] = { 0x02, (uint8_t)(addr >> 16), - (uint8_t)(addr >> 8), - (uint8_t)addr }; + uint8_t command[4] = { 0x02, (uint8_t)(addr >> 16), (uint8_t)(addr >> 8), (uint8_t)addr }; + set_gpio(0, 0); send_spi(command, 4); send_spi(data, n); @@ -237,8 +232,7 @@ static void flash_prog(int addr, uint8_t *data, int n) if (verbose) for (int i = 0; i < n; i++) - fprintf(stderr, "%02x%c", data[i], - i == n - 1 || i % 32 == 31 ? '\n' : ' '); + fprintf(stderr, "%02x%c", data[i], i == n - 1 || i % 32 == 31 ? '\n' : ' '); } static void flash_read(int addr, uint8_t *data, int n) @@ -246,9 +240,8 @@ static void flash_read(int addr, uint8_t *data, int n) if (verbose) fprintf(stderr, "read 0x%06X +0x%03X..\n", addr, n); - uint8_t command[4] = { 0x03, (uint8_t)(addr >> 16), - (uint8_t)(addr >> 8), - (uint8_t)addr }; + uint8_t command[4] = { 0x03, (uint8_t)(addr >> 16), (uint8_t)(addr >> 8), (uint8_t)addr }; + set_gpio(0, 0); send_spi(command, 4); memset(data, 0, n); @@ -257,8 +250,7 @@ static void flash_read(int addr, uint8_t *data, int n) if (verbose) for (int i = 0; i < n; i++) - fprintf(stderr, "%02x%c", data[i], - i == n - 1 || i % 32 == 31 ? '\n' : ' '); + fprintf(stderr, "%02x%c", data[i], i == n - 1 || i % 32 == 31 ? '\n' : ' '); } static void flash_wait() @@ -266,7 +258,8 @@ static void flash_wait() if (verbose) fprintf(stderr, "waiting.."); - while (1) { + while (1) + { uint8_t data[2] = { 0x05 }; set_gpio(0, 0); @@ -375,8 +368,7 @@ int main(int argc, char **argv) int opt; char *endptr; - while ((opt = getopt_long(argc, argv, "d:I:rR:o:cbnStv", - long_options, NULL)) != -1) { + while ((opt = getopt_long(argc, argv, "d:I:rR:o:cbnStv", long_options, NULL)) != -1) { switch (opt) { case 'd': devstr = optarg; @@ -391,9 +383,7 @@ int main(int argc, char **argv) else if (!strcmp(optarg, "D")) ifnum = INTERFACE_D; else - errx(EXIT_FAILURE, - "`%s' is not a valid interface (must be " - "`A', `B', `C', or `D')", optarg); + errx(EXIT_FAILURE, "`%s' is not a valid interface (must be `A', `B', `C', or `D')", optarg); break; case 'r': read_mode = true; @@ -408,8 +398,7 @@ int main(int argc, char **argv) else if (!strcmp(endptr, "M")) read_size *= 1024 * 1024; else - errx(EXIT_FAILURE, - "`%s' is not a valid size", optarg); + errx(EXIT_FAILURE, "`%s' is not a valid size", optarg); break; case 'o': rw_offset = strtol(optarg, &endptr, 0); @@ -420,8 +409,7 @@ int main(int argc, char **argv) else if (!strcmp(endptr, "M")) rw_offset *= 1024 * 1024; else - errx(EXIT_FAILURE, - "`%s' is not a valid offset", optarg); + errx(EXIT_FAILURE, "`%s' is not a valid offset", optarg); break; case 'c': check_mode = true; @@ -446,52 +434,45 @@ int main(int argc, char **argv) return EXIT_SUCCESS; default: /* error message has already been printed */ - fprintf(stderr, "Try `%s --help' " - "for more information.\n", argv[0]); + fprintf(stderr, "Try `%s --help' for more information.\n", argv[0]); return EXIT_FAILURE; } } if (read_mode + check_mode + prog_sram + test_mode > 1) - errx(EXIT_FAILURE, - "options `-r'/`-R', `-c', `-S', and `-t' are mutually " - "exclusive"); + errx(EXIT_FAILURE, "options `-r'/`-R', `-c', `-S', and `-t' are mutually exclusive"); if (bulk_erase && dont_erase) - errx(EXIT_FAILURE, - "options `-b' and `-n' are mutually exclusive"); + errx(EXIT_FAILURE, "options `-b' and `-n' are mutually exclusive"); if (bulk_erase && (read_mode || check_mode || prog_sram || test_mode)) - errx(EXIT_FAILURE, - "option `-b' only valid in programming mode"); + errx(EXIT_FAILURE, "option `-b' only valid in programming mode"); + if (dont_erase && (read_mode || check_mode || prog_sram || test_mode)) - errx(EXIT_FAILURE, - "option `-n' only valid in programming mode"); + errx(EXIT_FAILURE, "option `-n' only valid in programming mode"); if (rw_offset != 0 && prog_sram) errx(EXIT_FAILURE, "option `-o' not supported in SRAM mode"); + if (rw_offset != 0 && test_mode) errx(EXIT_FAILURE, "option `-o' not supported in test mode"); if (optind + 1 == argc) { if (test_mode) { warnx("test mode doesn't take a file name"); - fprintf(stderr, "Try `%s --help' " - "for more information.\n", argv[0]); + fprintf(stderr, "Try `%s --help' for more information.\n", argv[0]); return EXIT_FAILURE; } filename = argv[optind]; } else if (optind != argc) { warnx("too many arguments"); - fprintf(stderr, "Try `%s --help' " - "for more information.\n", argv[0]); + fprintf(stderr, "Try `%s --help' for more information.\n", argv[0]); return EXIT_FAILURE; } else if (bulk_erase) { filename = "/dev/null"; } else if (!test_mode) { warnx("missing argument"); - fprintf(stderr, "Try `%s --help' " - "for more information.\n", argv[0]); + fprintf(stderr, "Try `%s --help' for more information.\n", argv[0]); return EXIT_FAILURE; } @@ -504,17 +485,13 @@ int main(int argc, char **argv) if (test_mode) /* nop */; else if (read_mode) { - f = (strcmp(filename, "-") == 0) ? stdout - : fopen(filename, "wb"); + f = (strcmp(filename, "-") == 0) ? stdout : fopen(filename, "wb"); if (f == NULL) - err(EXIT_FAILURE, - "can't open '%s' for writing", filename); + err(EXIT_FAILURE, "can't open '%s' for writing", filename); } else { - f = (strcmp(filename, "-") == 0) ? stdin - : fopen(filename, "rb"); + f = (strcmp(filename, "-") == 0) ? stdin : fopen(filename, "rb"); if (f == NULL) - err(EXIT_FAILURE, - "can't open '%s' for reading", filename); + err(EXIT_FAILURE, "can't open '%s' for reading", filename); /* For regular programming, we need to read the file twice--once for programming and once for verifying--and @@ -530,32 +507,25 @@ int main(int argc, char **argv) if (fseek(f, 0L, SEEK_END) != -1) { file_size = ftell(f); if (file_size == -1) - err(EXIT_FAILURE, - "%s: ftell", filename); + err(EXIT_FAILURE, "%s: ftell", filename); if (fseek(f, 0L, SEEK_SET) == -1) - err(EXIT_FAILURE, - "%s: fseek", filename); + err(EXIT_FAILURE, "%s: fseek", filename); } else { FILE *pipe = f; f = tmpfile(); if (f == NULL) - errx(EXIT_FAILURE, - "can't open temporary file"); + errx(EXIT_FAILURE, "can't open temporary file"); file_size = 0; while (true) { static unsigned char buffer[4096]; - size_t rc = - fread(buffer, 1, 4096, pipe); + size_t rc = fread(buffer, 1, 4096, pipe); if (rc <= 0) break; - size_t wc = - fwrite(buffer, 1, rc, f); + size_t wc = fwrite(buffer, 1, rc, f); if (wc != rc) - errx(EXIT_FAILURE, - "can't write to " - "temporary file"); + errx(EXIT_FAILURE, "can't write to temporary file"); file_size += rc; } fclose(pipe); @@ -578,15 +548,12 @@ int main(int argc, char **argv) if (devstr != NULL) { if (ftdi_usb_open_string(&ftdic, devstr)) { - fprintf(stderr, "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(2); } } else { if (ftdi_usb_open(&ftdic, 0x0403, 0x6010)) { - fprintf(stderr, "Can't find iCE FTDI USB device " - "(vendor_id 0x0403, " - "device_id 0x6010).\n"); + fprintf(stderr, "Can't find iCE FTDI USB device (vendor_id 0x0403, device_id 0x6010).\n"); error(2); } } @@ -599,29 +566,25 @@ int main(int argc, char **argv) } if (ftdi_usb_purge_buffers(&ftdic)) { - fprintf(stderr, "Failed to purge buffers " - "on iCE FTDI USB device.\n"); + fprintf(stderr, "Failed to purge buffers on iCE FTDI USB device.\n"); error(2); } if (ftdi_get_latency_timer(&ftdic, &ftdi_latency) < 0) { - fprintf(stderr, "Failed to get latency timer (%s).\n", - ftdi_get_error_string(&ftdic)); + fprintf(stderr, "Failed to get latency timer (%s).\n", ftdi_get_error_string(&ftdic)); error(2); } /* 1 is the fastest polling, it means 1 kHz polling */ if (ftdi_set_latency_timer(&ftdic, 1) < 0) { - fprintf(stderr, "Failed to set latency timer (%s).\n", - ftdi_get_error_string(&ftdic)); + fprintf(stderr, "Failed to set latency timer (%s).\n", ftdi_get_error_string(&ftdic)); error(2); } ftdic_latency_set = true; if (ftdi_set_bitmode(&ftdic, 0xff, BITMODE_MPSSE) < 0) { - fprintf(stderr, "Failed to set BITMODE_MPSSE " - "on iCE FTDI USB device.\n"); + fprintf(stderr, "Failed to set BITMODE_MPSSE on iCE FTDI USB device.\n"); error(2); } @@ -639,7 +602,8 @@ int main(int argc, char **argv) usleep(100000); - if (test_mode) { + if (test_mode) + { fprintf(stderr, "reset..\n"); set_gpio(1, 0); @@ -657,7 +621,9 @@ int main(int argc, char **argv) usleep(250000); fprintf(stderr, "cdone: %s\n", get_cdone() ? "high" : "low"); - } else if (prog_sram) { + } + else if (prog_sram) + { // --------------------------------------------------------- // Reset // --------------------------------------------------------- @@ -698,7 +664,9 @@ int main(int argc, char **argv) send_byte(0x00); fprintf(stderr, "cdone: %s\n", get_cdone() ? "high" : "low"); - } else { + } + else + { // --------------------------------------------------------- // Reset // --------------------------------------------------------- @@ -719,22 +687,24 @@ int main(int argc, char **argv) // Program // --------------------------------------------------------- - if (!read_mode && !check_mode) { - if (!dont_erase) { - if (bulk_erase) { + if (!read_mode && !check_mode) + { + if (!dont_erase) + { + if (bulk_erase) + { flash_write_enable(); flash_bulk_erase(); flash_wait(); - } else { - fprintf(stderr, "file size: %ld\n", - file_size); + } + else + { + fprintf(stderr, "file size: %ld\n", file_size); int begin_addr = rw_offset & ~0xffff; - int end_addr = (rw_offset + file_size - + 0xffff) & ~0xffff; + int end_addr = (rw_offset + file_size + 0xffff) & ~0xffff; - for (int addr = begin_addr; - addr < end_addr; addr += 0x10000) { + for (int addr = begin_addr; addr < end_addr; addr += 0x10000) { flash_write_enable(); flash_64kB_sector_erase(addr); flash_wait(); @@ -768,8 +738,7 @@ int main(int argc, char **argv) for (int addr = 0; addr < read_size; addr += 256) { uint8_t buffer[256]; flash_read(rw_offset + addr, buffer, 256); - fwrite(buffer, read_size - addr > 256 ? 256 : - read_size - addr, 1, f); + fwrite(buffer, read_size - addr > 256 ? 256 : read_size - addr, 1, f); } } else { fprintf(stderr, "reading..\n"); @@ -780,8 +749,7 @@ int main(int argc, char **argv) break; flash_read(rw_offset + addr, buffer_flash, rc); if (memcmp(buffer_file, buffer_flash, rc)) { - fprintf(stderr, "Found difference " - "between flash and file!\n"); + fprintf(stderr, "Found difference between flash and file!\n"); error(3); } } -- cgit v1.2.3 From 917afd1a853599034b101c9e035ee690560b46fa Mon Sep 17 00:00:00 2001 From: Robert Ou Date: Sun, 16 Jul 2017 18:51:54 -0700 Subject: makefile: Do not ignore user-provided CFLAGS --- config.mk | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config.mk b/config.mk index aeb525f..364f8bc 100644 --- a/config.mk +++ b/config.mk @@ -1,8 +1,8 @@ CXX ?= clang++ CC ?= clang LDLIBS = -lm -lstdc++ -CFLAGS = -MD -O0 -ggdb -Wall -std=c99 -I/usr/local/include -CXXFLAGS = -MD -O0 -ggdb -Wall -std=c++11 -I/usr/local/include +CFLAGS += -MD -O0 -ggdb -Wall -std=c99 -I/usr/local/include +CXXFLAGS += -MD -O0 -ggdb -Wall -std=c++11 -I/usr/local/include PKG_CONFIG ?= pkg-config DESTDIR ?= PREFIX ?= /usr/local -- cgit v1.2.3 From 4111f9cca58fb9fd9ed1858ac56524eff294ff11 Mon Sep 17 00:00:00 2001 From: Robert Ou Date: Sun, 16 Jul 2017 20:52:58 -0700 Subject: makefile: Make install target work for Windows --- icebox/Makefile | 32 ++++++++++++++++---------------- icebram/Makefile | 4 ++-- icemulti/Makefile | 4 ++-- icepack/Makefile | 8 ++++---- icepll/Makefile | 4 ++-- iceprog/Makefile | 4 ++-- icetime/Makefile | 4 ++-- 7 files changed, 30 insertions(+), 30 deletions(-) diff --git a/icebox/Makefile b/icebox/Makefile index 446fb18..afeb13c 100644 --- a/icebox/Makefile +++ b/icebox/Makefile @@ -26,26 +26,26 @@ install: all cp chipdb-8k.txt $(DESTDIR)$(PREFIX)/share/icebox/ cp icebox.py $(DESTDIR)$(PREFIX)/bin/icebox.py cp iceboxdb.py $(DESTDIR)$(PREFIX)/bin/iceboxdb.py - cp icebox_chipdb.py $(DESTDIR)$(PREFIX)/bin/icebox_chipdb - cp icebox_diff.py $(DESTDIR)$(PREFIX)/bin/icebox_diff - cp icebox_explain.py $(DESTDIR)$(PREFIX)/bin/icebox_explain - cp icebox_colbuf.py $(DESTDIR)$(PREFIX)/bin/icebox_colbuf - cp icebox_html.py $(DESTDIR)$(PREFIX)/bin/icebox_html - cp icebox_maps.py $(DESTDIR)$(PREFIX)/bin/icebox_maps - cp icebox_vlog.py $(DESTDIR)$(PREFIX)/bin/icebox_vlog - cp icebox_stat.py $(DESTDIR)$(PREFIX)/bin/icebox_stat + cp icebox_chipdb.py $(DESTDIR)$(PREFIX)/bin/icebox_chipdb$(PY_EXE) + cp icebox_diff.py $(DESTDIR)$(PREFIX)/bin/icebox_diff$(PY_EXE) + cp icebox_explain.py $(DESTDIR)$(PREFIX)/bin/icebox_explain$(PY_EXE) + cp icebox_colbuf.py $(DESTDIR)$(PREFIX)/bin/icebox_colbuf$(PY_EXE) + cp icebox_html.py $(DESTDIR)$(PREFIX)/bin/icebox_html$(PY_EXE) + cp icebox_maps.py $(DESTDIR)$(PREFIX)/bin/icebox_maps$(PY_EXE) + cp icebox_vlog.py $(DESTDIR)$(PREFIX)/bin/icebox_vlog$(PY_EXE) + cp icebox_stat.py $(DESTDIR)$(PREFIX)/bin/icebox_stat$(PY_EXE) uninstall: rm -f $(DESTDIR)$(PREFIX)/bin/icebox.py rm -f $(DESTDIR)$(PREFIX)/bin/iceboxdb.py - rm -f $(DESTDIR)$(PREFIX)/bin/icebox_chipdb - rm -f $(DESTDIR)$(PREFIX)/bin/icebox_diff - rm -f $(DESTDIR)$(PREFIX)/bin/icebox_explain - rm -f $(DESTDIR)$(PREFIX)/bin/icebox_colbuf - rm -f $(DESTDIR)$(PREFIX)/bin/icebox_html - rm -f $(DESTDIR)$(PREFIX)/bin/icebox_maps - rm -f $(DESTDIR)$(PREFIX)/bin/icebox_vlog - rm -f $(DESTDIR)$(PREFIX)/bin/icebox_stat + rm -f $(DESTDIR)$(PREFIX)/bin/icebox_chipdb$(PY_EXE) + rm -f $(DESTDIR)$(PREFIX)/bin/icebox_diff$(PY_EXE) + rm -f $(DESTDIR)$(PREFIX)/bin/icebox_explain$(PY_EXE) + rm -f $(DESTDIR)$(PREFIX)/bin/icebox_colbuf$(PY_EXE) + rm -f $(DESTDIR)$(PREFIX)/bin/icebox_html$(PY_EXE) + rm -f $(DESTDIR)$(PREFIX)/bin/icebox_maps$(PY_EXE) + rm -f $(DESTDIR)$(PREFIX)/bin/icebox_vlog$(PY_EXE) + rm -f $(DESTDIR)$(PREFIX)/bin/icebox_stat$(PY_EXE) rm -f $(DESTDIR)$(PREFIX)/share/icebox/chipdb-384.txt rm -f $(DESTDIR)$(PREFIX)/share/icebox/chipdb-1k.txt rm -f $(DESTDIR)$(PREFIX)/share/icebox/chipdb-8k.txt diff --git a/icebram/Makefile b/icebram/Makefile index d16b80b..3904926 100644 --- a/icebram/Makefile +++ b/icebram/Makefile @@ -14,10 +14,10 @@ test: icebram install: all mkdir -p $(DESTDIR)$(PREFIX)/bin - cp icebram $(DESTDIR)$(PREFIX)/bin/icebram + cp icebram$(EXE) $(DESTDIR)$(PREFIX)/bin/icebram$(EXE) uninstall: - rm -f $(DESTDIR)$(PREFIX)/bin/icebram + rm -f $(DESTDIR)$(PREFIX)/bin/icebram$(EXE) clean: rm -f icebram diff --git a/icemulti/Makefile b/icemulti/Makefile index a168bac..5302158 100644 --- a/icemulti/Makefile +++ b/icemulti/Makefile @@ -11,10 +11,10 @@ icemulti$(EXE): icemulti.o install: all mkdir -p $(DESTDIR)$(PREFIX)/bin - cp icemulti $(DESTDIR)$(PREFIX)/bin/icemulti + cp icemulti$(EXE) $(DESTDIR)$(PREFIX)/bin/icemulti$(EXE) uninstall: - rm -f $(DESTDIR)$(PREFIX)/bin/icemulti + rm -f $(DESTDIR)$(PREFIX)/bin/icemulti$(EXE) clean: rm -f icemulti diff --git a/icepack/Makefile b/icepack/Makefile index 2578fe0..65d4c9a 100644 --- a/icepack/Makefile +++ b/icepack/Makefile @@ -18,12 +18,12 @@ iceunpack.exe: install: all mkdir -p $(DESTDIR)$(PREFIX)/bin - cp icepack $(DESTDIR)$(PREFIX)/bin/icepack - ln -sf icepack $(DESTDIR)$(PREFIX)/bin/iceunpack + cp icepack$(EXE) $(DESTDIR)$(PREFIX)/bin/icepack$(EXE) + ln -sf icepack$(EXE) $(DESTDIR)$(PREFIX)/bin/iceunpack$(EXE) uninstall: - rm -f $(DESTDIR)$(PREFIX)/bin/icepack - rm -f $(DESTDIR)$(PREFIX)/bin/iceunpack + rm -f $(DESTDIR)$(PREFIX)/bin/icepack$(EXE) + rm -f $(DESTDIR)$(PREFIX)/bin/iceunpack$(EXE) clean: rm -f icepack diff --git a/icepll/Makefile b/icepll/Makefile index 4efa4e1..87eabfa 100644 --- a/icepll/Makefile +++ b/icepll/Makefile @@ -11,10 +11,10 @@ icepll$(EXE): icepll.o install: all mkdir -p $(DESTDIR)$(PREFIX)/bin - cp icepll $(DESTDIR)$(PREFIX)/bin/icepll + cp icepll$(EXE) $(DESTDIR)$(PREFIX)/bin/icepll$(EXE) uninstall: - rm -f $(DESTDIR)$(PREFIX)/bin/icepll + rm -f $(DESTDIR)$(PREFIX)/bin/icepll$(EXE) clean: rm -f icepll diff --git a/iceprog/Makefile b/iceprog/Makefile index c61b470..d71a9b7 100644 --- a/iceprog/Makefile +++ b/iceprog/Makefile @@ -23,12 +23,12 @@ iceprog$(EXE): iceprog.o install: all mkdir -p $(DESTDIR)$(PREFIX)/bin - cp iceprog $(DESTDIR)$(PREFIX)/bin/iceprog + cp iceprog$(EXE) $(DESTDIR)$(PREFIX)/bin/iceprog$(EXE) mkdir -p $(DESTDIR)$(PREFIX)/share/man/man1 install -c -m 644 iceprog.1 $(DESTDIR)$(PREFIX)/share/man/man1 uninstall: - rm -f $(DESTDIR)$(PREFIX)/bin/iceprog + rm -f $(DESTDIR)$(PREFIX)/bin/iceprog$(EXE) rm -f $(DESTDIR)$(PREFIX)/share/man/man1/iceprog.1 clean: diff --git a/icetime/Makefile b/icetime/Makefile index f30a42a..5e959c7 100644 --- a/icetime/Makefile +++ b/icetime/Makefile @@ -19,10 +19,10 @@ timings.inc: timings.py ../icefuzz/timings_*.txt install: all mkdir -p $(DESTDIR)$(PREFIX)/bin - cp icetime $(DESTDIR)$(PREFIX)/bin/icetime + cp icetime$(EXE) $(DESTDIR)$(PREFIX)/bin/icetime$(EXE) uninstall: - rm -f $(DESTDIR)$(PREFIX)/bin/icetime + rm -f $(DESTDIR)$(PREFIX)/bin/icetime$(EXE) # View timing netlist: -- cgit v1.2.3 From 9acaac752ac53b51b9b33290394b7811048221fa Mon Sep 17 00:00:00 2001 From: Robert Ou Date: Mon, 17 Jul 2017 01:28:59 -0700 Subject: iceprog: Do not use nonstandard err.h This header does not exist under MinGW. Replace these functions with standard functions. --- iceprog/iceprog.c | 101 ++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 67 insertions(+), 34 deletions(-) diff --git a/iceprog/iceprog.c b/iceprog/iceprog.c index e7aab93..a01d51e 100644 --- a/iceprog/iceprog.c +++ b/iceprog/iceprog.c @@ -33,7 +33,6 @@ #include #include #include -#include #include #include @@ -382,8 +381,10 @@ int main(int argc, char **argv) ifnum = INTERFACE_C; else if (!strcmp(optarg, "D")) ifnum = INTERFACE_D; - else - errx(EXIT_FAILURE, "`%s' is not a valid interface (must be `A', `B', `C', or `D')", optarg); + else { + fprintf(stderr, "%s: `%s' is not a valid interface (must be `A', `B', `C', or `D')\n", argv[0], optarg); + return EXIT_FAILURE; + } break; case 'r': read_mode = true; @@ -397,8 +398,10 @@ int main(int argc, char **argv) read_size *= 1024; else if (!strcmp(endptr, "M")) read_size *= 1024 * 1024; - else - errx(EXIT_FAILURE, "`%s' is not a valid size", optarg); + else { + fprintf(stderr, "%s: `%s' is not a valid size\n", argv[0], optarg); + return EXIT_FAILURE; + } break; case 'o': rw_offset = strtol(optarg, &endptr, 0); @@ -408,8 +411,10 @@ int main(int argc, char **argv) rw_offset *= 1024; else if (!strcmp(endptr, "M")) rw_offset *= 1024 * 1024; - else - errx(EXIT_FAILURE, "`%s' is not a valid offset", optarg); + else { + fprintf(stderr, "%s: `%s' is not a valid offset\n", argv[0], optarg); + return EXIT_FAILURE; + } break; case 'c': check_mode = true; @@ -439,39 +444,51 @@ int main(int argc, char **argv) } } - if (read_mode + check_mode + prog_sram + test_mode > 1) - errx(EXIT_FAILURE, "options `-r'/`-R', `-c', `-S', and `-t' are mutually exclusive"); + if (read_mode + check_mode + prog_sram + test_mode > 1) { + fprintf(stderr, "%s: options `-r'/`-R', `-c', `-S', and `-t' are mutually exclusive\n", argv[0]); + return EXIT_FAILURE; + } - if (bulk_erase && dont_erase) - errx(EXIT_FAILURE, "options `-b' and `-n' are mutually exclusive"); + if (bulk_erase && dont_erase) { + fprintf(stderr, "%s: options `-b' and `-n' are mutually exclusive\n", argv[0]); + return EXIT_FAILURE; + } - if (bulk_erase && (read_mode || check_mode || prog_sram || test_mode)) - errx(EXIT_FAILURE, "option `-b' only valid in programming mode"); + if (bulk_erase && (read_mode || check_mode || prog_sram || test_mode)) { + fprintf(stderr, "%s: option `-b' only valid in programming mode\n", argv[0]); + return EXIT_FAILURE; + } - if (dont_erase && (read_mode || check_mode || prog_sram || test_mode)) - errx(EXIT_FAILURE, "option `-n' only valid in programming mode"); + if (dont_erase && (read_mode || check_mode || prog_sram || test_mode)) { + fprintf(stderr, "%s: option `-n' only valid in programming mode\n", argv[0]); + return EXIT_FAILURE; + } - if (rw_offset != 0 && prog_sram) - errx(EXIT_FAILURE, "option `-o' not supported in SRAM mode"); + if (rw_offset != 0 && prog_sram) { + fprintf(stderr, "%s: option `-o' not supported in SRAM mode\n", argv[0]); + return EXIT_FAILURE; + } - if (rw_offset != 0 && test_mode) - errx(EXIT_FAILURE, "option `-o' not supported in test mode"); + if (rw_offset != 0 && test_mode) { + fprintf(stderr, "%s: option `-o' not supported in test mode\n", argv[0]); + return EXIT_FAILURE; + } if (optind + 1 == argc) { if (test_mode) { - warnx("test mode doesn't take a file name"); + fprintf(stderr, "%s: test mode doesn't take a file name\n", argv[0]); fprintf(stderr, "Try `%s --help' for more information.\n", argv[0]); return EXIT_FAILURE; } filename = argv[optind]; } else if (optind != argc) { - warnx("too many arguments"); + fprintf(stderr, "%s: too many arguments\n", argv[0]); fprintf(stderr, "Try `%s --help' for more information.\n", argv[0]); return EXIT_FAILURE; } else if (bulk_erase) { filename = "/dev/null"; } else if (!test_mode) { - warnx("missing argument"); + fprintf(stderr, "%s: missing argument\n", argv[0]); fprintf(stderr, "Try `%s --help' for more information.\n", argv[0]); return EXIT_FAILURE; } @@ -486,12 +503,18 @@ int main(int argc, char **argv) /* nop */; else if (read_mode) { f = (strcmp(filename, "-") == 0) ? stdout : fopen(filename, "wb"); - if (f == NULL) - err(EXIT_FAILURE, "can't open '%s' for writing", filename); + if (f == NULL) { + fprintf(stderr, "%s: can't open '%s' for writing: ", argv[0], filename); + perror(0); + return EXIT_FAILURE; + } } else { f = (strcmp(filename, "-") == 0) ? stdin : fopen(filename, "rb"); - if (f == NULL) - err(EXIT_FAILURE, "can't open '%s' for reading", filename); + if (f == NULL) { + fprintf(stderr, "%s: can't open '%s' for reading: ", argv[0], filename); + perror(0); + return EXIT_FAILURE; + } /* For regular programming, we need to read the file twice--once for programming and once for verifying--and @@ -506,16 +529,24 @@ int main(int argc, char **argv) if (!prog_sram && !check_mode) { if (fseek(f, 0L, SEEK_END) != -1) { file_size = ftell(f); - if (file_size == -1) - err(EXIT_FAILURE, "%s: ftell", filename); - if (fseek(f, 0L, SEEK_SET) == -1) - err(EXIT_FAILURE, "%s: fseek", filename); + if (file_size == -1) { + fprintf(stderr, "%s: %s: ftell: ", argv[0], filename); + perror(0); + return EXIT_FAILURE; + } + if (fseek(f, 0L, SEEK_SET) == -1) { + fprintf(stderr, "%s: %s: fseek: ", argv[0], filename); + perror(0); + return EXIT_FAILURE; + } } else { FILE *pipe = f; f = tmpfile(); - if (f == NULL) - errx(EXIT_FAILURE, "can't open temporary file"); + if (f == NULL) { + fprintf(stderr, "%s: can't open temporary file\n", argv[0]); + return EXIT_FAILURE; + } file_size = 0; while (true) { @@ -524,8 +555,10 @@ int main(int argc, char **argv) if (rc <= 0) break; size_t wc = fwrite(buffer, 1, rc, f); - if (wc != rc) - errx(EXIT_FAILURE, "can't write to temporary file"); + if (wc != rc) { + fprintf(stderr, "%s: can't write to temporary file\n", argv[0]); + return EXIT_FAILURE; + } file_size += rc; } fclose(pipe); -- cgit v1.2.3 From 4e653c3b7ed6779247c5456bbf63277f027530ce Mon Sep 17 00:00:00 2001 From: Robert Ou Date: Mon, 17 Jul 2017 01:40:57 -0700 Subject: iceprog: Make errors print only the program name Previously, the entire argv[0] would be printed. --- iceprog/iceprog.c | 42 ++++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/iceprog/iceprog.c b/iceprog/iceprog.c index a01d51e..eca9496 100644 --- a/iceprog/iceprog.c +++ b/iceprog/iceprog.c @@ -347,6 +347,12 @@ static void help(const char *progname) int main(int argc, char **argv) { + /* used for error reporting */ + const char *my_name = argv[0]; + for (size_t i = 0; argv[0][i]; i++) + if (argv[0][i] == '/') + my_name = argv[0] + i + 1; + int read_size = 256 * 1024; int rw_offset = 0; @@ -382,7 +388,7 @@ int main(int argc, char **argv) else if (!strcmp(optarg, "D")) ifnum = INTERFACE_D; else { - fprintf(stderr, "%s: `%s' is not a valid interface (must be `A', `B', `C', or `D')\n", argv[0], optarg); + fprintf(stderr, "%s: `%s' is not a valid interface (must be `A', `B', `C', or `D')\n", my_name, optarg); return EXIT_FAILURE; } break; @@ -399,7 +405,7 @@ int main(int argc, char **argv) else if (!strcmp(endptr, "M")) read_size *= 1024 * 1024; else { - fprintf(stderr, "%s: `%s' is not a valid size\n", argv[0], optarg); + fprintf(stderr, "%s: `%s' is not a valid size\n", my_name, optarg); return EXIT_FAILURE; } break; @@ -412,7 +418,7 @@ int main(int argc, char **argv) else if (!strcmp(endptr, "M")) rw_offset *= 1024 * 1024; else { - fprintf(stderr, "%s: `%s' is not a valid offset\n", argv[0], optarg); + fprintf(stderr, "%s: `%s' is not a valid offset\n", my_name, optarg); return EXIT_FAILURE; } break; @@ -445,50 +451,50 @@ int main(int argc, char **argv) } if (read_mode + check_mode + prog_sram + test_mode > 1) { - fprintf(stderr, "%s: options `-r'/`-R', `-c', `-S', and `-t' are mutually exclusive\n", argv[0]); + fprintf(stderr, "%s: options `-r'/`-R', `-c', `-S', and `-t' are mutually exclusive\n", my_name); return EXIT_FAILURE; } if (bulk_erase && dont_erase) { - fprintf(stderr, "%s: options `-b' and `-n' are mutually exclusive\n", argv[0]); + fprintf(stderr, "%s: options `-b' and `-n' are mutually exclusive\n", my_name); return EXIT_FAILURE; } if (bulk_erase && (read_mode || check_mode || prog_sram || test_mode)) { - fprintf(stderr, "%s: option `-b' only valid in programming mode\n", argv[0]); + fprintf(stderr, "%s: option `-b' only valid in programming mode\n", my_name); return EXIT_FAILURE; } if (dont_erase && (read_mode || check_mode || prog_sram || test_mode)) { - fprintf(stderr, "%s: option `-n' only valid in programming mode\n", argv[0]); + fprintf(stderr, "%s: option `-n' only valid in programming mode\n", my_name); return EXIT_FAILURE; } if (rw_offset != 0 && prog_sram) { - fprintf(stderr, "%s: option `-o' not supported in SRAM mode\n", argv[0]); + fprintf(stderr, "%s: option `-o' not supported in SRAM mode\n", my_name); return EXIT_FAILURE; } if (rw_offset != 0 && test_mode) { - fprintf(stderr, "%s: option `-o' not supported in test mode\n", argv[0]); + fprintf(stderr, "%s: option `-o' not supported in test mode\n", my_name); return EXIT_FAILURE; } if (optind + 1 == argc) { if (test_mode) { - fprintf(stderr, "%s: test mode doesn't take a file name\n", argv[0]); + fprintf(stderr, "%s: test mode doesn't take a file name\n", my_name); fprintf(stderr, "Try `%s --help' for more information.\n", argv[0]); return EXIT_FAILURE; } filename = argv[optind]; } else if (optind != argc) { - fprintf(stderr, "%s: too many arguments\n", argv[0]); + fprintf(stderr, "%s: too many arguments\n", my_name); fprintf(stderr, "Try `%s --help' for more information.\n", argv[0]); return EXIT_FAILURE; } else if (bulk_erase) { filename = "/dev/null"; } else if (!test_mode) { - fprintf(stderr, "%s: missing argument\n", argv[0]); + fprintf(stderr, "%s: missing argument\n", my_name); fprintf(stderr, "Try `%s --help' for more information.\n", argv[0]); return EXIT_FAILURE; } @@ -504,14 +510,14 @@ int main(int argc, char **argv) else if (read_mode) { f = (strcmp(filename, "-") == 0) ? stdout : fopen(filename, "wb"); if (f == NULL) { - fprintf(stderr, "%s: can't open '%s' for writing: ", argv[0], filename); + fprintf(stderr, "%s: can't open '%s' for writing: ", my_name, filename); perror(0); return EXIT_FAILURE; } } else { f = (strcmp(filename, "-") == 0) ? stdin : fopen(filename, "rb"); if (f == NULL) { - fprintf(stderr, "%s: can't open '%s' for reading: ", argv[0], filename); + fprintf(stderr, "%s: can't open '%s' for reading: ", my_name, filename); perror(0); return EXIT_FAILURE; } @@ -530,12 +536,12 @@ int main(int argc, char **argv) if (fseek(f, 0L, SEEK_END) != -1) { file_size = ftell(f); if (file_size == -1) { - fprintf(stderr, "%s: %s: ftell: ", argv[0], filename); + fprintf(stderr, "%s: %s: ftell: ", my_name, filename); perror(0); return EXIT_FAILURE; } if (fseek(f, 0L, SEEK_SET) == -1) { - fprintf(stderr, "%s: %s: fseek: ", argv[0], filename); + fprintf(stderr, "%s: %s: fseek: ", my_name, filename); perror(0); return EXIT_FAILURE; } @@ -544,7 +550,7 @@ int main(int argc, char **argv) f = tmpfile(); if (f == NULL) { - fprintf(stderr, "%s: can't open temporary file\n", argv[0]); + fprintf(stderr, "%s: can't open temporary file\n", my_name); return EXIT_FAILURE; } file_size = 0; @@ -556,7 +562,7 @@ int main(int argc, char **argv) break; size_t wc = fwrite(buffer, 1, rc, f); if (wc != rc) { - fprintf(stderr, "%s: can't write to temporary file\n", argv[0]); + fprintf(stderr, "%s: can't write to temporary file\n", my_name); return EXIT_FAILURE; } file_size += rc; -- cgit v1.2.3 From 70e01c1802bef592452cada1cba0224185e8029e Mon Sep 17 00:00:00 2001 From: Clifford Wolf Date: Tue, 18 Jul 2017 18:37:47 +0200 Subject: Add pre- and post-synthesis testbench examples --- examples/icestick/.gitignore | 5 +++ examples/icestick/Makefile | 15 ++++++++ examples/icestick/rs232demo.v | 8 +++++ examples/icestick/rs232demo_tb.v | 75 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 103 insertions(+) create mode 100644 examples/icestick/rs232demo_tb.v diff --git a/examples/icestick/.gitignore b/examples/icestick/.gitignore index 539898f..bb30525 100644 --- a/examples/icestick/.gitignore +++ b/examples/icestick/.gitignore @@ -6,3 +6,8 @@ rs232demo.bin rs232demo.blif rs232demo.asc rs232demo.rpt +rs232demo_tb +rs232demo_tb.vcd +rs232demo_syn.v +rs232demo_syntb +rs232demo_syntb.vcd diff --git a/examples/icestick/Makefile b/examples/icestick/Makefile index 9294608..d687d14 100644 --- a/examples/icestick/Makefile +++ b/examples/icestick/Makefile @@ -18,6 +18,21 @@ all: $(PROJ).rpt $(PROJ).bin %.rpt: %.asc icetime -d $(DEVICE) -mtr $@ $< +%_tb: %_tb.v %.v + iverilog -o $@ $^ + +%_tb.vcd: %_tb + ./$< +vcd=$@ + +%_syn.v: %.blif + yosys -o $@ $^ + +%_syntb: %_tb.v %_syn.v + iverilog -o $@ $^ `yosys-config --datdir/ice40/cells_sim.v` + +%_syntb.vcd: %_syntb + ./$< +vcd=$@ + prog: $(PROJ).bin iceprog $< diff --git a/examples/icestick/rs232demo.v b/examples/icestick/rs232demo.v index f9e7546..40347e8 100644 --- a/examples/icestick/rs232demo.v +++ b/examples/icestick/rs232demo.v @@ -19,6 +19,14 @@ module top ( reg [3:0] bit_cnt = 0; reg recv = 0; + initial begin + LED1 = 0; + LED2 = 0; + LED3 = 0; + LED4 = 0; + LED5 = 0; + end + always @(posedge clk) begin buffer_valid <= 0; if (!recv) begin diff --git a/examples/icestick/rs232demo_tb.v b/examples/icestick/rs232demo_tb.v new file mode 100644 index 0000000..3e95abf --- /dev/null +++ b/examples/icestick/rs232demo_tb.v @@ -0,0 +1,75 @@ +module testbench; + localparam integer PERIOD = 12000000 / 9600; + + reg clk = 1; + always #5 clk = ~clk; + + reg RX = 1; + wire TX; + wire LED1; + wire LED2; + wire LED3; + wire LED4; + wire LED5; + + top uut ( + .clk (clk ), + .RX (RX ), + .TX (TX ), + .LED1(LED1), + .LED2(LED2), + .LED3(LED3), + .LED4(LED4), + .LED5(LED5) + ); + + task send_byte; + input [7:0] c; + integer i; + begin + RX <= 0; + repeat (PERIOD) @(posedge clk); + + for (i = 0; i < 8; i = i+1) begin + RX <= c[i]; + repeat (PERIOD) @(posedge clk); + end + + RX <= 1; + repeat (PERIOD) @(posedge clk); + end + endtask + + reg [4095:0] vcdfile; + + initial begin + if ($value$plusargs("vcd=%s", vcdfile)) begin + $dumpfile(vcdfile); + $dumpvars(0, testbench); + end + + // send break + repeat (20 * PERIOD) @(posedge clk); + RX <= 0; + repeat (20 * PERIOD) @(posedge clk); + RX <= 1; + repeat (20 * PERIOD) @(posedge clk); + + // turn all LEDs on + send_byte("1"); + send_byte("2"); + send_byte("3"); + send_byte("4"); + send_byte("5"); + + // turn all LEDs off + send_byte("1"); + send_byte("2"); + send_byte("3"); + send_byte("4"); + send_byte("5"); + + repeat (10 * PERIOD) @(posedge clk); + $finish; + end +endmodule -- cgit v1.2.3 From cb0a0f7ef81555cb5c8e51cabb806a302c92b91a Mon Sep 17 00:00:00 2001 From: Clifford Wolf Date: Wed, 19 Jul 2017 15:23:15 +0200 Subject: Improve rs232demo test bench --- examples/icestick/rs232demo_tb.v | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/examples/icestick/rs232demo_tb.v b/examples/icestick/rs232demo_tb.v index 3e95abf..5b9aee1 100644 --- a/examples/icestick/rs232demo_tb.v +++ b/examples/icestick/rs232demo_tb.v @@ -1,8 +1,11 @@ module testbench; localparam integer PERIOD = 12000000 / 9600; - reg clk = 1; - always #5 clk = ~clk; + // reg clk = 0; + // initial #10 forever #5 clk = ~clk; + + reg clk; + always #5 clk = (clk === 1'b0); reg RX = 1; wire TX; @@ -48,12 +51,7 @@ module testbench; $dumpvars(0, testbench); end - // send break - repeat (20 * PERIOD) @(posedge clk); - RX <= 0; - repeat (20 * PERIOD) @(posedge clk); - RX <= 1; - repeat (20 * PERIOD) @(posedge clk); + repeat (10 * PERIOD) @(posedge clk); // turn all LEDs on send_byte("1"); @@ -70,6 +68,7 @@ module testbench; send_byte("5"); repeat (10 * PERIOD) @(posedge clk); + $finish; end endmodule -- cgit v1.2.3 From 6124133269c7ff7bc550064e116c1bcfcbb412bf Mon Sep 17 00:00:00 2001 From: Clifford Wolf Date: Fri, 21 Jul 2017 16:56:15 +0200 Subject: Add icestick "checker" example --- examples/icestick/.gitignore | 9 +++++++ examples/icestick/Makefile | 7 +++--- examples/icestick/checker.v | 55 ++++++++++++++++++++++++++++++++++++++++++ examples/icestick/checker_tb.v | 40 ++++++++++++++++++++++++++++++ 4 files changed, 108 insertions(+), 3 deletions(-) create mode 100644 examples/icestick/checker.v create mode 100644 examples/icestick/checker_tb.v diff --git a/examples/icestick/.gitignore b/examples/icestick/.gitignore index bb30525..c854ccc 100644 --- a/examples/icestick/.gitignore +++ b/examples/icestick/.gitignore @@ -11,3 +11,12 @@ rs232demo_tb.vcd rs232demo_syn.v rs232demo_syntb rs232demo_syntb.vcd +checker.bin +checker.blif +checker.asc +checker.rpt +checker_tb +checker_tb.vcd +checker_syn.v +checker_syntb +checker_syntb.vcd diff --git a/examples/icestick/Makefile b/examples/icestick/Makefile index d687d14..8b8e741 100644 --- a/examples/icestick/Makefile +++ b/examples/icestick/Makefile @@ -1,5 +1,6 @@ PROJ = example # PROJ = rs232demo +# PROJ = checker PIN_DEF = icestick.pcf DEVICE = hx1k @@ -22,16 +23,16 @@ all: $(PROJ).rpt $(PROJ).bin iverilog -o $@ $^ %_tb.vcd: %_tb - ./$< +vcd=$@ + vvp -N $< +vcd=$@ %_syn.v: %.blif - yosys -o $@ $^ + yosys -p 'read_blif -wideports $^; write_verilog $@' %_syntb: %_tb.v %_syn.v iverilog -o $@ $^ `yosys-config --datdir/ice40/cells_sim.v` %_syntb.vcd: %_syntb - ./$< +vcd=$@ + vvp -N $< +vcd=$@ prog: $(PROJ).bin iceprog $< diff --git a/examples/icestick/checker.v b/examples/icestick/checker.v new file mode 100644 index 0000000..63c70fe --- /dev/null +++ b/examples/icestick/checker.v @@ -0,0 +1,55 @@ +// A simple circuit that can be used to detect brownouts and other hardware issues + +module top ( + input clk, + output LED1, + output LED2, + output LED3, + output LED4, + output LED5 +); + reg [7:0] reset_counter = 0; + reg resetn = 0; + + always @(posedge clk) begin + reset_counter <= reset_counter + 1; + resetn <= resetn | &reset_counter; + end + + reg error, rdmode, rdfin; + + reg [31:0] scratchpad [0:1023]; + reg [31:0] xorshift32_state; + reg [9:0] index; + + reg [31:0] next_xorshift32_state; + + always @* begin + next_xorshift32_state = xorshift32_state ^ ( xorshift32_state << 13); + next_xorshift32_state = next_xorshift32_state ^ (next_xorshift32_state >> 17); + next_xorshift32_state = next_xorshift32_state ^ (next_xorshift32_state << 5); + end + + always @(posedge clk) begin + xorshift32_state <= &index ? 123456789 : next_xorshift32_state; + index <= index + 1; + + if (!resetn) begin + xorshift32_state <= 123456789; + index <= 0; + error <= 0; + rdmode <= 0; + rdfin <= 0; + end else + if (!rdmode) begin + scratchpad[index] <= xorshift32_state; + rdmode <= &index; + end else begin + if (scratchpad[index] != xorshift32_state) error <= 1; + rdfin <= rdfin || &index; + end + end + + wire ok = resetn && rdfin && !error; + assign LED1 = error, LED2 = error, LED3 = error, LED4 = error, LED5 = ok; +endmodule diff --git a/examples/icestick/checker_tb.v b/examples/icestick/checker_tb.v new file mode 100644 index 0000000..241c89e --- /dev/null +++ b/examples/icestick/checker_tb.v @@ -0,0 +1,40 @@ +module testbench; + reg clk; + always #5 clk = (clk === 1'b0); + + wire ok; + + top uut ( + .clk(clk), + .LED5(ok) + ); + + reg [4095:0] vcdfile; + + initial begin + if ($value$plusargs("vcd=%s", vcdfile)) begin + $dumpfile(vcdfile); + $dumpvars(0, testbench); + end + end + + initial begin + @(posedge ok); + @(negedge ok); + $display("ERROR: detected falling edge on OK pin!"); + $stop; + end + + initial begin + repeat (3000) @(posedge clk); + + if (!ok) begin + $display("ERROR: OK pin not asserted after 3000 cycles!"); + $stop; + end + + repeat (10000) @(posedge clk); + $display("SUCCESS: OK pin still asserted after 10000 cycles."); + $finish; + end +endmodule -- cgit v1.2.3 From 4ac8fff26c3f29372d98c2230ab8cab76650f3d5 Mon Sep 17 00:00:00 2001 From: Clifford Wolf Date: Fri, 21 Jul 2017 17:43:05 +0200 Subject: Use better error pattern in icestick checker example --- examples/icestick/checker.v | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/icestick/checker.v b/examples/icestick/checker.v index 63c70fe..a441845 100644 --- a/examples/icestick/checker.v +++ b/examples/icestick/checker.v @@ -51,5 +51,5 @@ module top ( end wire ok = resetn && rdfin && !error; - assign LED1 = error, LED2 = error, LED3 = error, LED4 = error, LED5 = ok; + assign LED1 = 0, LED2 = error, LED3 = 0, LED4 = error, LED5 = ok; endmodule -- cgit v1.2.3 From 81e943e050dad652da795d21375bb700064116f4 Mon Sep 17 00:00:00 2001 From: Clifford Wolf Date: Fri, 21 Jul 2017 18:21:47 +0200 Subject: Add "DSP iCE board" to board list --- docs/index.html | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/index.html b/docs/index.html index ebf424d..be02756 100644 --- a/docs/index.html +++ b/docs/index.html @@ -515,6 +515,7 @@ Links to related projects. Contact me at clifford@clifford.at if you have an int

  • eCow-Logic pico-ITX Lattice ICE40 board
  • Nandland Go Board
  • myStorm board (iCE40 + STM32) +
  • DSP iCE board (another iCE40 + STM32 board)

    Lectures and Tutorials

    -- cgit v1.2.3