Page 1 of 1

enhance shutdown overlay

Posted: Sun Oct 06, 2019 5:36 pm
by rudiratlos
Hello,
currently I'm using a shell script (see below), that runs in the background and observe an active low signal (by a switch to ground).
If the low signal is longer than 3 seconds, the script will initiate a shutdown.
I want to get rid of this script and want to use the shutdown overlay. But this is not useable, because it initiates an imediate shutdown causes by a low signal. I can not define a minimum time of the low signal, e.g. 3 seconds.
Who can extend and is willing to enhance the shutdown overlay?

Thanks,
Rudi


script:

Code: Select all

#!/bin/bash
# required package:
# apt-get install raspi-gpio
# edit area start
gpionum=24
cnt4shutdown=3
# cnt*1000ms 3sec -> 3
# edit area end
#
/usr/bin/raspi-gpio set $gpionum ip pu
counter=0
while true; do
 	gpiolvl=$(/usr/bin/raspi-gpio get $gpionum | awk '{print $3;}')
#	echo "$gpiolvl"
	if [ "$gpiolvl" == "level=0" ] ; then
#		echo "Button has been pressed..."
		((counter = counter + 1))
		if [ "$counter" -ge "$cnt4shutdown" ] ; then
			logger -s -t shutdownbutton "Shutting Down..."
			sudo shutdown -h now
			sleep 30
			counter=0
		fi
 	else
		counter=0
 	fi
 	sleep 1
done

Re: enhance shutdown overlay

Posted: Sun Oct 06, 2019 6:14 pm
by DougieLawson
The overlay doesn't include any executable code. It simply turns your GPIO so that a signal on that pin will trigger a "KEY_POWER" event. The kernel then reacts to that KEY_POWER event (as if you'd pressed the power key on your laptop) and starts an orderly shutdown.

Best of luck finding the code in the Linux kernel that actions that simulated keyboard event.

Re: enhance shutdown overlay

Posted: Sun Oct 06, 2019 6:53 pm
by Andyroo
This old post may help https://www.raspberrypi.org/forums/view ... p?t=185571

A bit beyond me to know if it still works though :lol:

Re: enhance shutdown overlay

Posted: Mon Oct 07, 2019 12:49 pm
by PhilE
That post explains how the gpio-shutdown overlay works (it generates a POWER key press in response to a GPIO level change), but it doesn't help to add the delay. As Dougie said, overlays don't include code - they just allow Device Tree properties to be changed in a way that a suitably configurable driver can see. Fortunately, the gpio-keys driver that is used to generate the POWER key press has a property to control the debounce interval called, cryptically, "debounce-interval" it takes an integer in milliseconds, the default value being 5ms.

Modifying the gpio-shutdown overlay to set the debounce interval is a one-line change - you can download a patched version here: https://drive.google.com/file/d/11zr1gN ... sp=sharing
Install it by copying it to /boot/overlays.

The overlay update adds a new parameter - debounce - that takes a value in milliseconds. I've set the default value to 100ms (I thought 5 seemed a bit short for a significant action like a shutdown). For your application I think you want:

Code: Select all

dtoverlay=gpio-shutdown,gpio_pin=24,debounce=3000

Re: enhance shutdown overlay

Posted: Mon Oct 14, 2019 8:46 pm
by rudiratlos
wonderfull, thanks for this enhancement.
Is that in the official overlay?
because it's already documented:
https://raw.githubusercontent.com/raspb ... ays/README
described in section gpio-shutdown:
debounce Specify the debounce interval in milliseconds (default 100)

Re: enhance shutdown overlay

Posted: Mon Oct 14, 2019 9:11 pm
by PhilE
Yes, I tested it and it seemed to work as expected, so it's now in the standard firmware releases.

Re: enhance shutdown overlay

Posted: Sat Nov 02, 2019 9:44 pm
by deepo
Is there anyway to debug the gpio-shutdown overlay?
I can't make it work on GPIO3 on Raspbian Buster Lite (freshly apt updated) on my RPI 3B+ - running headless.
I can only make it work if I attach a monitor to the HDMI port.

/Mogens

Re: enhance shutdown overlay

Posted: Sat Nov 02, 2019 10:00 pm
by PhilE
In what way does it not work? Describe what happens when you short GPIO3 to ground.

Does it work on other GPIOs?

Re: enhance shutdown overlay

Posted: Sat Nov 02, 2019 10:03 pm
by deepo
If I install the package raspi-gpio package:

Code: Select all

sudo apt-get install raspi-gpio
And then sample GPIO3 a few times I can see that it changes state without having anything connected.

