From 96b1a90f28db2e55ce1c4026ec6b585498f26127 Mon Sep 17 00:00:00 2001 From: Piotr Esden-Tempski Date: Thu, 16 Aug 2018 14:22:53 -0700 Subject: Added more code comments. Main point was to group the code into three logical segments: * MPSSE/FTDI defines and functions * FLASH defines and functions * iceprog implementation core While I was at it I also added a few comments for stuff that was not immediately obvious what it does. --- iceprog/iceprog.c | 55 +++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 39 insertions(+), 16 deletions(-) (limited to 'iceprog') diff --git a/iceprog/iceprog.c b/iceprog/iceprog.c index 29d4c22..5a29733 100644 --- a/iceprog/iceprog.c +++ b/iceprog/iceprog.c @@ -2,6 +2,7 @@ * iceprog -- simple programming tool for FTDI-based Lattice iCE programmers * * Copyright (C) 2015 Clifford Wolf + * Copyright (C) 2018 Piotr Esden-Tempski * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -19,7 +20,6 @@ * ------------------- * http://www.latticesemi.com/~/media/Documents/UserManuals/EI/icestickusermanual.pdf * http://www.micron.com/~/media/documents/products/data-sheet/nor-flash/serial-nor/n25q/n25q_32mb_3v_65nm.pdf - * http://www.ftdichip.com/Support/Documents/AppNotes/AN_108_Command_Processor_for_MPSSE_and_MCU_Host_Bus_Emulation_Modes.pdf */ #define _GNU_SOURCE @@ -36,6 +36,10 @@ #include #include +// --------------------------------------------------------- +// MPSSE / FTDI definitions +// --------------------------------------------------------- + static struct ftdi_context ftdic; static bool ftdic_open = false; static bool verbose = false; @@ -76,6 +80,10 @@ enum mpsse_cmd MC_CPU_WE = 0x93, /* CPUMode write extended address */ }; +// --------------------------------------------------------- +// FLASH definitions +// --------------------------------------------------------- + /* Transfer Command bits */ /* All byte based commands consist of: @@ -146,6 +154,7 @@ enum flash_cmd { FC_GBL = 0x7E, /* Global Block Lock */ FC_GBU = 0x98, /* Global Block Unlock */ FC_RBL = 0x3D, /* Read Block Lock */ + FC_RPR = 0x3C, /* Read Sector Protection Registers (adesto) */ FC_IBL = 0x36, /* Individual Block Lock */ FC_IBU = 0x39, /* Individual Block Unlock */ FC_EPS = 0x75, /* Erase / Program Suspend */ @@ -156,6 +165,10 @@ enum flash_cmd { FC_RESET = 0x99, /* Reset Device */ }; +// --------------------------------------------------------- +// MPSSE / FTDI function implementations +// --------------------------------------------------------- + static void check_rx() { while (1) { @@ -270,6 +283,10 @@ static int get_cdone() return (data & 0x40) != 0; } +// --------------------------------------------------------- +// FLASH function implementations +// --------------------------------------------------------- + static void flash_read_id() { // fprintf(stderr, "read flash ID..\n"); @@ -399,7 +416,7 @@ static void flash_disable_protection() { fprintf(stderr, "disable flash protection...\n"); - //WRSR 0x00 + //WRSR 0x00 uint8_t data[2] = { 0x01, 0x00 }; set_gpio(0, 0); xfer_spi(data, 2); @@ -419,6 +436,9 @@ static void flash_disable_protection() } +// --------------------------------------------------------- +// iceprog implementation +// --------------------------------------------------------- static void help(const char *progname) { @@ -519,14 +539,15 @@ int main(int argc, char **argv) {NULL, 0, NULL, 0} }; + /* Decode command line parameters */ int opt; char *endptr; while ((opt = getopt_long(argc, argv, "d:I:rR:e:o:cbnStvp", long_options, NULL)) != -1) { switch (opt) { - case 'd': + case 'd': /* device string */ devstr = optarg; break; - case 'I': + case 'I': /* FTDI Chip interface select */ if (!strcmp(optarg, "A")) ifnum = INTERFACE_A; else if (!strcmp(optarg, "B")) @@ -540,10 +561,10 @@ int main(int argc, char **argv) return EXIT_FAILURE; } break; - case 'r': + case 'r': /* Read 256 bytes to file */ read_mode = true; break; - case 'R': + case 'R': /* Read n bytes to file */ read_mode = true; read_size = strtol(optarg, &endptr, 0); if (*endptr == '\0') @@ -557,7 +578,7 @@ int main(int argc, char **argv) return EXIT_FAILURE; } break; - case 'e': + case 'e': /* Erase blocks as if we were writing n bytes */ erase_mode = true; erase_size = strtol(optarg, &endptr, 0); if (*endptr == '\0') @@ -571,7 +592,7 @@ int main(int argc, char **argv) return EXIT_FAILURE; } break; - case 'o': + case 'o': /* set address offset */ rw_offset = strtol(optarg, &endptr, 0); if (*endptr == '\0') /* ok */; @@ -584,25 +605,25 @@ int main(int argc, char **argv) return EXIT_FAILURE; } break; - case 'c': + case 'c': /* do not write just check */ check_mode = true; break; - case 'b': + case 'b': /* bulk erase before writing */ bulk_erase = true; break; - case 'n': + case 'n': /* do not erase before writing */ dont_erase = true; break; - case 'S': + case 'S': /* write to sram directly */ prog_sram = true; break; - case 't': + case 't': /* just read flash id */ test_mode = true; break; - case 'v': + case 'v': /* provide verbose output */ verbose = true; break; - case 'p': + case 'p': /* disable flash protect before erase/write */ disable_protect = true; break; case -2: @@ -615,6 +636,8 @@ int main(int argc, char **argv) } } + /* Make sure that the combination of provided parameters makes sense */ + if (read_mode + erase_mode + check_mode + prog_sram + test_mode > 1) { fprintf(stderr, "%s: options `-r'/`-R', `-e`, `-c', `-S', and `-t' are mutually exclusive\n", my_name); return EXIT_FAILURE; @@ -877,7 +900,7 @@ int main(int argc, char **argv) fprintf(stderr, "cdone: %s\n", get_cdone() ? "high" : "low"); } - else + else /* program flash */ { // --------------------------------------------------------- // Reset -- cgit v1.2.3 From ee7eae0be87cc238e6b4be409d5fc3c711438e05 Mon Sep 17 00:00:00 2001 From: Piotr Esden-Tempski Date: Sat, 18 Aug 2018 14:38:02 -0700 Subject: Added easier to read chip_select/reset functions. Just calling a gpio function with two numbers was not very self explanatory. The functions now refer to the actual indended action, chip (de)select for flash, reset and chip (de)select for sram. Reading the code and understanding what steps are taken should be easier now. --- iceprog/iceprog.c | 86 ++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 60 insertions(+), 26 deletions(-) (limited to 'iceprog') diff --git a/iceprog/iceprog.c b/iceprog/iceprog.c index 5a29733..c0a1406 100644 --- a/iceprog/iceprog.c +++ b/iceprog/iceprog.c @@ -287,14 +287,48 @@ static int get_cdone() // FLASH function implementations // --------------------------------------------------------- +// the FPGA reset is released so also FLASH chip select should be deasserted +static void flash_release_reset() +{ + set_gpio(1, 1); +} + +// FLASH chip select assert/deassert +// should only happen while FPGA reset is asserted +static void flash_chip_select(bool assert) +{ + if(assert) + set_gpio(0, 0); + else + set_gpio(1, 0); +} + +// SRAM reset is pretty much the same as flash_chip_select(true) +// For ease of code reading we use this function instead +static void sram_reset() +{ + // Asserting chip select and reset lines + set_gpio(0, 0); +} + +// When accessing FPGA SRAM the reset should be released +static void sram_chip_select(bool assert) +{ + if(assert) + set_gpio(0, 1); + else + set_gpio(1, 1); +} + static void flash_read_id() { // fprintf(stderr, "read flash ID..\n"); uint8_t data[21] = { FC_JEDECID }; - set_gpio(0, 0); + + flash_chip_select(true); xfer_spi(data, 21); - set_gpio(1, 0); + flash_chip_select(false); fprintf(stderr, "flash ID:"); for (int i = 1; i < 21; i++) @@ -305,17 +339,17 @@ static void flash_read_id() static void flash_power_up() { uint8_t data[1] = { FC_RPD }; - set_gpio(0, 0); + flash_chip_select(true); xfer_spi(data, 1); - set_gpio(1, 0); + flash_chip_select(false); } static void flash_power_down() { uint8_t data[1] = { FC_PD }; - set_gpio(0, 0); + flash_chip_select(true); xfer_spi(data, 1); - set_gpio(1, 0); + flash_chip_select(false); } static void flash_write_enable() @@ -324,9 +358,9 @@ static void flash_write_enable() fprintf(stderr, "write enable..\n"); uint8_t data[1] = { FC_WE }; - set_gpio(0, 0); + flash_chip_select(true); xfer_spi(data, 1); - set_gpio(1, 0); + flash_chip_select(false); } static void flash_bulk_erase() @@ -334,9 +368,9 @@ static void flash_bulk_erase() fprintf(stderr, "bulk erase..\n"); uint8_t data[1] = { FC_CE }; - set_gpio(0, 0); + flash_chip_select(true); xfer_spi(data, 1); - set_gpio(1, 0); + flash_chip_select(false); } static void flash_64kB_sector_erase(int addr) @@ -345,9 +379,9 @@ static void flash_64kB_sector_erase(int addr) uint8_t command[4] = { FC_BE64, (uint8_t)(addr >> 16), (uint8_t)(addr >> 8), (uint8_t)addr }; - set_gpio(0, 0); + flash_chip_select(true); send_spi(command, 4); - set_gpio(1, 0); + flash_chip_select(false); } static void flash_prog(int addr, uint8_t *data, int n) @@ -357,10 +391,10 @@ static void flash_prog(int addr, uint8_t *data, int n) uint8_t command[4] = { FC_PP, (uint8_t)(addr >> 16), (uint8_t)(addr >> 8), (uint8_t)addr }; - set_gpio(0, 0); + flash_chip_select(true); send_spi(command, 4); send_spi(data, n); - set_gpio(1, 0); + flash_chip_select(false); if (verbose) for (int i = 0; i < n; i++) @@ -374,11 +408,11 @@ static void flash_read(int addr, uint8_t *data, int n) uint8_t command[4] = { FC_RD, (uint8_t)(addr >> 16), (uint8_t)(addr >> 8), (uint8_t)addr }; - set_gpio(0, 0); + flash_chip_select(true); send_spi(command, 4); memset(data, 0, n); xfer_spi(data, n); - set_gpio(1, 0); + flash_chip_select(false); if (verbose) for (int i = 0; i < n; i++) @@ -418,18 +452,18 @@ static void flash_disable_protection() //WRSR 0x00 uint8_t data[2] = { 0x01, 0x00 }; - set_gpio(0, 0); + flash_chip_select(true); xfer_spi(data, 2); - set_gpio(1, 0); + flash_chip_select(false); flash_wait(); //RDSR data[0] = 0x5; - set_gpio(0, 0);\ + flash_chip_select(true); xfer_spi(data, 2); - set_gpio(1, 0); + flash_chip_select(false); if(data[1] != 0x00) fprintf(stderr, "failed to disable protection, SR now equal to 0x%02x (expected 0x00)\n", data[1]); @@ -833,7 +867,7 @@ int main(int argc, char **argv) fprintf(stderr, "cdone: %s\n", get_cdone() ? "high" : "low"); - set_gpio(1, 1); + flash_release_reset(); usleep(100000); @@ -841,7 +875,7 @@ int main(int argc, char **argv) { fprintf(stderr, "reset..\n"); - set_gpio(1, 0); + flash_chip_select(false); usleep(250000); fprintf(stderr, "cdone: %s\n", get_cdone() ? "high" : "low"); @@ -852,7 +886,7 @@ int main(int argc, char **argv) flash_power_down(); - set_gpio(1, 1); + flash_release_reset(); usleep(250000); fprintf(stderr, "cdone: %s\n", get_cdone() ? "high" : "low"); @@ -865,10 +899,10 @@ int main(int argc, char **argv) fprintf(stderr, "reset..\n"); - set_gpio(0, 0); + sram_reset(); usleep(100); - set_gpio(0, 1); + sram_chip_select(true); usleep(2000); fprintf(stderr, "cdone: %s\n", get_cdone() ? "high" : "low"); @@ -908,7 +942,7 @@ int main(int argc, char **argv) fprintf(stderr, "reset..\n"); - set_gpio(1, 0); + flash_chip_select(false); usleep(250000); fprintf(stderr, "cdone: %s\n", get_cdone() ? "high" : "low"); -- cgit v1.2.3 From 3ba1acf31bc0ee83d8b545af03e144cb942d1f88 Mon Sep 17 00:00:00 2001 From: Piotr Esden-Tempski Date: Sat, 18 Aug 2018 14:46:12 -0700 Subject: Replaced some more magic numbers with FLASH command IDs. I missed those the first time around. --- iceprog/iceprog.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'iceprog') diff --git a/iceprog/iceprog.c b/iceprog/iceprog.c index c0a1406..ca3ce48 100644 --- a/iceprog/iceprog.c +++ b/iceprog/iceprog.c @@ -450,16 +450,16 @@ static void flash_disable_protection() { fprintf(stderr, "disable flash protection...\n"); - //WRSR 0x00 - uint8_t data[2] = { 0x01, 0x00 }; + // Write Status Register 1 <- 0x00 + uint8_t data[2] = { FC_WSR1, 0x00 }; flash_chip_select(true); xfer_spi(data, 2); flash_chip_select(false); flash_wait(); - //RDSR - data[0] = 0x5; + // Read Status Register 1 + data[0] = FC_RSR1; flash_chip_select(true); xfer_spi(data, 2); -- cgit v1.2.3 From f4ff8f76309d82773881a24cad9d13116ce36025 Mon Sep 17 00:00:00 2001 From: Piotr Esden-Tempski Date: Sat, 18 Aug 2018 15:26:37 -0700 Subject: Improved JEDEC ID read function. The function now checks how long the extended JEDEC ID field is for the particular FLASH chip and only reads the amount provided by the chip. --- iceprog/iceprog.c | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) (limited to 'iceprog') diff --git a/iceprog/iceprog.c b/iceprog/iceprog.c index ca3ce48..42abd86 100644 --- a/iceprog/iceprog.c +++ b/iceprog/iceprog.c @@ -322,16 +322,42 @@ static void sram_chip_select(bool assert) static void flash_read_id() { - // fprintf(stderr, "read flash ID..\n"); + /* JEDEC ID structure: + * Byte No. | Data Type + * ---------+---------- + * 0 | FC_JEDECID Request Command + * 1 | MFG ID + * 2 | Dev ID 1 + * 3 | Dev ID 2 + * 4 | Ext Dev Str Len + */ - uint8_t data[21] = { FC_JEDECID }; + uint8_t data[260] = { FC_JEDECID }; + int len = 5; // command + 4 response bytes + + if (verbose) fprintf(stderr, "read flash ID..\n"); flash_chip_select(true); - xfer_spi(data, 21); + + // Write command and read first 4 bytes + xfer_spi(data, len); + + if(data[4] == 0xFF) + fprintf(stderr, "Extended Device String Length is 0xFF, " + "this is likely a read error. Ignorig...\n"); + else { + // Read extended JEDEC ID bytes + if(data[4] != 0) { + len += data[4]; + xfer_spi(data + 5, len - 5); + } + } + flash_chip_select(false); + // TODO: Add full decode of the JEDEC ID. fprintf(stderr, "flash ID:"); - for (int i = 1; i < 21; i++) + for (int i = 1; i < len; i++) fprintf(stderr, " 0x%02X", data[i]); fprintf(stderr, "\n"); } -- cgit v1.2.3 From 20ef4efd8b6c969697784a31b3a5f374d882877a Mon Sep 17 00:00:00 2001 From: Piotr Esden-Tempski Date: Sat, 18 Aug 2018 15:29:11 -0700 Subject: Added the FTDI cable pinout for reference. --- iceprog/iceprog.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'iceprog') diff --git a/iceprog/iceprog.c b/iceprog/iceprog.c index 42abd86..7029f00 100644 --- a/iceprog/iceprog.c +++ b/iceprog/iceprog.c @@ -40,6 +40,19 @@ // MPSSE / FTDI definitions // --------------------------------------------------------- +/* FTDI bank pinout typically used for iCE dev boards + * BUS IO | Signal | Control + * -------+--------+-------------- + * xDBUS0 | SCK | MPSSE + * xDBUS1 | MOSI | MPSSE + * xDBUS2 | MISO | MPSSE + * xDBUS3 | nc | + * xDBUS4 | CS | GPIO + * xDBUS5 | nc | + * xDBUS6 | CDONE | GPIO + * xDBUS7 | CRESET | GPIO + */ + static struct ftdi_context ftdic; static bool ftdic_open = false; static bool verbose = false; -- cgit v1.2.3 From 868ac2d93f124fe54034f26f1bc43f7b53676028 Mon Sep 17 00:00:00 2001 From: Piotr Esden-Tempski Date: Sat, 18 Aug 2018 15:45:37 -0700 Subject: Add a function to read and decode the status register. Very useful for debugging purposes. ;) --- iceprog/iceprog.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 66 insertions(+), 2 deletions(-) (limited to 'iceprog') diff --git a/iceprog/iceprog.c b/iceprog/iceprog.c index 7029f00..a004e28 100644 --- a/iceprog/iceprog.c +++ b/iceprog/iceprog.c @@ -391,15 +391,79 @@ static void flash_power_down() flash_chip_select(false); } +static uint8_t flash_read_status() +{ + uint8_t data[2] = { FC_RSR1 }; + + flash_chip_select(true); + xfer_spi(data, 2); + flash_chip_select(false); + + if(verbose) { + fprintf(stderr, "SR1: 0x%02X\n", data[1]); + fprintf(stderr, " - SPRL: %s\n", + ((data[1] & (1 << 7)) == 0) ? + "unlocked" : + "locked"); + fprintf(stderr, " - SPM: %s\n", + ((data[1] & (1 << 6)) == 0) ? + "Byte/Page Prog Mode" : + "Sequential Prog Mode"); + fprintf(stderr, " - EPE: %s\n", + ((data[1] & (1 << 5)) == 0) ? + "Erase/Prog success" : + "Erase/Prog error"); + fprintf(stderr, "- SPM: %s\n", + ((data[1] & (1 << 4)) == 0) ? + "~WP asserted" : + "~WP deasserted"); + fprintf(stderr, " - SWP: "); + switch((data[1] >> 2) & 0x3) { + case 0: + fprintf(stderr, "All sectors unprotected\n"); + break; + case 1: + fprintf(stderr, "Some sectors protected\n"); + break; + case 2: + fprintf(stderr, "Reserved (xxxx 10xx)\n"); + break; + case 3: + fprintf(stderr, "All sectors protected\n"); + break; + } + fprintf(stderr, " - WEL: %s\n", + ((data[1] & (1 << 1)) == 0) ? + "Not write enabled" : + "Write enabled"); + fprintf(stderr, " - ~RDY: %s\n", + ((data[1] & (1 << 0)) == 0) ? + "Ready" : + "Busy"); + } + + usleep(1000); + + return data[1]; +} + static void flash_write_enable() { - if (verbose) - fprintf(stderr, "write enable..\n"); + if (verbose) { + fprintf(stderr, "status before enable:\n"); + flash_read_status(); + } + if (verbose) fprintf(stderr, "write enable..\n"); uint8_t data[1] = { FC_WE }; flash_chip_select(true); xfer_spi(data, 1); flash_chip_select(false); + + if (verbose) { + fprintf(stderr, "status after enable:\n"); + flash_read_status(); + } } static void flash_bulk_erase() -- cgit v1.2.3 From 9a58588772523ec68df51443500888e5f707ad5d Mon Sep 17 00:00:00 2001 From: Piotr Esden-Tempski Date: Sat, 18 Aug 2018 16:13:27 -0700 Subject: Slightly more robust flash wait function. In cases when the FLASH chip is bit flaky and the status register "bounces" this flash_wait should be able to perform a bit better. Also added more verbose output around block erase. --- iceprog/iceprog.c | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) (limited to 'iceprog') diff --git a/iceprog/iceprog.c b/iceprog/iceprog.c index a004e28..ad2bb6a 100644 --- a/iceprog/iceprog.c +++ b/iceprog/iceprog.c @@ -527,26 +527,43 @@ static void flash_wait() if (verbose) fprintf(stderr, "waiting.."); + int count = 0; while (1) { uint8_t data[2] = { FC_RSR1 }; - set_gpio(0, 0); + flash_chip_select(true); xfer_spi(data, 2); - set_gpio(1, 0); - - if ((data[1] & 0x01) == 0) - break; + flash_chip_select(false); - if (verbose) { - fprintf(stderr, "."); - fflush(stdout); + if ((data[1] & 0x01) == 0) { + if(count < 2) { + count++; + if (verbose) { + fprintf(stderr, "r"); + fflush(stderr); + } + } else { + if (verbose) { + fprintf(stderr, "R"); + fflush(stderr); + } + break; + } + } else { + if (verbose) { + fprintf(stderr, "."); + fflush(stderr); + } + count = 0; } + usleep(1000); } if (verbose) fprintf(stderr, "\n"); + } static void flash_disable_protection() @@ -973,7 +990,6 @@ int main(int argc, char **argv) flash_release_reset(); usleep(100000); - if (test_mode) { fprintf(stderr, "reset..\n"); @@ -1085,6 +1101,10 @@ int main(int argc, char **argv) for (int addr = begin_addr; addr < end_addr; addr += 0x10000) { flash_write_enable(); flash_64kB_sector_erase(addr); + if (verbose) { + fprintf(stderr, "Status after block erase:\n"); + flash_read_status(); + } flash_wait(); } } -- cgit v1.2.3 From 65ae583b3ebb9fa4a65cf6c98ddac90b6f748e10 Mon Sep 17 00:00:00 2001 From: Clifford Wolf Date: Sun, 19 Aug 2018 15:12:51 +0200 Subject: iceprog coding style, don't use "assert" as variable name Signed-off-by: Clifford Wolf --- iceprog/iceprog.c | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) (limited to 'iceprog') diff --git a/iceprog/iceprog.c b/iceprog/iceprog.c index ad2bb6a..6bbda1e 100644 --- a/iceprog/iceprog.c +++ b/iceprog/iceprog.c @@ -308,9 +308,9 @@ static void flash_release_reset() // FLASH chip select assert/deassert // should only happen while FPGA reset is asserted -static void flash_chip_select(bool assert) +static void flash_chip_select(bool en) { - if(assert) + if (en) set_gpio(0, 0); else set_gpio(1, 0); @@ -325,9 +325,9 @@ static void sram_reset() } // When accessing FPGA SRAM the reset should be released -static void sram_chip_select(bool assert) +static void sram_chip_select(bool en) { - if(assert) + if (en) set_gpio(0, 1); else set_gpio(1, 1); @@ -348,19 +348,20 @@ static void flash_read_id() uint8_t data[260] = { FC_JEDECID }; int len = 5; // command + 4 response bytes - if (verbose) fprintf(stderr, "read flash ID..\n"); + if (verbose) + fprintf(stderr, "read flash ID..\n"); flash_chip_select(true); // Write command and read first 4 bytes xfer_spi(data, len); - if(data[4] == 0xFF) + if (data[4] == 0xFF) fprintf(stderr, "Extended Device String Length is 0xFF, " "this is likely a read error. Ignorig...\n"); else { // Read extended JEDEC ID bytes - if(data[4] != 0) { + if (data[4] != 0) { len += data[4]; xfer_spi(data + 5, len - 5); } @@ -399,7 +400,7 @@ static uint8_t flash_read_status() xfer_spi(data, 2); flash_chip_select(false); - if(verbose) { + if (verbose) { fprintf(stderr, "SR1: 0x%02X\n", data[1]); fprintf(stderr, " - SPRL: %s\n", ((data[1] & (1 << 7)) == 0) ? @@ -454,7 +455,9 @@ static void flash_write_enable() flash_read_status(); } - if (verbose) fprintf(stderr, "write enable..\n"); + if (verbose) + fprintf(stderr, "write enable..\n"); + uint8_t data[1] = { FC_WE }; flash_chip_select(true); xfer_spi(data, 1); @@ -537,7 +540,7 @@ static void flash_wait() flash_chip_select(false); if ((data[1] & 0x01) == 0) { - if(count < 2) { + if (count < 2) { count++; if (verbose) { fprintf(stderr, "r"); @@ -585,7 +588,7 @@ static void flash_disable_protection() xfer_spi(data, 2); flash_chip_select(false); - if(data[1] != 0x00) + if (data[1] != 0x00) fprintf(stderr, "failed to disable protection, SR now equal to 0x%02x (expected 0x00)\n", data[1]); } -- cgit v1.2.3 From 22e8b744dace15be42459ab00f5f140aa5368786 Mon Sep 17 00:00:00 2001 From: Clifford Wolf Date: Sun, 19 Aug 2018 15:21:04 +0200 Subject: Minor style changes in iceprog Signed-off-by: Clifford Wolf --- iceprog/iceprog.c | 80 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 41 insertions(+), 39 deletions(-) (limited to 'iceprog') diff --git a/iceprog/iceprog.c b/iceprog/iceprog.c index 6bbda1e..550a23d 100644 --- a/iceprog/iceprog.c +++ b/iceprog/iceprog.c @@ -20,6 +20,7 @@ * ------------------- * http://www.latticesemi.com/~/media/Documents/UserManuals/EI/icestickusermanual.pdf * http://www.micron.com/~/media/documents/products/data-sheet/nor-flash/serial-nor/n25q/n25q_32mb_3v_65nm.pdf + * http://www.ftdichip.com/Support/Documents/AppNotes/AN_108_Command_Processor_for_MPSSE_and_MCU_Host_Bus_Emulation_Modes.pdf */ #define _GNU_SOURCE @@ -306,17 +307,20 @@ static void flash_release_reset() set_gpio(1, 1); } -// FLASH chip select assert/deassert +// FLASH chip select assert // should only happen while FPGA reset is asserted -static void flash_chip_select(bool en) +static void flash_chip_select() { - if (en) - set_gpio(0, 0); - else - set_gpio(1, 0); + set_gpio(0, 0); } -// SRAM reset is pretty much the same as flash_chip_select(true) +// FLASH chip select deassert +static void flash_chip_deselect() +{ + set_gpio(1, 0); +} + +// SRAM reset is the same as flash_chip_select() // For ease of code reading we use this function instead static void sram_reset() { @@ -324,13 +328,11 @@ static void sram_reset() set_gpio(0, 0); } +// SRAM chip select assert // When accessing FPGA SRAM the reset should be released -static void sram_chip_select(bool en) +static void sram_chip_select() { - if (en) - set_gpio(0, 1); - else - set_gpio(1, 1); + set_gpio(0, 1); } static void flash_read_id() @@ -351,7 +353,7 @@ static void flash_read_id() if (verbose) fprintf(stderr, "read flash ID..\n"); - flash_chip_select(true); + flash_chip_select(); // Write command and read first 4 bytes xfer_spi(data, len); @@ -367,7 +369,7 @@ static void flash_read_id() } } - flash_chip_select(false); + flash_chip_deselect(); // TODO: Add full decode of the JEDEC ID. fprintf(stderr, "flash ID:"); @@ -379,26 +381,26 @@ static void flash_read_id() static void flash_power_up() { uint8_t data[1] = { FC_RPD }; - flash_chip_select(true); + flash_chip_select(); xfer_spi(data, 1); - flash_chip_select(false); + flash_chip_deselect(); } static void flash_power_down() { uint8_t data[1] = { FC_PD }; - flash_chip_select(true); + flash_chip_select(); xfer_spi(data, 1); - flash_chip_select(false); + flash_chip_deselect(); } static uint8_t flash_read_status() { uint8_t data[2] = { FC_RSR1 }; - flash_chip_select(true); + flash_chip_select(); xfer_spi(data, 2); - flash_chip_select(false); + flash_chip_deselect(); if (verbose) { fprintf(stderr, "SR1: 0x%02X\n", data[1]); @@ -459,9 +461,9 @@ static void flash_write_enable() fprintf(stderr, "write enable..\n"); uint8_t data[1] = { FC_WE }; - flash_chip_select(true); + flash_chip_select(); xfer_spi(data, 1); - flash_chip_select(false); + flash_chip_deselect(); if (verbose) { fprintf(stderr, "status after enable:\n"); @@ -474,9 +476,9 @@ static void flash_bulk_erase() fprintf(stderr, "bulk erase..\n"); uint8_t data[1] = { FC_CE }; - flash_chip_select(true); + flash_chip_select(); xfer_spi(data, 1); - flash_chip_select(false); + flash_chip_deselect(); } static void flash_64kB_sector_erase(int addr) @@ -485,9 +487,9 @@ static void flash_64kB_sector_erase(int addr) uint8_t command[4] = { FC_BE64, (uint8_t)(addr >> 16), (uint8_t)(addr >> 8), (uint8_t)addr }; - flash_chip_select(true); + flash_chip_select(); send_spi(command, 4); - flash_chip_select(false); + flash_chip_deselect(); } static void flash_prog(int addr, uint8_t *data, int n) @@ -497,10 +499,10 @@ static void flash_prog(int addr, uint8_t *data, int n) uint8_t command[4] = { FC_PP, (uint8_t)(addr >> 16), (uint8_t)(addr >> 8), (uint8_t)addr }; - flash_chip_select(true); + flash_chip_select(); send_spi(command, 4); send_spi(data, n); - flash_chip_select(false); + flash_chip_deselect(); if (verbose) for (int i = 0; i < n; i++) @@ -514,11 +516,11 @@ static void flash_read(int addr, uint8_t *data, int n) uint8_t command[4] = { FC_RD, (uint8_t)(addr >> 16), (uint8_t)(addr >> 8), (uint8_t)addr }; - flash_chip_select(true); + flash_chip_select(); send_spi(command, 4); memset(data, 0, n); xfer_spi(data, n); - flash_chip_select(false); + flash_chip_deselect(); if (verbose) for (int i = 0; i < n; i++) @@ -535,9 +537,9 @@ static void flash_wait() { uint8_t data[2] = { FC_RSR1 }; - flash_chip_select(true); + flash_chip_select(); xfer_spi(data, 2); - flash_chip_select(false); + flash_chip_deselect(); if ((data[1] & 0x01) == 0) { if (count < 2) { @@ -575,18 +577,18 @@ static void flash_disable_protection() // Write Status Register 1 <- 0x00 uint8_t data[2] = { FC_WSR1, 0x00 }; - flash_chip_select(true); + flash_chip_select(); xfer_spi(data, 2); - flash_chip_select(false); + flash_chip_deselect(); flash_wait(); // Read Status Register 1 data[0] = FC_RSR1; - flash_chip_select(true); + flash_chip_select(); xfer_spi(data, 2); - flash_chip_select(false); + flash_chip_deselect(); if (data[1] != 0x00) fprintf(stderr, "failed to disable protection, SR now equal to 0x%02x (expected 0x00)\n", data[1]); @@ -997,7 +999,7 @@ int main(int argc, char **argv) { fprintf(stderr, "reset..\n"); - flash_chip_select(false); + flash_chip_deselect(); usleep(250000); fprintf(stderr, "cdone: %s\n", get_cdone() ? "high" : "low"); @@ -1024,7 +1026,7 @@ int main(int argc, char **argv) sram_reset(); usleep(100); - sram_chip_select(true); + sram_chip_select(); usleep(2000); fprintf(stderr, "cdone: %s\n", get_cdone() ? "high" : "low"); @@ -1064,7 +1066,7 @@ int main(int argc, char **argv) fprintf(stderr, "reset..\n"); - flash_chip_select(false); + flash_chip_deselect(); usleep(250000); fprintf(stderr, "cdone: %s\n", get_cdone() ? "high" : "low"); -- cgit v1.2.3