This sketch is an example on how to create a pass through USB to serial interface that allows you to flash the on board ESP devices of our Challenger boards. It works for devices that use either the ESP8285 as well as the ESP32C3/ESP8265 device. When this sketch is flashed on the Challenger board you can use esptool.py to flash the on board ESP-device with new firmware.
Note that the sketch must be compiled with the Adafruit TinyUSB option enabled, otherwise you will not get the required call backs from the USB stack.
If you want to get closer into how the sketch works you can enable different debug levels to see the entire operation of the download. You need to hook up an external serial port in order to get see this data though.
/*
Challenger USB to serial port.
This is an example on how to flash an onboard ESP8285 or ESP32C3.
TinyUSB must be selected for this sketch to work.
*/
#include <Arduino.h>
#ifdef USE_TINYUSB
#include <Adafruit_TinyUSB.h>
#define USE_OVERRIDE_LINE_STATE_CB 1
#endif
#include <ChallengerWiFi.h>
// Sets the level of debug info that is printed on the serial output
// 0 = No debug information at all
// 1 = Basic debug information
// 2 = Full debug information (Will make the download very slow)
#define DEBUG_LVL 0
#define PC_PORT Serial
#define DEBUG_PORT Serial1
#define PACKET_SIZE 64
#define digitalToggle(pin) digitalWrite(pin, !digitalRead(pin))
int led = LED_BUILTIN;
uint32_t baud_rate = 115200;
static int port = -1;
static int words = 0;
uint8_t ch;
char buffer[PACKET_SIZE];
size_t received;
size_t available;
uint32_t led_timer;
#if defined(USE_OVERRIDE_LINE_STATE_CB)
void tud_cdc_line_coding_cb(uint8_t itf, cdc_line_coding_t const* coding) {
(void) itf;
#if DEBUG_LVL >= 1
DEBUG_PORT.printf("\n");
#endif
if (coding->bit_rate != baud_rate) {
// flush and change baudrate to ESP8285/ESP32C3
ESP_SERIAL_PORT.begin(coding->bit_rate);
baud_rate = coding->bit_rate;
#if DEBUG_LVL >= 1
DEBUG_PORT.printf("Setting new baudrate to %d\n", baud_rate);
#endif
}
}
//
// This overrides the stack functionality to capture cdc line state
// changes.
//
void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts)
{
(void) itf; // interface ID, not used
digitalWrite(led, LOW);
if (rts) {
digitalWrite(PIN_ESP_RST, LOW);
#if DEBUG_LVL >= 1
DEBUG_PORT.printf("RST=low ");
#endif
} else {
digitalWrite(PIN_ESP_RST, HIGH);
#if DEBUG_LVL >= 1
DEBUG_PORT.printf("RST=high ");
#endif
}
if (dtr) {
digitalWrite(PIN_ESP_MODE, LOW);
#if DEBUG_LVL >= 1
DEBUG_PORT.printf("MODE=low");
#endif
} else {
digitalWrite(PIN_ESP_MODE, HIGH);
#if DEBUG_LVL >= 1
DEBUG_PORT.printf("MODE=high");
#endif
}
#if DEBUG_LVL >= 1
DEBUG_PORT.printf("\n");
#endif
// DTR = false is counted as disconnected
if (!dtr) {
// touch1200 only with first CDC instance (Serial)
if (itf == 0) {
cdc_line_coding_t coding;
tud_cdc_get_line_coding(&coding);
if (coding.bit_rate == 1200) {
TinyUSB_Port_EnterDFU();
}
}
}
}
#endif
// the setup routine runs once when you press reset:
void setup()
{
pinMode(led, OUTPUT);
digitalWrite(led, 0);
pinMode(PIN_SPI1_MOSI, OUTPUT);
digitalWrite(PIN_SPI1_MOSI, HIGH);
#if DEBUG_LVL >= 1
DEBUG_PORT.begin(921600);
DEBUG_PORT.println("\n\nDebug monitor !");
#endif
// Set default baudrates
PC_PORT.begin(baud_rate);
// Set initial state of control pins
// Normal start when the reset pin is released
if (Challenger2040WiFi.reset()) {
DEBUG_PORT.println(F("WiFi Chip reset OK !"));
} else {
DEBUG_PORT.println(F("Could not reset WiFi chip !"));
while(1);
}
while (!PC_PORT);
digitalWrite(led, LOW);
delay(100);
}
//
// The main loop were data is received from the host and then forwarded
// to the ESP8285/ESP32C3 and vice versa.
//
void loop() {
// Handle data from the USB port ment for the ESP8285/ESP32C3
available = PC_PORT.available();
if (available) {
received = PC_PORT.readBytes(buffer, available);
ESP_SERIAL_PORT.write(buffer, received);
#if DEBUG_LVL >= 2
if (port != 1) {
port = 1;
words = 0;
DEBUG_PORT.println("\nFrom USB to ESP8285/ESP32C3 ==================================");
}
for (int i=0;i<received;i++) {
DEBUG_PORT.printf("%02x ", buffer[i]);
if (++words > 15) {
words = 0;
DEBUG_PORT.println();
}
}
#endif
}
// Handle response data from the ESP8285/ESP32C3
available = ESP_SERIAL_PORT.available();
if (available) {
received = ESP_SERIAL_PORT.readBytes(buffer, available);
PC_PORT.write(buffer, received);
if (millis() - led_timer >= 10) {
led_timer = millis();
digitalToggle(led);
}
#if DEBUG_LVL >= 2
if (port != 2) {
port = 2;
words = 0;
DEBUG_PORT.println("\nFrom ESP8285/ESP32C3 to USB ==================================");
}
for (int i=0;i<received;i++) {
DEBUG_PORT.printf("%02x ", buffer[i]);
if (++words > 15) {
words = 0;
DEBUG_PORT.println();
}
}
#endif
}
}
0 Comments for “Pass through sketch for WiFi enabled Challenger boards.”