Code: Select all

[email protected]:~ $ /usr/bin/raspi-gpio get 3
GPIO 3: level=0 fsel=0 func=INPUT
[email protected]:~ $ /usr/bin/raspi-gpio get 3
GPIO 3: level=1 fsel=0 func=INPUT
I was under the impression that GPIO3 had an internal pull up resistor, so it shouldn't shift by itself.

/Mogens

Re: enhance shutdown overlay

Posted: Sat Nov 02, 2019 10:10 pm
by deepo
PhilE wrote:
Sat Nov 02, 2019 10:00 pm
In what way does it not work? Describe what happens when you short GPIO3 to ground.

Does it work on other GPIOs?
The only line I have in /boot/config.txt is:

Code: Select all

dtoverlay=gpio-shutdown
Nothing happens when I connect GPIO3 (pin 5) to GND (pin 6).

I haven't tried other GPIO's - will do that tomorrow. It's getting late here in Europe.

/Mogens

Re: enhance shutdown overlay

Posted: Sat Nov 02, 2019 10:13 pm
by PhilE
All GPIOs have weak internal pull resistors - up and down - but they aren't always enabled and the default value varies with the GPIO number. You are correct that GPIO3 by default pulls up, but that resistor can be disabled or changed to a pull down.

Unless you are on a Pi 4 you can't simply read back the state of the pull - it's necessary to add some extra circuitry to do that - but you can use raspi-gpio to enable the pull-up and see if the value read back changes.

Re: enhance shutdown overlay

Posted: Sat Nov 02, 2019 10:16 pm
by deepo
PhilE wrote:
Sat Nov 02, 2019 10:13 pm
All GPIOs have weak internal pull resistors - up and down - but they aren't always enabled and the default value varies with the GPIO number. You are correct that GPIO3 by default pulls up, but that resistor can be disabled or changed to a pull down.

Unless you are on a Pi 4 you can't simply read back the state of the pull - it's necessary to add some extra circuitry to do that - but you can use raspi-gpio to enable the pull-up and see if the value read back changes.
Is that required to make gpio-shutdown work on GPIO3?
I mean the new gpio dtparam options.

I've read in a few threads that GPIO3 has an internal pull-up due to being capable of i2c (or i2s?).

/Mogens

Re: enhance shutdown overlay

Posted: Sat Nov 02, 2019 10:21 pm
by rpdom
PhilE wrote:
Sat Nov 02, 2019 10:13 pm
You are correct that GPIO3 by default pulls up, but that resistor can be disabled or changed to a pull down.
Doesn't GPIO3 (pin 5) have a physical external 1K8 pull-up resistor on the board? That can't be disabled or changed.

Re: enhance shutdown overlay

Posted: Sat Nov 02, 2019 10:26 pm
by PhilE
Yes, both GPIOs 2 and 3 also have external 1.8K resistors that can't be disabled. If you are seeing the input value changing then something must be pulling the level down more strongly.

Re: enhance shutdown overlay

Posted: Sat Nov 02, 2019 10:37 pm
by deepo
PhilE wrote:
Sat Nov 02, 2019 10:26 pm
Yes, both GPIOs 2 and 3 also have external 1.8K resistors that can't be disabled. If you are seeing the input value changing then something must be pulling the level down more strongly.
What can that be on a standard Buster Lite image?

/Mogens

Re: enhance shutdown overlay

Posted: Sat Nov 02, 2019 10:42 pm
by PhilE
I can't think of a software explanation - this sounds like something is shorting it, but I'm just guessing. When you get a chance, see if GPIO2 (which should be electrically identical) behaves the same way, then try GPIO4 which has the default pull up but no external resistor.

Re: enhance shutdown overlay

Posted: Tue Nov 05, 2019 9:09 pm
by deepo
PhilE, I think you are right about it not being a software problem.

I tried GPIO2 and GPIO4 using this configuration:

Code: Select all

dtoverlay=gpio-shutdown,gpio_pin=2,active_low=1,gpio_pull=up
And it worked fine.

Then I tried this code:

Code: Select all

#!/bin/bash
while :
do
        /usr/bin/raspi-gpio get 3
done
And discovered that GPIO3 flickers a lot.
But it did stay solid 0 when I pressed the button. So there was hope...

I then applied a 220 Ohm resistor (all I had from a Arduino starter kit) between 3.3V on pin 1 to GPIO3.
Then GPIO3 was solid on, and the gpio-shutdown overlay worked fine!

I'll need to find a bigger resistor, as 220 is too small, draws too much power and gets a little warm.

/Mogens

Re: enhance shutdown overlay

