fix: infinite loop of rebooting

This commit is contained in:
eneller
2024-09-09 14:38:49 +02:00
parent 5ba2bd19df
commit 0c4b73276c
2 changed files with 23 additions and 17 deletions

View File

@@ -1 +1,2 @@
# should be in /etc/udev/rules.d/
ACTION=="add", ATTRS{idProduct}=="7523", ATTRS{idVendor}=="1a86", NAME="watchdog"

View File

@@ -1,48 +1,53 @@
const int HEARTBEAT_REBOOT_DELAY_MILLIS = 20000; //this is coupled to the HEARTBEAT_INTERVAL in src/main.rs
const int HEARTBEAT_BOOT_DELAY_MILLIS = 60000;
const int HEARTBEAT_REBOOT_DELAY_MILLIS = 2000; //this is coupled to the HEARTBEAT_INTERVAL in src/main.rs
const int HEARTBEAT_BOOT_DELAY_MILLIS = 9000;
const int PIN_REBOOT = 2;
const int PIN_BOOT = 3;
bool rebooted = false;
unsigned long startTimeMillis;
void setup() {
Serial.begin(9600);
pinMode(PIN_REBOOT, OUTPUT);
pinMode(PIN_BOOT, OUTPUT);
waitForSerial();
//NOTE might want to replace this initialization with a check in the first loop, so we dont instantly reboot
startTimeMillis = millis();
}
void loop(){
if (millis() - startTimeMillis > HEARTBEAT_REBOOT_DELAY_MILLIS){pin_switch(PIN_REBOOT);}
if (Serial.available() > 0) { // if something has been written to the serial port
startTimeMillis = millis();
clearSerial();
rebooted = false;
Serial.println("read serial");
}
unsigned long timeElapsed = millis() - startTimeMillis;
if (timeElapsed > HEARTBEAT_REBOOT_DELAY_MILLIS && not rebooted){
pin_switch(PIN_REBOOT);
startTimeMillis = millis();
rebooted = true;
Serial.println("reboot");
}
if (timeElapsed > HEARTBEAT_BOOT_DELAY_MILLIS){
pin_switch(PIN_BOOT);
startTimeMillis = millis();
Serial.println("boot");
}
}
void pin_switch(int pin){
//TODO connect the RST pins on the motherboard
Serial.println("Error!");
digitalWrite(pin, LOW);
delay(500);
digitalWrite(pin, HIGH);
waitForSerial();
}
/*
busy wait for first serial input
*/
void waitForSerial(){
while(Serial.available() == 0){
//check if we may need to initiate a normal boot instead of a reboot, e.g. after a power outage
if (millis() - startTimeMillis > HEARTBEAT_BOOT_DELAY_MILLIS){
pin_switch(PIN_BOOT);
}
}
}
void clearSerial(){
while (Serial.available()>0){