From eb875203611a02cb02af8f911646dd04e49b574f Mon Sep 17 00:00:00 2001 From: David Reguera Garcia Date: Thu, 22 Aug 2024 07:57:56 +0200 Subject: [PATCH 1/8] WIP - first thing --- CMakeLists.txt | 24 ++++ binmode/binmodes.c | 7 + binmode/binmodes.h | 1 + binmode/legacy2third.c | 311 +++++++++++++++++++++++++++++++++++++++++ binmode/legacy2third.h | 2 + 5 files changed, 345 insertions(+) create mode 100644 binmode/legacy2third.c create mode 100644 binmode/legacy2third.h diff --git a/CMakeLists.txt b/CMakeLists.txt index faa61151..84e107b1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,6 +4,29 @@ if(WIN32) else() set(USERHOME $ENV{HOME}) endif() +set(PICO_SDK_PATH ${USERHOME}/.pico-sdk/sdk/2.0.0) +set(PICO_TOOLCHAIN_PATH ${USERHOME}/.pico-sdk/toolchain/13_2_Rel1) +set(pioasm_HINT ${USERHOME}/.pico-sdk/tools/2.0.0/pioasm) +if(EXISTS ${pioasm_HINT}) + set(pioasm_DIR ${pioasm_HINT}) +endif() +set(picotool_HINT ${USERHOME}/.pico-sdk/picotool/2.0.0/picotool) +if(EXISTS ${picotool_HINT}) + set(picotool_DIR ${picotool_HINT}) +endif() +if(PICO_TOOLCHAIN_PATH MATCHES "RISCV") + set(PICO_PLATFORM rp2350-riscv CACHE STRING "Pico Platform") + if(PICO_TOOLCHAIN_PATH MATCHES "COREV") + set(PICO_COMPILER pico_riscv_gcc_zcb_zcmp) + endif() +endif() +# ==================================================================================== +# == DO NEVER EDIT THE NEXT LINES for Raspberry Pi Pico VS Code Extension to work == +if(WIN32) + set(USERHOME $ENV{USERPROFILE}) +else() + set(USERHOME $ENV{HOME}) +endif() if(WIN32) set(PICO_SDK_PATH ${USERHOME}/.pico-sdk/sdk/2.0.0) set(PICO_TOOLCHAIN_PATH ${USERHOME}/.pico-sdk/toolchain/13_2_Rel1) @@ -206,6 +229,7 @@ set(bp5_common mode/binloopback.c mode/binloopback.h binmode/logicanalyzer.h binmode/logicanalyzer.c binmode/sump.c binmode/sump.h binmode/dirtyproto.c binmode/dirtyproto.h + binmode/legacy2third.c binmode/legacy2third.h lib/arduino-ch32v003-swio/arduino_ch32v003.c lib/arduino-ch32v003-swio/arduino_ch32v003.h lib/arduino-ch32v003-swio/swio.h lib/arduino-ch32v003-swio/swio.c lib/picorvd/picoswio.c lib/picorvd/picoswio.h lib/picorvd/debug_defines.h diff --git a/binmode/binmodes.c b/binmode/binmodes.c index 630fdd03..ef2e9d02 100644 --- a/binmode/binmodes.c +++ b/binmode/binmodes.c @@ -5,6 +5,7 @@ #include "system_config.h" #include "binmode/sump.h" #include "binmode/dirtyproto.h" +#include "binmode/legacy2third.h" #include "lib/arduino-ch32v003-swio/arduino_ch32v003.h" void binmode_null_func(void) { @@ -28,6 +29,12 @@ const binmode_t binmodes[]={ &binmode_null_func, arduino_ch32v003_name, }, + { + &binmode_null_func, + &legacy2third_mode, + &binmode_null_func, + legacy2third_mode_name, + }, }; inline void binmode_setup(void){ diff --git a/binmode/binmodes.h b/binmode/binmodes.h index 159f5f2c..212525e5 100644 --- a/binmode/binmodes.h +++ b/binmode/binmodes.h @@ -6,6 +6,7 @@ enum { BINMODE_USE_SUMPLA=0, BINMODE_USE_DIRTYPROTO, BINMODE_USE_ARDUINO_CH32V003_SWIO, + BINMODE_USE_LEGACY2THIRD, BINMODE_MAXPROTO }; diff --git a/binmode/legacy2third.c b/binmode/legacy2third.c new file mode 100644 index 00000000..d3b9fd28 --- /dev/null +++ b/binmode/legacy2third.c @@ -0,0 +1,311 @@ +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project. + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + */ + +/* Legacy Binary Mode for third parties */ + +//#include +#include +#include "pico/stdlib.h" +#include "pirate.h" +#include "queue.h" +#include "usb_rx.h" +#include "usb_tx.h" +#include "pirate.h" +#include "opt_args.h" //needed for same reason as bytecode and needs same fix +#include "bytecode.h" +#include "modes.h" +#include "binio_helpers.h" +#include "tusb.h" +#include "binmode/binio.h" +#include "system_config.h" + + +const char legacy2third_mode_name[]="Legacy Binary Mode For Third Parties (Flashrom and AVRdude)"; + +uint8_t* cdc_buff; +uint32_t remain_bytes; +#define DEFAULT_MAX_TRIES 100000 + +uint32_t read_buff(uint8_t *buf, uint32_t len, uint32_t max_tries) +{ + uint32_t pending_data = 0; + uint32_t bytes_readed = 0; + uint32_t total_bytes_readed = 0; + + if (remain_bytes > 0) + { + bytes_readed = remain_bytes >= len ? len : remain_bytes; + memcpy(buf, cdc_buff, bytes_readed); + remain_bytes -= bytes_readed; + + if (remain_bytes > 0) + { + memmove(cdc_buff, cdc_buff + bytes_readed, remain_bytes); + } + + total_bytes_readed = bytes_readed; + } + + while (total_bytes_readed < len && max_tries--) + { + pending_data = tud_cdc_n_available(1); + if (pending_data > 0) + { + bytes_readed = tud_cdc_n_read(1, cdc_buff + remain_bytes, pending_data); + tud_task(); + remain_bytes += bytes_readed; + uint32_t bytes_to_copy = len - total_bytes_readed; + if (remain_bytes < bytes_to_copy) + { + bytes_to_copy = remain_bytes; + } + + memcpy(buf + total_bytes_readed, cdc_buff, bytes_to_copy); + total_bytes_readed += bytes_to_copy; + remain_bytes -= bytes_to_copy; + + if (remain_bytes > 0) + { + memmove(cdc_buff, cdc_buff + bytes_to_copy, remain_bytes); + } + } + } + + return total_bytes_readed; +} + +#define CDC_SEND_STR(cdc_n, str) tud_cdc_n_write(cdc_n, (uint8_t*)str, sizeof(str) - 1); tud_cdc_n_write_flush(1); + +void cdc_full_flush(uint32_t cdc_id) +{ + tud_cdc_n_read_flush(cdc_id); + tud_cdc_n_write_flush(cdc_id); + remain_bytes = 0; +} + +void legacy_protocol(void) +{ + uint8_t op_byte; + uint8_t extended_info; + uint8_t count_zero = 0; + uint32_t spi_speed = 0; + + cdc_full_flush(1); + + while (1) + { + op_byte = 0; + extended_info = 0; + tud_task(); + while (!read_buff(&op_byte, 1, DEFAULT_MAX_TRIES)); + printf("\r\n-\r\nop_byte=0x%02X", op_byte); + printf(", extended_info=0x%02X", extended_info); + + if (op_byte) + { + count_zero = 0; // ugly, but simple + if (op_byte >= 0x60 && op_byte <= 0x67) // this must be the first + { + extended_info = op_byte; + op_byte = 0x60; + } + else if (op_byte & 0x80) + { + extended_info = op_byte; + op_byte = 0x80; + } + else if (op_byte & 0x40) + { + extended_info = op_byte; + op_byte = 0x40; + } + } + + printf("\r\nop_byte=0x%02X", op_byte); + printf(", extended_info=0x%02X", extended_info); + switch (op_byte) + { + case 0x00: + if (!count_zero) + { + printf("\r\nBBIO1->"); + CDC_SEND_STR(1, "BBIO1"); + spi_speed = 0; + hwspi_deinit(); + psu_disable(); + } + else if (count_zero > 15) + { + count_zero = 0; + } + count_zero++; + break; + + case 0x0F: + printf("\r\nBus Pirate CLI prompt->"); + // ugly hack for fixed baudarate (look flashrom src!): + CDC_SEND_STR(1, "Bus Pirate v2.5\r\nCommunity Firmware v7.1\r\nHiZ>"); + break; + + case 0x01: + printf("\r\nSPI1->"); + CDC_SEND_STR(1, "SPI1"); + break; + + case 0x40: + printf("\r\npsu..."); + if (extended_info & 0b00001000) + { + uint8_t args[] = { 0x03, 0x21, 0x00, 0x80 }; + uint32_t result = binmode_psu_enable(args); + if(result) + { + printf("\r\nPSU ERROR CODE %d", result); + CDC_SEND_STR(1, "\x00"); + } + else + { + printf("\r\nPSU Enabled"); + CDC_SEND_STR(1, "\x01"); + } + } + break; + + case 0x60: + printf("\r\nspi_speed"); + spi_speed = 1000000; + CDC_SEND_STR(1, "\x01"); + break; + + case 0x80: + printf("\r\nhwspi_init"); + uint8_t data_bits = 8; + uint8_t cpol = 0; + uint8_t cpha = 0; + static const char mpin_labels[][5]={ + "CLK", + "MOSI", + "MISO", + "CS" + }; + + spi_init(SPI1_BASE, 1000000); // 1MHz + hwspi_init(data_bits, cpol, cpha); + system_bio_claim(true, 6, 1, mpin_labels[0]); + system_bio_claim(true, 7, 1, mpin_labels[1]); + system_bio_claim(true, 4, 1, mpin_labels[2]); + system_bio_claim(true, 5, 1, mpin_labels[3]); + CDC_SEND_STR(1, "\x01"); + break; + + case 0x03: + printf("\r\nhwspi_select"); + hwspi_select(); + CDC_SEND_STR(1, "\x01"); + break; + + case 0x04: + uint16_t bytes_to_read = 0; + uint16_t bytes_to_write = 0; + static uint8_t tmpbuf[0x2000]; + + memset(tmpbuf, 0, sizeof(tmpbuf)); + + while (!read_buff(tmpbuf, 4, DEFAULT_MAX_TRIES)); + + printf("\r\nbytes_to_write H: 0x%02X", tmpbuf[0]); + printf("\r\nbytes_to_write L: 0x%02x", tmpbuf[1]); + printf("\r\nbytes_to_read H: 0x%02X", tmpbuf[2]); + printf("\r\nbytes_to_read L: 0x%02x", tmpbuf[3]); + + bytes_to_write = (tmpbuf[0] << 8) | tmpbuf[1]; + bytes_to_read = (tmpbuf[2] << 8) | tmpbuf[3]; + printf("\r\nbytes_to_write: %d", bytes_to_write); + printf("\r\nbytes_to_read: %d", bytes_to_read); + + while (!read_buff(tmpbuf, bytes_to_write, DEFAULT_MAX_TRIES)); + + hwspi_select(); + printf("\r\n>> "); + int j = 0; + uint32_t total_bytes_spi = bytes_to_write > bytes_to_read ? bytes_to_write : bytes_to_read; + if (bytes_to_read) + { + total_bytes_spi++; + } + while (j < total_bytes_spi) + { + if (j >= bytes_to_write) + { + tmpbuf[j] = 0x00; + } + printf("\r\n[%d] 0x%02X -> | ", j, tmpbuf[j]); + tmpbuf[j] = hwspi_write_read(tmpbuf[j]); + printf("<- 0x%02X", tmpbuf[j]); + j++; + } + hwspi_deselect(); + + tmpbuf[0] = '\x01'; + + int bytes_sent = 0; + int chunk_size = 32; + int total_bytes = bytes_to_read + 1; + int total_cdc_bytes_sended = 0; + tud_cdc_n_read_flush(1); + while (bytes_sent < total_bytes) + { + int bytes_left = total_bytes - bytes_sent; + int current_chunk_size = (bytes_left < chunk_size) ? bytes_left : chunk_size; + while (tud_cdc_n_write_available(1) < current_chunk_size) + { + tud_task(); + tud_cdc_n_write_flush(1); + } + total_cdc_bytes_sended += tud_cdc_n_write(1, tmpbuf + bytes_sent, current_chunk_size); + tud_cdc_n_write_flush(1); + bytes_sent += current_chunk_size; + } + + printf("\r\ntotal_cdc_bytes_sended: %d", total_cdc_bytes_sended); + tud_task(); + break; + } + } +} + +// handler needs to be cooperative multitasking until mode is enabled +void legacy2third_mode(void){ + static uint32_t mode_active=0; + if (mode_active == 0){ + mode_active++; + uint8_t binmode_args = 1; + binmode_debug_level(&binmode_args); + script_enabled(); + cdc_buff = (uint8_t*) mem_alloc(0x2000); + remain_bytes = 0; + legacy_protocol(); + mem_free(cdc_buff); + hwspi_deinit(); + psu_disable(); + } +} + + + + + + diff --git a/binmode/legacy2third.h b/binmode/legacy2third.h new file mode 100644 index 00000000..f68dca04 --- /dev/null +++ b/binmode/legacy2third.h @@ -0,0 +1,2 @@ +void legacy2third_mode(void); +extern const char legacy2third_mode_name[]; \ No newline at end of file From 5ee534b21a8bd5ca804b4ed66964dd9dabca2f69 Mon Sep 17 00:00:00 2001 From: David Reguera Garcia Date: Fri, 23 Aug 2024 17:15:18 +0200 Subject: [PATCH 2/8] flashrom working! --- binmode/legacy2third.c | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/binmode/legacy2third.c b/binmode/legacy2third.c index d3b9fd28..ca007419 100644 --- a/binmode/legacy2third.c +++ b/binmode/legacy2third.c @@ -202,7 +202,7 @@ void legacy_protocol(void) "CS" }; - spi_init(SPI1_BASE, 1000000); // 1MHz + spi_init(SPI1_BASE, 10000000); // 10MHz hwspi_init(data_bits, cpol, cpha); system_bio_claim(true, 6, 1, mpin_labels[0]); system_bio_claim(true, 7, 1, mpin_labels[1]); @@ -241,31 +241,24 @@ void legacy_protocol(void) hwspi_select(); printf("\r\n>> "); int j = 0; - uint32_t total_bytes_spi = bytes_to_write > bytes_to_read ? bytes_to_write : bytes_to_read; - if (bytes_to_read) - { - total_bytes_spi++; - } + uint32_t total_bytes_spi = bytes_to_write + bytes_to_read; while (j < total_bytes_spi) { - if (j >= bytes_to_write) - { - tmpbuf[j] = 0x00; - } - printf("\r\n[%d] 0x%02X -> | ", j, tmpbuf[j]); - tmpbuf[j] = hwspi_write_read(tmpbuf[j]); - printf("<- 0x%02X", tmpbuf[j]); + //printf("\r\n[%d] 0x%02X -> | ", j, tmpbuf[j]); + tmpbuf[j] = hwspi_write_read(j >= bytes_to_write ? 0x00 : tmpbuf[j]); + //printf("<- 0x%02X", tmpbuf[j]); j++; } hwspi_deselect(); - tmpbuf[0] = '\x01'; - int bytes_sent = 0; int chunk_size = 32; int total_bytes = bytes_to_read + 1; int total_cdc_bytes_sended = 0; + uint32_t delta = bytes_to_write ? bytes_to_write - 1 : 1; + tmpbuf[delta] = '\x01'; tud_cdc_n_read_flush(1); + remain_bytes = 0; while (bytes_sent < total_bytes) { int bytes_left = total_bytes - bytes_sent; @@ -275,7 +268,7 @@ void legacy_protocol(void) tud_task(); tud_cdc_n_write_flush(1); } - total_cdc_bytes_sended += tud_cdc_n_write(1, tmpbuf + bytes_sent, current_chunk_size); + total_cdc_bytes_sended += tud_cdc_n_write(1, tmpbuf + delta + bytes_sent, current_chunk_size); tud_cdc_n_write_flush(1); bytes_sent += current_chunk_size; } @@ -291,6 +284,11 @@ void legacy_protocol(void) void legacy2third_mode(void){ static uint32_t mode_active=0; if (mode_active == 0){ + mode_active++; + system_config.binmode_usb_rx_queue_enable=false; + system_config.binmode_usb_tx_queue_enable=false; + } + else if (mode_active == 1){ mode_active++; uint8_t binmode_args = 1; binmode_debug_level(&binmode_args); @@ -298,6 +296,8 @@ void legacy2third_mode(void){ cdc_buff = (uint8_t*) mem_alloc(0x2000); remain_bytes = 0; legacy_protocol(); + system_config.binmode_usb_rx_queue_enable=true; + system_config.binmode_usb_tx_queue_enable=true; mem_free(cdc_buff); hwspi_deinit(); psu_disable(); From f30359d1c260c827c150370209e92d6ed623dccb Mon Sep 17 00:00:00 2001 From: David Reguera Garcia Date: Sat, 24 Aug 2024 04:03:35 +0200 Subject: [PATCH 3/8] asprogrammer dreg mod spi support --- binmode/legacy2third.c | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/binmode/legacy2third.c b/binmode/legacy2third.c index ca007419..46ff87c6 100644 --- a/binmode/legacy2third.c +++ b/binmode/legacy2third.c @@ -212,12 +212,19 @@ void legacy_protocol(void) break; case 0x03: + printf("\r\nhwspi_deselect"); + hwspi_deselect(); + CDC_SEND_STR(1, "\x01"); + break; + + case 0x02: printf("\r\nhwspi_select"); hwspi_select(); CDC_SEND_STR(1, "\x01"); break; case 0x04: + case 0x05: uint16_t bytes_to_read = 0; uint16_t bytes_to_write = 0; static uint8_t tmpbuf[0x2000]; @@ -236,27 +243,44 @@ void legacy_protocol(void) printf("\r\nbytes_to_write: %d", bytes_to_write); printf("\r\nbytes_to_read: %d", bytes_to_read); - while (!read_buff(tmpbuf, bytes_to_write, DEFAULT_MAX_TRIES)); + if (bytes_to_write) + { + while (!read_buff(tmpbuf, bytes_to_write, DEFAULT_MAX_TRIES)); + } - hwspi_select(); + if (0x04 == op_byte) + { + hwspi_select(); + } printf("\r\n>> "); int j = 0; uint32_t total_bytes_spi = bytes_to_write + bytes_to_read; while (j < total_bytes_spi) { - //printf("\r\n[%d] 0x%02X -> | ", j, tmpbuf[j]); + printf("\r\n[%d] 0x%02X -> | ", j, tmpbuf[j]); tmpbuf[j] = hwspi_write_read(j >= bytes_to_write ? 0x00 : tmpbuf[j]); - //printf("<- 0x%02X", tmpbuf[j]); + printf("<- 0x%02X", tmpbuf[j]); j++; } - hwspi_deselect(); + if (0x04 == op_byte) + { + hwspi_deselect(); + } int bytes_sent = 0; int chunk_size = 32; int total_bytes = bytes_to_read + 1; int total_cdc_bytes_sended = 0; - uint32_t delta = bytes_to_write ? bytes_to_write - 1 : 1; - tmpbuf[delta] = '\x01'; + uint32_t delta = bytes_to_write ? bytes_to_write - 1 : 0; + if (bytes_to_write) + { + tmpbuf[delta] = '\x01'; + } + else + { + CDC_SEND_STR(1, "\x01"); + total_bytes--; + } tud_cdc_n_read_flush(1); remain_bytes = 0; while (bytes_sent < total_bytes) From 75f9cf36f59c3fab2ce3251a79f9bb1bdb0adda1 Mon Sep 17 00:00:00 2001 From: David Reguera Garcia Date: Sun, 25 Aug 2024 04:12:42 +0200 Subject: [PATCH 4/8] avrdude working (beta yet) --- binmode/legacy2third.c | 125 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 119 insertions(+), 6 deletions(-) diff --git a/binmode/legacy2third.c b/binmode/legacy2third.c index 46ff87c6..bfbb307b 100644 --- a/binmode/legacy2third.c +++ b/binmode/legacy2third.c @@ -35,6 +35,10 @@ const char legacy2third_mode_name[]="Legacy Binary Mode For Third Parties (Flashrom and AVRdude)"; +#define TMPBUFF_SIZE 0x4000 +#define CDCBUFF_SIZE 0x4000 + +uint8_t* tmpbuf; uint8_t* cdc_buff; uint32_t remain_bytes; #define DEFAULT_MAX_TRIES 100000 @@ -96,6 +100,7 @@ void cdc_full_flush(uint32_t cdc_id) remain_bytes = 0; } + void legacy_protocol(void) { uint8_t op_byte; @@ -117,7 +122,12 @@ void legacy_protocol(void) if (op_byte) { count_zero = 0; // ugly, but simple - if (op_byte >= 0x60 && op_byte <= 0x67) // this must be the first + if (op_byte >= 0x10 && op_byte <= 0x1F) + { + extended_info = op_byte; + op_byte = 0x10; + } + else if (op_byte >= 0x60 && op_byte <= 0x67) // this must be the first { extended_info = op_byte; op_byte = 0x60; @@ -169,7 +179,8 @@ void legacy_protocol(void) printf("\r\npsu..."); if (extended_info & 0b00001000) { - uint8_t args[] = { 0x03, 0x21, 0x00, 0x80 }; + //uint8_t args[] = { 0x03, 0x21, 0x00, 0x80 }; + uint8_t args[] = { 0x05, 0x00, 0x00, 0x80 }; // 5v uint32_t result = binmode_psu_enable(args); if(result) { @@ -202,12 +213,13 @@ void legacy_protocol(void) "CS" }; - spi_init(SPI1_BASE, 10000000); // 10MHz + spi_init(SPI1_BASE, 100000); // ~0.1MHz hwspi_init(data_bits, cpol, cpha); system_bio_claim(true, 6, 1, mpin_labels[0]); system_bio_claim(true, 7, 1, mpin_labels[1]); system_bio_claim(true, 4, 1, mpin_labels[2]); system_bio_claim(true, 5, 1, mpin_labels[3]); + hwspi_select(); CDC_SEND_STR(1, "\x01"); break; @@ -223,13 +235,31 @@ void legacy_protocol(void) CDC_SEND_STR(1, "\x01"); break; + case 0x10: + printf("\r\nBulk SPI transfer"); + memset(tmpbuf, 0, TMPBUFF_SIZE); + uint32_t bytes2read = (extended_info & 0x0F) + 1; + printf("\r\nbytes_to_read: %d", bytes2read); + CDC_SEND_STR(1, "\x01"); + while (!read_buff(tmpbuf, bytes2read, DEFAULT_MAX_TRIES)); + printf("\r\n>> "); + for (int i = 0; i < bytes2read; i++) + { + printf("\r\n0x%02X | ", tmpbuf[i]); + tmpbuf[i] = hwspi_write_read(tmpbuf[i]); + printf("0x%02X ", tmpbuf[i]); + } + printf("\r\n"); + tud_cdc_n_write(1, tmpbuf, bytes2read); + tud_cdc_n_write_flush(1); + break; + case 0x04: case 0x05: uint16_t bytes_to_read = 0; uint16_t bytes_to_write = 0; - static uint8_t tmpbuf[0x2000]; - memset(tmpbuf, 0, sizeof(tmpbuf)); + memset(tmpbuf, 0, TMPBUFF_SIZE); while (!read_buff(tmpbuf, 4, DEFAULT_MAX_TRIES)); @@ -243,6 +273,13 @@ void legacy_protocol(void) printf("\r\nbytes_to_write: %d", bytes_to_write); printf("\r\nbytes_to_read: %d", bytes_to_read); + if (0x00 == bytes_to_read && 0x00 == bytes_to_write) + { + // for AVRDUDE + CDC_SEND_STR(1, "\x01"); + break; + } + if (bytes_to_write) { while (!read_buff(tmpbuf, bytes_to_write, DEFAULT_MAX_TRIES)); @@ -300,6 +337,77 @@ void legacy_protocol(void) printf("\r\ntotal_cdc_bytes_sended: %d", total_cdc_bytes_sended); tud_task(); break; + + case 0x06: // AVR EXTENDED COMMAND + CDC_SEND_STR(1, "\x01"); + while (!read_buff(&op_byte, 1, DEFAULT_MAX_TRIES)); + printf("\r\n-\r\nAVR op_byte=0x%02X", op_byte); + switch (op_byte) + { + case 0x00: + printf("\r\nAVR NOOP"); + CDC_SEND_STR(1, "\x01"); + break; + + case 0x01: + printf("\r\nAVR VERSION"); + CDC_SEND_STR(1, "\x01\x00\x01"); + break; + + case 0x02: + printf("\r\nAVR BULK READ"); + + memset(tmpbuf, 0, TMPBUFF_SIZE); + + while (!read_buff(tmpbuf, 8, DEFAULT_MAX_TRIES)); + + uint32_t addr = (tmpbuf[0] << 24) | (tmpbuf[1] << 16) | (tmpbuf[2] << 8) | tmpbuf[3]; + uint32_t len = (tmpbuf[4] << 24) | (tmpbuf[5] << 16) | (tmpbuf[6] << 8) | tmpbuf[7]; + + printf("\r\naddr: 0x%08X, len: 0x%08X", addr, len); + + if ((addr > 0xFFFF) || (len > 0xFFFF) || ((addr + len) > 0xFFFF)) + { + // error + CDC_SEND_STR(1, "\x00"); + break; + } + CDC_SEND_STR(1, "\x01"); + + printf("\r\n>> "); + while (len > 0) + { + hwspi_write_read(0x20); // AVR_FETCH_LOW_BYTE_COMMAND + hwspi_write_read((addr >> 8) & 0xFF); + hwspi_write_read(addr & 0xFF); + uint8_t byte_flash = hwspi_write_read(0x00); + printf("\r\n0x%02X", byte_flash); + tud_cdc_n_write_char(1, byte_flash); // Send the readed byte + tud_cdc_n_write_flush(1); + len--; + if (len > 0) + { + hwspi_write_read(0x28); // AVR_FETCH_HIGH_BYTE_COMMAND + hwspi_write_read((addr >> 8) & 0xFF); + hwspi_write_read(addr & 0xFF); + uint8_t byte_flash = hwspi_write_read(0x00); + printf("\r\n0x%02X", byte_flash); + tud_cdc_n_write_char(1, byte_flash); // Send the readed byte + tud_cdc_n_write_flush(1); + len--; + } + addr++; + } + printf("\r\n"); + break; + + default: + // error + CDC_SEND_STR(1, "\x00"); + break; + } + + break; } } } @@ -317,8 +425,13 @@ void legacy2third_mode(void){ uint8_t binmode_args = 1; binmode_debug_level(&binmode_args); script_enabled(); - cdc_buff = (uint8_t*) mem_alloc(0x2000); + cdc_buff = (uint8_t*) mem_alloc(CDCBUFF_SIZE + TMPBUFF_SIZE); + printf("\r\ncdc_buff: 0x%08X\r\n", cdc_buff); + tmpbuf = cdc_buff + CDCBUFF_SIZE; + memset(cdc_buff, 0, CDCBUFF_SIZE); + memset(tmpbuf, 0, TMPBUFF_SIZE); remain_bytes = 0; + cdc_full_flush(1); legacy_protocol(); system_config.binmode_usb_rx_queue_enable=true; system_config.binmode_usb_tx_queue_enable=true; From a43a93ddebbe9d6f4df390830bbe1741063eaf27 Mon Sep 17 00:00:00 2001 From: David Reguera Garcia Date: Mon, 26 Aug 2024 19:47:42 +0200 Subject: [PATCH 5/8] fixed! --- CMakeLists.txt | 2 +- binmode/binmodes.c | 6 +- binmode/binmodes.h | 2 +- binmode/legacy2third.c | 448 ---------------------- binmode/legacy2third.h | 2 - binmode/legacy4third.c | 833 +++++++++++++++++++++++++++++++++++++++++ binmode/legacy4third.h | 2 + 7 files changed, 840 insertions(+), 455 deletions(-) delete mode 100644 binmode/legacy2third.c delete mode 100644 binmode/legacy2third.h create mode 100644 binmode/legacy4third.c create mode 100644 binmode/legacy4third.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 84e107b1..6c1c52b0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -229,7 +229,7 @@ set(bp5_common mode/binloopback.c mode/binloopback.h binmode/logicanalyzer.h binmode/logicanalyzer.c binmode/sump.c binmode/sump.h binmode/dirtyproto.c binmode/dirtyproto.h - binmode/legacy2third.c binmode/legacy2third.h + binmode/legacy4third.c binmode/legacy4third.h lib/arduino-ch32v003-swio/arduino_ch32v003.c lib/arduino-ch32v003-swio/arduino_ch32v003.h lib/arduino-ch32v003-swio/swio.h lib/arduino-ch32v003-swio/swio.c lib/picorvd/picoswio.c lib/picorvd/picoswio.h lib/picorvd/debug_defines.h diff --git a/binmode/binmodes.c b/binmode/binmodes.c index ef2e9d02..a0312477 100644 --- a/binmode/binmodes.c +++ b/binmode/binmodes.c @@ -5,7 +5,7 @@ #include "system_config.h" #include "binmode/sump.h" #include "binmode/dirtyproto.h" -#include "binmode/legacy2third.h" +#include "binmode/legacy4third.h" #include "lib/arduino-ch32v003-swio/arduino_ch32v003.h" void binmode_null_func(void) { @@ -31,9 +31,9 @@ const binmode_t binmodes[]={ }, { &binmode_null_func, - &legacy2third_mode, + &legacy4third_mode, &binmode_null_func, - legacy2third_mode_name, + legacy4third_mode_name, }, }; diff --git a/binmode/binmodes.h b/binmode/binmodes.h index 212525e5..9fe5ca20 100644 --- a/binmode/binmodes.h +++ b/binmode/binmodes.h @@ -6,7 +6,7 @@ enum { BINMODE_USE_SUMPLA=0, BINMODE_USE_DIRTYPROTO, BINMODE_USE_ARDUINO_CH32V003_SWIO, - BINMODE_USE_LEGACY2THIRD, + BINMODE_USE_LEGACY4THIRD, BINMODE_MAXPROTO }; diff --git a/binmode/legacy2third.c b/binmode/legacy2third.c deleted file mode 100644 index bfbb307b..00000000 --- a/binmode/legacy2third.c +++ /dev/null @@ -1,448 +0,0 @@ -/* - * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). - * - * Written and maintained by the Bus Pirate project. - * - * To the extent possible under law, the project has - * waived all copyright and related or neighboring rights to Bus Pirate. This - * work is published from United States. - * - * For details see: http://creativecommons.org/publicdomain/zero/1.0/. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - */ - -/* Legacy Binary Mode for third parties */ - -//#include -#include -#include "pico/stdlib.h" -#include "pirate.h" -#include "queue.h" -#include "usb_rx.h" -#include "usb_tx.h" -#include "pirate.h" -#include "opt_args.h" //needed for same reason as bytecode and needs same fix -#include "bytecode.h" -#include "modes.h" -#include "binio_helpers.h" -#include "tusb.h" -#include "binmode/binio.h" -#include "system_config.h" - - -const char legacy2third_mode_name[]="Legacy Binary Mode For Third Parties (Flashrom and AVRdude)"; - -#define TMPBUFF_SIZE 0x4000 -#define CDCBUFF_SIZE 0x4000 - -uint8_t* tmpbuf; -uint8_t* cdc_buff; -uint32_t remain_bytes; -#define DEFAULT_MAX_TRIES 100000 - -uint32_t read_buff(uint8_t *buf, uint32_t len, uint32_t max_tries) -{ - uint32_t pending_data = 0; - uint32_t bytes_readed = 0; - uint32_t total_bytes_readed = 0; - - if (remain_bytes > 0) - { - bytes_readed = remain_bytes >= len ? len : remain_bytes; - memcpy(buf, cdc_buff, bytes_readed); - remain_bytes -= bytes_readed; - - if (remain_bytes > 0) - { - memmove(cdc_buff, cdc_buff + bytes_readed, remain_bytes); - } - - total_bytes_readed = bytes_readed; - } - - while (total_bytes_readed < len && max_tries--) - { - pending_data = tud_cdc_n_available(1); - if (pending_data > 0) - { - bytes_readed = tud_cdc_n_read(1, cdc_buff + remain_bytes, pending_data); - tud_task(); - remain_bytes += bytes_readed; - uint32_t bytes_to_copy = len - total_bytes_readed; - if (remain_bytes < bytes_to_copy) - { - bytes_to_copy = remain_bytes; - } - - memcpy(buf + total_bytes_readed, cdc_buff, bytes_to_copy); - total_bytes_readed += bytes_to_copy; - remain_bytes -= bytes_to_copy; - - if (remain_bytes > 0) - { - memmove(cdc_buff, cdc_buff + bytes_to_copy, remain_bytes); - } - } - } - - return total_bytes_readed; -} - -#define CDC_SEND_STR(cdc_n, str) tud_cdc_n_write(cdc_n, (uint8_t*)str, sizeof(str) - 1); tud_cdc_n_write_flush(1); - -void cdc_full_flush(uint32_t cdc_id) -{ - tud_cdc_n_read_flush(cdc_id); - tud_cdc_n_write_flush(cdc_id); - remain_bytes = 0; -} - - -void legacy_protocol(void) -{ - uint8_t op_byte; - uint8_t extended_info; - uint8_t count_zero = 0; - uint32_t spi_speed = 0; - - cdc_full_flush(1); - - while (1) - { - op_byte = 0; - extended_info = 0; - tud_task(); - while (!read_buff(&op_byte, 1, DEFAULT_MAX_TRIES)); - printf("\r\n-\r\nop_byte=0x%02X", op_byte); - printf(", extended_info=0x%02X", extended_info); - - if (op_byte) - { - count_zero = 0; // ugly, but simple - if (op_byte >= 0x10 && op_byte <= 0x1F) - { - extended_info = op_byte; - op_byte = 0x10; - } - else if (op_byte >= 0x60 && op_byte <= 0x67) // this must be the first - { - extended_info = op_byte; - op_byte = 0x60; - } - else if (op_byte & 0x80) - { - extended_info = op_byte; - op_byte = 0x80; - } - else if (op_byte & 0x40) - { - extended_info = op_byte; - op_byte = 0x40; - } - } - - printf("\r\nop_byte=0x%02X", op_byte); - printf(", extended_info=0x%02X", extended_info); - switch (op_byte) - { - case 0x00: - if (!count_zero) - { - printf("\r\nBBIO1->"); - CDC_SEND_STR(1, "BBIO1"); - spi_speed = 0; - hwspi_deinit(); - psu_disable(); - } - else if (count_zero > 15) - { - count_zero = 0; - } - count_zero++; - break; - - case 0x0F: - printf("\r\nBus Pirate CLI prompt->"); - // ugly hack for fixed baudarate (look flashrom src!): - CDC_SEND_STR(1, "Bus Pirate v2.5\r\nCommunity Firmware v7.1\r\nHiZ>"); - break; - - case 0x01: - printf("\r\nSPI1->"); - CDC_SEND_STR(1, "SPI1"); - break; - - case 0x40: - printf("\r\npsu..."); - if (extended_info & 0b00001000) - { - //uint8_t args[] = { 0x03, 0x21, 0x00, 0x80 }; - uint8_t args[] = { 0x05, 0x00, 0x00, 0x80 }; // 5v - uint32_t result = binmode_psu_enable(args); - if(result) - { - printf("\r\nPSU ERROR CODE %d", result); - CDC_SEND_STR(1, "\x00"); - } - else - { - printf("\r\nPSU Enabled"); - CDC_SEND_STR(1, "\x01"); - } - } - break; - - case 0x60: - printf("\r\nspi_speed"); - spi_speed = 1000000; - CDC_SEND_STR(1, "\x01"); - break; - - case 0x80: - printf("\r\nhwspi_init"); - uint8_t data_bits = 8; - uint8_t cpol = 0; - uint8_t cpha = 0; - static const char mpin_labels[][5]={ - "CLK", - "MOSI", - "MISO", - "CS" - }; - - spi_init(SPI1_BASE, 100000); // ~0.1MHz - hwspi_init(data_bits, cpol, cpha); - system_bio_claim(true, 6, 1, mpin_labels[0]); - system_bio_claim(true, 7, 1, mpin_labels[1]); - system_bio_claim(true, 4, 1, mpin_labels[2]); - system_bio_claim(true, 5, 1, mpin_labels[3]); - hwspi_select(); - CDC_SEND_STR(1, "\x01"); - break; - - case 0x03: - printf("\r\nhwspi_deselect"); - hwspi_deselect(); - CDC_SEND_STR(1, "\x01"); - break; - - case 0x02: - printf("\r\nhwspi_select"); - hwspi_select(); - CDC_SEND_STR(1, "\x01"); - break; - - case 0x10: - printf("\r\nBulk SPI transfer"); - memset(tmpbuf, 0, TMPBUFF_SIZE); - uint32_t bytes2read = (extended_info & 0x0F) + 1; - printf("\r\nbytes_to_read: %d", bytes2read); - CDC_SEND_STR(1, "\x01"); - while (!read_buff(tmpbuf, bytes2read, DEFAULT_MAX_TRIES)); - printf("\r\n>> "); - for (int i = 0; i < bytes2read; i++) - { - printf("\r\n0x%02X | ", tmpbuf[i]); - tmpbuf[i] = hwspi_write_read(tmpbuf[i]); - printf("0x%02X ", tmpbuf[i]); - } - printf("\r\n"); - tud_cdc_n_write(1, tmpbuf, bytes2read); - tud_cdc_n_write_flush(1); - break; - - case 0x04: - case 0x05: - uint16_t bytes_to_read = 0; - uint16_t bytes_to_write = 0; - - memset(tmpbuf, 0, TMPBUFF_SIZE); - - while (!read_buff(tmpbuf, 4, DEFAULT_MAX_TRIES)); - - printf("\r\nbytes_to_write H: 0x%02X", tmpbuf[0]); - printf("\r\nbytes_to_write L: 0x%02x", tmpbuf[1]); - printf("\r\nbytes_to_read H: 0x%02X", tmpbuf[2]); - printf("\r\nbytes_to_read L: 0x%02x", tmpbuf[3]); - - bytes_to_write = (tmpbuf[0] << 8) | tmpbuf[1]; - bytes_to_read = (tmpbuf[2] << 8) | tmpbuf[3]; - printf("\r\nbytes_to_write: %d", bytes_to_write); - printf("\r\nbytes_to_read: %d", bytes_to_read); - - if (0x00 == bytes_to_read && 0x00 == bytes_to_write) - { - // for AVRDUDE - CDC_SEND_STR(1, "\x01"); - break; - } - - if (bytes_to_write) - { - while (!read_buff(tmpbuf, bytes_to_write, DEFAULT_MAX_TRIES)); - } - - if (0x04 == op_byte) - { - hwspi_select(); - } - printf("\r\n>> "); - int j = 0; - uint32_t total_bytes_spi = bytes_to_write + bytes_to_read; - while (j < total_bytes_spi) - { - printf("\r\n[%d] 0x%02X -> | ", j, tmpbuf[j]); - tmpbuf[j] = hwspi_write_read(j >= bytes_to_write ? 0x00 : tmpbuf[j]); - printf("<- 0x%02X", tmpbuf[j]); - j++; - } - if (0x04 == op_byte) - { - hwspi_deselect(); - } - - int bytes_sent = 0; - int chunk_size = 32; - int total_bytes = bytes_to_read + 1; - int total_cdc_bytes_sended = 0; - uint32_t delta = bytes_to_write ? bytes_to_write - 1 : 0; - if (bytes_to_write) - { - tmpbuf[delta] = '\x01'; - } - else - { - CDC_SEND_STR(1, "\x01"); - total_bytes--; - } - tud_cdc_n_read_flush(1); - remain_bytes = 0; - while (bytes_sent < total_bytes) - { - int bytes_left = total_bytes - bytes_sent; - int current_chunk_size = (bytes_left < chunk_size) ? bytes_left : chunk_size; - while (tud_cdc_n_write_available(1) < current_chunk_size) - { - tud_task(); - tud_cdc_n_write_flush(1); - } - total_cdc_bytes_sended += tud_cdc_n_write(1, tmpbuf + delta + bytes_sent, current_chunk_size); - tud_cdc_n_write_flush(1); - bytes_sent += current_chunk_size; - } - - printf("\r\ntotal_cdc_bytes_sended: %d", total_cdc_bytes_sended); - tud_task(); - break; - - case 0x06: // AVR EXTENDED COMMAND - CDC_SEND_STR(1, "\x01"); - while (!read_buff(&op_byte, 1, DEFAULT_MAX_TRIES)); - printf("\r\n-\r\nAVR op_byte=0x%02X", op_byte); - switch (op_byte) - { - case 0x00: - printf("\r\nAVR NOOP"); - CDC_SEND_STR(1, "\x01"); - break; - - case 0x01: - printf("\r\nAVR VERSION"); - CDC_SEND_STR(1, "\x01\x00\x01"); - break; - - case 0x02: - printf("\r\nAVR BULK READ"); - - memset(tmpbuf, 0, TMPBUFF_SIZE); - - while (!read_buff(tmpbuf, 8, DEFAULT_MAX_TRIES)); - - uint32_t addr = (tmpbuf[0] << 24) | (tmpbuf[1] << 16) | (tmpbuf[2] << 8) | tmpbuf[3]; - uint32_t len = (tmpbuf[4] << 24) | (tmpbuf[5] << 16) | (tmpbuf[6] << 8) | tmpbuf[7]; - - printf("\r\naddr: 0x%08X, len: 0x%08X", addr, len); - - if ((addr > 0xFFFF) || (len > 0xFFFF) || ((addr + len) > 0xFFFF)) - { - // error - CDC_SEND_STR(1, "\x00"); - break; - } - CDC_SEND_STR(1, "\x01"); - - printf("\r\n>> "); - while (len > 0) - { - hwspi_write_read(0x20); // AVR_FETCH_LOW_BYTE_COMMAND - hwspi_write_read((addr >> 8) & 0xFF); - hwspi_write_read(addr & 0xFF); - uint8_t byte_flash = hwspi_write_read(0x00); - printf("\r\n0x%02X", byte_flash); - tud_cdc_n_write_char(1, byte_flash); // Send the readed byte - tud_cdc_n_write_flush(1); - len--; - if (len > 0) - { - hwspi_write_read(0x28); // AVR_FETCH_HIGH_BYTE_COMMAND - hwspi_write_read((addr >> 8) & 0xFF); - hwspi_write_read(addr & 0xFF); - uint8_t byte_flash = hwspi_write_read(0x00); - printf("\r\n0x%02X", byte_flash); - tud_cdc_n_write_char(1, byte_flash); // Send the readed byte - tud_cdc_n_write_flush(1); - len--; - } - addr++; - } - printf("\r\n"); - break; - - default: - // error - CDC_SEND_STR(1, "\x00"); - break; - } - - break; - } - } -} - -// handler needs to be cooperative multitasking until mode is enabled -void legacy2third_mode(void){ - static uint32_t mode_active=0; - if (mode_active == 0){ - mode_active++; - system_config.binmode_usb_rx_queue_enable=false; - system_config.binmode_usb_tx_queue_enable=false; - } - else if (mode_active == 1){ - mode_active++; - uint8_t binmode_args = 1; - binmode_debug_level(&binmode_args); - script_enabled(); - cdc_buff = (uint8_t*) mem_alloc(CDCBUFF_SIZE + TMPBUFF_SIZE); - printf("\r\ncdc_buff: 0x%08X\r\n", cdc_buff); - tmpbuf = cdc_buff + CDCBUFF_SIZE; - memset(cdc_buff, 0, CDCBUFF_SIZE); - memset(tmpbuf, 0, TMPBUFF_SIZE); - remain_bytes = 0; - cdc_full_flush(1); - legacy_protocol(); - system_config.binmode_usb_rx_queue_enable=true; - system_config.binmode_usb_tx_queue_enable=true; - mem_free(cdc_buff); - hwspi_deinit(); - psu_disable(); - } -} - - - - - - diff --git a/binmode/legacy2third.h b/binmode/legacy2third.h deleted file mode 100644 index f68dca04..00000000 --- a/binmode/legacy2third.h +++ /dev/null @@ -1,2 +0,0 @@ -void legacy2third_mode(void); -extern const char legacy2third_mode_name[]; \ No newline at end of file diff --git a/binmode/legacy4third.c b/binmode/legacy4third.c new file mode 100644 index 00000000..32c856cc --- /dev/null +++ b/binmode/legacy4third.c @@ -0,0 +1,833 @@ +/* + * This file is part of the Bus Pirate project (http://code.google.com/p/the-bus-pirate/). + * + * Written and maintained by the Bus Pirate project. + * + * To the extent possible under law, the project has + * waived all copyright and related or neighboring rights to Bus Pirate. This + * work is published from United States. + * + * For details see: http://creativecommons.org/publicdomain/zero/1.0/. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + */ + +/* Legacy Binary Mode for third parties */ + + +/* ************************ +WARNING: It's very easy to break this code if you don't know what you're doing. +There are things that might seem unnecessary, but they're not! Be very careful! +************************ */ + +// VERY EXPERIMENTAL & BETA + + +//#include +#include +#include +#include "pico/stdlib.h" +#include "pirate.h" +#include "queue.h" +#include "usb_rx.h" +#include "usb_tx.h" +#include "pirate.h" +#include "opt_args.h" //needed for same reason as bytecode and needs same fix +#include "bytecode.h" +#include "modes.h" +#include "binio_helpers.h" +#include "tusb.h" +#include "binmode/binio.h" +#include "system_config.h" +#include "ui/ui_prompt.h" +#include "ui/ui_term.h" +#include "pirate/hwspi.h" +#include "pirate/mem.h" + +const char legacy4third_mode_name[]="Legacy Binary Mode for Flashrom and AVRdude (EXPERIMENTAL)"; + +#define TMPBUFF_SIZE 0x4000 +#define CDCBUFF_SIZE 0x4000 +#define DEFAULT_MAX_TRIES 100000 + +static uint8_t volts_integer; +static uint8_t volts_decimal; +static uint16_t current_integer; +static uint8_t current_decimal; +static uint8_t* tmpbuf; +static uint8_t* cdc_buff; +static uint32_t remain_bytes; + + +void disable_psu_legacy(void) +{ + uint8_t binmode_args = 0x00; + uint32_t result = binmode_psu_disable(&binmode_args); +} + +void enable_psu_legacy(void) +{ + uint8_t args[] = { 0x05, 0x00, 0x00, 0x80 }; // 5v + uint32_t result = binmode_psu_enable(args); +} + +void setup_spi_legacy(uint32_t spi_speed, uint8_t data_bits, uint8_t cpol, uint8_t cpah, uint8_t cs) +{ + /* + Example: + data_bits = 0x08; + uint8_t cpol = 0x00; + uint8_t cpah = 0x00; + uint8_t cs = 0x01; + uint32_t spi_speed = 10000000; // ~10Mhz + */ + uint8_t spi_binmode_args[] = { + (spi_speed >> 24) & 0xFF, + (spi_speed >> 16) & 0xFF, + (spi_speed >> 8) & 0xFF, + spi_speed & 0xFF, + data_bits, + cpol, + cpah, + cs + }; + mode_change((uint8_t*)"SPI"); + binmode_config(spi_binmode_args); + system_config.binmode_usb_rx_queue_enable=false; + system_config.binmode_usb_tx_queue_enable=false; +} + +void enable_debug_legacy(void) +{ + uint8_t binmode_args = 1; + binmode_debug_level(&binmode_args); +} + +uint32_t read_buff(uint8_t *buf, uint32_t len, uint32_t max_tries) +{ + uint32_t pending_data = 0; + uint32_t bytes_readed = 0; + uint32_t total_bytes_readed = 0; + + if (remain_bytes > 0) + { + bytes_readed = remain_bytes >= len ? len : remain_bytes; + memcpy(buf, cdc_buff, bytes_readed); + remain_bytes -= bytes_readed; + + if (remain_bytes > 0) + { + memmove(cdc_buff, cdc_buff + bytes_readed, remain_bytes); + } + + total_bytes_readed = bytes_readed; + } + + while (total_bytes_readed < len && max_tries--) + { + pending_data = tud_cdc_n_available(1); + if (pending_data > 0) + { + bytes_readed = tud_cdc_n_read(1, cdc_buff + remain_bytes, pending_data); + tud_task(); + remain_bytes += bytes_readed; + uint32_t bytes_to_copy = len - total_bytes_readed; + if (remain_bytes < bytes_to_copy) + { + bytes_to_copy = remain_bytes; + } + + memcpy(buf + total_bytes_readed, cdc_buff, bytes_to_copy); + total_bytes_readed += bytes_to_copy; + remain_bytes -= bytes_to_copy; + + if (remain_bytes > 0) + { + memmove(cdc_buff, cdc_buff + bytes_to_copy, remain_bytes); + } + } + } + + return total_bytes_readed; +} + +#define CDC_SEND_STR(cdc_n, str) tud_cdc_n_write(cdc_n, (uint8_t*)str, sizeof(str) - 1); tud_cdc_n_write_flush(1); + +void cdc_full_flush(uint32_t cdc_id) +{ + tud_cdc_n_read_flush(cdc_id); + tud_cdc_n_write_flush(cdc_id); + remain_bytes = 0; +} + +void reset_legacy(void) +{ + hwspi_deinit(); + disable_psu_legacy(); + uint8_t binmode_args = 0; + binmode_pullup_disable(&binmode_args); + binmode_reset(&binmode_args); + mode_change((uint8_t*)"HiZ"); +} + + +void legacy_protocol(void) +{ + uint8_t op_byte; + uint8_t extended_info; + uint8_t count_zero = 0; + uint32_t spi_speed = 0; + uint8_t cs_init = 0x01; + + cdc_full_flush(1); + + while (1) + { + op_byte = 0; + extended_info = 0; + tud_task(); + while (!read_buff(&op_byte, 1, DEFAULT_MAX_TRIES)); + if (binmode_debug) + { + printf("\r\n-\r\nop_byte=0x%02X", op_byte); + printf(", extended_info=0x%02X", extended_info); + } + + if (op_byte) + { + count_zero = 0; // ugly, but simple + if (op_byte >= 0x10 && op_byte <= 0x1F) + { + extended_info = op_byte; + op_byte = 0x10; + } + else if (op_byte >= 0x60 && op_byte <= 0x67) // this must be the first + { + extended_info = op_byte; + op_byte = 0x60; + } + else if (op_byte & 0x80) + { + extended_info = op_byte; + op_byte = 0x80; + } + else if (op_byte & 0x40) + { + extended_info = op_byte; + op_byte = 0x40; + } + } + + if (binmode_debug) + { + printf("\r\nop_byte=0x%02X", op_byte); + printf(", extended_info=0x%02X", extended_info); + } + switch (op_byte) + { + case 0x00: + if (!count_zero) + { + if (binmode_debug) + { + printf("\r\nBBIO1->"); + } + CDC_SEND_STR(1, "BBIO1"); + spi_speed = 0; + reset_legacy(); + system_config.binmode_usb_rx_queue_enable=false; + system_config.binmode_usb_tx_queue_enable=false; + } + else if (count_zero > 15) + { + count_zero = 0; + } + count_zero++; + break; + + case 0x0F: + if (binmode_debug) + { + printf("\r\nBus Pirate CLI prompt"); + } + // ugly hack for fixed baudarate (look flashrom src!): + CDC_SEND_STR(1, "Bus Pirate v2.5\r\nCommunity Firmware v7.1\r\nHiZ>"); + break; + + case 0x01: + if (binmode_debug) + { + printf("\r\nSPI1->"); + } + CDC_SEND_STR(1, "SPI1"); + break; + + case 0x40: + if (binmode_debug) + { + printf("\r\npsu..."); + } + + // PSU + if ((extended_info & 0b00001000) == 0) + { + disable_psu_legacy(); + } + else if (extended_info & 0b00001000) + { + //uint8_t args[] = { 0x03, 0x21, 0x00, 0x80 }; + //uint8_t args[] = { 0x05, 0x00, 0x00, 0x80 }; // 5v + uint8_t args[] = { + volts_integer, + volts_decimal, + (uint8_t)(current_integer >> 8), + (uint8_t)(current_integer & 0xFF) + }; + uint32_t result = binmode_psu_enable(args); + if(result) + { + if (binmode_debug) + { + printf("\r\nPSU ERROR CODE %d", result); + } + } + else + { + if (binmode_debug) + { + printf("\r\nPSU Enabled"); + } + } + } + + // Pull-ups + if ((extended_info & 0b00000100) == 0) + { + if (binmode_debug) + { + printf("\r\npullup_disable"); + } + uint8_t binmode_args = 0; + binmode_pullup_disable(&binmode_args); + } + else if (extended_info & 0b00000100) + { + if (binmode_debug) + { + printf("\r\npullup_enable"); + } + uint8_t binmode_args = 0; + binmode_pullup_enable(&binmode_args); + } + + // AUX + if ((extended_info & 0b00000010) == 0) + { + if (binmode_debug) + { + printf("\r\naux_disable"); + } + } + else if (extended_info & 0b00000010) + { + if (binmode_debug) + { + printf("\r\naux_enable"); + } + } + + // CS + if ((extended_info & 0b00000001) == 0) + { + if (binmode_debug) + { + printf("\r\ncs 0"); + } + cs_init = 0x00; + } + else if (extended_info & 0b00000001) + { + if (binmode_debug) + { + printf("\r\ncs 1"); + } + cs_init = 0x01; + } + + CDC_SEND_STR(1, "\x01"); + break; + + case 0x60: + if (binmode_debug) + { + printf("\r\nspi_speed"); + } + switch (extended_info & 0x9F) + { + case 0b00: // 30kHz + spi_speed = 30000; + break; + + case 0b01: // 125kHz + spi_speed = 125000; + break; + + case 0b10: // 250kHz + spi_speed = 250000; + break; + + case 0b11: // 1MHz + spi_speed = 1000000; + break; + + case 0b100: // 2MHz + spi_speed = 2000000; + break; + + case 0b101: // 2.6MHz + spi_speed = 2600000; + break; + + case 0b110: // 4MHz + spi_speed = 4000000; + break; + + case 0b111: // 8MHz + spi_speed = 8000000; + break; + + default: + spi_speed = 0; + break; + } + if (binmode_debug) + { + printf("\r\nspi_speed: %d", spi_speed); + } + CDC_SEND_STR(1, "\x01"); + break; + + case 0x80: + if (binmode_debug) + { + printf("\r\nhwspi_init"); + } + + // SMP sample time (middle=0) + if ((extended_info & 0x1) == 0) + { + if (binmode_debug) + { + printf("\r\nSMP 0"); + } + } + else if (extended_info & 0x1) + { + if (binmode_debug) + { + printf("\r\nSMP 1"); + } + } + + // CKE clock edge (active to idle=1) + if ((extended_info & 0x2) == 0) + { + if (binmode_debug) + { + printf("\r\nCKE 0"); + } + } + else if (extended_info & 0x2) + { + if (binmode_debug) + { + printf("\r\nCKE 1"); + } + } + + // CKP clock idle phase (low=0) + if ((extended_info & 0x4) == 0) + { + if (binmode_debug) + { + printf("\r\nCKP 0"); + } + } + else if (extended_info & 0x4) + { + if (binmode_debug) + { + printf("\r\nCKP 1"); + } + } + + // Output HiZ(0)/3.3v(1) + if ((extended_info & 0x8) == 0) + { + if (binmode_debug) + { + printf("\r\nHiZ"); + } + } + else if (extended_info & 0x8) + { + if (binmode_debug) + { + printf("\r\n3.3v"); + } + } + + setup_spi_legacy(spi_speed, 8, 0, 0, cs_init); + hwspi_select(); + CDC_SEND_STR(1, "\x01"); + + /* + uint8_t data_bits = 8; + uint8_t cpol = 0; + uint8_t cpha = 0; + static const char mpin_labels[][5]={ + "CLK", + "MOSI", + "MISO", + "CS" + }; + + spi_init(SPI1_BASE, 100000); // ~0.1MHz + hwspi_init(data_bits, cpol, cpha); + system_bio_claim(true, 6, 1, mpin_labels[0]); + system_bio_claim(true, 7, 1, mpin_labels[1]); + system_bio_claim(true, 4, 1, mpin_labels[2]); + system_bio_claim(true, 5, 1, mpin_labels[3]); + */ + break; + + case 0x03: + if (binmode_debug) + { + printf("\r\nhwspi_deselect"); + } + hwspi_deselect(); + CDC_SEND_STR(1, "\x01"); + break; + + case 0x02: + if (binmode_debug) + { + printf("\r\nhwspi_select"); + } + hwspi_select(); + CDC_SEND_STR(1, "\x01"); + break; + + case 0x10: + if (binmode_debug) + { + printf("\r\nBulk SPI transfer"); + } + memset(tmpbuf, 0, TMPBUFF_SIZE); + uint32_t bytes2read = (extended_info & 0x0F) + 1; + if (binmode_debug) + { + printf("\r\nbytes_to_read: %d", bytes2read); + } + CDC_SEND_STR(1, "\x01"); + while (!read_buff(tmpbuf, bytes2read, DEFAULT_MAX_TRIES)); + if (binmode_debug) + { + printf("\r\n>> "); + } + for (int i = 0; i < bytes2read; i++) + { + if (binmode_debug) + { + printf("\r\n0x%02X | ", tmpbuf[i]); + } + tmpbuf[i] = hwspi_write_read(tmpbuf[i]); + if (binmode_debug) + { + printf("0x%02X", tmpbuf[i]); + } + } + if (binmode_debug) + { + printf("\r\n"); + } + tud_cdc_n_write(1, tmpbuf, bytes2read); + tud_cdc_n_write_flush(1); + break; + + // SPI b00000100 (0x04) - Write then read & b00000101 (0x05) - Write then read, no CS + case 0x04: + case 0x05: + uint16_t bytes_to_read = 0; + uint16_t bytes_to_write = 0; + + memset(tmpbuf, 0, TMPBUFF_SIZE); + + while (!read_buff(tmpbuf, 4, DEFAULT_MAX_TRIES)); + if (binmode_debug) + { + printf("\r\nbytes_to_write H: 0x%02X", tmpbuf[0]); + printf("\r\nbytes_to_write L: 0x%02x", tmpbuf[1]); + printf("\r\nbytes_to_read H: 0x%02X", tmpbuf[2]); + printf("\r\nbytes_to_read L: 0x%02x", tmpbuf[3]); + } + bytes_to_write = (tmpbuf[0] << 8) | tmpbuf[1]; + bytes_to_read = (tmpbuf[2] << 8) | tmpbuf[3]; + if (binmode_debug) + { + printf("\r\nbytes_to_write: %d", bytes_to_write); + printf("\r\nbytes_to_read: %d", bytes_to_read); + } + + if (0x00 == bytes_to_read && 0x00 == bytes_to_write) + { + // for AVRDUDE + CDC_SEND_STR(1, "\x01"); + break; + } + + if (bytes_to_write) + { + while (!read_buff(tmpbuf, bytes_to_write, DEFAULT_MAX_TRIES)); + } + + if (0x04 == op_byte) + { + hwspi_select(); + } + if (binmode_debug) + { + printf("\r\n>> "); + } + int j = 0; + uint32_t total_bytes_spi = bytes_to_write + bytes_to_read; + while (j < total_bytes_spi) + { + if (binmode_debug) + { + printf("\r\n[%d] 0x%02X -> | ", j, tmpbuf[j]); + } + tmpbuf[j] = hwspi_write_read(j >= bytes_to_write ? 0x00 : tmpbuf[j]); + if (binmode_debug) + { + printf("<- 0x%02X", tmpbuf[j]); + } + j++; + } + if (0x04 == op_byte) + { + hwspi_deselect(); + } + + int bytes_sent = 0; + int chunk_size = 32; + int total_bytes = bytes_to_read + 1; + int total_cdc_bytes_sended = 0; + uint32_t delta = bytes_to_write ? bytes_to_write - 1 : 0; + if (bytes_to_write) + { + tmpbuf[delta] = '\x01'; + } + else + { + CDC_SEND_STR(1, "\x01"); + total_bytes--; + } + tud_cdc_n_read_flush(1); + remain_bytes = 0; + while (bytes_sent < total_bytes) + { + int bytes_left = total_bytes - bytes_sent; + int current_chunk_size = (bytes_left < chunk_size) ? bytes_left : chunk_size; + while (tud_cdc_n_write_available(1) < current_chunk_size) + { + tud_task(); + tud_cdc_n_write_flush(1); + } + total_cdc_bytes_sended += tud_cdc_n_write(1, tmpbuf + delta + bytes_sent, current_chunk_size); + tud_cdc_n_write_flush(1); + bytes_sent += current_chunk_size; + } + if (binmode_debug) + { + printf("\r\ntotal_cdc_bytes_sended: %d", total_cdc_bytes_sended); + } + tud_task(); + break; + + case 0x06: // AVR EXTENDED COMMAND + CDC_SEND_STR(1, "\x01"); + while (!read_buff(&op_byte, 1, DEFAULT_MAX_TRIES)); + if (binmode_debug) + { + printf("\r\n-\r\nAVR op_byte=0x%02X", op_byte); + } + switch (op_byte) + { + case 0x00: + if (binmode_debug) + { + printf("\r\nAVR NOOP"); + } + CDC_SEND_STR(1, "\x01"); + break; + + case 0x01: + if (binmode_debug) + { + printf("\r\nAVR VERSION"); + } + CDC_SEND_STR(1, "\x01\x00\x01"); + break; + + case 0x02: + if (binmode_debug) + { + printf("\r\nAVR BULK READ"); + } + + memset(tmpbuf, 0, TMPBUFF_SIZE); + + while (!read_buff(tmpbuf, 8, DEFAULT_MAX_TRIES)); + + uint32_t addr = (tmpbuf[0] << 24) | (tmpbuf[1] << 16) | (tmpbuf[2] << 8) | tmpbuf[3]; + uint32_t len = (tmpbuf[4] << 24) | (tmpbuf[5] << 16) | (tmpbuf[6] << 8) | tmpbuf[7]; + + if (binmode_debug) + { + printf("\r\naddr: 0x%08X, len: 0x%08X", addr, len); + } + + if ((addr > 0xFFFF) || (len > 0xFFFF) || ((addr + len) > 0xFFFF)) + { + // error + CDC_SEND_STR(1, "\x00"); + break; + } + CDC_SEND_STR(1, "\x01"); + + if (binmode_debug) + { + printf("\r\n>> "); + } + while (len > 0) + { + hwspi_write_read(0x20); // AVR_FETCH_LOW_BYTE_COMMAND + hwspi_write_read((addr >> 8) & 0xFF); + hwspi_write_read(addr & 0xFF); + uint8_t byte_flash = hwspi_write_read(0x00); + if (binmode_debug) + { + printf("\r\n0x%02X", byte_flash); + } + tud_cdc_n_write_char(1, byte_flash); // Send the readed byte + tud_cdc_n_write_flush(1); + len--; + if (len > 0) + { + hwspi_write_read(0x28); // AVR_FETCH_HIGH_BYTE_COMMAND + hwspi_write_read((addr >> 8) & 0xFF); + hwspi_write_read(addr & 0xFF); + uint8_t byte_flash = hwspi_write_read(0x00); + if (binmode_debug) + { + printf("\r\n0x%02X", byte_flash); + } + tud_cdc_n_write_char(1, byte_flash); // Send the readed byte + tud_cdc_n_write_flush(1); + len--; + } + addr++; + } + if (binmode_debug) + { + printf("\r\n"); + } + break; + + default: + // error + CDC_SEND_STR(1, "\x00"); + break; + } + + break; + } + } +} + + +// handler needs to be cooperative multitasking until mode is enabled +void legacy4third_mode(void){ + static uint32_t mode_active = 0; + if (mode_active == 0) + { + mode_active++; + // enable_debug_legacy(); + system_config.binmode_usb_rx_queue_enable=false; + system_config.binmode_usb_tx_queue_enable=false; + } + else if (mode_active == 1) + { + mode_active++; + + prompt_result result = { 0 }; + + printf("\r\n%sPower supply\r\nVolts (0.80V-5.00V)%s", ui_term_color_info(), ui_term_color_reset()); + + float volts = 0.0f; + ui_prompt_float(&result, 0.8f, 5.0f, 3.3f, true, &volts, false); + + volts_integer = (uint8_t)floorf(volts); + volts_decimal = (uint8_t)((volts - floorf(volts)) * 100); + + if (binmode_debug) + { + printf("\r\nVolts: int = %u, dec = %u\n", volts_integer, volts_decimal); + } + + float current = 0.0f; + printf("\r\n%sMaximum current (0mA-500mA), for none%s", ui_term_color_info(), ui_term_color_reset()); + ui_prompt_float(&result, 0.0f, 500.0f, 100.0f, true, ¤t, true); + + current_integer = (uint16_t)floorf(current); + current_decimal = (uint8_t)((current - floorf(current)) * 100); + + if (binmode_debug) + { + printf("\r\nCurrent: int = %u, dec = %u\n", current_integer, current_decimal); + } + + script_enabled(); + cdc_buff = (uint8_t*) mem_alloc(CDCBUFF_SIZE + TMPBUFF_SIZE, 0); + if (binmode_debug) + { + printf("\r\ncdc_buff: 0x%08X\r\n", cdc_buff); + } + printf("\r\nDone! Just execute flashrom or avrdude using the binary com port\r\n"); + tmpbuf = cdc_buff + CDCBUFF_SIZE; + memset(cdc_buff, 0, CDCBUFF_SIZE); + memset(tmpbuf, 0, TMPBUFF_SIZE); + remain_bytes = 0; + cdc_full_flush(1); + legacy_protocol(); + system_config.binmode_usb_rx_queue_enable=true; + system_config.binmode_usb_tx_queue_enable=true; + mem_free(cdc_buff); + reset_legacy(); + script_disabled(); + /* + uint8_t binmode_args = 0; + binmode_reset_buspirate(&binmode_args); + */ + } +} + + + + + + diff --git a/binmode/legacy4third.h b/binmode/legacy4third.h new file mode 100644 index 00000000..3e5d1e2a --- /dev/null +++ b/binmode/legacy4third.h @@ -0,0 +1,2 @@ +void legacy4third_mode(void); +extern const char legacy4third_mode_name[]; \ No newline at end of file From e10a62963c14ce55bceff71791ffcc9b8f38aa39 Mon Sep 17 00:00:00 2001 From: Dreg Date: Mon, 26 Aug 2024 19:53:19 +0200 Subject: [PATCH 6/8] Update legacy4third.h extra space --- binmode/legacy4third.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/binmode/legacy4third.h b/binmode/legacy4third.h index 3e5d1e2a..8dc96c3c 100644 --- a/binmode/legacy4third.h +++ b/binmode/legacy4third.h @@ -1,2 +1,3 @@ void legacy4third_mode(void); -extern const char legacy4third_mode_name[]; \ No newline at end of file +extern const char legacy4third_mode_name[]; + From 0b1bad574e365fc2cf1830bb6211f417d7aba37d Mon Sep 17 00:00:00 2001 From: David Reguera Garcia Date: Mon, 26 Aug 2024 19:59:22 +0200 Subject: [PATCH 7/8] solved merge conflict --- binmode/binmodes.c | 9 +++++---- binmode/legacy4third.c | 2 -- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/binmode/binmodes.c b/binmode/binmodes.c index b2999c27..c2235973 100644 --- a/binmode/binmodes.c +++ b/binmode/binmodes.c @@ -45,10 +45,11 @@ const binmode_t binmodes[] = { .binmode_cleanup = falaio_cleanup, }, { - &binmode_null_func, - &legacy4third_mode, - &binmode_null_func, - legacy4third_mode_name, + .lock_terminal = true, + .binmode_name = legacy4third_mode_name, + .binmode_setup = binmode_null_func_void, + .binmode_service = legacy4third_mode, + .binmode_cleanup = binmode_null_func_void, }, }; diff --git a/binmode/legacy4third.c b/binmode/legacy4third.c index 32c856cc..a9da8ab5 100644 --- a/binmode/legacy4third.c +++ b/binmode/legacy4third.c @@ -801,7 +801,6 @@ void legacy4third_mode(void){ printf("\r\nCurrent: int = %u, dec = %u\n", current_integer, current_decimal); } - script_enabled(); cdc_buff = (uint8_t*) mem_alloc(CDCBUFF_SIZE + TMPBUFF_SIZE, 0); if (binmode_debug) { @@ -818,7 +817,6 @@ void legacy4third_mode(void){ system_config.binmode_usb_tx_queue_enable=true; mem_free(cdc_buff); reset_legacy(); - script_disabled(); /* uint8_t binmode_args = 0; binmode_reset_buspirate(&binmode_args); From a26cc87f04b50e96988dd850918b37641d0bc6a8 Mon Sep 17 00:00:00 2001 From: David Reguera Garcia Date: Tue, 27 Aug 2024 02:50:03 +0200 Subject: [PATCH 8/8] comestic changes --- binmode/legacy4third.c | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/binmode/legacy4third.c b/binmode/legacy4third.c index a9da8ab5..ed462a6c 100644 --- a/binmode/legacy4third.c +++ b/binmode/legacy4third.c @@ -51,6 +51,7 @@ const char legacy4third_mode_name[]="Legacy Binary Mode for Flashrom and AVRdude #define TMPBUFF_SIZE 0x4000 #define CDCBUFF_SIZE 0x4000 #define DEFAULT_MAX_TRIES 100000 +#define CDC_SEND_STR(cdc_n, str) tud_cdc_n_write(cdc_n, (uint8_t*)str, sizeof(str) - 1); tud_cdc_n_write_flush(1); static uint8_t volts_integer; static uint8_t volts_decimal; @@ -64,15 +65,10 @@ static uint32_t remain_bytes; void disable_psu_legacy(void) { uint8_t binmode_args = 0x00; + uint32_t result = binmode_psu_disable(&binmode_args); } -void enable_psu_legacy(void) -{ - uint8_t args[] = { 0x05, 0x00, 0x00, 0x80 }; // 5v - uint32_t result = binmode_psu_enable(args); -} - void setup_spi_legacy(uint32_t spi_speed, uint8_t data_bits, uint8_t cpol, uint8_t cpah, uint8_t cs) { /* @@ -93,6 +89,7 @@ void setup_spi_legacy(uint32_t spi_speed, uint8_t data_bits, uint8_t cpol, uint8 cpah, cs }; + mode_change((uint8_t*)"SPI"); binmode_config(spi_binmode_args); system_config.binmode_usb_rx_queue_enable=false; @@ -102,6 +99,7 @@ void setup_spi_legacy(uint32_t spi_speed, uint8_t data_bits, uint8_t cpol, uint8 void enable_debug_legacy(void) { uint8_t binmode_args = 1; + binmode_debug_level(&binmode_args); } @@ -153,8 +151,6 @@ uint32_t read_buff(uint8_t *buf, uint32_t len, uint32_t max_tries) return total_bytes_readed; } -#define CDC_SEND_STR(cdc_n, str) tud_cdc_n_write(cdc_n, (uint8_t*)str, sizeof(str) - 1); tud_cdc_n_write_flush(1); - void cdc_full_flush(uint32_t cdc_id) { tud_cdc_n_read_flush(cdc_id); @@ -164,9 +160,10 @@ void cdc_full_flush(uint32_t cdc_id) void reset_legacy(void) { + uint8_t binmode_args = 0; + hwspi_deinit(); disable_psu_legacy(); - uint8_t binmode_args = 0; binmode_pullup_disable(&binmode_args); binmode_reset(&binmode_args); mode_change((uint8_t*)"HiZ"); @@ -275,10 +272,9 @@ void legacy_protocol(void) { disable_psu_legacy(); } - else if (extended_info & 0b00001000) + else { - //uint8_t args[] = { 0x03, 0x21, 0x00, 0x80 }; - //uint8_t args[] = { 0x05, 0x00, 0x00, 0x80 }; // 5v + //uint8_t args[] = { 0x03, 0x21, 0x00, 0x80 }; // 3.3v uint8_t args[] = { volts_integer, volts_decimal, @@ -286,6 +282,7 @@ void legacy_protocol(void) (uint8_t)(current_integer & 0xFF) }; uint32_t result = binmode_psu_enable(args); + if(result) { if (binmode_debug) @@ -312,7 +309,7 @@ void legacy_protocol(void) uint8_t binmode_args = 0; binmode_pullup_disable(&binmode_args); } - else if (extended_info & 0b00000100) + else { if (binmode_debug) { @@ -330,7 +327,7 @@ void legacy_protocol(void) printf("\r\naux_disable"); } } - else if (extended_info & 0b00000010) + else { if (binmode_debug) { @@ -347,7 +344,7 @@ void legacy_protocol(void) } cs_init = 0x00; } - else if (extended_info & 0b00000001) + else { if (binmode_debug) { @@ -423,7 +420,7 @@ void legacy_protocol(void) printf("\r\nSMP 0"); } } - else if (extended_info & 0x1) + else { if (binmode_debug) { @@ -439,7 +436,7 @@ void legacy_protocol(void) printf("\r\nCKE 0"); } } - else if (extended_info & 0x2) + else { if (binmode_debug) { @@ -455,7 +452,7 @@ void legacy_protocol(void) printf("\r\nCKP 0"); } } - else if (extended_info & 0x4) + else { if (binmode_debug) { @@ -471,7 +468,7 @@ void legacy_protocol(void) printf("\r\nHiZ"); } } - else if (extended_info & 0x8) + else { if (binmode_debug) {