|
| 1 | +/* |
| 2 | + ESP8285/ESP32C3 helper class for the Challenger RP2040 WiFi enabled boards |
| 3 | +
|
| 4 | + Copyright (c) 2021,2022 P. Oldberg <[email protected]> |
| 5 | +
|
| 6 | + This library is free software; you can redistribute it and/or |
| 7 | + modify it under the terms of the GNU Lesser General Public |
| 8 | + License as published by the Free Software Foundation; either |
| 9 | + version 2.1 of the License, or (at your option) any later version. |
| 10 | +
|
| 11 | + This library is distributed in the hope that it will be useful, |
| 12 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | + Lesser General Public License for more details. |
| 15 | +
|
| 16 | + You should have received a copy of the GNU Lesser General Public |
| 17 | + License along with this library; if not, write to the Free Software |
| 18 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | +*/ |
| 20 | + |
| 21 | +#include <Arduino.h> |
| 22 | +#include <ChallengerWiFi.h> |
| 23 | + |
| 24 | +Challenger2040WiFiClass::Challenger2040WiFiClass(HardwareSerial* serial) { |
| 25 | + _serial = serial; |
| 26 | + |
| 27 | + pinMode(PIN_ESP_RST, OUTPUT); |
| 28 | + digitalWrite(PIN_ESP_RST, LOW); // Hold ESP in reset |
| 29 | + pinMode(PIN_ESP_MODE, OUTPUT); |
| 30 | + digitalWrite(PIN_ESP_MODE, HIGH); // Prepare for normal start |
| 31 | + |
| 32 | + pinMode(D15, OUTPUT); |
| 33 | + digitalWrite(D15, HIGH); // This is to correct for the |
| 34 | + // patched PCB. |
| 35 | +} |
| 36 | + |
| 37 | +// Do a HW reset by applying a low pulse to the reset line for 1mSec |
| 38 | +void Challenger2040WiFiClass::doHWReset() { |
| 39 | + pinMode(D15, OUTPUT); |
| 40 | + digitalWrite(D15, HIGH); // This is to correct for the |
| 41 | + delay(1); |
| 42 | + digitalWrite(PIN_ESP_RST, LOW); // Hold ESP in reset |
| 43 | + delay(1); |
| 44 | + digitalWrite(PIN_ESP_RST, HIGH); // Release ESP reset |
| 45 | +} |
| 46 | + |
| 47 | +// Set the mode flag high to indicate normal run operation and do a HW |
| 48 | +// reset. |
| 49 | +void Challenger2040WiFiClass::runReset() { // Prepare ESP for normal op |
| 50 | + digitalWrite(PIN_ESP_MODE, HIGH); // Prepare for normal start |
| 51 | + doHWReset(); |
| 52 | +} |
| 53 | + |
| 54 | +// Set the mode flag low to indicate flash operation and do a HW |
| 55 | +// reset. |
| 56 | +void Challenger2040WiFiClass::flashReset() { // Prepare ESP for flashing |
| 57 | + digitalWrite(PIN_ESP_MODE, LOW); // Prepare for normal start |
| 58 | + doHWReset(); |
| 59 | +} |
| 60 | + |
| 61 | +// Wait for the modem to reply with a "ready" prompt. This can be done |
| 62 | +// after a sw or hw reset have been performed to ensure that the AT |
| 63 | +// interpreter is up and running. |
| 64 | +bool Challenger2040WiFiClass::waitForReady() { |
| 65 | + int timeout = 20; // Aprox max 2 sec |
| 66 | + |
| 67 | + _serial->setTimeout(100); |
| 68 | + String rdy = _serial->readStringUntil('\n'); |
| 69 | + while (!rdy.startsWith("ready") && timeout--) { |
| 70 | + rdy = _serial->readStringUntil('\n'); |
| 71 | + } |
| 72 | + _serial->setTimeout(1000); // Reset default timeout to 1000 |
| 73 | + if (timeout) { |
| 74 | + return true; |
| 75 | + } |
| 76 | + return false; |
| 77 | +} |
| 78 | + |
| 79 | +// Reset the ESP and wait for the "ready" prompt to be returned. |
| 80 | +bool Challenger2040WiFiClass::reset() { |
| 81 | + runReset(); |
| 82 | + _serial->begin(DEFAULT_ESP_BAUDRATE); |
| 83 | + return waitForReady(); |
| 84 | +} |
| 85 | + |
| 86 | +// Checks to see if the modem responds to the "AT" poll command. |
| 87 | +bool Challenger2040WiFiClass::isAlive() { |
| 88 | + int timeout = 100; |
| 89 | + |
| 90 | + _serial->setTimeout(250); |
| 91 | + _serial->println(F("AT")); |
| 92 | + String rdy = _serial->readStringUntil('\n'); |
| 93 | + while (!rdy.startsWith(F("OK")) && timeout--) { |
| 94 | + _serial->println(F("AT")); |
| 95 | + rdy = _serial->readStringUntil('\n'); |
| 96 | + } |
| 97 | + _serial->setTimeout(1000); |
| 98 | + |
| 99 | + if (timeout) { |
| 100 | + return true; |
| 101 | + } |
| 102 | + return false; |
| 103 | +} |
| 104 | + |
| 105 | +// Change the baud rate of the ESP device as well as the local UART. |
| 106 | +// No checking is done on the input baud rate so the user must know what |
| 107 | +// baud rates are valid. The function ends by checking if the ESP is |
| 108 | +// reachable by doing an "AT" poll. |
| 109 | +bool Challenger2040WiFiClass::changeBaudRate(int baud) { |
| 110 | + _serial->print(F("AT+UART_CUR=")); |
| 111 | + _serial->print(baud); |
| 112 | + _serial->println(F(",8,1,0,0")); |
| 113 | + delay(100); |
| 114 | + _serial->end(); |
| 115 | + _serial->begin(baud); |
| 116 | + return isAlive(); |
| 117 | +} |
| 118 | + |
| 119 | +// This method should be called id the builtin object isn't needed any more |
| 120 | +// It basically just releases the UART pins for other use. |
| 121 | +void Challenger2040WiFiClass::release() { |
| 122 | + _serial->end(); |
| 123 | +} |
| 124 | + |
| 125 | +// We can assign a new hardware serial port to accommodate the ESP device here. |
| 126 | +// The function will release the previously used serial port and assign the |
| 127 | +// new port. The ESP will be left in a reset state ready to start normal |
| 128 | +// operation when exiting the function. |
| 129 | +// This function is useful for when using the PIO serial ports to communicate |
| 130 | +// with the ESP instead of the built in hardware serial port. |
| 131 | +void Challenger2040WiFiClass::setSerial(HardwareSerial* serial) { |
| 132 | + |
| 133 | + release(); |
| 134 | + _serial = serial; |
| 135 | + |
| 136 | + pinMode(PIN_ESP_RST, OUTPUT); |
| 137 | + digitalWrite(PIN_ESP_RST, LOW); // Hold ESP in reset |
| 138 | + pinMode(PIN_ESP_MODE, OUTPUT); |
| 139 | + digitalWrite(PIN_ESP_MODE, HIGH); // Prepare for normal start |
| 140 | +} |
| 141 | + |
| 142 | +// Return the current serial object |
| 143 | +HardwareSerial* Challenger2040WiFiClass::getSerial() { |
| 144 | + return _serial; |
| 145 | +} |
| 146 | + |
| 147 | +Challenger2040WiFiClass Challenger2040WiFi; |
0 commit comments