|
| 1 | +/* |
| 2 | + SerialNINAPassthrough - Use esptool to flash the u-blox NINA (ESP32) module |
| 3 | + Arduino MKR WiFi 1010, Arduino MKR Vidor 4000, and Arduino UNO WiFi Rev.2. |
| 4 | +
|
| 5 | + Copyright (c) 2018 Arduino SA. All rights reserved. |
| 6 | +
|
| 7 | + This library is free software; you can redistribute it and/or |
| 8 | + modify it under the terms of the GNU Lesser General Public |
| 9 | + License as published by the Free Software Foundation; either |
| 10 | + version 2.1 of the License, or (at your option) any later version. |
| 11 | +
|
| 12 | + This library is distributed in the hope that it will be useful, |
| 13 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | + Lesser General Public License for more details. |
| 16 | +
|
| 17 | + You should have received a copy of the GNU Lesser General Public |
| 18 | + License along with this library; if not, write to the Free Software |
| 19 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | +*/ |
| 21 | + |
| 22 | +#ifdef ARDUINO_SAMD_MKRVIDOR4000 |
| 23 | +#include <VidorPeripherals.h> |
| 24 | + |
| 25 | +unsigned long baud = 119400; |
| 26 | +#else |
| 27 | +unsigned long baud = 115200; |
| 28 | +#endif |
| 29 | + |
| 30 | +int rts = -1; |
| 31 | +int dtr = -1; |
| 32 | + |
| 33 | +#include <MixedSerial.h> |
| 34 | + |
| 35 | +#undef SerialNina |
| 36 | +//SoftwareSerial SerialNina(29, 30); |
| 37 | +MixedSerial SerialNina(Serial2, 30); |
| 38 | + |
| 39 | +void setup() { |
| 40 | + Serial.begin(baud); |
| 41 | + while (!Serial) {} |
| 42 | + |
| 43 | +#ifdef ARDUINO_SAMD_MKRVIDOR4000 |
| 44 | + FPGA.begin(); |
| 45 | +#endif |
| 46 | + |
| 47 | + SerialNina.begin(baud); |
| 48 | + |
| 49 | +#ifdef ARDUINO_SAMD_MKRVIDOR4000 |
| 50 | + FPGA.pinMode(FPGA_NINA_GPIO0, OUTPUT); |
| 51 | + FPGA.pinMode(FPGA_SPIWIFI_RESET, OUTPUT); |
| 52 | +#else |
| 53 | + pinMode(NINA_GPIO0, OUTPUT); |
| 54 | + pinMode(NINA_RESETN, OUTPUT); |
| 55 | +#endif |
| 56 | + |
| 57 | + digitalWrite(NINA_GPIO0, HIGH); |
| 58 | + |
| 59 | + digitalWrite(NINA_RESETN, HIGH); |
| 60 | + delay(100); |
| 61 | + digitalWrite(NINA_RESETN, LOW); |
| 62 | + delay(100); |
| 63 | + digitalWrite(NINA_RESETN, HIGH); |
| 64 | + |
| 65 | +#ifdef ARDUINO_AVR_UNO_WIFI_REV2 |
| 66 | + // manually put the NINA in upload mode |
| 67 | + digitalWrite(NINA_GPIO0, LOW); |
| 68 | + |
| 69 | + digitalWrite(NINA_RESETN, LOW); |
| 70 | + delay(100); |
| 71 | + digitalWrite(NINA_RESETN, HIGH); |
| 72 | + delay(100); |
| 73 | + digitalWrite(NINA_RESETN, LOW); |
| 74 | +#endif |
| 75 | +} |
| 76 | + |
| 77 | +long last = 0; |
| 78 | + |
| 79 | +void loop() { |
| 80 | + |
| 81 | +/* |
| 82 | + if (last - millis() > 10000) { |
| 83 | + // manually put the NINA in upload mode |
| 84 | + digitalWrite(NINA_GPIO0, HIGH); |
| 85 | +
|
| 86 | + digitalWrite(NINA_RESETN, HIGH); |
| 87 | + delay(100); |
| 88 | + digitalWrite(NINA_RESETN, LOW); |
| 89 | + delay(100); |
| 90 | + digitalWrite(NINA_RESETN, HIGH); |
| 91 | + last = millis(); |
| 92 | + } |
| 93 | +*/ |
| 94 | +#ifndef ARDUINO_AVR_UNO_WIFI_REV2 |
| 95 | + if (rts != Serial.rts()) { |
| 96 | +#ifdef ARDUINO_SAMD_MKRVIDOR4000 |
| 97 | + FPGA.digitalWrite(FPGA_SPIWIFI_RESET, (Serial.rts() == 1) ? LOW : HIGH); |
| 98 | +#else |
| 99 | + digitalWrite(NINA_RESETN, Serial.rts() ? LOW : HIGH); |
| 100 | +#endif |
| 101 | + rts = Serial.rts(); |
| 102 | + } |
| 103 | + |
| 104 | + if (dtr != Serial.dtr()) { |
| 105 | +#ifdef ARDUINO_SAMD_MKRVIDOR4000 |
| 106 | + FPGA.digitalWrite(FPGA_NINA_GPIO0, (Serial.dtr() == 1) ? HIGH : LOW); |
| 107 | +#else |
| 108 | + digitalWrite(NINA_GPIO0, (Serial.dtr() == 0) ? HIGH : LOW); |
| 109 | +#endif |
| 110 | + dtr = Serial.dtr(); |
| 111 | + } |
| 112 | +#endif |
| 113 | + |
| 114 | + if (Serial.available()) { |
| 115 | + SerialNina.write(Serial.read()); |
| 116 | + } |
| 117 | + |
| 118 | + if (SerialNina.available()) { |
| 119 | + Serial.write(SerialNina.read()); |
| 120 | + } |
| 121 | + |
| 122 | +#ifndef ARDUINO_AVR_UNO_WIFI_REV2 |
| 123 | + // check if the USB virtual serial wants a new baud rate |
| 124 | + if (Serial.baud() != baud) { |
| 125 | + rts = -1; |
| 126 | + dtr = -1; |
| 127 | + |
| 128 | + baud = Serial.baud(); |
| 129 | +#ifndef ARDUINO_SAMD_MKRVIDOR4000 |
| 130 | + SerialNina.begin(baud); |
| 131 | +#endif |
| 132 | + } |
| 133 | +#endif |
| 134 | +} |
0 commit comments