Unverified Commit 3bcbb246 authored by Pontus Oldberg's avatar Pontus Oldberg Committed by GitHub

Updated helper class for Challenger NB board. (#629)

parent 26752f6f
...@@ -20,8 +20,8 @@ ...@@ -20,8 +20,8 @@
#pragma once #pragma once
#define DEFAULT_ESP32_BAUDRATE 115200 #define DEFAULT_ESP8285_BAUDRATE 115200
#define DEFAULT_ESP_BAUDRATE DEFAULT_ESP32_BAUDRATE #define DEFAULT_ESP_BAUDRATE DEFAULT_ESP8285_BAUDRATE
class Challenger2040WiFiClass { class Challenger2040WiFiClass {
public: public:
......
/* /*
ESP8285 helper class for the Challenger RP2040 WiFi boards ESP8285/ESP32C3 helper class for the Challenger RP2040 WiFi enabled boards
Copyright (c) 2021 P. Oldberg <pontus@ilabs.se> Copyright (c) 2021,2022 P. Oldberg <pontus@ilabs.se>
This library is free software; you can redistribute it and/or This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public modify it under the terms of the GNU Lesser General Public
...@@ -21,31 +21,33 @@ ...@@ -21,31 +21,33 @@
#include <Arduino.h> #include <Arduino.h>
#include <ChallengerWiFi.h> #include <ChallengerWiFi.h>
Challenger2040WiFiClass::Challenger2040WiFiClass() { Challenger2040WiFiClass::Challenger2040WiFiClass(HardwareSerial* serial) {
pinMode(PIN_ESP8285_RST, OUTPUT); _serial = serial;
digitalWrite(PIN_ESP8285_RST, LOW); // Hold ESP8285 in reset
pinMode(PIN_ESP8285_MODE, OUTPUT); pinMode(PIN_ESP_RST, OUTPUT);
digitalWrite(PIN_ESP8285_MODE, HIGH); // Prepare for normal start digitalWrite(PIN_ESP_RST, LOW); // Hold ESP in reset
pinMode(PIN_ESP_MODE, OUTPUT);
digitalWrite(PIN_ESP_MODE, HIGH); // Prepare for normal start
} }
// Do a HW reset by applying a low pulse to the reset line for 1mSec // Do a HW reset by applying a low pulse to the reset line for 1mSec
void Challenger2040WiFiClass::doHWReset() { void Challenger2040WiFiClass::doHWReset() {
digitalWrite(PIN_ESP8285_RST, LOW); // Hold ESP8285 in reset digitalWrite(PIN_ESP_RST, LOW); // Hold ESP in reset
delay(1); delay(1);
digitalWrite(PIN_ESP8285_RST, HIGH); // Release ESP8285 reset digitalWrite(PIN_ESP_RST, HIGH); // Release ESP reset
} }
// Set the mode flag high to indicate normal run operation and do a HW // Set the mode flag high to indicate normal run operation and do a HW
// reset. // reset.
void Challenger2040WiFiClass::runReset() { // Prepare ESP8285 for normal op void Challenger2040WiFiClass::runReset() { // Prepare ESP for normal op
digitalWrite(PIN_ESP8285_MODE, HIGH); // Prepare for normal start digitalWrite(PIN_ESP_MODE, HIGH); // Prepare for normal start
doHWReset(); doHWReset();
} }
// Set the mode flag low to indicate flash operation and do a HW // Set the mode flag low to indicate flash operation and do a HW
// reset. // reset.
void Challenger2040WiFiClass::flashReset() { // Prepare ESP8285 for flashing void Challenger2040WiFiClass::flashReset() { // Prepare ESP for flashing
digitalWrite(PIN_ESP8285_MODE, LOW); // Prepare for normal start digitalWrite(PIN_ESP_MODE, LOW); // Prepare for normal start
doHWReset(); doHWReset();
} }
...@@ -55,53 +57,82 @@ void Challenger2040WiFiClass::flashReset() { // Prepare ESP8285 for flashing ...@@ -55,53 +57,82 @@ void Challenger2040WiFiClass::flashReset() { // Prepare ESP8285 for flashing
bool Challenger2040WiFiClass::waitForReady() { bool Challenger2040WiFiClass::waitForReady() {
int timeout = 20; // Aprox max 2 sec int timeout = 20; // Aprox max 2 sec
Serial2.begin(DEFAULT_ESP8285_BAUDRATE); _serial->setTimeout(100);
Serial2.setTimeout(100); String rdy = _serial->readStringUntil('\n');
String rdy = Serial2.readStringUntil('\n');
while(!rdy.startsWith("ready") && timeout--) { while(!rdy.startsWith("ready") && timeout--) {
rdy = Serial2.readStringUntil('\n'); rdy = _serial->readStringUntil('\n');
} }
Serial2.setTimeout(1000); // Reset default timeout to 1000 _serial->setTimeout(1000); // Reset default timeout to 1000
if (timeout) if (timeout)
return true; return true;
return false; return false;
} }
// Reset the ESP8285 and wait for the "ready" prompt to be returned. // Reset the ESP and wait for the "ready" prompt to be returned.
bool Challenger2040WiFiClass::reset() { bool Challenger2040WiFiClass::reset() {
runReset(); runReset();
_serial->begin(DEFAULT_ESP_BAUDRATE);
return waitForReady(); return waitForReady();
} }
// Checks to see if the modem responds to the "AT" poll command. // Checks to see if the modem responds to the "AT" poll command.
bool Challenger2040WiFiClass::isAlive() { bool Challenger2040WiFiClass::isAlive() {
int timeout = 5; int timeout = 100;
Serial2.setTimeout(250); _serial->setTimeout(250);
Serial2.println(F("AT")); _serial->println(F("AT"));
String rdy = Serial2.readStringUntil('\n'); String rdy = _serial->readStringUntil('\n');
while(!rdy.startsWith(F("OK")) && timeout--) { while(!rdy.startsWith(F("OK")) && timeout--) {
rdy = Serial2.readStringUntil('\n'); _serial->println(F("AT"));
rdy = _serial->readStringUntil('\n');
} }
Serial2.setTimeout(1000); _serial->setTimeout(1000);
if (timeout) if (timeout)
return true; return true;
return false; return false;
} }
// Change the baud rate of the ESP8285 as well as the local UART. // Change the baud rate of the ESP device as well as the local UART.
// No checking is done on the input baud rate so the user must know what // No checking is done on the input baud rate so the user must know what
// baud rates are valid. The function ends by checking if the ESP8285 is // baud rates are valid. The function ends by checking if the ESP is
// reachable by doing an "AT" poll. // reachable by doing an "AT" poll.
bool Challenger2040WiFiClass::changeBaudRate(int baud) { bool Challenger2040WiFiClass::changeBaudRate(int baud) {
Serial2.print(F("AT+UART_CUR=")); _serial->print(F("AT+UART_CUR="));
Serial2.print(baud); _serial->print(baud);
Serial2.println(F(",8,1,0,0")); _serial->println(F(",8,1,0,0"));
delay(100); delay(100);
Serial2.end(); _serial->end();
Serial2.begin(baud); _serial->begin(baud);
return isAlive(); return isAlive();
} }
// This method should be called id the builtin object isn't needed any more
// It basically just releases the UART pins for other use.
void Challenger2040WiFiClass::release() {
_serial->end();
}
// We can assign a new hardware serial port to accommodate the ESP device here.
// The function will release the previously used serial port and assign the
// new port. The ESP will be left in a reset state ready to start normal
// operation when exiting the function.
// This function is useful for when using the PIO serial ports to communicate
// with the ESP instead of the built in hardware serial port.
void Challenger2040WiFiClass::setSerial(HardwareSerial* serial) {
release();
_serial = serial;
pinMode(PIN_ESP_RST, OUTPUT);
digitalWrite(PIN_ESP_RST, LOW); // Hold ESP in reset
pinMode(PIN_ESP_MODE, OUTPUT);
digitalWrite(PIN_ESP_MODE, HIGH); // Prepare for normal start
}
// Return the current serial object
HardwareSerial* Challenger2040WiFiClass::getSerial() {
return _serial;
}
Challenger2040WiFiClass Challenger2040WiFi; Challenger2040WiFiClass Challenger2040WiFi;
/* /*
ESP8285 helper class for the Challenger RP2040 WiFi boards ESP8285/ESP32C3 helper class for the Challenger RP2040 WiFi enabled boards
Copyright (c) 2021 P. Oldberg <pontus@ilabs.se> Copyright (c) 2021,2022 P. Oldberg <pontus@ilabs.se>
This library is free software; you can redistribute it and/or This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public modify it under the terms of the GNU Lesser General Public
...@@ -17,20 +17,27 @@ ...@@ -17,20 +17,27 @@
License along with this library; if not, write to the Free Software License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/ */
#pragma once #pragma once
#define DEFAULT_ESP8285_BAUDRATE 115200 #define DEFAULT_ESP8285_BAUDRATE 115200
#define DEFAULT_ESP_BAUDRATE DEFAULT_ESP8285_BAUDRATE
class Challenger2040WiFiClass { class Challenger2040WiFiClass {
public: public:
Challenger2040WiFiClass(); Challenger2040WiFiClass(HardwareSerial* = &ESP_SERIAL_PORT);
void doHWReset(); void doHWReset();
void runReset(); void runReset();
void flashReset(); void flashReset();
bool waitForReady(); bool waitForReady();
bool reset(); bool reset();
bool isAlive(); bool isAlive();
bool changeBaudRate(int baud); bool changeBaudRate(int);
void release();
void setSerial(HardwareSerial*);
HardwareSerial* getSerial();
private:
HardwareSerial* _serial;
}; };
extern Challenger2040WiFiClass Challenger2040WiFi; extern Challenger2040WiFiClass Challenger2040WiFi;
#pragma once #pragma once
#include <ChallengerWiFi.h>
#define PINS_COUNT (24u) #define PINS_COUNT (24u)
#define NUM_DIGITAL_PINS (24u) #define NUM_DIGITAL_PINS (24u)
#define NUM_ANALOG_INPUTS (4u) #define NUM_ANALOG_INPUTS (4u)
...@@ -20,6 +18,13 @@ ...@@ -20,6 +18,13 @@
#define PIN_SERIAL2_RX (5u) #define PIN_SERIAL2_RX (5u)
#define PIN_ESP8285_RST (19u) #define PIN_ESP8285_RST (19u)
#define PIN_ESP8285_MODE (13u) #define PIN_ESP8285_MODE (13u)
#define ESP8285_SERIAL Serial2
// Uart define esp serial abstraction pins
#define PIN_ESP_TX PIN_SERIAL2_TX
#define PIN_ESP_RX PIN_SERIAL2_RX
#define PIN_ESP_RST PIN_ESP8285_RST
#define PIN_ESP_MODE PIN_ESP8285_MODE
#define ESP_SERIAL_PORT ESP8285_SERIAL
// SPI // SPI
#define PIN_SPI0_MISO (24u) #define PIN_SPI0_MISO (24u)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment