Skip to content

Instantly share code, notes, and snippets.

@macchina
Created June 29, 2017 20:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save macchina/98c530195e4fdb830ba55cf5d59e9539 to your computer and use it in GitHub Desktop.
Save macchina/98c530195e4fdb830ba55cf5d59e9539 to your computer and use it in GitHub Desktop.
#include "SamNonDuePin.h"
bool led = 0;
bool toggleBootMode = false;
bool passThrough = false;
bool pipe = false;
const int XB_nRST = X4;
const int XB_GPIO0 = 29;
const int LED_YELLOW = X0;
int doDelay = 0;
void setup()
{
while (!Serial) ;
SerialUSB.begin(115200);
Serial.begin(115200);
SerialUSB.println(printf("System Reset"));
SerialUSB.println("> ");
pinModeNonDue(XB_nRST, OUTPUT);
digitalWriteNonDue(XB_nRST, HIGH);
pinMode(XB_GPIO0, OUTPUT);
digitalWrite(XB_GPIO0, HIGH);
pinModeNonDue(LED_YELLOW, OUTPUT);
digitalWriteNonDue(LED_YELLOW, LOW);
// reboot ESP32 on startup
digitalWriteNonDue(XB_nRST, LOW);
toggleBootMode = true;
doDelay = 500;
}
void loop()
{
if(doDelay > 0) {
SerialUSB.print("Delay: ");
SerialUSB.print(doDelay);
delay(doDelay);
SerialUSB.print(".\n> ");
doDelay = 0;
}
if(toggleBootMode) {
digitalWriteNonDue(XB_nRST, HIGH);
toggleBootMode = false;
}
while(SerialUSB.available()) {
serialEventUSB();
}
}
void serialEvent() {
// 'activity' light
digitalWriteNonDue(LED_YELLOW, led ? HIGH : LOW);
led = led ? 0 : 1;
// Serial data from XB_ can just be passed straight through
while(Serial.available()) {
SerialUSB.write((uint8_t)Serial.read());
SerialUSB.flush();
}
}
void serialEventUSB() {
// 'activity' light
digitalWriteNonDue(LED_YELLOW, led ? HIGH : LOW);
led = led ? 0 : 1;
uint8_t c = SerialUSB.read();
if(pipe && c == 0x03) {
pipe = false;
SerialUSB.println("Ending pipe");
return;
}
// if already in flash mode just write and stop
if(pipe || passThrough) {
Serial.write(c);
Serial.flush();
return;
}
// proper data coming back from ESP32 can be passed back
SerialUSB.write(c);
SerialUSB.flush();
// simple command set
// b: enter boot mode
// r: reset
// both b and r lower nRST
// and toggle bootmode (back up in 500ms)
if(c == 'b' || c == 'r') {
SerialUSB.println("\n\nReseting ESP32");
digitalWriteNonDue(XB_nRST, LOW);
toggleBootMode = true;
doDelay = 500;
}
// b also lowers GPIO0 and enables pass-through
if(c == 'b') {
Serial.begin(115200);
digitalWrite(XB_GPIO0, LOW);
passThrough = true;
} else if(c == 'p') {
SerialUSB.println("\nPiping data to ESP32");
pipe = true;
} else if(c == '\n') {
SerialUSB.print("> ");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment