Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
SerialNINAPassthrough sketch in working condition for MKR Vidor 4000 (https://forum.arduino.cc/index.php?topic=675947.0) (based on PRs 112, 111, 110, 109) https://github.com/arduino-libraries/WiFiNINA/pulls
/*
SerialNINAPassthrough - Use esptool to flash the u-blox NINA (ESP32) module
Arduino MKR WiFi 1010, Arduino MKR Vidor 4000, and Arduino UNO WiFi Rev.2.
Copyright (c) 2018 Arduino SA. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifdef ARDUINO_SAMD_MKRVIDOR4000
#include <VidorPeripherals.h>
#endif
unsigned long baud = 115200;
#define BUFFER_SIZE 512
static unsigned int buffer_length;
static char buffer[BUFFER_SIZE];
int rts = -1;
int dtr = -1;
void setup() {
Serial.begin(baud);
#ifdef ARDUINO_SAMD_MKRVIDOR4000
FPGA.begin();
#endif
SerialNina.begin(baud);
#ifdef ARDUINO_SAMD_MKRVIDOR4000
FPGA.pinMode(FPGA_NINA_GPIO0, OUTPUT);
FPGA.pinMode(FPGA_SPIWIFI_RESET, OUTPUT);
#else
pinMode(NINA_GPIO0, OUTPUT);
pinMode(NINA_RESETN, OUTPUT);
#endif
#ifdef ARDUINO_AVR_UNO_WIFI_REV2
// manually put the NINA in upload mode
digitalWrite(NINA_GPIO0, LOW);
digitalWrite(NINA_RESETN, LOW);
delay(100);
digitalWrite(NINA_RESETN, HIGH);
delay(100);
digitalWrite(NINA_RESETN, LOW);
#endif
}
void loop() {
#ifndef ARDUINO_AVR_UNO_WIFI_REV2
if (rts != Serial.rts()) {
#ifdef ARDUINO_SAMD_MKRVIDOR4000
FPGA.digitalWrite(FPGA_SPIWIFI_RESET, (Serial.rts() == 1) ? LOW : HIGH);
#elif defined(ARDUINO_SAMD_NANO_33_IOT)
digitalWrite(NINA_RESETN, Serial.rts() ? LOW : HIGH);
#else
digitalWrite(NINA_RESETN, Serial.rts());
#endif
rts = Serial.rts();
}
if (dtr != Serial.dtr()) {
#ifdef ARDUINO_SAMD_MKRVIDOR4000
FPGA.digitalWrite(FPGA_NINA_GPIO0, (Serial.dtr() == 0) ? HIGH : LOW);
#else
digitalWrite(NINA_GPIO0, (Serial.dtr() == 0) ? HIGH : LOW);
#endif
dtr = Serial.dtr();
}
#endif
if (Serial.available() > 0) {
buffer_length = min(Serial.available(), BUFFER_SIZE);
Serial.readBytes(buffer, buffer_length);
SerialNina.write(buffer, buffer_length);
}
if (SerialNina.available() > 0) {
buffer_length = min(SerialNina.available(), BUFFER_SIZE);
SerialNina.readBytes(buffer, buffer_length);
Serial.write(buffer, buffer_length);
}
#ifndef ARDUINO_AVR_UNO_WIFI_REV2
// check if the USB virtual serial wants a new baud rate
if (Serial.baud() != baud) {
rts = -1;
dtr = -1;
baud = Serial.baud();
SerialNina.begin(baud);
}
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment