1
1
/*
2
- ESP8285 helper class for the Challenger RP2040 WiFi boards
2
+ ESP8285/ESP32C3 helper class for the Challenger RP2040 WiFi enabled boards
3
3
4
- Copyright (c) 2021 P. Oldberg <[email protected] >
4
+ Copyright (c) 2021,2022 P. Oldberg <[email protected] >
5
5
6
6
This library is free software; you can redistribute it and/or
7
7
modify it under the terms of the GNU Lesser General Public
21
21
#include < Arduino.h>
22
22
#include < ChallengerWiFi.h>
23
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
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
29
31
}
30
32
31
33
// Do a HW reset by applying a low pulse to the reset line for 1mSec
32
34
void Challenger2040WiFiClass::doHWReset () {
33
- digitalWrite (PIN_ESP8285_RST , LOW); // Hold ESP8285 in reset
35
+ digitalWrite (PIN_ESP_RST , LOW); // Hold ESP in reset
34
36
delay (1 );
35
- digitalWrite (PIN_ESP8285_RST , HIGH); // Release ESP8285 reset
37
+ digitalWrite (PIN_ESP_RST , HIGH); // Release ESP reset
36
38
}
37
39
38
40
// Set the mode flag high to indicate normal run operation and do a HW
39
41
// 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
42
44
doHWReset ();
43
45
}
44
46
45
47
// Set the mode flag low to indicate flash operation and do a HW
46
48
// 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
49
51
doHWReset ();
50
52
}
51
53
@@ -55,53 +57,82 @@ void Challenger2040WiFiClass::flashReset() { // Prepare ESP8285 for flashing
55
57
bool Challenger2040WiFiClass::waitForReady () {
56
58
int timeout = 20 ; // Aprox max 2 sec
57
59
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 ' );
61
62
while (!rdy.startsWith (" ready" ) && timeout--) {
62
- rdy = Serial2. readStringUntil (' \n ' );
63
+ rdy = _serial-> readStringUntil (' \n ' );
63
64
}
64
- Serial2. setTimeout (1000 ); // Reset default timeout to 1000
65
+ _serial-> setTimeout (1000 ); // Reset default timeout to 1000
65
66
if (timeout)
66
67
return true ;
67
68
return false ;
68
69
}
69
70
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.
71
72
bool Challenger2040WiFiClass::reset () {
72
73
runReset ();
74
+ _serial->begin (DEFAULT_ESP_BAUDRATE);
73
75
return waitForReady ();
74
76
}
75
77
76
78
// Checks to see if the modem responds to the "AT" poll command.
77
79
bool Challenger2040WiFiClass::isAlive () {
78
- int timeout = 5 ;
80
+ int timeout = 100 ;
79
81
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 ' );
83
85
while (!rdy.startsWith (F (" OK" )) && timeout--) {
84
- rdy = Serial2.readStringUntil (' \n ' );
86
+ _serial->println (F (" AT" ));
87
+ rdy = _serial->readStringUntil (' \n ' );
85
88
}
86
- Serial2. setTimeout (1000 );
89
+ _serial-> setTimeout (1000 );
87
90
88
91
if (timeout)
89
92
return true ;
90
93
return false ;
91
94
}
92
95
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.
94
97
// 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
96
99
// reachable by doing an "AT" poll.
97
100
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
+ _serial-> print (F (" AT+UART_CUR=" ));
102
+ _serial-> print (baud);
103
+ _serial-> println (F (" ,8,1,0,0" ));
101
104
delay (100 );
102
- Serial2. end ();
103
- Serial2. begin (baud);
105
+ _serial-> end ();
106
+ _serial-> begin (baud);
104
107
return isAlive ();
105
108
}
106
109
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
+
107
138
Challenger2040WiFiClass Challenger2040WiFi;
0 commit comments