From 4c2c104859e987d000843ce1bc4b0084aa3008a7 Mon Sep 17 00:00:00 2001 From: Denis Ahrens Date: Mon, 19 Sep 2022 13:51:16 +0200 Subject: [PATCH 1/6] flush FTDI buffer to avoid 16ms latency timeout with this patch I could cut down the time to read a 4MB spi flash from 1min to 30sec. --- Firmware/base.c | 3 +++ Firmware/spi.c | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/Firmware/base.c b/Firmware/base.c index 021ca2bc..1f844b83 100644 --- a/Firmware/base.c +++ b/Firmware/base.c @@ -708,6 +708,9 @@ void user_serial_initialise(void) { U1STA = 0x0400; IFS0bits.U1RXIF = NO; + + /* FTDI CTS setup. */ + FTDI_CTS_DIR = OUTPUT; } bool user_serial_transmit_done(void) { return U1STAbits.TRMT; } diff --git a/Firmware/spi.c b/Firmware/spi.c index 2d79dd33..00ff5126 100644 --- a/Firmware/spi.c +++ b/Firmware/spi.c @@ -942,6 +942,10 @@ void spi_enter_binary_io(void) { REPORT_IO_FAILURE(); break; } +#ifdef BUSPIRATEV3 + /* avoid the 16ms FTDI buffer send latency */ + FTDI_CTS = !FTDI_CTS; +#endif } } From f39a18a6d556fd0a470c1c523ce87489ab2e6ecb Mon Sep 17 00:00:00 2001 From: Denis Ahrens Date: Sun, 31 Mar 2024 12:58:53 +0200 Subject: [PATCH 2/6] identify A6 revisions of the PIC24FJ256GB106 --- Firmware/hardwarev4.h | 1 + Firmware/messages_v4.h | 2 ++ Firmware/messages_v4.s | 6 ++++++ Firmware/proc_menu.c | 4 ++++ 4 files changed, 13 insertions(+) diff --git a/Firmware/hardwarev4.h b/Firmware/hardwarev4.h index 249c4b19..b97cf08b 100644 --- a/Firmware/hardwarev4.h +++ b/Firmware/hardwarev4.h @@ -176,6 +176,7 @@ #define PIC_REV_A3 0x0001 #define PIC_REV_A5 0x0003 +#define PIC_REV_A6 0x0004 static inline void bp_enable_usb_led(void) { BP_LEDUSB_DIR = OUTPUT; diff --git a/Firmware/messages_v4.h b/Firmware/messages_v4.h index 9a6a771d..a51ab2da 100644 --- a/Firmware/messages_v4.h +++ b/Firmware/messages_v4.h @@ -367,6 +367,8 @@ void MSG_CHIP_REVISION_A3_str(void); #define MSG_CHIP_REVISION_A3 bp_message_write_buffer(__builtin_tbladdress(MSG_CHIP_REVISION_A3_str)) void MSG_CHIP_REVISION_A5_str(void); #define MSG_CHIP_REVISION_A5 bp_message_write_buffer(__builtin_tbladdress(MSG_CHIP_REVISION_A5_str)) +void MSG_CHIP_REVISION_A6_str(void); +#define MSG_CHIP_REVISION_A6 bp_message_write_buffer(__builtin_tbladdress(MSG_CHIP_REVISION_A6_str)) void MSG_CHIP_REVISION_ID_BEGIN_str(void); #define MSG_CHIP_REVISION_ID_BEGIN bp_message_write_buffer(__builtin_tbladdress(MSG_CHIP_REVISION_ID_BEGIN_str)) void MSG_CHIP_REVISION_UNKNOWN_str(void); diff --git a/Firmware/messages_v4.s b/Firmware/messages_v4.s index f2d1e0ab..897342ee 100644 --- a/Firmware/messages_v4.s +++ b/Firmware/messages_v4.s @@ -1096,6 +1096,12 @@ _MSG_CHIP_REVISION_A3_str: _MSG_CHIP_REVISION_A5_str: .pasciz "A5" + ; MSG_CHIP_REVISION_A6 + .section .text.MSG_CHIP_REVISION_A6, code + .global _MSG_CHIP_REVISION_A6_str +_MSG_CHIP_REVISION_A6_str: + .pasciz "A6" + ; MSG_CHIP_REVISION_ID_BEGIN .section .text.MSG_CHIP_REVISION_ID_BEGIN, code .global _MSG_CHIP_REVISION_ID_BEGIN_str diff --git a/Firmware/proc_menu.c b/Firmware/proc_menu.c index e28861ef..98fae5ef 100644 --- a/Firmware/proc_menu.c +++ b/Firmware/proc_menu.c @@ -1452,6 +1452,10 @@ void print_version_info(void) { MSG_CHIP_REVISION_A5; break; + case PIC_REV_A6: + MSG_CHIP_REVISION_A6; + break; + default: MSG_CHIP_REVISION_UNKNOWN; break; From 3435094593d3dcbed81543f8a7b3c0f6fbe95f76 Mon Sep 17 00:00:00 2001 From: Denis Ahrens Date: Sun, 31 Mar 2024 13:19:25 +0200 Subject: [PATCH 3/6] changed bootloader to version 4.12 1. USB led logic is corrected (was reversed) 2. pirate-loader hello now works without resetting the buspirate 3. you get into the bootloader while pressing the mode button at start 4. changed version to 4.12 --- .../BPv4-bootloader/firmware-v1/boot_config.h | 5 --- .../BPv4-bootloader/firmware-v1/bootloader.c | 12 ++++--- .../BPv4-bootloader/firmware-v1/globals.h | 4 +-- .../BPv4-bootloader/firmware-v1/main.c | 32 ++++++++++++++++--- 4 files changed, 37 insertions(+), 16 deletions(-) diff --git a/Bootloaders/BPv4-bootloader/firmware-v1/boot_config.h b/Bootloaders/BPv4-bootloader/firmware-v1/boot_config.h index 434c5b40..95ecceac 100644 --- a/Bootloaders/BPv4-bootloader/firmware-v1/boot_config.h +++ b/Bootloaders/BPv4-bootloader/firmware-v1/boot_config.h @@ -45,11 +45,6 @@ Low power request macro #ifndef __BOOT_CONFIG_H__ #define __BOOT_CONFIG_H__ -#define MODELED LATBbits.LATB8 -#define USBLED LATBbits.LATB10 //Active LOW - -#define USBLEDON() USBLED = 0 - #define USB_VID (0x04d8) #define USB_PID (0xFAFF) //; DangerousPrototypes Bootloader VID/PID #define USB_DEV 0x0002 diff --git a/Bootloaders/BPv4-bootloader/firmware-v1/bootloader.c b/Bootloaders/BPv4-bootloader/firmware-v1/bootloader.c index 58cf856f..d318051c 100644 --- a/Bootloaders/BPv4-bootloader/firmware-v1/bootloader.c +++ b/Bootloaders/BPv4-bootloader/firmware-v1/bootloader.c @@ -22,7 +22,7 @@ BYTE bldone = 0; extern BYTE cdc_In_buffer[64]; extern BYTE cdc_Out_buffer[64]; #define VER_H 0x04 -#define VER_L 0x0a +#define VER_L 0x0c unsigned int userversion __attribute__((space(prog),address(BLENDADDR-9))) = ((VER_H<<8)|VER_L); @@ -110,7 +110,6 @@ void bootloader(void) { if(crc!=0){ bootstruct.blreturn='N';//return checksum error goto error; - } //calculate flash address @@ -130,9 +129,14 @@ void bootloader(void) { WritePage(); break; case 0xff: - U1CONbits.USBEN=0; //USB off + bootstruct.blreturn = 'K'; + cdc_In_buffer[0] = bootstruct.blreturn; //answer OK + putUnsignedCharArrayUsbUsart(cdc_In_buffer, 1); + i=0x7FFF; + while(i--); + U1CONbits.USBEN=0; //USB off //delay a while so computer sees us turn off USB - j=0xFFFF; + j=0x0010; while(j--){ i=0xFFFF; while(i--); diff --git a/Bootloaders/BPv4-bootloader/firmware-v1/globals.h b/Bootloaders/BPv4-bootloader/firmware-v1/globals.h index 6e8d1b78..d0693200 100644 --- a/Bootloaders/BPv4-bootloader/firmware-v1/globals.h +++ b/Bootloaders/BPv4-bootloader/firmware-v1/globals.h @@ -23,8 +23,8 @@ typedef unsigned char BYTE; #define LedSetup() TRISBbits.TRISB8=0;TRISBbits.TRISB9=0;TRISBbits.TRISB10=0 -#define uLedOn() LATBbits.LATB10 = 0 -#define uLedOff() LATBbits.LATB10 = 1 +#define uLedOn() LATBbits.LATB10 = 1 +#define uLedOff() LATBbits.LATB10 = 0 #define uLedToggle() LATBbits.LATB10 ^= LATBbits.LATB10 #define mLedOn() LATBbits.LATB8 = 1 diff --git a/Bootloaders/BPv4-bootloader/firmware-v1/main.c b/Bootloaders/BPv4-bootloader/firmware-v1/main.c index 27b23bd3..264f8655 100644 --- a/Bootloaders/BPv4-bootloader/firmware-v1/main.c +++ b/Bootloaders/BPv4-bootloader/firmware-v1/main.c @@ -42,6 +42,18 @@ #include #include +// Hardware 'NORMAL' button on BPv4 definitions +#define BP_BUTTON_DIR TRISCbits.TRISC14 +#define BP_BUTTON PORTCbits.RC14 +#define BP_BUTTON_IF IFS1bits.CNIF +#define BP_BUTTON_SETUP() \ + BP_BUTTON_DIR = 1; \ + CNPU1 |= 0b1; \ + CNEN1 |= 0b1; \ + IEC1bits.CNIE = 0; \ + BP_BUTTON_IF = 0; +#define BP_BUTTON_ISDOWN() (!BP_BUTTON) // 0=DOWN (PRESSED) / 1=UP (UNPRESSED) + #define FIRMWARE_SIGNATURE 0x31415926 bool __attribute__((address(0x47FA), persistent)) skip_pgc_pgd_check; @@ -85,17 +97,27 @@ int main(void) { PGC_OUT = 0; PGC_TRIS = 0; - if ((firmware_signature != FIRMWARE_SIGNATURE) || !skip_pgc_pgd_check) { + LedSetup(); + + BP_BUTTON_SETUP(); + + if ((firmware_signature != FIRMWARE_SIGNATURE) || !skip_pgc_pgd_check || BP_BUTTON_ISDOWN()) { volatile int i; skip_pgc_pgd_check = false; - i = 5000; - while (i--) - ; + int j = 35; + while (j--) { + i = 65000; + while (i--); + } + + vLedOn(); + mLedOn(); + uLedOn(); for (i = 0; i < 20; i++) { - if ((PGD_IN == 1)) { // go to user space on first mis-match + if ((PGD_IN == 1 && !(BP_BUTTON_ISDOWN()))) { // go to user space on first mis-match // continue to bootloader, or exit asm(".equ BLJUMPADDRESS, 0x2000"); asm volatile("mov #BLJUMPADDRESS, w1 \n" // bootloader location From 6252850213f629f5cb40951de5b53f46707d01dd Mon Sep 17 00:00:00 2001 From: Denis Ahrens Date: Sun, 31 Mar 2024 13:27:32 +0200 Subject: [PATCH 4/6] update pirate-loader to 1.0.4 pirate-loader does not show errors anymore after successful actions like hello or flashing new firmware. also better error reporting. increased version to 1.0.4 should be used with bootloader 4.12 but works with 4.10 also --- .../pirate-loader/pirate-loader.c | 98 ++++++++++++------- 1 file changed, 65 insertions(+), 33 deletions(-) diff --git a/Bootloaders/BPv4-bootloader/pirate-loader/pirate-loader.c b/Bootloaders/BPv4-bootloader/pirate-loader/pirate-loader.c index 3ec80983..e4d74a80 100644 --- a/Bootloaders/BPv4-bootloader/pirate-loader/pirate-loader.c +++ b/Bootloaders/BPv4-bootloader/pirate-loader/pirate-loader.c @@ -57,7 +57,7 @@ #include #include -#define PIRATE_LOADER_VERSION "1.0.3" +#define PIRATE_LOADER_VERSION "1.0.4" #define STR_EXPAND(tok) #tok #define OS_NAME(tok) STR_EXPAND(tok) @@ -77,7 +77,6 @@ int write(int fd, const void* buf, int len) int res = 0; unsigned long bwritten = 0; - res = WriteFile(hCom, buf, len, &bwritten, NULL); if( res == FALSE ) @@ -197,8 +196,10 @@ typedef unsigned long uint32; #endif #define BOOTLOADER_HELLO_STR "\xC1" -#define BOOTLOADER_OK 0x4B +#define BOOTLOADER_OK 'K' #define BOOTLOADER_PROT 'P' +#define BOOTLOADER_CHECKSUM 'N' + #define PIC_WORD_SIZE (3) #define PIC_NUM_ROWS_IN_PAGE 8 #define PIC_NUM_WORDS_IN_ROW 64 @@ -484,41 +485,43 @@ int sendCommandAndWaitForResponse(int fd, uint8 *command) res = readWithTimeout(fd, response, 1, 5); if( res != 1 ) { - puts("ERROR"); + puts("ERROR, timeout"); return -1; } - else if (response[0]== BOOTLOADER_PROT) - { - printf("(SKIPPED by bootloader)..."); - return 0; - } - else if ( response[0] != BOOTLOADER_OK ) - { - printf("ERROR [%02x]\n", response[0]); - return -1; - } - else - { - return 0; - } + switch (response[0]) + { + case BOOTLOADER_OK: + return 0; + break; + + case BOOTLOADER_PROT: + printf("(SKIPPED by bootloader)..."); + return 0; + break; + + case BOOTLOADER_CHECKSUM: + printf("cmd checksum error...\n"); + return -1; + break; + + default: + printf("ERROR [0x%02x]\n", response[0]); + return -1; + } } - int sendFirmware(int fd, uint8* data, uint8* pages_used) { uint32 u_addr; - uint32 page = 0; uint32 done = 0; uint32 row = 0; uint8 command[256] = {0}; - for( page=0; page 0 ) { - puts("\nFirmware updated successfully :)!"); + puts("\nFirmware updated successfully! :)"); //printf("Use screen %s 115200 to verify\n", g_device_path); } else @@ -1243,9 +1264,20 @@ int main (int argc, const char** argv) puts("\nError updating firmware :("); goto Error; } - } + uint8 command[6] = {0}; + + command[0] = 1; // fake data + command[1] = 2; + command[2] = 3; + command[COMMAND_OFFSET] = 0xff; + command[LENGTH_OFFSET ] = 0x01; //1 byte, CRC + command[PAYLOAD_OFFSET] = makeCrc(command, 5); + + //send byebye (cmd: 0xff) + int ret = sendCommandAndWaitForResponse(dev_fd, command); + Finished: if( bin_buff ) { From d95f66b6f94e59d6f71c2e669343f426a9ad888a Mon Sep 17 00:00:00 2001 From: Denis Ahrens Date: Sun, 31 Mar 2024 13:39:19 +0200 Subject: [PATCH 5/6] fix flashrom support for BusPirateV4 flashrom starts with binary mode and then wants to return to interactive mode to get the version and stuff. the method to reset only works for BPv3. the BPv4 uses USB which does not like the reset. Use another method to get back to the interactive mode. --- Firmware/binary_io.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Firmware/binary_io.c b/Firmware/binary_io.c index 3c8f0fab..1d1a08ee 100644 --- a/Firmware/binary_io.c +++ b/Firmware/binary_io.c @@ -276,7 +276,7 @@ static inline void handle_clear_pwm(void); static inline void handle_read_adc_one_shot(void); static inline void handle_read_adc_continuously(void); static inline void handle_frequency_measurement(void); -static inline void handle_bitbang_command(const bitbang_command command); +static inline int handle_bitbang_command(const bitbang_command command); static void read_and_transmit_adc_measurement(void); @@ -368,14 +368,15 @@ void enter_binary_bitbang_mode(void) { uint8_t input_byte = user_serial_read_byte(); if ((input_byte & 0b10000000) == 0) { - handle_bitbang_command((bitbang_command)input_byte); + if (handle_bitbang_command((bitbang_command)input_byte)) + break; } else { user_serial_transmit_character(bitbang_pin_state_set(input_byte)); } } } -void handle_bitbang_command(const bitbang_command command) { +int handle_bitbang_command(const bitbang_command command) { switch (command) { case BITBANG_COMMAND_RESET: send_binary_io_mode_identifier(); @@ -448,7 +449,7 @@ void handle_bitbang_command(const bitbang_command command) { user_serial_wait_transmission_done(); #if defined(BUSPIRATEV4) reset_state(); - return; + return -1; #else __asm volatile("RESET"); #endif /* BUSPIRATEV4 */ @@ -500,6 +501,7 @@ void handle_bitbang_command(const bitbang_command command) { } break; } + return 0; } void reset_state(void) { From 6754461e34af142452659e0c9d09f3b2cc2fe580 Mon Sep 17 00:00:00 2001 From: Denis Ahrens Date: Sun, 31 Mar 2024 21:07:35 +0200 Subject: [PATCH 6/6] changed the spi speed table back to where it was A change about 8 years ago made the SPI speed table incompatible with the flashrom tool. flashrom thinks it set 8MHz SPI speed when in reality it was only 2.6MHz. Changing the table back to the state it was to make it (again) compatible with the flashrom tool. --- Firmware/messages_v3.s | 3 ++- Firmware/messages_v4.s | 2 +- Firmware/spi.c | 10 +++++----- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/Firmware/messages_v3.s b/Firmware/messages_v3.s index 7ef865ea..128abc90 100644 --- a/Firmware/messages_v3.s +++ b/Firmware/messages_v3.s @@ -1526,7 +1526,8 @@ _MSG_SPI_SAMPLE_PROMPT_str: .section .text.MSG_SPI_SPEED_PROMPT, code .global _MSG_SPI_SPEED_PROMPT_str _MSG_SPI_SPEED_PROMPT_str: - .pasciz "Set speed:\r\n 1. 30KHz\r\n 2. 125KHz\r\n 3. 250KHz\r\n 4. 1MHz\r\n 5. 50KHz\r\n 6. 1.3MHz\r\n 7. 2MHz\r\n 8. 2.6MHz\r\n 9. 3.2MHz\r\n10. 4MHz\r\n11. 5.3MHz\r\n12. 8MHz" + .pasciz "Set speed:\r\n 1. 30kHz\r\n 2. 125kHz\r\n 3. 250kHz\r\n 4. 1MHz\r\n 5. 2MHz\r\n 6. 2.6MHz\r\n 7. 4MHz\r\n 8. 8MHz\r\n 9. 50KHz\r\n10. 1.3MHz\r\n11. 3.2MHz\r\n12. 5.3MHz" + ; MSG_UART_BAUD_CALCULATED .section .text.MSG_UART_BAUD_CALCULATED, code diff --git a/Firmware/messages_v4.s b/Firmware/messages_v4.s index 897342ee..b56ea8d6 100644 --- a/Firmware/messages_v4.s +++ b/Firmware/messages_v4.s @@ -1622,7 +1622,7 @@ _MSG_SPI_SAMPLE_PROMPT_str: .section .text.MSG_SPI_SPEED_PROMPT, code .global _MSG_SPI_SPEED_PROMPT_str _MSG_SPI_SPEED_PROMPT_str: - .pasciz "Set speed:\r\n 1. 30KHz\r\n 2. 125KHz\r\n 3. 250KHz\r\n 4. 1MHz\r\n 5. 50KHz\r\n 6. 1.3MHz\r\n 7. 2MHz\r\n 8. 2.6MHz\r\n 9. 3.2MHz\r\n10. 4MHz\r\n11. 5.3MHz\r\n12. 8MHz" + .pasciz "Set speed:\r\n 1. 30kHz\r\n 2. 125kHz\r\n 3. 250kHz\r\n 4. 1MHz\r\n 5. 2MHz\r\n 6. 2.6MHz\r\n 7. 4MHz\r\n 8. 8MHz\r\n 9. 50KHz\r\n10. 1.3MHz\r\n11. 3.2MHz\r\n12. 5.3MHz" ; MSG_UART_BAUD_CALCULATED .section .text.MSG_UART_BAUD_CALCULATED, code diff --git a/Firmware/spi.c b/Firmware/spi.c index 00ff5126..5788f790 100644 --- a/Firmware/spi.c +++ b/Firmware/spi.c @@ -257,14 +257,14 @@ static const uint8_t spi_bus_speed[] = { 0b00011000, /* 125 kHz - Primary prescaler 64:1 / Secondary prescaler 2:1 */ 0b00011100, /* 250 kHz - Primary prescaler 64:1 / Secondary prescaler 1:1 */ 0b00011101, /* 1 MHz - Primary prescaler 16:1 / Secondary prescaler 1:1 */ - 0b00001100, /* 50 kHz - Primary prescaler 64:1 / Secondary prescaler 5:1 */ - 0b00010110, /* 1.3 MHz - Primary prescaler 4:1 / Secondary prescaler 3:1 */ 0b00011010, /* 2 MHz - Primary prescaler 4:1 / Secondary prescaler 2:1 */ 0b00001011, /* 2.6 MHz - Primary prescaler 1:1 / Secondary prescaler 6:1 */ - 0b00001111, /* 3.2 MHz - Primary prescaler 1:1 / Secondary prescaler 5:1 */ 0b00011110, /* 4 MHz - Primary prescaler 4:1 / Secondary prescaler 1:1 */ - 0b00010111, /* 5.3 MHz - Primary prescaler 1:1 / Secondary prescaler 3:1 */ - 0b00011011 /* 8 MHz - Primary prescaler 1:1 / Secondary prescaler 2:1 */ + 0b00011011, /* 8 MHz - Primary prescaler 1:1 / Secondary prescaler 2:1 */ + 0b00001100, /* 50 kHz - Primary prescaler 64:1 / Secondary prescaler 5:1 */ + 0b00010110, /* 1.3 MHz - Primary prescaler 4:1 / Secondary prescaler 3:1 */ + 0b00001111, /* 3.2 MHz - Primary prescaler 1:1 / Secondary prescaler 5:1 */ + 0b00010111 /* 5.3 MHz - Primary prescaler 1:1 / Secondary prescaler 3:1 */ }; void engage_spi_cs(bool write_with_read) {