Posted: Tue Nov 05, 2019 9:19 pm
by PhilE
With nothing attached to GPIO 3, the input value should be contantly 1.

Re: enhance shutdown overlay

Posted: Tue Nov 05, 2019 9:21 pm
by deepo
PhilE wrote:
Tue Nov 05, 2019 9:19 pm
With nothing attached to GPIO 3, the input value should be contantly 1.
Yes, but it's not, it's flickering as I stated.
My other RPi 3B+ does the same, but it's running an older Stretch with desktop and Kodi.

/Mogens

Re: enhance shutdown overlay

Posted: Thu Nov 07, 2019 7:04 am
by deepo
I give up.
I tried with a 10k Ohm resistor, but that was not enough. The signal still flickers.
And I feel I'm forcing the circuit when applying a smaller resistor.
And if I e.g. use GPIO4 as input, coupled together with GPIO3, then the flickering of course shows up on GPIO4, which makes it useless.

Could someone please try this themselves, and maybe have a look at what could be driving GPIO3?

I flashed Buster Lite unto a new micro SD card, updated the installation with apt-get update and apt-get upgrade.
I then added dtoverlay=gpio-shutdown to /boot/config.txt
And then I used the script above that polls GPIO3 to see the status of the input.

/Mogens

Re: enhance shutdown overlay

Posted: Thu Nov 07, 2019 9:31 am
by PhilE
An fresh installation of the 2019-09-26 release of Raspbian, prepared in advance by adding enable_uart=1 to config.txt, boots headless on my 3B+ with a serial console. GPIO3 is constantly high:

Code: Select all

[email protected]:~$ while true; do raspi-gpio get 3; done
GPIO 3: level=1 fsel=0 func=INPUT
GPIO 3: level=1 fsel=0 func=INPUT
GPIO 3: level=1 fsel=0 func=INPUT
GPIO 3: level=1 fsel=0 func=INPUT
GPIO 3: level=1 fsel=0 func=INPUT
GPIO 3: level=1 fsel=0 func=INPUT
GPIO 3: level=1 fsel=0 func=INPUT
GPIO 3: level=1 fsel=0 func=INPUT
GPIO 3: level=1 fsel=0 func=INPUT
...

Re: enhance shutdown overlay

Posted: Thu Nov 07, 2019 7:24 pm
by deepo
I'm still getting an input that flickers.
With or without enable_uart=1 in /boot/config.txt

Code: Select all

GPIO 3: level=1 fsel=0 func=INPUT
GPIO 3: level=1 fsel=0 func=INPUT
GPIO 3: level=0 fsel=0 func=INPUT
GPIO 3: level=0 fsel=0 func=INPUT
GPIO 3: level=0 fsel=0 func=INPUT
GPIO 3: level=0 fsel=0 func=INPUT
GPIO 3: level=0 fsel=0 func=INPUT
GPIO 3: level=0 fsel=0 func=INPUT
GPIO 3: level=1 fsel=0 func=INPUT
GPIO 3: level=1 fsel=0 func=INPUT
GPIO 3: level=0 fsel=0 func=INPUT
GPIO 3: level=0 fsel=0 func=INPUT
GPIO 3: level=0 fsel=0 func=INPUT
GPIO 3: level=0 fsel=0 func=INPUT
GPIO 3: level=0 fsel=0 func=INPUT
GPIO 3: level=0 fsel=0 func=INPUT
GPIO 3: level=0 fsel=0 func=INPUT
GPIO 3: level=0 fsel=0 func=INPUT
GPIO 3: level=0 fsel=0 func=INPUT
GPIO 3: level=0 fsel=0 func=INPUT
Maybe it's broken...

/Mogens

Re: enhance shutdown overlay

Posted: Thu Nov 07, 2019 7:27 pm
by PhilE
I can't think of another explanation.

Re: enhance shutdown overlay

Posted: Thu Nov 14, 2019 9:38 pm
by deepo
I ran the pigpio test program:

Code: Select all

[email protected]:~/gpiotest $ ./gpiotest
This program checks the Pi's (user) gpios.

The program reads and writes all the gpios.  Make sure NOTHING
is connected to the gpios during this test.

The program uses the pigpio daemon which must be running.

To start the daemon use the command sudo pigpiod.

Press the ENTER key to continue or ctrl-C to abort...

Testing...
Pull up on gpio 3 failed.
Skipped non-user gpios: 0 1 28 29 30 31
Tested user gpios: 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
Failed user gpios: 3
And sure enough, it reports GPIO3 as failed.
Time to put an RPI 4B on the wish list for Christmas :)

/Mogens