From 821380d21c3b670ae2eda46d1d7340f7e5acb554 Mon Sep 17 00:00:00 2001 From: Alessandro Mauri Date: Sun, 5 Apr 2026 20:08:18 +0200 Subject: [PATCH] independent programmer --- fw/programmer/src/bitbang_rvswdio.h | 6 +- fw/programmer/src/bitbang_rvswdio_pico.h | 7 +- fw/programmer/src/main.cpp | 129 +++++++++-------------- 3 files changed, 56 insertions(+), 86 deletions(-) diff --git a/fw/programmer/src/bitbang_rvswdio.h b/fw/programmer/src/bitbang_rvswdio.h index 09011d5..296630d 100644 --- a/fw/programmer/src/bitbang_rvswdio.h +++ b/fw/programmer/src/bitbang_rvswdio.h @@ -188,7 +188,7 @@ static int InitializeSWDSWIO( struct SWIOState * state ) if( MCFReadReg32( state, DMSTATUS, &dmstatus ) != 0 || MCFReadReg32( state, DMCONTROL, &dmcontrol ) != 0 ) { - //BB_PRINTF_DEBUG( "Could not read from RVSWD connection\n" ); + BB_PRINTF_DEBUG( "Could not read from RVSWD connection\n" ); state->opmode = 0; continue; } @@ -198,7 +198,7 @@ static int InitializeSWDSWIO( struct SWIOState * state ) ( ( dmstatus >> 8 ) & 0xf ) != 0x03 ) || dmcontrol != 1 ) { - //BB_PRINTF_DEBUG( "DMSTATUS invalid (Probably no RVSWD chip)\n" ); + BB_PRINTF_DEBUG( "DMSTATUS invalid (Probably no RVSWD chip)\n" ); state->opmode = 0; continue; } @@ -207,7 +207,7 @@ static int InitializeSWDSWIO( struct SWIOState * state ) BB_PRINTF_DEBUG( "Found RVSWD interface\n" ); return 0; } - //printf( "TIMEOUT\n" ); + BB_PRINTF_DEBUG( "TIMEOUT\n" ); return -55; } diff --git a/fw/programmer/src/bitbang_rvswdio_pico.h b/fw/programmer/src/bitbang_rvswdio_pico.h index 27b6f2c..4136f6d 100644 --- a/fw/programmer/src/bitbang_rvswdio_pico.h +++ b/fw/programmer/src/bitbang_rvswdio_pico.h @@ -37,7 +37,7 @@ static inline void ConfigureIOForRVSWD(void) // Single wire input-output SDI (just SWDIO) static inline void ConfigureIOForRVSWIO(void) { - BB_PRINTF_DEBUG( "TODO: add support for SWIO\n" ); + //BB_PRINTF_DEBUG( "TODO: add support for SWIO\n" ); } @@ -84,7 +84,7 @@ static void MCFWriteReg32( struct SWIOState * state, uint8_t command, uint32_t v { // only supported mode is SWD if (state->opmode != 2) { - BB_PRINTF_DEBUG( "TODO: add support for SWIO\n" ); + //BB_PRINTF_DEBUG( "TODO: add support for SWIO\n" ); return; } @@ -138,7 +138,7 @@ static int MCFReadReg32( struct SWIOState * state, uint8_t command, uint32_t * v { // only supported mode is SWD if (state->opmode != 2) { - BB_PRINTF_DEBUG( "TODO: add support for SWIO\n" ); + //BB_PRINTF_DEBUG( "TODO: add support for SWIO\n" ); return -1; } @@ -192,5 +192,6 @@ static int MCFReadReg32( struct SWIOState * state, uint8_t command, uint32_t * v read_end: interrupts(); sleep_us(STOP_WAIT); + BB_PRINTF_DEBUG("wrong parity: %d\n", parity_read); return (parity == parity_read) ? 0 : -1; } diff --git a/fw/programmer/src/main.cpp b/fw/programmer/src/main.cpp index df6993a..a680dd9 100644 --- a/fw/programmer/src/main.cpp +++ b/fw/programmer/src/main.cpp @@ -1,90 +1,59 @@ - #include "pico/time.h" - #include - #include +#include "pico/time.h" +#include +#include - //#define BB_PRINTF_DEBUG(...) Serial.printf(__VA_ARGS__) - #define BB_PRINTF_DEBUG(...) - #include "bitbang_rvswdio.h" - #include "bitbang_rvswdio_pico.h" +#define BB_PRINTF_DEBUG(...) Serial.printf(__VA_ARGS__) +//#define BB_PRINTF_DEBUG(...) +#include "bitbang_rvswdio.h" +#include "bitbang_rvswdio_pico.h" - #define PROTOCOL_START '!' - #define PROTOCOL_ACK '+' - #define PROTOCOL_TEST '?' - #define PROTOCOL_POWER_ON 'p' - #define PROTOCOL_POWER_OFF 'P' - #define PROTOCOL_WRITE_REG 'w' - #define PROTOCOL_READ_REG 'r' +SWIOState state = {.opmode = 2}; - bool last_dtr = false; - SWIOState state = {.opmode = 2}; - - void setup() { - Serial.begin(115200); - Serial.setTimeout(100); - ConfigureIOForRVSWD(); - - // We do NOT block waiting for Serial here, - // we handle the connection dynamically in the loop. - } +void setup() { + Serial.begin(115200); + Serial.setTimeout(100); +} - void setup1() - { - pinMode(LED_BUILTIN, OUTPUT); - } +void setup1() +{ + pinMode(LED_BUILTIN, OUTPUT); +} - void loop() { - // Monitor DTR line to simulate Arduino Reset behavior - bool current_dtr = Serial.dtr(); - if (current_dtr && !last_dtr) { - // minichlink just opened the port - ConfigureIOForRVSWD(); - while (Serial.available() > 0) { Serial.read(); } - delay(100); - Serial.write(PROTOCOL_START); // Announce readiness - } - last_dtr = current_dtr; +void loop() { + if (Serial.available() > 0) { + char cmd = Serial.read(); + int ret = 0; + uint8_t reg; - if (Serial.available() > 0) { - char cmd = Serial.read(); - uint8_t reg; - uint32_t val; - - switch (cmd) { - case PROTOCOL_TEST: - Serial.write(PROTOCOL_ACK); - break; - case PROTOCOL_POWER_ON: - // Not needed for rvswd - sleep_us(10); - Serial.write(PROTOCOL_ACK); - break; - case PROTOCOL_POWER_OFF: - // Not needed for rvswd - sleep_us(10); - Serial.write(PROTOCOL_ACK); - break; - case PROTOCOL_WRITE_REG: - if (Serial.readBytes((char*)®, sizeof(uint8_t)) != 1) break; - if (Serial.readBytes((char*)&val, sizeof(uint32_t)) != 4) break; - MCFWriteReg32(&state, reg, val); - Serial.write(PROTOCOL_ACK); - break; - case PROTOCOL_READ_REG: - if (Serial.readBytes((char*)®, sizeof(uint8_t)) != 1) break; - MCFReadReg32(&state, reg, &val); - Serial.write((char*)&val, sizeof(uint32_t)); - break; - } - } - } + switch (cmd) { + case '!': + state = {}; + ret = InitializeSWDSWIO(&state); + if (ret != 0) { + Serial.printf("error initializing SWD: %d\n", ret); + break; + } + ret = DetermineChipTypeAndSectorInfo(&state, NULL); + if (ret != 0) { + Serial.printf("failed to determine chip type: %d\n", ret); + break; + } + Serial.printf("chip type is %x\n", state.target_chip_type); + break; + default: + Serial.printf("unknown command '%c'\n", cmd); + break; + } + } +} - void loop1() - { - digitalWrite(LED_BUILTIN, 1); - delay(200); - digitalWrite(LED_BUILTIN, 0); - delay(200); - } +void loop1() +{ + digitalWrite(LED_BUILTIN, 1); + delay(200); + digitalWrite(LED_BUILTIN, 0); + delay(200); +}