strix/arduino/strix_nina_passthrough/strix_nina_passthrough.ino

57 lines
1.8 KiB
C++

// STRIX NINA passthrough: connects the USB serial port to the NINA-W102 WiFi module's UART so
// esptool can flash its firmware. The STRIX FPGA image passes the module's UART (D14 -> RX,
// D13 <- TX), GPIO0 (D6) and RESETN (D7) through to the SAMD21 without inversion; asserted USB
// RTS holds the module in reset and asserted DTR pulls GPIO0 low, as esptool's reset sequence
// expects. Based on Arduino's SerialNINAPassthrough.
#include <StrixFPGA.h>
using namespace strix;
const int NINA_GPIO0_PIN = 6;
const int NINA_RESETN_PIN = 7;
StrixFPGA fpga;
unsigned long baud = 115200;
int rts = -1;
int dtr = -1;
void setup() {
Serial.begin(baud);
fpga.begin();
fpga.write(REG_GEN_ENABLE, 0); // reduce load while the module runs
fpga.write(REG_NINA_CTRL, 1); // let the module out of reset
Serial1.begin(baud);
pinMode(NINA_GPIO0_PIN, OUTPUT);
pinMode(NINA_RESETN_PIN, OUTPUT);
}
void loop() {
if (rts != Serial.rts()) {
digitalWrite(NINA_RESETN_PIN, Serial.rts() ? LOW : HIGH);
rts = Serial.rts();
}
if (dtr != Serial.dtr()) {
digitalWrite(NINA_GPIO0_PIN, Serial.dtr() ? LOW : HIGH); // DTR asserted: download mode
dtr = Serial.dtr();
}
// Forward in chunks: one USB write per byte is too slow at esptool's rates and lets the UART's
// receive buffer overflow.
static uint8_t buffer[256];
size_t count = 0;
while (Serial1.available() && count < sizeof(buffer)) buffer[count++] = Serial1.read();
if (count) Serial.write(buffer, count);
count = 0;
while (Serial.available() && count < sizeof(buffer)) buffer[count++] = Serial.read();
if (count) Serial1.write(buffer, count);
// Follow baud rate changes requested by esptool.
if (Serial.baud() != baud) {
rts = -1;
dtr = -1;
baud = Serial.baud();
Serial1.begin(baud);
}
}