Using the Arduino Chip of LattePanda as a System Watchdog

userHead Youyou 2023-09-06 14:16:58 2267 Views0 Replies

Background


Have you ever faced this situation? When the operating system of the mainboard unexpectedly hangs or crashes, you're left with no choice but to manually reboot. This becomes cumbersome if you're traveling, or if the board is placed in an inaccessible location. What if the system could automatically reboot during such hiccups? This is where a Watchdog comes into play.

 

Thanks to the integration of an Arduino chip in the LattePanda board, we have a solution. Since the Arduino operates independently of the main system, it remains functional even if the operating system freezes or crashes. Hence, the onboard Arduino chip can serve as a Watchdog for the LattePanda.

 

This article provides a guide on how to set this up, complete with the sample code.

 

Required Materials

 

LattePanda Delta or LattePanda Alpha

 

Jumper Wires

 

Concept:

 

LattePanda Delta/Alpha Control Pins

 

The S3 and RST Pins are shown with the red arrow on the left is shown in the image above.

 

Among the pins of the LattePanda Delta/Alpha, there's the S3 pin. When the LattePanda is in sleep, hibernation, or shut down, this pin is at a low level. It’s at a high level when the LattePanda is active, serving as an indicator of the system's operational status. 

 

There's also the RST pin, which by default is at 3.3V. By pulling this pin low for 50ms, you can reboot the LattePanda.

 

 

Watchdog Functionality

 

By monitoring the S3 pin:

 

When it’s at a low level, it indicates the LattePanda is either shut down, sleeping, or hibernating. The Watchdog is not active.

 

When it’s at a high level, two scenarios are possible:

 

1. LattePanda has been powered on. By default, the Watchdog isn't active. Once the OS is fully loaded, the upper computer software sends an activation command to the Watchdog. Following this, the software must "feed the dog" by sending a signal within a 1-minute interval. Failure to do so indicates a potential OS issue, prompting the Watchdog to restart the LattePanda.

 

2. If the LattePanda wakes up from sleep or hibernation mode, the software continues to "feed the dog".

 

 

Hardware Connection


Arduino Pin  <-> LattePanda Pin

 

        D3          <->       S3

 

        D4          <->       RST

 

 

Arduino Code

 

const int statePin = 3;     // Connect LattePanda's S3 pin to Arduino's D3
const int resetPin = 4;     // Connect LattePanda's RST pin to Arduino's D4

bool watchdogActive = false;
unsigned long lastReceivedTime;

void setup() 
{
  pinMode(statePin, INPUT); // Set as input to read the state pin's level
  pinMode(resetPin, INPUT); // By default, the RST pin on LattePanda is 3.3V. Initialize Arduino pin as INPUT to match this level.
  Serial.begin(9600);
}

void loop() 
{
  // Check if the watchdog should be activated
  if (digitalRead(statePin) == HIGH)
  {
    if (Serial.available() > 0) 
    {
      char receivedChar = Serial.read();
      if (receivedChar == 's') 
      {
        watchdogActive = true;
        lastReceivedTime = millis();
      }
    }
  } else {
    watchdogActive = false;
  }

  // If watchdog is active and 's' character isn't received within 1 minute, reboot LattePanda board
  if (watchdogActive && millis() - lastReceivedTime > 60000U) 
  {
    pinMode(resetPin,OUTPUT);   // Set pin to OUTPUT mode
    digitalWrite(resetPin, LOW);  // Pull down the RST pin to reboot the LattePanda board
    delay(50);                    // Maintain a LOW level for 50ms
    pinMode(resetPin, INPUT); // Set pin back to INPUT mode to match default 3.3V level of LattePanda's RST pin
    watchdogActive = false;       // Stop the watchdog function after rebooting the LattePanda board
  }
}

 

Select the LattePanda Leonardo board and the corresponding serial port number in the Arduino IDE, compile the code and upload it.

 

Upper Computer Software Code

import serial
import serial.tools.list_ports
import time
import sys
import traceback

def find_arduino_leonardo():
    # Scan for available COM ports
    ports = serial.tools.list_ports.comports()
    for port in ports:
        # Check if "LattePanda Leonardo" is in the description of any found port
        if "LattePanda Leonardo" in port.description:
            return port.device
    return None

def main():
    try:
        # Locate the Arduino Leonardo on the LattePanda board
        device = find_arduino_leonardo()
        
        # Exit if the device is not found
        if device is None:
            print("LattePanda Leonardo device not found. Please check if the device is connected and drivers are installed.")
            input("Press any key to exit...")
            sys.exit(1)
        
        # Open a Serial connection to the Arduino Leonardo
        ser = serial.Serial(device, 9600)
        while True:
            # Send the 's' character to keep the watchdog timer alive
            ser.write(b's')
            time.sleep(30)
    except Exception as e:
        # Handle exceptions and print the error message
        print("An error occurred:")
        print(str(e))
        print(traceback.format_exc())
        input("Press any key to exit...")

if __name__ == "__main__":
    main()

 

I used python as the upper computer software. You can package it into an executable file and set it to start automatically at boot.

 

 

Precautions


When Windows Update is in progress, the Upper computer software may be closed, resulting in the inability to feed the dog in time, so please close the Windows Update function.