PetrSvetr
Posts: 17
Joined: Wed Mar 01, 2017 6:49 am
Location: The Czech Republic

external watchdog on GPIO output

Wed Sep 13, 2017 12:41 pm

In Linux it is generally possible to redirect kernel watchdog module ticking to GPIO (external HW watchdog) instead of the build in HW watchdog on the motherboard.
See:
https://www.kernel.org/doc/Documentatio ... io-wdt.txt
https://cateee.net/lkddb/web-lkddb/GPIO_WATCHDOG.html

Is it possible to do the same setting using the watchdog I have found in the /boot/overlays/ ?

Example code from above mentioned link:

Code: Select all

	watchdog: watchdog {
		/* ADM706 */
		compatible = "linux,wdt-gpio";
		gpios = <&gpio3 9 GPIO_ACTIVE_LOW>;
		hw_algo = "toggle";
		hw_margin_ms = <1600>;
	};

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 1365
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: external watchdog on GPIO output

Wed Sep 13, 2017 1:01 pm

Your question is ambiguous - which watchdog do you want to use, and how do you want to control it?

PetrSvetr
Posts: 17
Joined: Wed Mar 01, 2017 6:49 am
Location: The Czech Republic

Re: external watchdog on GPIO output

Wed Sep 13, 2017 1:55 pm

Sorry I was not clear/specific enough. In general, watchdog "ticking" to the GPIO pin can be used for any of the EXTERNAL HW watchdog.
In the example in my initial post here, the AM706 is mentioned.
In our case, we are going to build a power source for the raspberry equipped by PIC microcontroler which will act as an external watchdog as well.

I wonder if watchdog kernel module ticking can be redirect to some of the GPIO pin instead of ticking to the internal Broadcom watchdog timer.

From documentation of Linux watchdog API:
A Watchdog Timer (WDT) is a hardware circuit that can reset the computer system in case of a software fault. You probably knew that already.
Usually a userspace daemon will notify the kernel watchdog driver via the /dev/watchdog special device file that userspace is still alive, at regular intervals. When such a notification occurs, the driver will usually tell the hardware watchdog that everything is in order, and that the watchdog should wait for yet another little while to reset the system. If userspace fails (RAM error, kernel bug, whatever), the notifications cease to occur, and the hardware watchdog will reset the system (causing a reboot) after the timeout occurs.

PiGraham
Posts: 2679
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: external watchdog on GPIO output

Wed Sep 13, 2017 2:30 pm

Have you tried it? It looks like it should toggle gpio 9

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 1365
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: external watchdog on GPIO output

Wed Sep 13, 2017 2:37 pm

One of us is confused.

A kernel watchdog driver knows how to arm, disarm (perhaps) and tickle/refresh a specific watchdog. The BCM2835 family of processors has such a watchdog and the driver is present and enabled in Pi kernels (unless the user has said "dtparam=watchdog=off"). However, the watchdog remains dormant until something arms it from user space via /dev/watchdog.

As far as I know there is no kernel module that exists to provide a tick(le) - they only channel the signal to the specific watchdog hardware. I would be a bit concerned about an automated watchdog tick because ideally you want the knowledge that the device is in a happy and working state to come from user space. You especially don't want to tickle the watchdog from an interrupt or timer callback because huge parts of the OS could be broken while interrupts are continuing to be serviced.

Raspbian provides a watchdog daemon that takes care of some of work for you - you just need to install it (sudo apt-get install watchdog) and edit /etc/watchdog.conf to add any appropriate health detection mechanisms. This daemon will use the current watchdog driver, which on a vanilla Pi will be the BCM2835 hardware watchdog. It sounds like you want to hook up the GPIO driver instead, so you will need an overlay with

Code: Select all

target  = <&watchdog>;
__overlay__ {
	compatible = "linux,wdt-gpio";
	gpios = <&gpio 9 GPIO_ACTIVE_LOW>;
	hw_algo = "toggle";
	hw_margin_ms = <1600>;
};
With this overlay applied the wdt-gpio watchdog should be used instead, and "dtparam=watchdog=off" will disable the GPIO watchdog instead.

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 1365
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: external watchdog on GPIO output

Wed Sep 13, 2017 2:42 pm

N.B. I've changed "gpio3" to "gpio", since that is the label for the DT node for the Pi GPIO controller.

PiGraham
Posts: 2679
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: external watchdog on GPIO output

Wed Sep 13, 2017 3:40 pm

