#!/bin/sh

s_enable=1
s_validtime=3600


read_reg()
{
local reg_addr=$1
local str=$(himd.l $reg_addr 4 | grep "0000:");
reg_val=0x$(echo $str | cut -d ' ' -f 2);
}

wdg_enable()
{
himm 0x20040c00 0x1acce551 > /dev/null;

if [ $1 -eq 0 ] ; then 
himm 0x20040008 0 > /dev/null;
else
himm 0x20040008 0 > /dev/null;
himm 0x2004000c 0 > /dev/null;
himm 0x20040000 0x70000000 > /dev/null;
himm 0x20040008 3 > /dev/null;
fi

himm 0x20040c00 0 > /dev/null;

read_reg 0x20050000;
let reg_val=$(($reg_val | 0x00800000));
himm 0x20050000 $reg_val > /dev/null;
}

wdg_feed()
{
himm 0x20040c00 0x1acce551 > /dev/null;
himm 0x2004000c 0 > /dev/null;
himm 0x20040c00 0 > /dev/null;
}

main()
{
if [ $# -gt 0 ] ; then
s_enable=$1
fi
if [ $# -gt 1 ] ; then
s_validtime=$2
fi

if [ $s_enable -eq 0 ] ; then
echo "Disable watchdog.";
else
echo "Enable and feed watchdog, validtime=$s_validtime";
fi

wdg_enable $s_enable;

if [ $s_enable -ne 0 ] ; then
local elapsed=0;
local interval=3;
while [ $elapsed -lt $s_validtime ] ; do
wdg_feed;
let elapsed+=$interval; sleep $interval;
done
fi

echo "Finished process watchdog.";
}

main $@ &

