diff --git a/armsrc/bwm_forward.c b/armsrc/bwm_forward.c index 74c933b0c..b7eaf6445 100644 --- a/armsrc/bwm_forward.c +++ b/armsrc/bwm_forward.c @@ -334,11 +334,15 @@ uint32_t bwm_read_ng(uint8_t *data, size_t len) { // send the request at the current baud, wait for that ack, then switch our own // UART4 to match. On timeout (old ESP without the command, or a lost ack) we // leave the link at the boot baud - it keeps working, just slower. +// +// The ESP keeps its baud across an AT32-only reset (bootloader, hw reset, +// flash), so probe both rates first and adopt the one it answers at. // --------------------------------------------------------------------------- #define BWM_BAUD_ACK_WAIT_MS 300 // per-attempt wait for the ESP ack #define BWM_BAUD_ATTEMPTS 3 // resend attempts before giving up #define BWM_BAUD_VERIFY_MS 150 // per-probe wait for the GET_BAUD reply #define BWM_BAUD_VERIFY_TRIES 5 // GET_BAUD probes before declaring the switch failed +#define BWM_BAUD_PROBE_ROUNDS 4 // boot/target probe pairs // Probe the ESP at the CURRENT baud with GET_UART_BAUD; true only if it answers, // i.e. it really is running at the baud we just switched to. Retried by the @@ -370,10 +374,50 @@ static bool bwm_verify_baud(void) { return false; } +// Switch to `baud` and probe up to `tries` times. +static bool bwm_probe_at(uint32_t baud, int tries) { + bwm_uart_set_baud(baud); + SpinDelay(2); + for (int v = 0; v < tries; v++) { + if (bwm_verify_baud()) { + return true; + } + } + return false; +} + +// Reset framer, FIFO and flow control after a baud change. +static void bwm_link_reset(void) { + s_p.state = S_IDLE; + s_fifo_head = 0; + s_fifo_tail = 0; + s_fwd_inflight = 0; +} + bool bwm_fwd_negotiate_baud(uint32_t target) { if (target == 0 || target == bwm_uart_get_baud()) { return false; } + const uint32_t boot_baud = bwm_uart_get_baud(); + + // Locate the ESP: alternate so a cold-booted one (boot baud) and one that + // survived our reset (target baud) are both found. + bool at_boot = false, at_target = false; + for (int r = 0; r < BWM_BAUD_PROBE_ROUNDS && !at_boot && !at_target; r++) { + at_boot = bwm_probe_at(boot_baud, 1); + if (!at_boot) { + at_target = bwm_probe_at(target, 1); + } + } + if (at_target) { + bwm_link_reset(); // already at target + return true; + } + if (!at_boot) { + bwm_uart_set_baud(boot_baud); // no ESP: stay at boot baud + bwm_link_reset(); + return false; + } // Build the SET_UART_BAUD host-command frame once (payload = u32 LE baud). uint8_t frame[2 + 2 + 2 + 4 + 2]; @@ -400,43 +444,18 @@ bool bwm_fwd_negotiate_baud(uint32_t target) { for (int ms = 0; ms < BWM_BAUD_ACK_WAIT_MS; ms++) { bwm_pump(); // parser sets s_baud_ack on the SLAVE_RESP if (s_baud_ack) { - // ESP acked at the old baud and is committing to the new one. - // Switch our side and let it settle. - bwm_uart_set_baud(target); - SpinDelay(5); - - // VERIFY the ESP is really at the new baud before trusting it. A - // lost or spurious ack must not leave the two ends at different - // bauds - that silently breaks every AT32<->ESP exchange (forward - // traffic, hw bwmwifi, ...). Retry so one dropped probe on a good - // link does not cause a needless fallback. - bool verified = false; - for (int v = 0; v < BWM_BAUD_VERIFY_TRIES; v++) { - if (bwm_verify_baud()) { - verified = true; - break; - } + // ESP acked and is switching; verify it before trusting the + // new baud, else fall back so both ends stay in sync. + bool verified = bwm_probe_at(target, BWM_BAUD_VERIFY_TRIES); + if (verified == false) { + bwm_uart_set_baud(boot_baud); } - - s_p.state = S_IDLE; // fresh framer either way - s_fifo_head = 0; - s_fifo_tail = 0; - s_fwd_inflight = 0; - - if (verified) { - return true; - } - - // Could not confirm the ESP at the new baud: fall back so the - // ends stay in sync. 460800 is the always-safe boot default. - bwm_uart_set_baud(BWM_UART_BAUD); - s_p.state = S_IDLE; - s_fifo_head = 0; - s_fifo_tail = 0; - return false; + bwm_link_reset(); + return verified; } SpinDelay(1); } } + bwm_link_reset(); return false; // no ack: stay at the boot baud, link still usable } diff --git a/armsrc/bwm_forward.h b/armsrc/bwm_forward.h index 5ef6cf71e..8d8e0515c 100644 --- a/armsrc/bwm_forward.h +++ b/armsrc/bwm_forward.h @@ -92,10 +92,11 @@ uint32_t bwm_read_ng(uint8_t *data, size_t len); // >0 when raw bytes are waiting on the FPC USART (gate for receive_ng()). uint16_t bwm_fwd_rxdata_available(void); -// Negotiate the ESP<->AT32 UART up to `target` baud (app_com cmd 1011), then -// re-init UART4 to match. Returns true if the ESP acked and the link switched; -// false (link left at the boot baud) on timeout or an unsupported rate. Must be -// called once after bwm_uart_init(), before normal forward traffic starts. +// Bring the ESP<->AT32 UART to `target` baud: adopt it if the ESP is already +// there (its baud survives an AT32-only reset), else negotiate up via app_com +// cmd 1011 and re-init UART4 to match. Returns true if the link runs at +// `target`; false (left at the boot baud) if no ESP answered or the switch +// could not be verified. Call once after bwm_uart_init(). bool bwm_fwd_negotiate_baud(uint32_t target); #endif // __BWM_FORWARD_H diff --git a/doc/md/PM5_Start_Here/PM5-BWM-USAGE.md b/doc/md/PM5_Start_Here/PM5-BWM-USAGE.md index d88e0afdd..c3a75b358 100644 --- a/doc/md/PM5_Start_Here/PM5-BWM-USAGE.md +++ b/doc/md/PM5_Start_Here/PM5-BWM-USAGE.md @@ -152,8 +152,9 @@ The BWM advertises as: > yours with `btmgmt find`, or use the bridge's name-scan (`-n Proxmark5`), which > avoids needing the address at all. -There are two ways to connect: a **native transport** (Linux) and a **cross-platform -Python bridge** (Linux / macOS / Windows / WSL / iOS). +There are three ways to connect: a **native transport** (Linux), a **cross-platform +Python bridge** (Linux / macOS / Windows / WSL / iOS), and a **bridge app** on Android +([2.4](#24-android--termux--ble-bridge-app)). ### 2.1 Native BLE transport — Linux only, no bridge @@ -240,6 +241,39 @@ python3 pm5_ble_bridge.py --connect 127.0.0.1:7777 > For a plain **serial** port, `--wait` keeps its original meaning: wait ~20 s for > the device node to appear. +### 2.4 Android — Termux + BLE bridge app + +Termux has no Bluetooth access, so a bridge app exposes the BWM's BLE characteristic +as a local TCP port that the client then opens — same idea as +[termux_notes.md](../../termux_notes.md), just BLE instead of classic Bluetooth. +Tested with the paid +[BT/USB/TCP Bridge](https://play.google.com/store/apps/details?id=masar.bluetoothbridge.pro) +app, which lets you pick the GATT characteristic; the free version works too but is +limited to 10 minutes per session (the limit resets after quitting the app). + +1. Build the client in Termux (see [termux_notes.md](../../termux_notes.md)). +2. In the bridge app: + +| Setting | Value | +| ---------------- | ----------------------------------------------------------------- | +| Device A | Start TCP server (default port `54321`) | +| Device B | Connect to BLE device → `Proxmark5` | +| Characteristic | the last one: service `0000ae86-…`, characteristic `0000ae88-…`, for RX+TX | + +3. In Termux, connect over the local port: + +``` +./client/proxmark3 tcp:localhost:54321 +``` + +No pairing is needed (the BWM has no BLE security). The same app also does classic +Bluetooth, so one setup covers a Blueshark-equipped Proxmark3 too. + +> [!NOTE] +> If you would rather not use a bridge app, put the phone in hotspot mode and use +> WiFi instead ([3](#3-wifi-sta--tcp-server)): the client then connects straight to +> the BWM's TCP server, with no app in between. + --- ## 3. WiFi (STA + TCP server) diff --git a/doc/termux_notes.md b/doc/termux_notes.md index 798218d92..1c3851320 100644 --- a/doc/termux_notes.md +++ b/doc/termux_notes.md @@ -24,6 +24,7 @@ - [USB-UART Bridge Application for UDP to USB bridging](#usb-uart-bridge-application-for-udp-to-usb-bridging) - [Bluetooth connection](#bluetooth-connection) - [BT-UART Bridge Application for TCP to BT bridging](#bt-uart-bridge-application-for-tcp-to-bt-bridging) + - [BLE bridging for the Proxmark5 BWM](#ble-bridging-for-the-proxmark5-bwm) - [TCP connection](#tcp-connection) - [UDP connection](#udp-connection) - [Troubleshooting](#troubleshooting-1) @@ -184,6 +185,19 @@ Choose your registered PM3 device as 'Device B' -> 'Connect to classic Bluetooth Ensure 'Retransmission' is set to 'both ways'. It is possible to record the config as autostart, cf 'Settings' -> 'Autostart setting'. +#### BLE bridging for the Proxmark5 BWM +^[Top](#top) + +The Proxmark5 Battery Wireless Module (BWM) uses BLE, not classic Bluetooth. +The [paid version of the BT/USB/TCP Bridge app](https://play.google.com/store/apps/details?id=masar.bluetoothbridge.pro) handles it (the free version works with 10 minute time limit, can be reset by restarting the app): + +In the app, select TCP server as 'Device A' (default port 54321). +Choose 'Device B' -> 'Connect to BLE device' -> `Proxmark5`. +When asked for the characteristic, choose: service UUID starting with `0000ae86`, characteristic starting with `0000ae88`, for RX+TX. +No pairing is needed. Then connect from Termux as in [TCP connection](#tcp-connection) using port 54321. + +See [PM5-BWM-USAGE.md](md/PM5_Start_Here/PM5-BWM-USAGE.md) for the BWM firmware build flag and the WiFi alternative, which needs no bridge app at all. + ### TCP connection ^[Top](#top)