#!/bin/bash

# The reset device (an STM6522) is connected to GPIO 0. Toggling its ~SR0 pin resets its countdown.
# This script shall be called very late during shutdown. This ensures that the external watchdog is
# reset and the device is allowed to have the same amount of time as after a cold start (power cycle). 
# This prevents unexpected differences in behaviour between reboot and cold start.

# This could be solved much more elegantly with libgpiod with only the command 'gpioset --mode=time --usec=1000 gpiochip0 0=1'
# However, all of GPIO handling should transition to the newer and recommended libgpiod, as mixed operation has some pitfalls
[ -e /sys/class/gpio/gpio0 ] || echo 0 > /sys/class/gpio/export
echo 'out' > /sys/class/gpio/gpio0/direction
echo 1 > /sys/class/gpio/gpio0/value
sleep 1.0e-3
echo 0 > /sys/class/gpio/gpio0/value
# Print to the serial console if it is available
[ -c /dev/ttymxc3 ] && echo "External Watchdog has been reset" > /dev/ttymxc3
