Merge pull request #3583 from nieldk/BWM-work

Improve BLE negitiation and corrected wifi status
This commit is contained in:
Iceman authored and GitHub committed 2026-09-02 20:06:40 +07:00
commit 89c0682990
6 files changed
+149 -58

No files matched your search

+8 -25
View File
@@ -3029,44 +3029,30 @@ static void PacketReceived(PacketCommandNG *packet) {
}
#endif
case CMD_MEASURE_ANTENNA_TUNING_HF: {
if (packet->length != 1) {
if (packet->length != 1)
reply_ng(CMD_MEASURE_ANTENNA_TUNING_HF, PM3_EINVARG, NULL, 0);
break;
}
switch (packet->data.asBytes[0]) {
case 1: { // MEASURE_ANTENNA_TUNING_HF_START
case 1: // MEASURE_ANTENNA_TUNING_HF_START
// Let the FPGA drive the high-frequency antenna around 13.56 MHz.
FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER);
reply_ng(CMD_MEASURE_ANTENNA_TUNING_HF, PM3_SUCCESS, NULL, 0);
break;
}
case 2: {
case 2:
if (button_status == BUTTON_SINGLE_CLICK) {
reply_ng(CMD_MEASURE_ANTENNA_TUNING_HF, PM3_EOPABORTED, NULL, 0);
break;
}
// Re-assert the field on every sample. It's a insurance for a missed packet.
if (FpgaGetCurrent() != FPGA_BITSTREAM_HF) {
FpgaDownloadAndGo(FPGA_BITSTREAM_HF);
}
FpgaWriteConfWord(FPGA_MAJOR_MODE_HF_READER);
uint32_t volt = AdcRssiAvgToMilliVolt(ADC_RSSI_CH_HF);
reply_ng(CMD_MEASURE_ANTENNA_TUNING_HF, PM3_SUCCESS, (uint8_t *)&volt, sizeof(volt));
break;
}
case 3: {
case 3:
FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);
reply_ng(CMD_MEASURE_ANTENNA_TUNING_HF, PM3_SUCCESS, NULL, 0);
break;
}
default: {
default:
reply_ng(CMD_MEASURE_ANTENNA_TUNING_HF, PM3_EINVARG, NULL, 0);
break;
}
}
break;
}
@@ -3886,7 +3872,6 @@ static void PacketReceived(PacketCommandNG *packet) {
uint16_t mv = (packet->length >= 2)
? (uint16_t)(packet->data.asBytes[0] | (packet->data.asBytes[1] << 8))
: BWM_DEFAULT_VCHG_MV;
StartTicks();
I2C_init(true);
uint16_t applied = bwm_charger_set_vchg(mv);
reply_ng(CMD_PM5_BWM_SET_VCHG, applied ? PM3_SUCCESS : PM3_EFAILED, (uint8_t *)&applied, sizeof(applied));
@@ -3898,7 +3883,6 @@ static void PacketReceived(PacketCommandNG *packet) {
uint16_t cap = (packet->length >= 2)
? (uint16_t)(packet->data.asBytes[0] | (packet->data.asBytes[1] << 8))
: BWM_DEFAULT_DESIGN_CAP_MAH;
StartTicks();
I2C_init(true);
bool ok = bwm_gauge_provision_capacity(cap);
reply_ng(CMD_PM5_BWM_SET_CAP, ok ? PM3_SUCCESS : PM3_EFAILED, (uint8_t *)&cap, sizeof(cap));
@@ -3909,7 +3893,6 @@ static void PacketReceived(PacketCommandNG *packet) {
// Payload: 1 byte, non-zero = enable (default), zero = disable.
// One-shot: reverts on the charger watchdog timeout (~160 s).
bool enable = (packet->length >= 1) ? (packet->data.asBytes[0] != 0) : true;
StartTicks();
I2C_init(true);
bool ok = bwm_charger_set_charge(enable);
reply_ng(CMD_PM5_BWM_CHARGE_EN, ok ? PM3_SUCCESS : PM3_EFAILED, NULL, 0);
@@ -3925,9 +3908,9 @@ static void PacketReceived(PacketCommandNG *packet) {
res = bwm_wifi_forward_down();
reply_ng(CMD_PM5_BWM_WIFI, res, (uint8_t *)&ip, sizeof(ip));
} else if (action == BWM_WIFI_ACTION_STATUS) {
uint8_t connected = 0;
res = bwm_wifi_forward_status(&connected, &ip);
uint8_t st[5] = { connected,
uint8_t state = 0;
res = bwm_wifi_forward_status(&state, &ip);
uint8_t st[5] = { state,
(uint8_t)(ip & 0xFF), (uint8_t)((ip >> 8) & 0xFF),
(uint8_t)((ip >> 16) & 0xFF), (uint8_t)((ip >> 24) & 0xFF) };
reply_ng(CMD_PM5_BWM_WIFI, res, st, sizeof(st));
+70 -7
View File
@@ -59,6 +59,10 @@ static volatile int16_t s_fwd_inflight = 0;
// (the ESP's ack for a baud-set request). Consumed by bwm_fwd_negotiate_baud().
static volatile bool s_baud_ack = false;
// Set true by the parser on a SLAVE_RESP for BWM_CMD_GET_UART_BAUD - proof the
// ESP is alive and responding at the baud we just switched to (verify step).
static volatile bool s_getbaud_ack = false;
int bwm_fwd_writebuffer_sync(const uint8_t *data, size_t len) {
static uint8_t frame[BWM_TX_BUFSZ]; // single-threaded bare-metal: static OK
@@ -74,11 +78,11 @@ int bwm_fwd_writebuffer_sync(const uint8_t *data, size_t len) {
// disconnected ESP can't hard-hang us. A window >= 1 means single command
// replies never block - only sustained bursts hit the cap.
{
uint32_t spins = 0;
uint32_t t0 = GetTickCount();
while (s_fwd_inflight >= BWM_FC_WINDOW) {
bwm_pump();
if (++spins > BWM_FC_ACK_TIMEOUT_SPINS) {
s_fwd_inflight = 0; // best-effort: assume the pipe cleared
if (GetTickCountDelta(t0) > BWM_FC_ACK_TIMEOUT_MS) {
s_fwd_inflight = 0; // best-effort: assume the pipe cleared, never hard-hang
break;
}
}
@@ -244,6 +248,9 @@ static void bwm_feed_byte(bwm_parser_t *p, uint8_t byte) {
} else if ((p->is_bcast == false) && p->cmd == BWM_CMD_SET_UART_BAUD) {
// SLAVE_RESP ack for a baud-set request (see negotiate below)
s_baud_ack = true;
} else if ((p->is_bcast == false) && p->cmd == BWM_CMD_GET_UART_BAUD) {
// SLAVE_RESP for our GET_BAUD verify probe
s_getbaud_ack = true;
}
}
// valid non-DATA_FORWARD frames and CRC failures alike: just resync
@@ -330,6 +337,38 @@ uint32_t bwm_read_ng(uint8_t *data, size_t len) {
// ---------------------------------------------------------------------------
#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
// 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
// caller so a single lost probe on a good link does not force a needless revert.
static bool bwm_verify_baud(void) {
uint8_t f[2 + 2 + 2 + 2];
size_t i = 0;
f[i++] = BWM_HDR_HOST_CMD_1;
f[i++] = BWM_HDR_HOST_CMD_2;
f[i++] = (uint8_t)(BWM_CMD_GET_UART_BAUD & 0xFF);
f[i++] = (uint8_t)((BWM_CMD_GET_UART_BAUD >> 8) & 0xFF);
f[i++] = 0;
f[i++] = 0;
uint16_t crc = bwm_crc16(f, i, BWM_CRC16_INIT);
f[i++] = (uint8_t)(crc & 0xFF);
f[i++] = (uint8_t)((crc >> 8) & 0xFF);
s_getbaud_ack = false;
s_p.state = S_IDLE;
bwm_uart_write(f, i);
uint32_t t0 = GetTickCount();
while (GetTickCountDelta(t0) < BWM_BAUD_VERIFY_MS) {
bwm_pump();
if (s_getbaud_ack) {
return true;
}
}
return false;
}
bool bwm_fwd_negotiate_baud(uint32_t target) {
if (target == 0 || target == bwm_uart_get_baud()) {
@@ -362,15 +401,39 @@ bool bwm_fwd_negotiate_baud(uint32_t target) {
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 both settle before any traffic.
// Switch our side and let it settle.
bwm_uart_set_baud(target);
SpinDelay(5);
// Fresh start on the faster link: clear framer + de-fifo + window.
// 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;
}
}
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;
s_fwd_inflight = 0;
return true;
return false;
}
SpinDelay(1);
}
+7 -2
View File
@@ -51,6 +51,7 @@
// SET is a HOST_CMD carrying u32 LE baud; the ESP replies with a SLAVE_RESP
// echoing this cmd (len 0) at the OLD baud, then commits to the new baud.
#define BWM_CMD_SET_UART_BAUD 1011
#define BWM_CMD_GET_UART_BAUD 1009 // read back the ESP's live baud (negotiation verify)
// Flow control (ack window) - ARM-side only, no BWM firmware change required.
// The ESP already replies to every forward frame with a SLAVE_RESP echoing
// cmd=SEND_FORWARD_DATA, and it sends that ack only *after* app_ble_send() has
@@ -66,8 +67,12 @@
// keep enough headroom to ride out delayed acks. Only ~1 response is ever
// really in flight during an upload, so this does not risk an ESP overrun.
#define BWM_FC_WINDOW 16 // max un-acked forward frames in flight
#ifndef BWM_FC_ACK_TIMEOUT_SPINS
#define BWM_FC_ACK_TIMEOUT_SPINS 200000 // safety valve: proceed if an ack is lost (avoid hard hang)
#ifndef BWM_FC_ACK_TIMEOUT_MS
// Hard cap (ms) on how long a forward write may block the main loop waiting for
// acks. A spin COUNT was unbounded in wall-clock time and could hang the main
// loop long enough that the client gives up and the device looks dead (USB still
// enumerates on interrupts). Time-bounded => the main loop is always serviced.
#define BWM_FC_ACK_TIMEOUT_MS 50 // safety valve: proceed if acks stall, never hard-hang
#endif // safety valve: give up waiting for credit (avoid hard hang)
#define BWM_CRC16_POLY 0x1021
+24 -11
View File
@@ -225,20 +225,33 @@ int bwm_wifi_forward_up(const char *ssid, const char *password,
return PM3_SUCCESS;
}
int bwm_wifi_forward_status(uint8_t *connected, uint32_t *ip_out) {
int bwm_wifi_forward_status(uint8_t *state, uint32_t *ip_out) {
const uint32_t TO = 2000;
uint32_t ip = 0;
uint8_t ipb[12];
uint16_t il = sizeof(ipb);
int r = bwm_cmd(BWM_CMD_GET_WIFI_CFG_IP_ADDR, NULL, 0, ipb, &il, TO);
if (r != PM3_SUCCESS) {
return r; // BWM / UART not responding
*ip_out = 0;
*state = BWM_WIFI_STATE_OFF;
// Ask the ESP for the WiFi connect state (not just the IP): this tells apart
// off / disconnected / connecting / connected, which the IP alone cannot.
uint8_t st = 0;
uint16_t sl = sizeof(st);
int r = bwm_cmd(BWM_CMD_GET_WIFI_CONNECT_STATUS, NULL, 0, &st, &sl, TO);
if (r == PM3_ETIMEOUT) {
return r; // BWM genuinely not responding
}
if (il >= 4) {
ip = (uint32_t)ipb[0] | ((uint32_t)ipb[1] << 8) | ((uint32_t)ipb[2] << 16) | ((uint32_t)ipb[3] << 24);
if (r != PM3_SUCCESS || sl < 1) {
*state = BWM_WIFI_STATE_OFF; // BWM answered; WiFi subsystem is off (BLE-only)
return PM3_SUCCESS;
}
*state = st; // 0 down, 1 connecting, 2 connected, 3 reconnecting, 4 stopped
// Only chase an IP when actually connected.
if (st == BWM_WIFI_STATE_CONNECTED) {
uint8_t ipb[12];
uint16_t il = sizeof(ipb);
if ((bwm_cmd(BWM_CMD_GET_WIFI_CFG_IP_ADDR, NULL, 0, ipb, &il, TO) == PM3_SUCCESS) && (il >= 4)) {
*ip_out = (uint32_t)ipb[0] | ((uint32_t)ipb[1] << 8) | ((uint32_t)ipb[2] << 16) | ((uint32_t)ipb[3] << 24);
}
}
*ip_out = ip;
*connected = (ip != 0) ? 1 : 0; // a lease == a usable connection
return PM3_SUCCESS;
}
+8 -1
View File
@@ -56,6 +56,13 @@ int bwm_wifi_forward_down(void);
// BWM IPv4 (host order, a in low byte; 0 if none) to *ip_out and 1/0 to
// *connected (true == has a DHCP lease). Returns PM3_EFAILED if the BWM/UART
// does not answer.
int bwm_wifi_forward_status(uint8_t *connected, uint32_t *ip_out);
// WiFi connect state reported by --status (mirrors the ESP enum; 0xFF = off).
#define BWM_WIFI_STATE_DISCONNECTED 0
#define BWM_WIFI_STATE_CONNECTING 1
#define BWM_WIFI_STATE_CONNECTED 2
#define BWM_WIFI_STATE_RECONNECT 3
#define BWM_WIFI_STATE_STOPPED 4
#define BWM_WIFI_STATE_OFF 0xFF
int bwm_wifi_forward_status(uint8_t *state, uint32_t *ip_out);
#endif
+32 -12
View File
@@ -1609,18 +1609,38 @@ static int CmdBWMWifi(const char *Cmd) {
PrintAndLogEx(FAILED, "could not query BWM WiFi status (BWM present?)");
return r.status;
}
uint8_t connected = (r.length >= 1) ? r.data.asBytes[0] : 0;
uint8_t state = (r.length >= 1) ? r.data.asBytes[0] : 0xFF;
uint32_t ip = 0;
if (r.length >= 5) {
ip = r.data.asBytes[1] | (r.data.asBytes[2] << 8) | (r.data.asBytes[3] << 16) | ((uint32_t)r.data.asBytes[4] << 24);
}
if (connected && ip) {
PrintAndLogEx(SUCCESS, "BWM WiFi connected, IP " _YELLOW_("%u.%u.%u.%u"),
ip & 0xFF, (ip >> 8) & 0xFF, (ip >> 16) & 0xFF, (ip >> 24) & 0xFF);
PrintAndLogEx(HINT, "Connect with: " _YELLOW_("pm3 -p tcp:%u.%u.%u.%u:<port>"),
ip & 0xFF, (ip >> 8) & 0xFF, (ip >> 16) & 0xFF, (ip >> 24) & 0xFF);
} else {
PrintAndLogEx(INFO, "BWM WiFi not connected (no IP). If a join is in progress, re-check in a few seconds.");
switch (state) {
case 0xFF:
PrintAndLogEx(INFO, "BWM WiFi disabled (BLE-only). Bring it up with " _YELLOW_("hw bwmwifi --ssid <ssid> --pwd <pwd>"));
break;
case 2: // connected
if (ip) {
PrintAndLogEx(SUCCESS, "BWM WiFi connected, IP " _YELLOW_("%u.%u.%u.%u"),
ip & 0xFF, (ip >> 8) & 0xFF, (ip >> 16) & 0xFF, (ip >> 24) & 0xFF);
PrintAndLogEx(HINT, "Connect with: " _YELLOW_("pm3 -p tcp:%u.%u.%u.%u:<port>"),
ip & 0xFF, (ip >> 8) & 0xFF, (ip >> 16) & 0xFF, (ip >> 24) & 0xFF);
} else {
PrintAndLogEx(INFO, "BWM WiFi associated, waiting for a DHCP lease...");
}
break;
case 1: // connecting
PrintAndLogEx(INFO, "BWM WiFi connecting...");
break;
case 3: // reconnect wait
PrintAndLogEx(INFO, "BWM WiFi reconnecting...");
break;
case 4: // task stopped
PrintAndLogEx(INFO, "BWM WiFi connect task stopped");
break;
case 0: // disconnected
default:
PrintAndLogEx(INFO, "BWM WiFi configured but not connected");
break;
}
return PM3_SUCCESS;
}
@@ -1809,7 +1829,7 @@ static int CmdBwmSetCap(const char *Cmd) {
uint8_t payload[2] = { (uint8_t)(cap & 0xFF), (uint8_t)((cap >> 8) & 0xFF) };
PrintAndLogEx(INFO, "Programming BWM gauge design capacity to " _YELLOW_("%d mAh") "...", cap);
PrintAndLogEx(INFO, "Run this " _YELLOW_("once"));
PrintAndLogEx(INFO, "Run this " _YELLOW_("once") "; then perform a full charge/discharge learning cycle.");
clearCommandBuffer();
SendCommandNG(CMD_PM5_BWM_SET_CAP, payload, sizeof(payload));
@@ -1910,9 +1930,9 @@ static int CmdPing(const char *Cmd) {
uint32_t len = arg_get_u32_def(ctx, 1, 32);
CLIParserFree(ctx);
if (len > g_conn.max_cmd_data_size)
len = g_conn.max_cmd_data_size;
if (len > PM3_CMD_DATA_SIZE)
len = PM3_CMD_DATA_SIZE;
if (len) {
PrintAndLogEx(INFO, "Ping sent with payload len... " _YELLOW_("%d"), len);
} else {