feat: watchdog now restarts pc successfully

This commit is contained in:
eneller
2024-08-28 16:15:57 +02:00
parent fcf3a75918
commit 09efbb6536
3 changed files with 12 additions and 4 deletions

1
heartbeat.rules Normal file
View File

@@ -0,0 +1 @@
ACTION=="add", ATTRS{idProduct}=="7523", ATTRS{idVendor}=="1a86", NAME="watchdog"

View File

@@ -1,5 +1,5 @@
const int HEARTBEAT_MAX_DELAY_MILLIS = 20000; //this is coupled to the HEARTBEAT_INTERVAL in src/main.rs const int HEARTBEAT_MAX_DELAY_MILLIS = 20000; //this is coupled to the HEARTBEAT_INTERVAL in src/main.rs
const int PIN_SWITCH = 13; const int PIN_SWITCH = 2;
unsigned long startTimeMillis; unsigned long startTimeMillis;
void setup() { void setup() {
@@ -17,6 +17,7 @@ void loop(){
if (tickMillis > HEARTBEAT_MAX_DELAY_MILLIS){rst();} if (tickMillis > HEARTBEAT_MAX_DELAY_MILLIS){rst();}
if (Serial.available() > 0) { // if something has been written to the serial port if (Serial.available() > 0) { // if something has been written to the serial port
startTimeMillis = millis(); startTimeMillis = millis();
clearSerial();
} }
} }
@@ -24,7 +25,7 @@ void loop(){
void rst(){ void rst(){
//TODO connect the RST pins on the motherboard //TODO connect the RST pins on the motherboard
Serial.println("Error!"); Serial.println("Error!");
digialWrite(PIN_SWITCH, LOW); digitalWrite(PIN_SWITCH, LOW);
delay(500); delay(500);
digitalWrite(PIN_SWITCH, HIGH); digitalWrite(PIN_SWITCH, HIGH);
waitForSerial(); waitForSerial();
@@ -37,3 +38,8 @@ void waitForSerial(){
while(Serial.available() == 0){} while(Serial.available() == 0){}
} }
void clearSerial(){
while (Serial.available()>0){
char t = Serial.read();
}
}

View File

@@ -1,9 +1,10 @@
use std::{thread, io}; use std::{thread, io};
use std::time::Duration; use std::time::Duration;
use serialport::{available_ports, SerialPort, SerialPortType}; use serialport::SerialPort;
const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(5); const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(5);
const HEARTBEAT_MESSAGE: &str = "OK"; const HEARTBEAT_MESSAGE: &str = "OK";
const WATCHDOG_DEVICE: &str = "/dev/watchdog";
fn main() { fn main() {
@@ -12,7 +13,7 @@ fn main() {
fn setup() -> Box<dyn SerialPort>{ fn setup() -> Box<dyn SerialPort>{
//TODO find serial port of microcontroller //TODO find serial port of microcontroller
let port = serialport::new("/dev/pts/4", 9600) let port = serialport::new(WATCHDOG_DEVICE, 9600)
.open() .open()
.expect("Failed to open port"); .expect("Failed to open port");
return port; return port;