from machine import UART, Pin
import time

CANDIDATES = [4, 12, 13, 14, 16, 17, 21, 22, 23, 25, 26, 27, 32, 33]

BAUDRATES = [921600]

HEADER = 0x55  # IMU WIT frame header

def test_uart(rx, tx, baud):
    try:
        uart = UART(
            2,
            baudrate=baud,
            rx=Pin(rx),
            tx=Pin(tx),
            timeout=100,
            rxbuf=1024
        )

        time.sleep(0.1)

        start = time.ticks_ms()
        samples = []

        while time.ticks_diff(time.ticks_ms(), start) < 1000:
            if uart.any():
                data = uart.read()
                if data:
                    samples.append(data)

        uart.deinit()

        # 合并所有数据
        if samples:
            merged = b''.join(samples)

            print(f"\nRX={rx}, TX={tx}, BAUD={baud}")
            print("RAW HEX:", merged[:80])  # 前80字节
            print("HEX:", merged[:80].hex())

            return merged
        else:
            return b""

    except Exception as e:
        return b""

print("=== UART PIN SCAN START ===")

best = (0, None, None, None)

for rx in CANDIDATES:
    for tx in CANDIDATES:
        if rx == tx:
            continue

        for baud in BAUDRATES:
            c, t = test_uart(rx, tx, baud)

            print(f"RX={rx}, TX={tx}, BAUD={baud} -> 0x55={c}, bytes={t}")

            if c > best[0]:
                best = (c, rx, tx, baud)

print("\n=== BEST MATCH ===")
print(best)