Public Access
- Uploader sends 'FLASH <baud>' first; the ESP32 restarts the Nano and bridges at that baud rate, then answers OK - --baud=auto (default) tries 57600 (old ATmegaBOOT) then 115200 (Optiboot) - Optional nano_reset GPIO (NPN to Nano RESET) pulses reset after power on, needed for Optiboot which skips the bootloader after a power cycle - Clear errors for outdated ESP32 firmware and missing wires - README: bootloader table, reset wire, troubleshooting Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
42 lines
1.3 KiB
C++
42 lines
1.3 KiB
C++
#pragma once
|
|
|
|
#include <Arduino.h>
|
|
#include <WiFi.h>
|
|
|
|
// WiFi to serial bridge for flashing the Nano from the PC (pio run -e nano_ota -t upload).
|
|
//
|
|
// The uploader (tools/nano_net_upload.py) connects and first sends one line:
|
|
// FLASH <baud>\n
|
|
// with the baud rate of the Nano's bootloader (57600 old bootloader, 115200 Optiboot).
|
|
// onStart(baud) then restarts the Nano into its bootloader, the bridge answers "OK\n",
|
|
// and from then on bytes are copied between the TCP connection and the Nano's serial port
|
|
// until the client disconnects or is idle too long. onEnd() is then called.
|
|
class NanoFlashBridge
|
|
{
|
|
public:
|
|
NanoFlashBridge(uint16_t port, HardwareSerial &serial, void (*onStart)(uint32_t baud), void (*onEnd)());
|
|
|
|
void begin();
|
|
|
|
// Call from loop(). Returns true while a flash session is running; the caller must not
|
|
// send anything to the Nano then.
|
|
bool handle();
|
|
|
|
private:
|
|
// Max wait for the FLASH line after connecting
|
|
static const uint32_t HEADER_TIMEOUT = 2000;
|
|
// Give up when no data moves in either direction for this long
|
|
static const uint32_t IDLE_TIMEOUT = 10000;
|
|
|
|
WiFiServer _server;
|
|
WiFiClient _client;
|
|
HardwareSerial &_serial;
|
|
void (*_onStart)(uint32_t baud);
|
|
void (*_onEnd)();
|
|
bool _active = false;
|
|
uint32_t _last_activity = 0;
|
|
|
|
uint32_t _readHeader(WiFiClient &client);
|
|
void _end();
|
|
};
|