Port the "reduce idle power" feature from the Fantasi firmware (soeinova/Fantasi @ 9671309, plus its later ACC-SOF fix) to the PM5 (AT32F435) target. Problem ------- The PM5 firmware ran the ARM core at 288 MHz with a 1.3 V LDO and a main loop that busy-polled at full speed even when the device was idle. The 288 MHz PLL and the 1.3 V LDO dominate idle draw, and the busy loop adds to it. Nothing scaled back when there was no work. Change ------ New armsrc/pm5_power.c drives a refcounted clock/voltage governor: - Idle (nothing pending, boost count 0, boot grace elapsed, FPGA off): take SCLK off the PLL onto HICK-48, power the PLL down, drop the LDO to 1.1 V, park the FPGA 24 MHz clock (PA8 low), and halt the core in WFI until the next IRQ or the 1 ms SysTick wake. - Any timing-critical work holds a 288 MHz boost: AppMain wraps PacketReceived (which on the PM5 also covers a standalone mode, since it is only entered through a command there); the BWM low-batt poll and the RGB indicator wrap their I2C. Boost is refcounted and IRQ-guarded. - USB (OS image) moves to the crystal-less HICK-48 clock, ACC-trimmed against the SOF, so powering the PLL down never touches USB. The bootrom keeps the HEXT/PLL USB path (AS_BOOTROM), so DFU flashing is unchanged. - bwm_uart_clock_update() re-inits UART4 through the SDK usart_init() on each clock switch (the divider set at 288 MHz is 6x off at 48 MHz). - FpgaIsOff() (fpga_core.c, PM5 only) tracks the last conf word so the governor never downclocks while a reader field / emulation is running. - Wake preamble on the BWM link: the companion module firmware (Proxmark5_BWM_esp32) light-sleeps 2 s after the last byte on the link and its UART wakes on RX edges but loses the bytes that carried them. bwm_uart_write() therefore leads with four 0x55 bytes and a 10 ms settle on its first write and after 1 s without a write of its own (RX is not tracked: the ring can be drained long after the bytes came in). The module only starts light-sleeping once it has seen a preamble, so the one sent while it was still booting is repeated after the boot-time link negotiation. A module firmware without light sleep drops the preamble as noise. New `hw powersave on|off` toggles the governor at runtime (default on); `hw status` reports the idle-clock and WFI-asleep fractions. Behaviour change ---------------- - PM5 idles at 48 MHz with the PLL off and the CPU halted; it boosts to 288 MHz for commands and BWM housekeeping. Command timing and throughput are unchanged (intra-command work is always boosted). - PM5 USB now runs off HICK-48 (ACC-trimmed) instead of the HEXT PLL. - New command id CMD_PM5_POWERSAVE (0x0180). Testing ------- Built warning-free (gcc) for PLATFORM=PM5 (with and without PLATFORM_EXTRAS=BWM), PM3RDV4 and PM3GENERIC, armsrc and bootrom. Client built via the default Makefile. Not built with clang or on Windows/macOS; nothing outside PM5/AT32 guards changes. Flashed to a PM5 and exercised against a MIFARE Classic 4K card: hw version/status/ping (4000 & 512 B), hw tune, hf search, hf 14a info/reader (x12 across idle gaps), hf mf info/chk/rdbl, lf search/read, hw powersave off/on, held-field (raw -sk) correctly blocks the downclock, hw reset + re-enumerate. All pass; USB stays up across downclocks. Idle reaches ~80-95% at 48 MHz and ~90%+ WFI-halted after a few seconds idle. Measured at the USB port: idle draw 283 mA -> 207 mA with the stock BWM firmware. Co-authored-by: Soei Nova <soeinova@proton.me> Co-authored-by: noproto <noproto@zeroday.engineering> Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
214 lines
8.9 KiB
C
214 lines
8.9 KiB
C
//-----------------------------------------------------------------------------
|
|
// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
|
|
//
|
|
// This program is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// See LICENSE.txt for the text of the license.
|
|
//-----------------------------------------------------------------------------
|
|
// AT32F435 UART4 driver for the Proxmark5 BWM link - see bwm_uart_at32.h.
|
|
//
|
|
// RX is serviced by circular DMA (DMA1 channel 2) rather than a per-byte
|
|
// RX interrupt. The old RDBF ISR dropped bytes on overrun as soon as the
|
|
// main loop was busy (FPGA / USB) - fine at 460800, fatal at higher bauds,
|
|
// where the dropped byte becomes an app_com CRC-16 failure and a stall.
|
|
// Circular DMA lets the controller sink every byte independent of CPU load,
|
|
// so BWM_UART_BAUD can be raised to lift BLE/WiFi transfer rates.
|
|
//-----------------------------------------------------------------------------
|
|
|
|
#include "bwm_uart_at32.h"
|
|
|
|
#include "pm3_cmd.h"
|
|
#include "ticks_apis.h" // GetTickCount / GetTickCountDelta / SpinDelay
|
|
#include "at32f435_437.h"
|
|
#include "at32f435_437_crm.h"
|
|
#include "at32f435_437_gpio.h"
|
|
#include "at32f435_437_usart.h"
|
|
#include "at32f435_437_dma.h"
|
|
#include "at32f435_437_misc.h"
|
|
|
|
#define BWM_UART UART4
|
|
#define BWM_UART_GPIO GPIOA
|
|
#define BWM_UART_TX_PIN GPIO_PINS_0
|
|
#define BWM_UART_RX_PIN GPIO_PINS_1
|
|
#define BWM_UART_TX_SRC GPIO_PINS_SOURCE0
|
|
#define BWM_UART_RX_SRC GPIO_PINS_SOURCE1
|
|
#define BWM_UART_MUX GPIO_MUX_8
|
|
|
|
// SSC already owns DMA1 channel 1 (see fpga_hw_at32.c); UART4 RX uses channel 2.
|
|
#define BWM_DMA_CHANNEL DMA1_CHANNEL2
|
|
#define BWM_DMA_MUX_CHANNEL DMA1MUX_CHANNEL2
|
|
|
|
// Power-of-two so head/tail wrap with a mask. DMA target buffer.
|
|
// Must comfortably exceed one full forward frame or the DMA laps the reader.
|
|
// A frame is app_com(6) + NG(10) + up to PM3_CMD_DATA_SIZE data + CRC(2); at
|
|
// PM3_CMD_DATA_SIZE=4064 that is ~4082 bytes, so 4096 leaves ~14 bytes of slack
|
|
// and overruns the instant the consumer lags. 4x headroom on a 512K part.
|
|
#define BWM_RX_RING_SZ 16384
|
|
static volatile uint8_t s_rx_ring[BWM_RX_RING_SZ];
|
|
static volatile uint16_t s_rx_tail = 0; // software read cursor; head comes from DMA
|
|
|
|
static volatile bool s_inited = false;
|
|
static volatile uint32_t s_cur_baud = BWM_UART_BAUD;
|
|
|
|
// The ESP light-sleeps BWM_ESP_AWAKE_MS after the last byte either way and loses
|
|
// the bytes whose edges wake it, so the first write, and any after
|
|
// BWM_ESP_WAKE_AFTER_MS of silence of ours (RX is not tracked, see bwm_uart_read),
|
|
// leads with a disposable preamble; ESP fw without light sleep resyncs past it.
|
|
#define BWM_ESP_AWAKE_MS 2000 // == UART_LINK_AWAKE_MS on the ESP
|
|
#define BWM_ESP_WAKE_AFTER_MS 1000 // preamble when quiet longer than this (margin for drift)
|
|
#define BWM_ESP_WAKE_SETTLE_MS 10 // light-sleep exit + UART driver back up
|
|
static volatile uint32_t s_last_traffic_tick = 0;
|
|
static volatile bool s_tx_ever = false; // nothing sent yet: the tick above means nothing
|
|
|
|
// Bytes the DMA controller has written so far, wrapped into the ring.
|
|
// The channel's DTCNT counts DOWN from buffer_size and reloads to buffer_size
|
|
// at wrap (loop mode), so head = size - remaining, always in [0, size-1].
|
|
static inline uint16_t bwm_uart_rx_head(void) {
|
|
return (uint16_t)((BWM_RX_RING_SZ - dma_data_number_get(BWM_DMA_CHANNEL))
|
|
& (BWM_RX_RING_SZ - 1));
|
|
}
|
|
|
|
// Bring UART4 + circular RX DMA up at `baud`. Safe to call repeatedly: on a
|
|
// re-config it tears the channel down first, so the ring restarts empty (which
|
|
// also discards bytes straddling a baud switch). Mirrors the SSC RX setup in
|
|
// fpga_hw_at32.c.
|
|
static void bwm_uart_configure(uint32_t baud) {
|
|
crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
|
|
crm_periph_clock_enable(CRM_UART4_PERIPH_CLOCK, TRUE);
|
|
crm_periph_clock_enable(CRM_DMA1_PERIPH_CLOCK, TRUE);
|
|
|
|
gpio_init_type gpio_init_struct;
|
|
gpio_default_para_init(&gpio_init_struct);
|
|
gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
|
|
gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
|
|
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
|
|
gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
|
|
gpio_init_struct.gpio_pins = BWM_UART_TX_PIN | BWM_UART_RX_PIN;
|
|
gpio_init(BWM_UART_GPIO, &gpio_init_struct);
|
|
gpio_pin_mux_config(BWM_UART_GPIO, BWM_UART_TX_SRC, BWM_UART_MUX);
|
|
gpio_pin_mux_config(BWM_UART_GPIO, BWM_UART_RX_SRC, BWM_UART_MUX);
|
|
|
|
// Quiesce before reconfiguring (matters on the re-config path).
|
|
dma_channel_enable(BWM_DMA_CHANNEL, FALSE);
|
|
usart_enable(BWM_UART, FALSE);
|
|
|
|
usart_init(BWM_UART, baud, USART_DATA_8BITS, USART_STOP_1_BIT);
|
|
usart_parity_selection_config(BWM_UART, USART_PARITY_NONE);
|
|
usart_transmitter_enable(BWM_UART, TRUE);
|
|
usart_receiver_enable(BWM_UART, TRUE);
|
|
|
|
// --- RX via circular DMA ---
|
|
s_rx_tail = 0;
|
|
|
|
dma_reset(BWM_DMA_CHANNEL);
|
|
|
|
dma_init_type dma_init_struct;
|
|
dma_default_para_init(&dma_init_struct);
|
|
dma_init_struct.buffer_size = BWM_RX_RING_SZ;
|
|
dma_init_struct.direction = DMA_DIR_PERIPHERAL_TO_MEMORY;
|
|
dma_init_struct.peripheral_base_addr = (uint32_t) & (BWM_UART->dt);
|
|
dma_init_struct.peripheral_inc_enable = FALSE;
|
|
dma_init_struct.memory_base_addr = (uint32_t)s_rx_ring;
|
|
dma_init_struct.memory_inc_enable = TRUE;
|
|
dma_init_struct.peripheral_data_width = DMA_PERIPHERAL_DATA_WIDTH_BYTE;
|
|
dma_init_struct.memory_data_width = DMA_MEMORY_DATA_WIDTH_BYTE;
|
|
dma_init_struct.loop_mode_enable = TRUE; // circular: reloads at count 0
|
|
// MEDIUM: single-byte requests, must not starve the SSC bulk channel (HIGH).
|
|
dma_init_struct.priority = DMA_PRIORITY_MEDIUM;
|
|
dma_init(BWM_DMA_CHANNEL, &dma_init_struct);
|
|
|
|
dmamux_enable(DMA1, TRUE);
|
|
dmamux_init(BWM_DMA_MUX_CHANNEL, DMAMUX_DMAREQ_ID_UART4_RX);
|
|
|
|
usart_dma_receiver_enable(BWM_UART, TRUE);
|
|
dma_channel_enable(BWM_DMA_CHANNEL, TRUE);
|
|
|
|
usart_enable(BWM_UART, TRUE);
|
|
s_cur_baud = baud;
|
|
}
|
|
|
|
void bwm_uart_init(void) {
|
|
if (s_inited) {
|
|
return;
|
|
}
|
|
bwm_uart_configure(BWM_UART_BAUD);
|
|
s_inited = true;
|
|
}
|
|
|
|
void bwm_uart_set_baud(uint32_t baud) {
|
|
if (baud == 0 || baud == s_cur_baud) {
|
|
return;
|
|
}
|
|
bwm_uart_configure(baud);
|
|
}
|
|
|
|
uint32_t bwm_uart_get_baud(void) {
|
|
return s_cur_baud;
|
|
}
|
|
|
|
void bwm_uart_clock_update(void) {
|
|
if (s_inited == false) {
|
|
return;
|
|
}
|
|
// Recompute the baud divider for the new APB1 clock. usart_init() only
|
|
// rewrites the divider and the (unchanged) 8N1 frame bits; it does not
|
|
// touch the enable bits or the DMA request, so it is safe on a live port.
|
|
usart_init(BWM_UART, s_cur_baud, USART_DATA_8BITS, USART_STOP_1_BIT);
|
|
}
|
|
|
|
static void bwm_uart_write_raw(const uint8_t *data, size_t len) {
|
|
for (size_t i = 0; i < len; i++) {
|
|
while (usart_flag_get(BWM_UART, USART_TDBE_FLAG) == RESET) {
|
|
}
|
|
usart_data_transmit(BWM_UART, data[i]);
|
|
}
|
|
while (usart_flag_get(BWM_UART, USART_TDC_FLAG) == RESET) {
|
|
}
|
|
}
|
|
|
|
int bwm_uart_write(const uint8_t *data, size_t len) {
|
|
if ((s_tx_ever == false) || (GetTickCountDelta(s_last_traffic_tick) > BWM_ESP_WAKE_AFTER_MS)) {
|
|
// 0x55 = five rising edges per byte; the ESP wakes after three.
|
|
static const uint8_t wake[4] = { 0x55, 0x55, 0x55, 0x55 };
|
|
bwm_uart_write_raw(wake, sizeof(wake));
|
|
SpinDelay(BWM_ESP_WAKE_SETTLE_MS);
|
|
}
|
|
bwm_uart_write_raw(data, len);
|
|
s_last_traffic_tick = GetTickCount();
|
|
s_tx_ever = true;
|
|
return PM3_SUCCESS;
|
|
}
|
|
|
|
void bwm_uart_wake_next(void) {
|
|
s_tx_ever = false;
|
|
}
|
|
|
|
uint16_t bwm_uart_rx_available(void) {
|
|
// An unhandled overrun (ROERR) latches on this USART and stops it feeding the
|
|
// DMA - after one overrun every subsequent byte is lost until a re-init, which
|
|
// is why a stalled OTA "recovers on re-run" but mostly fails in a session.
|
|
// Clear it here so reception resumes on its own. ROERR clears by reading STS
|
|
// then DT; we only do that when the flag is actually set - the DMA is stalled
|
|
// then, so the byte we consume is the already-lost overrun byte.
|
|
if (usart_flag_get(BWM_UART, USART_ROERR_FLAG) != RESET) {
|
|
(void)BWM_UART->sts;
|
|
(void)BWM_UART->dt;
|
|
}
|
|
return (uint16_t)((bwm_uart_rx_head() - s_rx_tail) & (BWM_RX_RING_SZ - 1));
|
|
}
|
|
|
|
uint32_t bwm_uart_read(uint8_t *data, size_t len) {
|
|
uint16_t head = bwm_uart_rx_head();
|
|
uint32_t n = 0;
|
|
while (n < len && s_rx_tail != head) {
|
|
data[n++] = s_rx_ring[s_rx_tail];
|
|
s_rx_tail = (uint16_t)((s_rx_tail + 1) & (BWM_RX_RING_SZ - 1));
|
|
}
|
|
// RX does not refresh s_last_traffic_tick: the ring can be drained long after
|
|
// the ESP sent the bytes, so it says nothing about the module's awake window.
|
|
return n;
|
|
}
|