From 6bc4bab1584dcc3315f6a20592d442f688eb0dc9 Mon Sep 17 00:00:00 2001 From: Jens Jensen Date: Thu, 17 Dec 2015 17:08:00 -0600 Subject: [PATCH 01/16] fixing read on buspirate --- Makefile | 3 ++- bp.h | 1 + nrfprog.c | 80 ++++++++++++++++++++++++++++++++++--------------------- 3 files changed, 53 insertions(+), 31 deletions(-) diff --git a/Makefile b/Makefile index 28bcd6b..ff0facc 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,9 @@ all: nrfprog CFLAGS=-ggdb -Wall +DEBUG := $(if $(DEBUG),-DDEBUG_PRINT) nrfprog: nrfprog.c bp.h nrf24le1.h - gcc ${CFLAGS} -o nrfprog nrfprog.c + gcc ${CFLAGS} -o nrfprog nrfprog.c $(DEBUG) clean: rm -f nrfprog diff --git a/bp.h b/bp.h index a0518d7..e2ae701 100644 --- a/bp.h +++ b/bp.h @@ -35,6 +35,7 @@ #define BP_SPI_CFG_CKE 0x02 #define BP_SPI_CFG_SMP 0x01 #define BP_SPI_WR_RD 0x04 + #define BP_SPI_WR_RD_NO_CS 0x05 #define BP_I2C 0x02 #define BP_UART 0x03 diff --git a/nrfprog.c b/nrfprog.c index b5eaee2..320591f 100644 --- a/nrfprog.c +++ b/nrfprog.c @@ -72,8 +72,11 @@ void ser_write(int fd, uint8_t *stream, uint16_t sz) { int ret, ct; uint8_t c; TRACE_MSG; - + tcdrain(fd); // Write out one byte at a time +#ifdef DEBUG_PRINT + printf("ser_write: "); +#endif for(ct=0; ct 25) { + if(err_ct > 1) { printf("Unable to read!\n"); break; } } #ifdef DEBUG_PRINT - printf("0x%02X ", buf[pos]); + printf("%02X ", buf[pos]); + if (pos % 16 == 15) { + printf("\n"); + } #endif } #ifdef DEBUG_PRINT printf("\n"); #endif + check_resp(fd, "SPI Read Complete"); + return; } @@ -392,19 +408,16 @@ void spi_wait(int fd, uint8_t *cmd, uint16_t len, char *resp_exp, uint16_t resp uint8_t bytes[3]; char *buf; TRACE_MSG; - buf = (char *)malloc(resp_len * sizeof(char)); for(ct = 0; ct < 25; ct++) { // Initiate the WR_RD operation ser_cmd(fd, BP_SPI_WR_RD); - // Send the read/write length and command bytes write_bytes = len; read_bytes = resp_len; s2b(write_bytes, bytes); ser_write(fd, (uint8_t *) bytes, 2); s2b(read_bytes, bytes); ser_write(fd, (uint8_t *) bytes, 2); - // Send the command ser_write(fd, cmd, len); @@ -438,7 +451,6 @@ void spi_write(int fd, uint8_t *buf, uint16_t len, int start_addr, char *err) { uint16_t write_bytes, read_bytes; uint8_t bytes[3]; TRACE_MSG; - // Set WREN to enable writing bytes[0] = NRF24_SPI_WREN; spi_cmd(fd, bytes, 1, "Enable Writing"); @@ -491,6 +503,7 @@ void read_hex(int fd, int ip, char *fn) { spi_cmd(fd, cmd, 2, "FSR Register Write"); // Do the Read Operation + printf("starting read operation\n"); if(ip) read_bytes = NRF24_INFO_SZ; else @@ -582,9 +595,16 @@ int write_hex(int fd, char *fn) { int main(int argc, char **argv) { int fd; TRACE_MSG; + if (argc == 0) { + printf("usage: nrfprog PORT [FILE]\n"); + exit(1); + } + + char *port = argv[1]; + char *fname = argv[2]; printf("Opening the Bus Pirate UART\n"); - fd = ser_open("/dev/ttyUSB0"); + fd = ser_open(port); printf("Setting the Bus Pirate to Binary Mode\n"); ser_bp_bin(fd); @@ -596,15 +616,15 @@ int main(int argc, char **argv) { read_hex(fd, 1, "info_page.dat"); // Read/Write the Hex File, if requested - if(argc > 1) { + if(argc > 2) { // If we can't open the file, we're reading out the memory contents into it - if(access(argv[1], R_OK)) { - printf("Reading from the device to %s\n", argv[1]); - read_hex(fd, 0, argv[1]); + if(access(fname, R_OK)) { + printf("Reading from the device to %s\n", fname); + read_hex(fd, 0, fname); // Otherwise, write it out } else { - printf("Writing %s to the device\n", argv[1]); - if(write_hex(fd, argv[1]) == 0) + printf("Writing %s to the device\n", fname); + if(write_hex(fd, fname) == 0) printf("Write Verified\n"); } } From 56dab75a362c38e122db7fc589af5e44d9795859 Mon Sep 17 00:00:00 2001 From: Jens Jensen Date: Thu, 17 Dec 2015 17:16:20 -0600 Subject: [PATCH 02/16] update readme --- README | 9 --------- README.md | 26 ++++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 9 deletions(-) delete mode 100644 README create mode 100644 README.md diff --git a/README b/README deleted file mode 100644 index 2312891..0000000 --- a/README +++ /dev/null @@ -1,9 +0,0 @@ -This is a simple programmer for the nRF24LE1 using the Bus Pirate as the SPI -device. - -Type "make" to build the source. - -Running the program directly (./nrfprog) will dump the InfoPage. Put this some -where safe, as this contains information unique to your nRF24LE1. Running the -program, but passing a .hex file as a parameter will make the programmer write -the .hex file to the device. diff --git a/README.md b/README.md new file mode 100644 index 0000000..3b0c15d --- /dev/null +++ b/README.md @@ -0,0 +1,26 @@ +This is a simple programmer for the nRF24LE1 using the Bus Pirate as the SPI +device. + +## building +Type "make" to build the source. + +## usage +`nrfprog PORT [filename]` +Running the program directly (./nrfprog) will dump the InfoPage. Put this some +where safe, as this contains information unique to your nRF24LE1. Running the +program, but passing a .hex file as a parameter will make the programmer write +the .hex file to the device. + +## debugging +for debug build, use `DEBUG=1 make` + +## references + * bus pirate binary modes: http://dangerousprototypes.com/docs/SPI_%28binary%29 + * nrf24le1 datasheet: https://www.nordicsemi.com/kor/nordic/content_download/2443/29442/file/nRF24LE1_Product_Specification_rev1_6.pdf + +## mods +forked from repo JoseJX/nrfprog +mods by zerog2k: + * fixes to buspirate not reading info page and main flash blocks + * more debugging + * pass in serial port as param From 82407d01a8fb04a63b16720e3a28ab7ce1059285 Mon Sep 17 00:00:00 2001 From: Jens Jensen Date: Thu, 17 Dec 2015 17:20:15 -0600 Subject: [PATCH 03/16] readme update --- README.md | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 3b0c15d..cca911c 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ +# NRF24LE1 BusPirate programmer This is a simple programmer for the nRF24LE1 using the Bus Pirate as the SPI device. @@ -6,6 +7,7 @@ Type "make" to build the source. ## usage `nrfprog PORT [filename]` + Running the program directly (./nrfprog) will dump the InfoPage. Put this some where safe, as this contains information unique to your nRF24LE1. Running the program, but passing a .hex file as a parameter will make the programmer write @@ -18,9 +20,3 @@ for debug build, use `DEBUG=1 make` * bus pirate binary modes: http://dangerousprototypes.com/docs/SPI_%28binary%29 * nrf24le1 datasheet: https://www.nordicsemi.com/kor/nordic/content_download/2443/29442/file/nRF24LE1_Product_Specification_rev1_6.pdf -## mods -forked from repo JoseJX/nrfprog -mods by zerog2k: - * fixes to buspirate not reading info page and main flash blocks - * more debugging - * pass in serial port as param From 7ea0cc399fb41444109b92f3a323c289ab7c199d Mon Sep 17 00:00:00 2001 From: Jens Jensen Date: Thu, 17 Dec 2015 17:21:45 -0600 Subject: [PATCH 04/16] readme updates --- README.md | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index cca911c..67e2bd1 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,13 @@ This is a simple programmer for the nRF24LE1 using the Bus Pirate as the SPI device. ## building -Type "make" to build the source. +``` +make clean +make +``` + +### debugging +for debug build, use `DEBUG=1 make` ## usage `nrfprog PORT [filename]` @@ -13,9 +19,6 @@ where safe, as this contains information unique to your nRF24LE1. Running the program, but passing a .hex file as a parameter will make the programmer write the .hex file to the device. -## debugging -for debug build, use `DEBUG=1 make` - ## references * bus pirate binary modes: http://dangerousprototypes.com/docs/SPI_%28binary%29 * nrf24le1 datasheet: https://www.nordicsemi.com/kor/nordic/content_download/2443/29442/file/nRF24LE1_Product_Specification_rev1_6.pdf From 10ae0cc093021b1e448eef5b891008d684dca2b7 Mon Sep 17 00:00:00 2001 From: Jens Jensen Date: Thu, 17 Dec 2015 18:52:40 -0600 Subject: [PATCH 05/16] readme and updates --- README.md | 15 +++++++++++++-- nrfprog.c | 6 ++++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 67e2bd1..bf9c418 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# NRF24LE1 BusPirate programmer +# BusPirate NRF24LE1 programmer This is a simple programmer for the nRF24LE1 using the Bus Pirate as the SPI device. @@ -12,7 +12,7 @@ make for debug build, use `DEBUG=1 make` ## usage -`nrfprog PORT [filename]` +`nrfprog BUSPIRATE_PORT [filename]` Running the program directly (./nrfprog) will dump the InfoPage. Put this some where safe, as this contains information unique to your nRF24LE1. Running the @@ -23,3 +23,14 @@ the .hex file to the device. * bus pirate binary modes: http://dangerousprototypes.com/docs/SPI_%28binary%29 * nrf24le1 datasheet: https://www.nordicsemi.com/kor/nordic/content_download/2443/29442/file/nRF24LE1_Product_Specification_rev1_6.pdf +## hardware + +| bus pirate | nrf 24pin | nrf 32 pin | nrf 48 pin | +|-------------|-----------|------------|------------| +| GND (brown) | GND | GND | GND | +| 3V3 (red) | PROG | PROG | PROG | +| AUX (blue) | RESET | RESET | RESET | +| CS | P0.5 | P1.1 | P2.0 | +| MISO | P0.4 | P1.0 | P1.6 | +| MOSI | P0.3 | P0.7 | P1.5 | +| CLK | P0.2 | P0.5 | P1.2 | diff --git a/nrfprog.c b/nrfprog.c index 320591f..01cceaf 100644 --- a/nrfprog.c +++ b/nrfprog.c @@ -350,7 +350,9 @@ void spi_read(int fd, uint8_t *buf, uint16_t len, int start_addr, char *err) { uint8_t bytes[3]; int pos, err_ct = 0; TRACE_MSG; +#ifdef DEBUG_PRINT printf("spi_read: %s\n", err); +#endif // Initiate the WR_RD operation ser_cmd(fd, BP_SPI_CS(0)); ser_cmd(fd, BP_SPI_WR_RD_NO_CS); @@ -369,7 +371,7 @@ void spi_read(int fd, uint8_t *buf, uint16_t len, int start_addr, char *err) { ser_cmd(fd, BP_SPI_CS(1)); check_resp(fd, "SPI Read CS"); - usleep(200000); // wait for bp spi read buffer + usleep(200000); // wait for bp spi read to buffer check_resp(fd, "SPI Read Start"); #ifdef DEBUG_PRINT @@ -595,7 +597,7 @@ int write_hex(int fd, char *fn) { int main(int argc, char **argv) { int fd; TRACE_MSG; - if (argc == 0) { + if (argc < 2) { printf("usage: nrfprog PORT [FILE]\n"); exit(1); } From aab8848385893ef1ae2e98afa37754274a3e2f42 Mon Sep 17 00:00:00 2001 From: Jens Jensen Date: Thu, 17 Dec 2015 19:12:10 -0600 Subject: [PATCH 06/16] write fixes --- nrfprog.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/nrfprog.c b/nrfprog.c index 01cceaf..a649262 100644 --- a/nrfprog.c +++ b/nrfprog.c @@ -105,7 +105,7 @@ void ser_cmd(int fd, int cmd) { #endif ser_write(fd, &ccmd, 1); // Delay for the hardware to respond - usleep(1000); + usleep(10000); return; } @@ -354,9 +354,9 @@ void spi_read(int fd, uint8_t *buf, uint16_t len, int start_addr, char *err) { printf("spi_read: %s\n", err); #endif // Initiate the WR_RD operation - ser_cmd(fd, BP_SPI_CS(0)); - ser_cmd(fd, BP_SPI_WR_RD_NO_CS); - + //ser_cmd(fd, BP_SPI_CS(0)); + //ser_cmd(fd, BP_SPI_WR_RD_NO_CS); + ser_cmd(fd, BP_SPI_WR_RD); // Send the read/write length and command bytes s2b(write_bytes, bytes); ser_write(fd, (uint8_t *) bytes, 2); @@ -368,11 +368,11 @@ void spi_read(int fd, uint8_t *buf, uint16_t len, int start_addr, char *err) { s2b(start_addr, bytes); ser_write(fd, bytes, 2); // Delay for the response - ser_cmd(fd, BP_SPI_CS(1)); - - check_resp(fd, "SPI Read CS"); + //ser_cmd(fd, BP_SPI_CS(1)); usleep(200000); // wait for bp spi read to buffer - check_resp(fd, "SPI Read Start"); + + check_resp(fd, "SPI Read CMD"); + //check_resp(fd, "SPI Read Start"); #ifdef DEBUG_PRINT printf("READ %i bytes\n", len); @@ -382,7 +382,7 @@ void spi_read(int fd, uint8_t *buf, uint16_t len, int start_addr, char *err) { while(read(fd, &(buf[pos]), 1) != 1) { usleep(1000); err_ct++; - if(err_ct > 1) { + if(err_ct > 10) { printf("Unable to read!\n"); break; } @@ -397,7 +397,7 @@ void spi_read(int fd, uint8_t *buf, uint16_t len, int start_addr, char *err) { #ifdef DEBUG_PRINT printf("\n"); #endif - check_resp(fd, "SPI Read Complete"); + //check_resp(fd, "SPI Read Complete"); return; @@ -473,7 +473,8 @@ void spi_write(int fd, uint8_t *buf, uint16_t len, int start_addr, char *err) { s2b(start_addr, bytes); ser_write(fd, bytes, 2); ser_write(fd, buf, len); - check_resp(fd, "SPI Write"); + usleep(150000); // wait for bp buffer + check_resp(fd, "spi_write"); // Check that we've completed the write command bytes[0] = NRF24_SPI_RDSR; From 45118eeaeead761f6381dc8ef754b2a5400ade3b Mon Sep 17 00:00:00 2001 From: Jens Jensen Date: Thu, 17 Dec 2015 23:35:00 -0600 Subject: [PATCH 07/16] fixed writing --- nrfprog.c | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/nrfprog.c b/nrfprog.c index a649262..27e2e1c 100644 --- a/nrfprog.c +++ b/nrfprog.c @@ -167,8 +167,7 @@ void check_resp(int fd, char *err) { int ret, ct = 0; uint8_t buf; TRACE_MSG; - //tcdrain(fd); - ///usleep(10); + // Check up to 25 times for a response while(ct < 50) { ret = read(fd, &buf, 1); @@ -231,10 +230,6 @@ void ser_bp_spi_cfg(int fd) { check_resp(fd, "SPI Turn on AUX pin"); } - // Wait 15 seconds to set up the xprotolab - printf("Waiting to set up the XProtoLab\n"); -// usleep(15000000); - usleep(3000000); return; } @@ -340,6 +335,7 @@ void spi_cmd(int fd, uint8_t *cmd, int cmd_len, char *err) { s2b(read_bytes, b); ser_write(fd, (uint8_t *) &b, 2); ser_write(fd, cmd, write_bytes); + usleep(1000); check_resp(fd, "SPI Command"); return; } @@ -354,8 +350,6 @@ void spi_read(int fd, uint8_t *buf, uint16_t len, int start_addr, char *err) { printf("spi_read: %s\n", err); #endif // Initiate the WR_RD operation - //ser_cmd(fd, BP_SPI_CS(0)); - //ser_cmd(fd, BP_SPI_WR_RD_NO_CS); ser_cmd(fd, BP_SPI_WR_RD); // Send the read/write length and command bytes s2b(write_bytes, bytes); @@ -368,11 +362,9 @@ void spi_read(int fd, uint8_t *buf, uint16_t len, int start_addr, char *err) { s2b(start_addr, bytes); ser_write(fd, bytes, 2); // Delay for the response - //ser_cmd(fd, BP_SPI_CS(1)); usleep(200000); // wait for bp spi read to buffer - check_resp(fd, "SPI Read CMD"); - //check_resp(fd, "SPI Read Start"); + check_resp(fd, "SPI Read Start"); #ifdef DEBUG_PRINT printf("READ %i bytes\n", len); @@ -397,8 +389,6 @@ void spi_read(int fd, uint8_t *buf, uint16_t len, int start_addr, char *err) { #ifdef DEBUG_PRINT printf("\n"); #endif - //check_resp(fd, "SPI Read Complete"); - return; } @@ -473,7 +463,7 @@ void spi_write(int fd, uint8_t *buf, uint16_t len, int start_addr, char *err) { s2b(start_addr, bytes); ser_write(fd, bytes, 2); ser_write(fd, buf, len); - usleep(150000); // wait for bp buffer + usleep(200000); // wait for bp buffer check_resp(fd, "spi_write"); // Check that we've completed the write command @@ -579,6 +569,7 @@ int write_hex(int fd, char *fn) { } // Verfiy that the flash was performed correctly + printf("Verifying flash...\n"); result = 0; for(pos = 0; pos < sz; pos++) { if(data[pos] != verify_data[pos]) { From 6ee4cbd0482b11c06ce58b9254100acf3b1dae39 Mon Sep 17 00:00:00 2001 From: Jens Jensen Date: Fri, 18 Dec 2015 08:29:57 -0600 Subject: [PATCH 08/16] status output for verification --- nrfprog.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nrfprog.c b/nrfprog.c index 27e2e1c..13e23a1 100644 --- a/nrfprog.c +++ b/nrfprog.c @@ -563,13 +563,13 @@ int write_hex(int fd, char *fn) { } // Read out the data + printf("Verifying flash...\n"); verify_data = (uint8_t *)calloc(sizeof(uint8_t), NRF24_FLASH_SZ); for(pos=0; pos < sz; pos = pos + NRF24_BLOCK_SZ) { spi_read(fd, &(verify_data[pos]), NRF24_BLOCK_SZ, pos, "Flash Read"); } // Verfiy that the flash was performed correctly - printf("Verifying flash...\n"); result = 0; for(pos = 0; pos < sz; pos++) { if(data[pos] != verify_data[pos]) { From 8aa4d1425f687479f4858c81de435ba31cad37b7 Mon Sep 17 00:00:00 2001 From: Jens Jensen Date: Fri, 18 Dec 2015 12:39:36 -0600 Subject: [PATCH 09/16] more doc updates --- README.md | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index bf9c418..286a97f 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,11 @@ make ``` ### debugging -for debug build, use `DEBUG=1 make` +for debug build, use: +``` +make clean +DEBUG=1 make +``` ## usage `nrfprog BUSPIRATE_PORT [filename]` @@ -19,18 +23,19 @@ where safe, as this contains information unique to your nRF24LE1. Running the program, but passing a .hex file as a parameter will make the programmer write the .hex file to the device. +## hardware + +| bus pirate | nrf 24 pin | nrf 32 pin | nrf 48 pin | +|--------------|------------|------------|------------| +| GND (brown) | GND | GND | GND | +| 3V3 (red) | PROG | PROG | PROG | +| AUX (blue) | RESET | RESET | RESET | +| CS (white) | P0.5 | P1.1 | P2.0 | +| MISO (black) | P0.4 | P1.0 | P1.6 | +| MOSI (grey) | P0.3 | P0.7 | P1.5 | +| CLK (purple) | P0.2 | P0.5 | P1.2 | + ## references * bus pirate binary modes: http://dangerousprototypes.com/docs/SPI_%28binary%29 * nrf24le1 datasheet: https://www.nordicsemi.com/kor/nordic/content_download/2443/29442/file/nRF24LE1_Product_Specification_rev1_6.pdf -## hardware - -| bus pirate | nrf 24pin | nrf 32 pin | nrf 48 pin | -|-------------|-----------|------------|------------| -| GND (brown) | GND | GND | GND | -| 3V3 (red) | PROG | PROG | PROG | -| AUX (blue) | RESET | RESET | RESET | -| CS | P0.5 | P1.1 | P2.0 | -| MISO | P0.4 | P1.0 | P1.6 | -| MOSI | P0.3 | P0.7 | P1.5 | -| CLK | P0.2 | P0.5 | P1.2 | From 2c1cb83702b911ddd223f092d7fd4a21917924e7 Mon Sep 17 00:00:00 2001 From: zerog2k Date: Thu, 24 Dec 2015 14:23:20 -0600 Subject: [PATCH 10/16] clarification of programming read/write mode --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 286a97f..168fdb4 100644 --- a/README.md +++ b/README.md @@ -20,8 +20,8 @@ DEBUG=1 make Running the program directly (./nrfprog) will dump the InfoPage. Put this some where safe, as this contains information unique to your nRF24LE1. Running the -program, but passing a .hex file as a parameter will make the programmer write -the .hex file to the device. +program, but passing a existing .hex file as a parameter will make the programmer write +the .hex file to the device. If you pass in filename of non-existing file, nrfprog will attempt to read from chip into the file. ## hardware From 5a7903df3640d8de0e046f6545cf52c5869a4f7f Mon Sep 17 00:00:00 2001 From: Jens Jensen Date: Tue, 24 Jan 2017 22:59:45 -0600 Subject: [PATCH 11/16] speed up timings --- nrfprog.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/nrfprog.c b/nrfprog.c index 13e23a1..bc6d630 100644 --- a/nrfprog.c +++ b/nrfprog.c @@ -105,7 +105,7 @@ void ser_cmd(int fd, int cmd) { #endif ser_write(fd, &ccmd, 1); // Delay for the hardware to respond - usleep(10000); + usleep(2000); return; } @@ -174,7 +174,7 @@ void check_resp(int fd, char *err) { if(ret != -1) break; // If we didn't get any data, wait for 0.25s and try again - usleep(250000); + usleep(100000); ct++; } #ifdef DEBUG_PRINT @@ -210,7 +210,7 @@ void ser_bp_spi_cfg(int fd) { ret = read(fd, &buf, 4); if(ret == 4 && strncmp(buf, BP_SPI_STRING, 4) == 0) { // Set the SPI Speed to 30KHz - ser_cmd(fd, BP_SPI_SPEED(BP_SPI_SPEED_30K)); + ser_cmd(fd, BP_SPI_SPEED(BP_SPI_SPEED_125K)); check_resp(fd, "SPI Set Speed"); // Set the SPI Configuration (pin output 3.3V, Clock Idle Phase Low, Clock Edge Active to Idle = 1), Sample Time Middle) @@ -335,7 +335,7 @@ void spi_cmd(int fd, uint8_t *cmd, int cmd_len, char *err) { s2b(read_bytes, b); ser_write(fd, (uint8_t *) &b, 2); ser_write(fd, cmd, write_bytes); - usleep(1000); + usleep(200); check_resp(fd, "SPI Command"); return; } @@ -362,7 +362,7 @@ void spi_read(int fd, uint8_t *buf, uint16_t len, int start_addr, char *err) { s2b(start_addr, bytes); ser_write(fd, bytes, 2); // Delay for the response - usleep(200000); // wait for bp spi read to buffer + usleep(100000); // wait for bp spi read to buffer check_resp(fd, "SPI Read Start"); @@ -372,7 +372,7 @@ void spi_read(int fd, uint8_t *buf, uint16_t len, int start_addr, char *err) { for(pos = 0; pos < len; pos++) { err_ct = 0; while(read(fd, &(buf[pos]), 1) != 1) { - usleep(1000); + usleep(200); err_ct++; if(err_ct > 10) { printf("Unable to read!\n"); @@ -420,7 +420,7 @@ void spi_wait(int fd, uint8_t *cmd, uint16_t len, char *resp_exp, uint16_t resp for(rd_ct=0; rd_ct < resp_len; rd_ct++) { err_ct = 0; while(read(fd, &(buf[rd_ct]), 1) != 1) { - usleep(1000); + usleep(200); err_ct++; if(err_ct > 25) { printf("Unable to read!\n"); @@ -463,7 +463,7 @@ void spi_write(int fd, uint8_t *buf, uint16_t len, int start_addr, char *err) { s2b(start_addr, bytes); ser_write(fd, bytes, 2); ser_write(fd, buf, len); - usleep(200000); // wait for bp buffer + usleep(100000); // wait for bp buffer check_resp(fd, "spi_write"); // Check that we've completed the write command From c418926155665a3ed274d212dd623ea3b2c54791 Mon Sep 17 00:00:00 2001 From: Jens Jensen Date: Thu, 16 Feb 2017 09:07:53 -0600 Subject: [PATCH 12/16] more aggressive timings --- nrfprog.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/nrfprog.c b/nrfprog.c index bc6d630..317f302 100644 --- a/nrfprog.c +++ b/nrfprog.c @@ -168,13 +168,13 @@ void check_resp(int fd, char *err) { uint8_t buf; TRACE_MSG; - // Check up to 25 times for a response - while(ct < 50) { + // Check multiple times for a response + while(ct < 25) { ret = read(fd, &buf, 1); if(ret != -1) break; - // If we didn't get any data, wait for 0.25s and try again - usleep(100000); + // If we didn't get any data, wait and try again + usleep(60000); ct++; } #ifdef DEBUG_PRINT @@ -362,7 +362,7 @@ void spi_read(int fd, uint8_t *buf, uint16_t len, int start_addr, char *err) { s2b(start_addr, bytes); ser_write(fd, bytes, 2); // Delay for the response - usleep(100000); // wait for bp spi read to buffer + usleep(60000); // wait for bp spi read to buffer check_resp(fd, "SPI Read Start"); @@ -463,7 +463,7 @@ void spi_write(int fd, uint8_t *buf, uint16_t len, int start_addr, char *err) { s2b(start_addr, bytes); ser_write(fd, bytes, 2); ser_write(fd, buf, len); - usleep(100000); // wait for bp buffer + usleep(60000); // wait for bp buffer check_resp(fd, "spi_write"); // Check that we've completed the write command From 87bd1371fdc29e5af2a216959b82cfb132bbcf60 Mon Sep 17 00:00:00 2001 From: Jens Jensen Date: Sun, 26 Feb 2017 14:52:39 -0600 Subject: [PATCH 13/16] timing and size changes to support nrf24lu1 programming --- nrf24le1.h | 2 +- nrfprog.c | 60 +++++++++++++++++++++++++++++++++++------------------- 2 files changed, 40 insertions(+), 22 deletions(-) diff --git a/nrf24le1.h b/nrf24le1.h index d1edef1..39a0af1 100644 --- a/nrf24le1.h +++ b/nrf24le1.h @@ -4,7 +4,7 @@ #define NRF24_BLOCK_SZ (512) #define NRF24_FLASH_SZ (32*NRF24_BLOCK_SZ) -#define NRF24_INFO_SZ (256) +#define NRF24_INFO_SZ (512) #define NRF24_SPI_WRSR (0x01) #define NRF24_SPI_PROGRAM (0x02) diff --git a/nrfprog.c b/nrfprog.c index 317f302..2e0b5b7 100644 --- a/nrfprog.c +++ b/nrfprog.c @@ -26,7 +26,9 @@ int ser_open(char *port) { TRACE_MSG; // Open the port - fd = open(port, O_RDWR | O_NOCTTY | O_NDELAY); + //fd = open(port, O_RDWR | O_NOCTTY | O_NDELAY); + fd = open(port, O_RDWR | O_NOCTTY); + if(fd == -1) { printf("Could not open the serial port: %s\n", port); exit(-1); @@ -72,15 +74,18 @@ void ser_write(int fd, uint8_t *stream, uint16_t sz) { int ret, ct; uint8_t c; TRACE_MSG; - tcdrain(fd); + //tcdrain(fd); // Write out one byte at a time #ifdef DEBUG_PRINT printf("ser_write: "); #endif for(ct=0; ct Date: Fri, 12 Jan 2018 16:36:17 -0600 Subject: [PATCH 14/16] fix page/block size define --- nrf24le1.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nrf24le1.h b/nrf24le1.h index 39a0af1..cb124e8 100644 --- a/nrf24le1.h +++ b/nrf24le1.h @@ -2,8 +2,8 @@ #define __NRF24LE1_H_ -#define NRF24_BLOCK_SZ (512) -#define NRF24_FLASH_SZ (32*NRF24_BLOCK_SZ) +#define NRF24_PAGE_SZ (512) +#define NRF24_FLASH_SZ (32*NRF24_PAGE_SZ) #define NRF24_INFO_SZ (512) #define NRF24_SPI_WRSR (0x01) From c25b0b8c7c76df49cc6bb422b468dbbd0e2be2f5 Mon Sep 17 00:00:00 2001 From: Jens Jensen Date: Fri, 12 Jan 2018 17:00:20 -0600 Subject: [PATCH 15/16] improve bp spi mode entry reliabilty --- nrfprog.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/nrfprog.c b/nrfprog.c index 2e0b5b7..1a01d56 100644 --- a/nrfprog.c +++ b/nrfprog.c @@ -215,10 +215,11 @@ void ser_bp_spi_cfg(int fd) { char buf[5]; TRACE_MSG; memset(buf, 0, 5); - + // clear serial buffers from buspirate reset + tcflush(fd, TCIOFLUSH); // Go to SPI Mode ser_cmd(fd, BP_SPI); - + usleep(10000); // Check that we're in SPI Mode ret = read(fd, &buf, 4); if(ret == 4 && strncmp(buf, BP_SPI_STRING, 4) == 0) { From 59f180c8edc766f6b731905dbbd55c8bec9fd261 Mon Sep 17 00:00:00 2001 From: Jens Jensen Date: Fri, 12 Jan 2018 23:05:28 -0600 Subject: [PATCH 16/16] improve logging, fix SPI clock edge sampling --- README.md | 4 +-- nrfprog.c | 78 ++++++++++++++++++++++++++++++++++++------------------- 2 files changed, 53 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 168fdb4..e411a85 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ -# BusPirate NRF24LE1 programmer -This is a simple programmer for the nRF24LE1 using the Bus Pirate as the SPI +# BusPirate NRF24LE1/NRF24LU1+ programmer +This is a simple programmer for the nRF24LE1 (or nRF24LU1p) using the Bus Pirate as the SPI device. ## building diff --git a/nrfprog.c b/nrfprog.c index 1a01d56..c3e1a83 100644 --- a/nrfprog.c +++ b/nrfprog.c @@ -58,6 +58,8 @@ int ser_open(char *port) { cfg.c_oflag &= ~OPOST; // Disable Software Flow Control cfg.c_iflag &= ~(IXON | IXOFF | IXANY); + //cfg.c_cc[VMIN] = 1; // read blocking + //cfg.c_cc[VTIME] = 1; // timeout // Send the config to the port tcsetattr(fd, TCSANOW, &cfg); return fd; @@ -114,11 +116,11 @@ void ser_cmd(int fd, int cmd) { uint8_t ccmd = cmd; TRACE_MSG; #ifdef DEBUG_PRINT - printf("bp_cmd: "); + printf("bp_cmd: 0x%02X\n", ccmd); #endif ser_write(fd, &ccmd, 1); // Delay for the hardware to respond - usleep(2000); + usleep(1000); return; } @@ -128,23 +130,30 @@ void ser_bp_bin(int fd) { char buf[6]; TRACE_MSG; memset(buf, 0, 6); - + usleep(10000); + tcflush(fd, TCIOFLUSH); // We try up to 25 times to make it work - for(count=0; count < 25; count++) { + for(count=0; count < 21; count++) { ser_cmd(fd, BP_BIN_RESET); ret = read(fd, buf, 5); + usleep(1000); #ifdef DEBUG_PRINT int tmp; printf("sz: %i\n", ret); for(tmp=0; tmp 10) { + if(err_ct > 20) { printf("Unable to read!\n"); break; } @@ -434,7 +447,7 @@ void spi_wait(int fd, uint8_t *cmd, uint16_t len, char *resp_exp, uint16_t resp for(rd_ct=0; rd_ct < resp_len; rd_ct++) { err_ct = 0; while(read(fd, &(buf[rd_ct]), 1) != 1) { - usleep(200); + usleep(400); err_ct++; if(err_ct > 25) { printf("Unable to read!\n"); @@ -557,7 +570,8 @@ int write_hex(int fd, char *fn) { spi_cmd(fd, cmd_bytes, 2, "Set INFEN"); // erase and write the data pages - for(pos = 0; pos < sz; pos = pos + NRF24_PAGE_SZ) { + printf(" erasing: "); + for(pos = 0; pos < sz; pos += NRF24_PAGE_SZ) { // Set WREN to enable writing cmd_bytes[0] = NRF24_SPI_WREN; spi_cmd(fd, cmd_bytes, 1, "Enable Writing"); @@ -573,21 +587,31 @@ int write_hex(int fd, char *fn) { spi_wait(fd, cmd_bytes, 1, &result, 1, "Waiting for the erase to complete"); // Write the data //spi_write(fd, &(data[pos]), NRF24_PAGE_SZ, pos, "Writing to Flash"); + printf(".."); fflush(stdout); } + printf("\n"); + //sleep(1); // do write separately. nrf24lu1 can only handle max 256 bytes PROGRAM cmd, nrf24le1 can handle 1024 - for(pos = 0; pos < sz; pos = pos + NRF24_PAGE_SZ/2) + printf(" writing: "); + for(pos = 0; pos < sz; pos += NRF24_PAGE_SZ/2) { spi_write(fd, &(data[pos]), NRF24_PAGE_SZ/2, pos, "Writing to Flash"); - - - + printf("."); fflush(stdout); + } + printf("\n"); // Read out the data - printf("Verifying flash...\n"); - verify_data = (uint8_t *)calloc(sizeof(uint8_t), 8*NRF24_FLASH_SZ); - for(pos=0; pos < sz; pos = pos + 8*NRF24_PAGE_SZ) { - spi_read(fd, &(verify_data[pos]), 8*NRF24_PAGE_SZ, pos, "Flash Read"); + printf("Verifying flash.\n"); + printf(" reading: "); + verify_data = (uint8_t *)calloc(sizeof(uint8_t), NRF24_FLASH_SZ); + for(pos=0; pos < sz; pos += NRF24_PAGE_SZ) { + if ((sz - pos) > NRF24_PAGE_SZ) + spi_read(fd, &(verify_data[pos]), NRF24_PAGE_SZ, pos, "Flash Read"); + else + spi_read(fd, &(verify_data[pos]), (sz - pos), pos, "Flash Read"); + printf(".."); fflush(stdout); } - + printf("\n"); + printf(" checking data...\n"); // Verfiy that the flash was performed correctly result = 0; for(pos = 0; pos < sz; pos++) { @@ -638,7 +662,7 @@ int main(int argc, char **argv) { } else { printf("Writing %s to the device\n", fname); if(write_hex(fd, fname) == 0) - printf("Write Verified\n"); + printf("Write Verified: OK\n"); } }