|
| 1 | +/* |
| 2 | + ESP8285 helper class for the Challenger RP2040 WiFi boards |
| 3 | +
|
| 4 | + Copyright (c) 2021 P. Oldberg <pontus@ilabs.se> |
| 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() { |
| 25 | + pinMode(PIN_ESP8285_RST, OUTPUT); |
| 26 | + digitalWrite(PIN_ESP8285_RST, LOW); // Hold ESP8285 in reset |
| 27 | + pinMode(PIN_ESP8285_MODE, OUTPUT); |
| 28 | + digitalWrite(PIN_ESP8285_MODE, HIGH); // Prepare for normal start |
| 29 | +} |
| 30 | + |
| 31 | +// Do a HW reset by applying a low pulse to the reset line for 1mSec |
| 32 | +void Challenger2040WiFiClass::doHWReset() { |
| 33 | + digitalWrite(PIN_ESP8285_RST, LOW); // Hold ESP8285 in reset |
| 34 | + delay(1); |
| 35 | + digitalWrite(PIN_ESP8285_RST, HIGH); // Release ESP8285 reset |
| 36 | +} |
| 37 | + |
| 38 | +// Set the mode flag high to indicate normal run operation and do a HW |
| 39 | +// reset. |
| 40 | +void Challenger2040WiFiClass::runReset() { // Prepare ESP8285 for normal op |
| 41 | + digitalWrite(PIN_ESP8285_MODE, HIGH); // Prepare for normal start |
| 42 | + doHWReset(); |
| 43 | +} |
| 44 | + |
| 45 | +// Set the mode flag low to indicate flash operation and do a HW |
| 46 | +// reset. |
| 47 | +void Challenger2040WiFiClass::flashReset() { // Prepare ESP8285 for flashing |
| 48 | + digitalWrite(PIN_ESP8285_MODE, LOW); // Prepare for normal start |
| 49 | + doHWReset(); |
| 50 | +} |
| 51 | + |
| 52 | +// Wait for the modem to reply with a "ready" prompt. This can be done |
| 53 | +// after a sw or hw reset have been performed to ensure that the AT |
| 54 | +// interpreter is up and running. |
| 55 | +bool Challenger2040WiFiClass::waitForReady() { |
| 56 | + int timeout = 20; // Aprox max 2 sec |
| 57 | + |
| 58 | + Serial2.begin(DEFAULT_ESP8285_BAUDRATE); |
| 59 | + Serial2.setTimeout(100); |
| 60 | + String rdy = Serial2.readStringUntil('\n'); |
| 61 | + while(!rdy.startsWith("ready") && timeout--) { |
| 62 | + rdy = Serial2.readStringUntil('\n'); |
| 63 | + } |
| 64 | + Serial2.setTimeout(1000); // Reset default timeout to 1000 |
| 65 | + if (timeout) |
| 66 | + return true; |
| 67 | + return false; |
| 68 | +} |
| 69 | + |
| 70 | +// Reset the ESP8285 and wait for the "ready" prompt to be returned. |
| 71 | +bool Challenger2040WiFiClass::reset() { |
| 72 | + runReset(); |
| 73 | + return waitForReady(); |
| 74 | +} |
| 75 | + |
| 76 | +// Checks to see if the modem responds to the "AT" poll command. |
| 77 | +bool Challenger2040WiFiClass::isAlive() { |
| 78 | + int timeout = 5; |
| 79 | + |
| 80 | + Serial2.setTimeout(250); |
| 81 | + Serial2.println(F("AT")); |
| 82 | + String rdy = Serial2.readStringUntil('\n'); |
| 83 | + while(!rdy.startsWith(F("OK")) && timeout--) { |
| 84 | + rdy = Serial2.readStringUntil('\n'); |
| 85 | + } |
| 86 | + Serial2.setTimeout(1000); |
| 87 | + |
| 88 | + if (timeout) |
| 89 | + return true; |
| 90 | + return false; |
| 91 | +} |
| 92 | + |
| 93 | +// Change the baud rate of the ESP8285 as well as the local UART. |
| 94 | +// No checking is done on the input baud rate so the user must know what |
| 95 | +// baud rates are valid. The function ends by checking if the ESP8285 is |
| 96 | +// reachable by doing an "AT" poll. |
| 97 | +bool Challenger2040WiFiClass::changeBaudRate(int baud) { |
| 98 | + Serial2.print(F("AT+UART_CUR=")); |
| 99 | + Serial2.print(baud); |
| 100 | + Serial2.println(F(",8,1,0,0")); |
| 101 | + delay(100); |
| 102 | + Serial2.end(); |
| 103 | + Serial2.begin(baud); |
| 104 | + return isAlive(); |
| 105 | +} |
| 106 | + |
| 107 | +Challenger2040WiFiClass Challenger2040WiFi; |
0 commit comments