PhilE wrote:
Wed Sep 13, 2017 2:37 pm
One of us is confused.
...
As far as I know there is no kernel module that exists to provide a tick(le) - they only channel the signal to the specific watchdog hardware. I would be a bit concerned about an automated watchdog tick because ideally you want the knowledge that the device is in a happy and working state to come from user space. You especially don't want to tickle the watchdog from an interrupt or timer callback because huge parts of the OS could be broken while interrupts are continuing to be serviced.
This is just my interpretation of info from the OP, but...
PetrSvetr wrote:
Wed Sep 13, 2017 1:55 pm

From documentation of Linux watchdog API:
A Watchdog Timer (WDT) is a hardware circuit that can reset the computer system in case of a software fault. You probably knew that already.
Usually a userspace daemon will notify the kernel watchdog driver via the /dev/watchdog special device file that userspace is still alive, at regular intervals. When such a notification occurs, the driver will usually tell the hardware watchdog that everything is in order, and that the watchdog should wait for yet another little while to reset the system. If userspace fails (RAM error, kernel bug, whatever), the notifications cease to occur, and the hardware watchdog will reset the system (causing a reboot) after the timeout occurs.
I take that to mean that a userspace demon will do the tick (or I suppose it t could be the user's application code)
nd the driver toggles a gpio line rather than writing to HW WDT register. The pulse on the gpio resets the external WDT circuit.

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 1365
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: external watchdog on GPIO output

Wed Sep 13, 2017 3:47 pm

Which doesn't fit with the other statement:
I wonder if watchdog kernel module ticking can be redirect to some of the GPIO pin instead of ticking to the internal Broadcom watchdog timer.
Anyway, it's probably just a language issue so let's wait for OP to respond.

PetrSvetr
Posts: 17
Joined: Wed Mar 01, 2017 6:49 am
Location: The Czech Republic

Re: external watchdog on GPIO output

Wed Sep 13, 2017 8:33 pm

From my understanding of the HW watchdog function (in general) and from the Linux watchdog SW implementation:
  1. HW watchdog is an independent HW circuit/device, generally the countdown timer
    • this countdown timer has to be reset to the initial countdown value periodically otherwise the HW reset of the system is triggered when countdown reaches to zero (this is called "to kick" or "to feed" the watchdog sometimes).
    • internal watchdog is usually an independent electronic circuit integrated directly to the system chip (SoC, CPU ...)
    • external watchdog is connected to the system board via some of the existing interface/connector/GPIO pin ....
  2. Linux watchdog is a complex software machinery to feed the HW watchdog periodically and it is consisted of following three parts:
    • watchdog daemon (running in user space)
    • /dev/watchdog special device file
    • kernel watchdog driver
    Usually a userspace daemon will notify the kernel watchdog driver via the /dev/watchdog special device file that userspace is still alive, at
    regular intervals. When such a notification occurs, the driver will usually tell the hardware watchdog (regularly) that everything is in order, and that the watchdog should wait for yet another little while to reset the system. If userspace fails (RAM error, kernel bug, whatever), the notifications cease to occur, and the hardware watchdog will reset the system (causing a reboot) after the timeout occurs.

    If I understand above mentioned properly, the kernel driver has to kick HW watchdog repeatedly/regularly.

    I am looking for the way how the kernel driver can repeatedly kick the external watchdog connected to the GPIO output pin.

    I don’t know if it is necessary/possible to install an other watchdog kernel driver, which allows above described, instead of the standard driver provided in the Raspbian Linux distribution.

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 1365
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: external watchdog on GPIO output

Wed Sep 13, 2017 9:24 pm

Your confusion is that there isn't one kernel watchdog driver - there are several. What you actually want is for the user space daemon to repeatedly signal a different kernel driver - the one that kicks with the external watchdog via
GPIO. Fortunately I've already told you how - just follow my instructions above.

PetrSvetr
Posts: 17
Joined: Wed Mar 01, 2017 6:49 am
Location: The Czech Republic

Re: external watchdog on GPIO output

Wed Sep 13, 2017 10:51 pm

As far no success

compiled following dts

Code: Select all

/dts-v1/;
/plugin/;

/ {
    compatible = "brcm,bcm2708";
    fragment@0 {
		target  = <&watchdog>;
		__overlay__ {
			compatible = "linux,wdt-gpio";
			gpios = <&gpio 19 0>;
			hw_algo = "toggle";
			hw_margin_ms = <500>; //Maximum time to reset watchdog circuit (milliseconds).
		};
	};
};
  • sudo dtc -@ -I dts -O dtb -o my_wdt.dtb my_wdt.dts
  • cp my_wdt.dtb /boot/overlays/
  • line "dtoverlay=my_wdt" added to the /boot/config.txt
  • reboot
  • pin 19 not kicking

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 1365
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: external watchdog on GPIO output

Thu Sep 14, 2017 6:32 am

The compiled overlay should either be called my_wdt-overlay.dtb or my_wdt.dtbo.

Use "sudo vcdbg log msg" to check for loading errors.

PetrSvetr
Posts: 17
Joined: Wed Mar 01, 2017 6:49 am
Location: The Czech Republic

Re: external watchdog on GPIO output

Thu Sep 14, 2017 7:57 am

Thank you. Just to be sure, I am not doing any stupid mistake, I have tested following:

Code: Select all

/dts-v1/;
/plugin/;

/ {
    compatible = "brcm,bcm2708";

    fragment@0 {
        target = <&leds>;
        __overlay__ {
            my_led: myled {
                label = "MYLED";
                gpios = <&gpio 19 0>;
                linux,default-trigger = "timer";
            };
        };
    };
};
From my last experiments I remember "anyname.dtb" naming convention was working for me, I just want to be sure I remember well, otherwise I will respect and follow PhilE naming recommendations:

dtc -@ -I dts -O dtb -o myled.dtb myled.dts
cp myled.dtb /boot/overlays/
echo "dtoverlay=myled" >> /boot/config.txt
reboot
sudo vcdbg log msg
......
001519.112: brfs: File read: /mfs/sd/config.txt
001519.420: dtparam: i2c_arm=on
001525.279: dtparam: audio=on
001533.050: dtparam: i2c_vc=on
001537.899: brfs: File read: 1741 bytes
001558.638: brfs: File read: /mfs/sd/overlays/myled.dtb
001564.754: Loaded overlay 'myled'
002692.905: Device tree loaded to 0xbfebe00 (size 0x4105)
Pin 19 tickling 1Hz frequency as expected (measuring with average digital voltmeter only on pins 19 and GND)

Than:
comment out line "dtoverlay=myled" in /boot/config.txt
uncomment line dtoverlay=my_wdt
cp my_wdt.dtbo /boot/overlays
reboot


sudo vcdbg log msg
...............
001517.560: brfs: File read: /mfs/sd/config.txt
001517.867: dtparam: i2c_arm=on
001523.727: dtparam: audio=on
001531.493: dtparam: i2c_vc=on
001536.339: brfs: File read: 1742 bytes
001540.775: brfs: File read: /mfs/sd/overlays/my_wdt.dtbo
001544.619: Loaded overlay 'my_wdt'
002661.323: Device tree loaded to 0xbfebe00 (size 0x4132)
.....
Pin 19 NOT tickling

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 1365
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: external watchdog on GPIO output

Thu Sep 14, 2017 8:11 am

Have you rebuilt your kernel with "CONFIG_GPIO_WATCHDOG=m" ? If not, your Pi won't have the necessary gpio_wdt driver.

PiGraham
Posts: 2679
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: external watchdog on GPIO output

Thu Sep 14, 2017 8:43 am

Your myled ticks because you have attached the gpio to a timer

Code: Select all

linux,default-trigger = "timer";
For a watchdog to be effective you need a user process to drive the tick. There's not much point using a hardware timer to do that since it won't protect much at all.

This seems like a crucial question:
PhilE wrote: Have you rebuilt your kernel with "CONFIG_GPIO_WATCHDOG=m" ? If not, your Pi won't have the necessary gpio_wdt driver.
Do you have the /dev/watchdog special device file ? I assume gpio_wdt (or other watchdog driver) creates that.
Do you have a daemon running that kicks /dev/watchdog ?
It seems to me that if you have all that the gpio_wdt driver should respond to activity on /dev/watchdog by toggling the designated gpio.
If the demon fails to kick dev/watchdog, because it isn't running, there will be no toggling of the gpio, the external watchdog will not be reset and it will timeout and reset the system, which is the purpose of a watchdog.

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 1365
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: external watchdog on GPIO output

Thu Sep 14, 2017 8:51 am

PiGraham - the myled test was a clever way of demonstrating that the overlay loading and the GPIO were working, but it was not intended to replace a proper watchdog daemon.

PiGraham
Posts: 2679
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: external watchdog on GPIO output

Thu Sep 14, 2017 9:00 am

PhilE wrote:
Thu Sep 14, 2017 8:51 am
PiGraham - the myled test was a clever way of demonstrating that the overlay loading and the GPIO were working, but it was not intended to replace a proper watchdog daemon.
Yes, I realise that. The device tree stuff seems to be working. PetrSvetr's comment
Pin 19 NOT tickling
suggests some confusion. He needs something to make it tick so I pointed him to checks of the other bits he needs to make that happen.

PetrSvetr
Posts: 17
Joined: Wed Mar 01, 2017 6:49 am
Location: The Czech Republic

Re: external watchdog on GPIO output

Thu Sep 14, 2017 12:06 pm

PiGraham wrote:
Thu Sep 14, 2017 8:43 am
Your myled ticks because you have attached the gpio to a timer
Yes I know it, exactly how PhilE writes, it was just the way to demonstrate I am able write/compile/install/load the Device Tree Overlay properly and I can observe/measure tickles on the intended pin. We don’t intend to use it as a replacement of the watchdog, because exactly as you write. But for testing purposes it is OK.
PiGraham wrote:
Thu Sep 14, 2017 8:43 am
Do you have the /dev/watchdog special device file ? I assume gpio_wdt (or other watchdog driver) creates that.
Yes, /dev/watchdog exists.
sudo apt-get install watchdog creates this device file by default and watchdog daemon is running after system restart by default
service --status-all
.......
[ + ] watchdog
[ - ] wd_keepalive
The problem is, exactly, as PhilE writes
PhilE wrote: Have you rebuilt your kernel with "CONFIG_GPIO_WATCHDOG=m" ? If not, your Pi won't have the necessary gpio_wdt driver.
There is not gpio_wdt driver in the default Raspbian Stretch distro (kernel 4.9.41+):
root@raspberrypi:/home/pi# find / -name "*wdt*" -print
/boot/overlays/my_wdt.dtbo
/sys/firmware/devicetree/base/soc/watchdog@7e100000/my_wdt
/sys/bus/platform/drivers/bcm2835-wdt
/home/pi/hats/eepromutils/my_wdt.dtb
/home/pi/hats/eepromutils/my_wdt.dtbo
/home/pi/hats/eepromutils/my_wdt.dts
I have made following:
aptitude install raspberrypi-kernel-headers
cd /lib/modules/4.9.41+/build
grep WATCH .config
CONFIG_WATCHDOG=y
CONFIG_WATCHDOG_CORE=y
# CONFIG_WATCHDOG_NOWAYOUT is not set
# CONFIG_WATCHDOG_SYSFS is not set
# CONFIG_SOFT_WATCHDOG is not set
# CONFIG_GPIO_WATCHDOG is not set
# CONFIG_XILINX_WATCHDOG is not set
# CONFIG_ZIIRAVE_WATCHDOG is not set
# CONFIG_ARM_SP805_WATCHDOG is not set
# CONFIG_CADENCE_WATCHDOG is not set
# CONFIG_DW_WATCHDOG is not set
# CONFIG_MAX63XX_WATCHDOG is not set
# CONFIG_USBPCWATCHDOG is not set
# CONFIG_WATCHDOG_PRETIMEOUT_GOV is not set
# CONFIG_WQ_WATCHDOG is not set
More details here:
cd /lib/modules/4.9.41+/build/drivers/watchdog
more Kconfig

Now it is obvious the gpio_wdt is not present in the current kernel.
Following questions are coming in my mind:
  • Am I really the only person who needs this module?
  • Can presence of this module in the kernel confuse something?
  • Is it possible to ask kernel developers to add gpio_wdt to the next Raspbian distro?
Compiling the whole kernel or even only module for specific kernel is over my knowledge.
Any hints how to continue?

PiGraham
Posts: 2679
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: external watchdog on GPIO output

Thu Sep 14, 2017 12:41 pm

That sounds like progress. At least you know exactly where you are and what is required.

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 1365
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: external watchdog on GPIO output

Thu Sep 14, 2017 1:46 pm

Before you ask for the module to be included as standard we should check that it works:

1. sudo rpi-update && sudo reboot
This will get you a 4.9.50+ kernel.
2. Download gpio_wdt.ko. Note that this is compiled for a Pi 1 or Zero, not a Pi 2 or Pi 3, based on the kernel version string you posted.
3.

Code: Select all

sudo mkdir /lib/modules/`uname -r`/kernel/drivers/watchdog
sudo cp gpio_wdt.ko /lib/modules/`uname -r`/kernel/drivers/watchdog
sudo depmod
4. Use this modified overlay:

Code: Select all

/dts-v1/;
/plugin/;

/ {
    compatible = "brcm,bcm2708";
    fragment@0 {
                target  = <&soc>;
                __overlay__ {
                        gpio_watchdog {
                                compatible = "linux,wdt-gpio";
                                gpios = <&gpio 19 0>;
                                hw_algo = "toggle";
                                hw_margin_ms = <500>; //Maximum time to reset wa
tchdog circuit (milliseconds).
                        };
                };
        };
};
Overwriting the existing watchdog, as the firstoverlay did, stops the usual reboot and poweroff commands working, so this overlay creates a second watchdog (/dev/watchdog1). Compile and install it as before, then reboot. You should find that /dev/watchdog1 exists, and that gpio_wdt is loaded.
5. Now run "sudo cat /dev/watchdog1", and pin 19 should start to pulse. After a minute with no further /dev/watchdog1 activity the pulsing will stop and the signal will remain high, telling the external watchdog hardware to cause a reboot.

Assuming all that works for you, you can then request that the module is included in standard kernel builds:
6. Go to https://github.com/raspberrypi/linux/issues and click on the green "New Issue" button.
7. Ask for CONFIG_GPIO_WATCHDOG=m to be added to the standard configurations, explaining what it does and why this is useful.
8. I'll put on my other hat (pelwell), confirm that nobody objects, and add it.

PetrSvetr
Posts: 17
Joined: Wed Mar 01, 2017 6:49 am
Location: The Czech Republic

Re: external watchdog on GPIO output

Thu Sep 14, 2017 3:17 pm

PhilE wrote:
Thu Sep 14, 2017 1:46 pm
You should find that /dev/watchdog1 exists, and that gpio_wdt is loaded.
5. Now run "sudo cat /dev/watchdog1", and pin 19 should start to pulse. After a minute with no further /dev/watchdog1 activity the pulsing will stop and the signal will remain high, telling the external watchdog hardware to cause a reboot.
I am confirming above mentioned works exactly as described.
Thank you very much PhilE
The only question I did now solve yet is how to start the watchdog daemon to write to the /dev/watchdog1 instead to the /dev/watchdog.
I thing it will be necessary to configure the /etc/watchdog.conf after the install of the watchdog.
from man page:
watchdog-device = <device>
Set the watchdog device name. Default is to disable keep alive support.
I don’t understand: "Default is to disable keep alive support."
I will test it and post the result here.
PhilE wrote:
Thu Sep 14, 2017 1:46 pm
Assuming all that works for you, you can then request that the module is included in standard kernel builds:
6. Go to https://github.com/raspberrypi/linux/issues and click on the green "New Issue" button.
7. Ask for CONFIG_GPIO_WATCHDOG=m to be added to the standard configurations, explaining what it does and why this is useful.
I have created new issue on https://github.com/raspberrypi/linux/issues
Thank you again, PhilE

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 1365
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: external watchdog on GPIO output

Thu Sep 14, 2017 3:23 pm

It's in the raspberrypi/linux tree now, so the module will be in the next rpi-update release.

PetrSvetr
Posts: 17
Joined: Wed Mar 01, 2017 6:49 am
Location: The Czech Republic

Re: external watchdog on GPIO output

Sun Sep 17, 2017 7:44 am

Maybe a silly question, but how to get informed about new kernel release for Raspbian?
I am googling for a while but not success to find any info about it
Last edited by PetrSvetr on Sun Sep 17, 2017 8:27 am, edited 1 time in total.

User avatar
DougieLawson
Posts: 30188
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: external watchdog on GPIO output

Sun Sep 17, 2017 8:14 am

It doesn't happen. The kernel is delivered with the raspberrypi-kernel and raspberrypi-bootloader packages.

If you want to get an experimental, untested, may trash your system kernel you can run sudo rpi-update.

You can also follow commits at https://github.com/raspberrypi/linux and https://github.com/hexxeh/rpi-firmware
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.

PetrSvetr
Posts: 17
Joined: Wed Mar 01, 2017 6:49 am
Location: The Czech Republic

Re: external watchdog on GPIO output

Sun Sep 17, 2017 8:44 am

So you think the only way for me is to periodically check for kernel updates or wait for next official release of Raspbian?

Return to “Device Tree”

Who is online

Users browsing this forum: No registered users and 2 guests