11/*
2- ESP8285 helper class for the Challenger RP2040 WiFi boards
2+ ESP8285/ESP32C3 helper class for the Challenger RP2040 WiFi enabled boards
33
4- Copyright (c) 2021 P. Oldberg <[email protected] > 4+ Copyright (c) 2021,2022 P. Oldberg <[email protected] > 55
66 This library is free software; you can redistribute it and/or
77 modify it under the terms of the GNU Lesser General Public
2121#include < Arduino.h>
2222#include < ChallengerWiFi.h>
2323
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
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
2931}
3032
3133// Do a HW reset by applying a low pulse to the reset line for 1mSec
3234void Challenger2040WiFiClass::doHWReset () {
33- digitalWrite (PIN_ESP8285_RST , LOW); // Hold ESP8285 in reset
35+ digitalWrite (PIN_ESP_RST , LOW); // Hold ESP in reset
3436 delay (1 );
35- digitalWrite (PIN_ESP8285_RST , HIGH); // Release ESP8285 reset
37+ digitalWrite (PIN_ESP_RST , HIGH); // Release ESP reset
3638}
3739
3840// Set the mode flag high to indicate normal run operation and do a HW
3941// reset.
40- void Challenger2040WiFiClass::runReset () { // Prepare ESP8285 for normal op
41- digitalWrite (PIN_ESP8285_MODE , HIGH); // Prepare for normal start
42+ void Challenger2040WiFiClass::runReset () { // Prepare ESP for normal op
43+ digitalWrite (PIN_ESP_MODE , HIGH); // Prepare for normal start
4244 doHWReset ();
4345}
4446
4547// Set the mode flag low to indicate flash operation and do a HW
4648// reset.
47- void Challenger2040WiFiClass::flashReset () { // Prepare ESP8285 for flashing
48- digitalWrite (PIN_ESP8285_MODE , LOW); // Prepare for normal start
49+ void Challenger2040WiFiClass::flashReset () { // Prepare ESP for flashing
50+ digitalWrite (PIN_ESP_MODE , LOW); // Prepare for normal start
4951 doHWReset ();
5052}
5153
@@ -55,53 +57,82 @@ void Challenger2040WiFiClass::flashReset() { // Prepare ESP8285 for flashing
5557bool Challenger2040WiFiClass::waitForReady () {
5658 int timeout = 20 ; // Aprox max 2 sec
5759
58- Serial2.begin (DEFAULT_ESP8285_BAUDRATE);
59- Serial2.setTimeout (100 );
60- String rdy = Serial2.readStringUntil (' \n ' );
60+ _serial->setTimeout (100 );
61+ String rdy = _serial->readStringUntil (' \n ' );
6162 while (!rdy.startsWith (" ready" ) && timeout--) {
62- rdy = Serial2. readStringUntil (' \n ' );
63+ rdy = _serial-> readStringUntil (' \n ' );
6364 }
64- Serial2. setTimeout (1000 ); // Reset default timeout to 1000
65+ _serial-> setTimeout (1000 ); // Reset default timeout to 1000
6566 if (timeout)
6667 return true ;
6768 return false ;
6869}
6970
70- // Reset the ESP8285 and wait for the "ready" prompt to be returned.
71+ // Reset the ESP and wait for the "ready" prompt to be returned.
7172bool Challenger2040WiFiClass::reset () {
7273 runReset ();
74+ _serial->begin (DEFAULT_ESP_BAUDRATE);
7375 return waitForReady ();
7476}
7577
7678// Checks to see if the modem responds to the "AT" poll command.
7779bool Challenger2040WiFiClass::isAlive () {
78- int timeout = 5 ;
80+ int timeout = 100 ;
7981
80- Serial2. setTimeout (250 );
81- Serial2. println (F (" AT" ));
82- String rdy = Serial2. readStringUntil (' \n ' );
82+ _serial-> setTimeout (250 );
83+ _serial-> println (F (" AT" ));
84+ String rdy = _serial-> readStringUntil (' \n ' );
8385 while (!rdy.startsWith (F (" OK" )) && timeout--) {
84- rdy = Serial2.readStringUntil (' \n ' );
86+ _serial->println (F (" AT" ));
87+ rdy = _serial->readStringUntil (' \n ' );
8588 }
86- Serial2. setTimeout (1000 );
89+ _serial-> setTimeout (1000 );
8790
8891 if (timeout)
8992 return true ;
9093 return false ;
9194}
9295
93- // Change the baud rate of the ESP8285 as well as the local UART.
96+ // Change the baud rate of the ESP device as well as the local UART.
9497// 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
98+ // baud rates are valid. The function ends by checking if the ESP is
9699// reachable by doing an "AT" poll.
97100bool Challenger2040WiFiClass::changeBaudRate (int baud) {
98- Serial2. print (F (" AT+UART_CUR=" ));
99- Serial2. print (baud);
100- Serial2. println (F (" ,8,1,0,0" ));
101+ _serial-> print (F (" AT+UART_CUR=" ));
102+ _serial-> print (baud);
103+ _serial-> println (F (" ,8,1,0,0" ));
101104 delay (100 );
102- Serial2. end ();
103- Serial2. begin (baud);
105+ _serial-> end ();
106+ _serial-> begin (baud);
104107 return isAlive ();
105108}
106109
110+ // This method should be called id the builtin object isn't needed any more
111+ // It basically just releases the UART pins for other use.
112+ void Challenger2040WiFiClass::release () {
113+ _serial->end ();
114+ }
115+
116+ // We can assign a new hardware serial port to accommodate the ESP device here.
117+ // The function will release the previously used serial port and assign the
118+ // new port. The ESP will be left in a reset state ready to start normal
119+ // operation when exiting the function.
120+ // This function is useful for when using the PIO serial ports to communicate
121+ // with the ESP instead of the built in hardware serial port.
122+ void Challenger2040WiFiClass::setSerial (HardwareSerial* serial) {
123+
124+ release ();
125+ _serial = serial;
126+
127+ pinMode (PIN_ESP_RST, OUTPUT);
128+ digitalWrite (PIN_ESP_RST, LOW); // Hold ESP in reset
129+ pinMode (PIN_ESP_MODE, OUTPUT);
130+ digitalWrite (PIN_ESP_MODE, HIGH); // Prepare for normal start
131+ }
132+
133+ // Return the current serial object
134+ HardwareSerial* Challenger2040WiFiClass::getSerial () {
135+ return _serial;
136+ }
137+
107138Challenger2040WiFiClass Challenger2040WiFi;
0 commit comments