paulv
Posts: 558
Joined: Tue Jan 15, 2013 12:10 pm
Location: Netherlands

How-To Create a GPIO Halt Signal

Wed Jul 01, 2015 8:19 pm

Several times in the past I saw requests for a GPIO pin/pad to be used as a halt signal. ie. change state when the Pi is halted.
You can of course use the TXD pin, it goes low when the Pi is halted, but that pin is also used to transmit the boot information. If you want to use this signal to switch off the power to the Pi after it has been halted, it's difficult to use the TXD pin.

Luckily, there is another method available. I literally stumbled on this method when following the activity in another thread, which tries to use the gpio-poweroff device tree overlay. Unfortunately, that possibility seems to need a bit more work and it has some side effects you need to know. Here is that thread: viewtopic.php?f=107&t=113789

In any case, here is a method to make it work through modifying the dt-blob.dts file.
First of all, download the latest dt-blob.dts file from this link.https://www.raspberrypi.org/documentati ... uration.md
Scroll all the way down until you see the download link. Be aware of the caveat mentioned in the file about the dt-blob versions.

Save this file on your Pi and make a copy:
cp dts-blob.dts my-blob.dts
Fire-up your favorite editor and look up the section that is relevant for your Pi, or make the change to all Pi sections, to be safe. You can use any normal GPIO pin, I use GPIO-25.
I'm using the older B version, so I need to change the pins_rev2 section:

Code: Select all

      pins_rev2 {
         pin_config {
            [email protected] {
               polarity = "active_high";
               termination = "pull_down";
               startup_state = "inactive";
               function = "input";
            }; // pin
// using GPIO-25 as the power-down signal pad
// active high:
            [email protected] { function = "output"; termination = "pull_up"; polarity = "active_high"; startup_state = "inactive"; };
//
This will actually keep the pin low as soon as power is applied until the Pi is halted, when it will go high. You need to compile this file and put it in the /boot directory as follows:
sudo dtc -I dts -O dtb -o /boot/dt-blob.bin my-blob.dts
The new dt-blob.bin file will be used during the boot process instead of the default dt-blog.bin file that is compiled into the start.elf boot code.

To create an active low signal, you will need to change the polarity of the signal. You can do that by using a MOSFET. Make sure you select one that is capable of switching at a 3V3 voltage level, or use a higher voltage if you can, depending on the /KILL signal voltage level.
Kill Signal.png
Kill Signal.png (8.81 KiB) Viewed 11171 times
And finally, just after doing a shutdown and taking the power from the Pi, this is the result at power-up:
dt-blob w FET vs 3V3.JPG
dt-blob w FET vs 3V3.JPG (34.12 KiB) Viewed 11171 times
Top trace (A) is the 3V3 supply, and the lower trace (B) is the /KILL signal. Here is a screen shot when the Pi is halted.
dt-blob w FET vs TXD.JPG
dt-blob w FET vs TXD.JPG (34.25 KiB) Viewed 11171 times
Trace A is the TXD pad, which also goes low, and trace B is the active low /KILL signal.

The /KILL signal can now be used to safely disable the output of the power supply when the Pi has been halted.
If the power is not cycled, shorting the two pins of P6 or shorting the SCL0 (GPIO-3) to GND will restart the Pi.

I hope this helps,
paulv
Last edited by paulv on Sun Aug 09, 2015 7:43 pm, edited 5 times in total.

paulv
Posts: 558
Joined: Tue Jan 15, 2013 12:10 pm
Location: Netherlands

Re: How-To Create a GPIO Halt Signal

Thu Jul 02, 2015 3:16 am

To make the post above complete with the active high signal, here are the screen shots for that version, so without the MOSFET polarity changer.
IMG_2148.JPG
VCC applied
IMG_2148.JPG (35 KiB) Viewed 11143 times
Trace A is the 3V3 supply and trace B the active high GPIO-25 pin.
IMG_2149.JPG
Halted
IMG_2149.JPG (34.48 KiB) Viewed 11143 times
Trace A is the TXD pin and Trace B is the active high GPIO-25 pin.
After the Pi has been halted and you perform a reset by shortening the pins of P6, or SCL0 (GPIO-3) to GND, this is the result.
IMG_2150.JPG
Reset from Halt
IMG_2150.JPG (22.2 KiB) Viewed 11143 times
Trace A is the TXD pin and Trace B is GPIO-25.

Enjoy,
paulv

paulv
Posts: 558
Joined: Tue Jan 15, 2013 12:10 pm
Location: Netherlands

GPIO pad changes with Device Tree

Mon Jul 06, 2015 4:47 am

I wrote the post in the link below a while back, put put it in the advanced sub-forum.
It's better home is probably here, in the Device Tree sub-forum and easier to find.

viewtopic.php?f=29&t=112403

BTW, I'm still puzzled by the CE0 (GPIO-8), CE1 (GPIO-7) and GPCLK0 (GPIO-4) behavior.
I can't explain it, can you?

paulv
Posts: 558
Joined: Tue Jan 15, 2013 12:10 pm
Location: Netherlands

How-To Create a GPIO Halt Signal

Mon Jul 06, 2015 4:51 am

Here is another post that I initially put in the Advanced sub-forum, but I should have put it here if I knew about this new sub-forum earlier.

Here's the link: viewtopic.php?f=29&t=114666

texy
Forum Moderator
Forum Moderator
Posts: 5160
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: How-To Create a GPIO Halt Signal

Mon Jul 06, 2015 6:26 am

paulv wrote:Here is another post that I initially put in the Advanced sub-forum, but I should have put it here if I knew about this new sub-forum earlier.

Here's the link: viewtopic.php?f=29&t=114666
I have merged the 3 threads. Request a MOD to do this, rather than create duplicates.
Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

paulv
Posts: 558
Joined: Tue Jan 15, 2013 12:10 pm
Location: Netherlands

Re: How-To Create a GPIO Halt Signal

Mon Jul 06, 2015 11:30 am

Thank you Texy! I didn't know I could request that.

The other topic that is better hosted in the Device Tree sections is this one: viewtopic.php?f=29&t=112403
Can you move that to the Device Tree section too?
Tks,
paulv

texy
Forum Moderator
Forum Moderator
Posts: 5160
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: How-To Create a GPIO Halt Signal

Mon Jul 06, 2015 12:50 pm

paulv wrote:Thank you Texy! I didn't know I could request that.

The other topic that is better hosted in the Device Tree sections is this one: viewtopic.php?f=29&t=112403
Can you move that to the Device Tree section too?
Tks,
paulv
Hi,
I've moved that thread for you.
You can raise a report to the moderation team by hitting the red exclamation mark inside an 'upside-down' triangle of the post/thread in question.
regards,
Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

FM81
Posts: 518
Joined: Wed Apr 17, 2013 4:33 pm

Re: How-To Create a GPIO Halt Signal

Mon Jul 13, 2015 2:08 pm

Hi at all!

Can one provide a working complete my-blob.dts for exactly this scenario please? I've tried the appended file, but if I have the resulting dt-blob.bin on my BOOT-Partition the raspberry seems to start correct (according to the screen-messages), but the is no network and no USB (and also no keyboard). Means I can check/verify NOTHING what's wrong in this state. If I remove /boot/dt-blob.bin everything is fine again.

I'm use an Raspberry Model 2, so I've made the changes to the sections pins_2b1 and pins_2b2 because I didn't know, whats the difference between Pi 2 Model B rev 1.0 and Pi 2 Model B rev 1.1 and which of these I own.

Compiling with 'dtc' has given no errors of course, before.
Can anyone explain, what I'm doing wrong? Thanks ...

Greetings, FM_81
Attachments
dt-blob.dts.gz
(1.82 KiB) Downloaded 227 times
A: What does the command 'cat /dev/urandom', can you tell me please?
B: Yeah, that's very simple: It feeds your cat with radioactive material!

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

Re: How-To Create a GPIO Halt Signal

Tue Jul 14, 2015 8:33 am

The dt-blob.dts you attached is the default for new firmware, so it is correct in itself, but firmware produced in the last 6 weeks has had fewer hard-coded board settings, relying more on the contents of the dt-blob. In order for that to work, more board variants were added - the "bplus" variant used to support two revisions of B+, the A+, and two Pi 2 Bs, but now these have been split out.
Because the old "bplus" would only be correct for one of those 5, it was less confusing to give all the replacements new names.
You don't say what firmware/kernel you are using, but I would guess it predates the dt-blob reorganisation - perhaps the last Raspbian release?

Another change made last week prevents the forcible resetting of all pins to their defaults, albeit briefly if the dt-blob contains other settings. Without this patch, any pin which doesn't use the default polarity and pulls will glitch during boot, whether due to power-on or a reset.

Finally, if the latest firmware doesn't find a matching section in the dt-blob.bin it will ignore it and use the built-in version, with a warning in the boot log (sudo vcdbg log msg).

My advice would be to avoid using a custom dt-blob.bin if at all possible; it is better to choose a pin with suitable defaults and to use that instead.

FM81
Posts: 518
Joined: Wed Apr 17, 2013 4:33 pm

Re: How-To Create a GPIO Halt Signal

Tue Jul 14, 2015 11:52 am

Thanks PhilE for your reply!
I use the release from February 2015, but of course I did an UPGRADE before I've started with these tests.

Kernel:

Code: Select all

[email protected]:/home/pi# uname -r
3.18.11-v7+
Firmware:

Code: Select all

[email protected]:/home/pi# /opt/vc/bin/vcgencmd version
Apr 21 2015 14:42:19 
Copyright (c) 2012 Broadcom
version 2d5ad04b63af4233440c3f7c8587108223201102 (clean) (release)
I've also did an apt-get-update plus apt-get-upgrade few minutes ago, but this didn't do very much:

Code: Select all

Die folgenden Pakete werden aktualisiert (Upgrade):
  libsdl1.2debian raspi-config
Greetings, FM_81

PS: Please do not misunderstood, I don't understand all your explanations; two reasons: I'm very new to all that device-tree-stuff (but not new to linux) and english isn't my native language. How ever, thanks for your time ...

EDIT: Just as an idea, didn't know if possible:
Can it be created as an overlay in /boot/overlays instead of replacing the whole dt-blob.bin ???
A: What does the command 'cat /dev/urandom', can you tell me please?
B: Yeah, that's very simple: It feeds your cat with radioactive material!

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

Re: How-To Create a GPIO Halt Signal

Tue Jul 14, 2015 12:14 pm

For the very latest firmware, use rpi-update, but there will a new Raspbian release soon so you might be better off waiting for that.
EDIT: Just as an idea, didn't know if possible:
Can it be created as an overlay in /boot/overlays instead of replacing the whole dt-blob.bin ???
Perhaps. If you can tell me clearly what you are trying to do then I'll let you know whether it is possible, and if that is the best way to do it.

FM81
Posts: 518
Joined: Wed Apr 17, 2013 4:33 pm

Re: How-To Create a GPIO Halt Signal

Tue Jul 14, 2015 12:36 pm

PhilE wrote:Perhaps. If you can tell me clearly what you are trying to do then I'll let you know whether it is possible, and if that is the best way to do it.
Define one gpio (no matter which, in best case one, not used by other/additional functions) as output and pull it to LOW-level as soon as possible during boot-up AND make sure that it falls back to HIGH-level when poweroff- or halt- or shutdown-state is reached.

(In the past I've realized the "pull LOW" via script in '/etc/init.d', but as earlier this would happen, as better for my needs!)

Thanks, again, FM_81
A: What does the command 'cat /dev/urandom', can you tell me please?
B: Yeah, that's very simple: It feeds your cat with radioactive material!

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

Re: How-To Create a GPIO Halt Signal

Tue Jul 14, 2015 1:11 pm

If you read this post (viewtopic.php?p=783274#p783274) you will see that paulv is advocating using an external inverter. The advantage of this is that the signal won't glitch during a reboot, even with old firmware, and you can use the gpio-poweroff overlay I created a few weeks ago:

Code: Select all

Name:   gpio-poweroff
Info:   Drives a GPIO high or low on reboot
Load:   gpio-poweroff,<param>=<val>
Params: gpiopin                  GPIO for signalling (default 26)

        active_low               Set if the power control device requires a
                                 high->low transition to trigger a power-down.
                                 Note that this will require the support of a
                                 custom dt-blob.bin to prevent a power-down
                                 during the boot process, and that a reboot
                                 will also cause the pin to go low.


FM81
Posts: 518
Joined: Wed Apr 17, 2013 4:33 pm

Re: How-To Create a GPIO Halt Signal

Tue Jul 14, 2015 5:35 pm

I'm very lost at the moment! Your overlay looks completely different than any back-compiled from the '/boot/overlay/*.dtb'? I didn't know what to do with?

My scenario ist as followed:
1. GPIO02 goes to LOW during boot via an Script in '/etc/init.d/'. (I use GPIO02 because there is a real 1k8 pull-up.) At the same moment the NiMH-accu will be connected via another GPIO.
2. Everything runs (the NiMH-accu is loaded during this, if necessary) until the mains-power is cut.
3. The system detects the loss of AC-power (via an binary, running on raspi) and shuts down.
4. The shutdown is buffered by the Ni-MH-accu.
5. After shutdown is complete GPIO02 goes back to HIGH. (With GPIO02 I don't have to do any thing here, to get this.)
6. An external circuit (can be an ATMEL, OPV-RC-delay or what ever) detects this, and cuts with a delay of ~10 seconds the power from the NiMH-accu too.
7. Everything is off, until you give AC-power back now ...

All I want, is a replacement for steps 1 and 5? And NO, I didn't want to rebuild anyone else's circuits, or buy some "ready-to-go-variants". I'm using this since 03/13 an it has worked fine for me. My only idea was, the device-tree could give some optimizations ...
May be, I've missed something, or I've understood the usage of device-tree not correct, or it is not made for such usage, or it is to complicated for me at all?

Thanks again, FM_81
A: What does the command 'cat /dev/urandom', can you tell me please?
B: Yeah, that's very simple: It feeds your cat with radioactive material!

aldobaldo
Posts: 11
Joined: Wed Sep 27, 2017 6:30 pm

Re: How-To Create a GPIO Halt Signal

Fri Sep 29, 2017 1:56 pm

It seems this is an issue of Free BSD
"On Raspberry Pi, GPIO output pins retain state on halt"
https://bugs.freebsd.org/bugzilla/show_ ... ?id=211979

I added a comment to say that it would be handy a conf file or other one-time setup (SIMPLE!) in order to have a behavior like this (for example)
GPIO pin GEN1 output goes to 0 immediately after halt

(example: this will be connected to a circuit that cuts the power, active LOW)

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

Re: How-To Create a GPIO Halt Signal

Fri Sep 29, 2017 2:11 pm

Apologies, FM_81, I didn't see your reply until now. In case this is still of any interest to you:

The text I posted was the README entry for the now-standard gpio-poweroff overlay. It is present in all current firmware and Raspbian images. I believe that using this overlay you will be able to replace steps 1 and 5 in your scenario. All you should need to do is to make sure you are running a recent kernel and to add this your config.txt:

Code: Select all

dtoverlay=gpio-poweroff,gpiopin=2
From reading the code, at shutdown time it will driver GPIO2 high for 100ms, then low for 100ms, then high again. If power is cut off in the next 3 seconds you will get a kernel warning message.

aldobaldo
Posts: 11
Joined: Wed Sep 27, 2017 6:30 pm

Re: How-To Create a GPIO Halt Signal

Fri Sep 29, 2017 4:09 pm

PhilE please better explain.

I don't have a complicated ACPI power system.
I just have a system where I can cut off power basically with a 0 (I am still undecided if to use any common Push-Button power off IC or another circuit that I designed).

You wrote that editing /boot/config.txt with
dtoverlay=gpio-poweroff,gpiopin=2
at shutdown time it will drive GPIO2 high for 100ms, then low for 100ms, then high again.

1) Is "at shutdown time" = at the end of the shutdown ?
my need: a 0 after the shutdown is complete or shortly after
101 will also work: I don't mind to wait 100ms
But hey, it will be just 10, because after few milliseconds of the 0, the power will be cut off by my circuit, and the Pi will be powered off (so no last 1 :) )
Will this work this way ???

2) Is GPIO2 the GEN2 pin of the GPIO as in https://en.wikipedia.org/wiki/Raspberry_Pi ???

Thank you for your patience - I have found quite difficult to find documentation about this topic, and I believe it is the same for the other people interested in cutting the power of their Pi or Pi+circuit via software after a regular shutdown.

aldobaldo
Posts: 11
Joined: Wed Sep 27, 2017 6:30 pm

Re: How-To Create a GPIO Halt Signal

Fri Sep 29, 2017 4:12 pm

Addenda: I'm using Raspian Jessie Lite (but as you can see from my first post of today I was able to find info on this only on FreeBSD)

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

Re: How-To Create a GPIO Halt Signal

Fri Sep 29, 2017 4:20 pm

1) Is "at shutdown time" = at the end of the shutdown ?
Yes - it is the time when it safe to turn off the power.
But hey, it will be just 10, because after few milliseconds of the 0, the power will be cut off by my circuit, and the Pi will be powered off (so no last 1 :) )
Will this work this way ???
Hmm - don't forget that the default state before the kernel is ready for the power to be removed is to drive the pin low, so your "0"-detector will trigger very early. If you want to use "0" as the "off" state then you should add ",active_low" to the dtoverlay line.
2) Is GPIO2 the GEN2 pin of the GPIO as in https://en.wikipedia.org/wiki/Raspberry_Pi ???
No - it is the pin where there is a "2" in the "GPIO#" column - this is pin 3 of the 40-way header, also known as SDA1. You can configure the overlay to use a different GPIO pin by changing the value assigned to the "gpiopin" parameter.

aldobaldo
Posts: 11
Joined: Wed Sep 27, 2017 6:30 pm

Re: How-To Create a GPIO Halt Signal

Fri Sep 29, 2017 6:52 pm

Thanks! Let's see if I understood, and one additional question.

Recap: My power-off/power-cut circuit needs a 0 for some milliseconds, this only at shutdown when the system can be safely shut off. If I understood correctly, to have this for GPIO#8, I simply add to /boot/config.txt this line:
dtoverlay=gpio-poweroff,gpiopin=8,active_low

Of course the other side of the story is that GPIO#8 must be 1 normally, when the power is on.
This should not be complicated, I selected 8 because from page 102 of the Broadcom / ARM manual:
https://www.raspberrypi.org/documentati ... herals.pdf
it seems that (at power on by default) GPIO# 0..8 and 34..36 are set up as input, pullup; and most of the rest as input, pulldown
(apparently using 7 or 8 I don't lose any function as SPI0_CE0_N SPI0_CE1_N can be put on 35,36 if needed).

Then there is the boot. Reading here and there, I understand that during the boot GPIO# 14,15 are setup to the Bcm/ARM function ALT1, so as output (for the Serial/UART; this is ok, I will need it :) ).
And here the question (default configuration; Raspian Jessie Lite): I can not find documentation for the other GPIOs - can I suppose that the boot does not touch the ARM presets??

If so, GPIO#8 starts as input pulled up to VCC (and I read that the internal pull up R is something like 50K). This would be probably sufficient for a CMOS logic to "see" this as a 1. To have a 1 for other logic/transistors a (smaller) external R to 3.3V will fit the purpose. And maybe after the boot my software can set GPIO#8 to output and to 1 (ok no need to do this as there is the R, but anyway...).

Ok that should be all. Humm well, while we are here, is there a sort of gpio overlay like the gpio-poweroff above also for the first steps of the boot??
Thank you again!

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

Re: How-To Create a GPIO Halt Signal

Fri Sep 29, 2017 7:15 pm

You are making this sound more complicated than it really is:

1. From power on, GPIO 8 pulls high.

2. Once the kernel starts it will load the gpio-poweroff module, with the GPIO configured to be active low, which makes the pin an output driving high.

3. During shutdown, once the kernel has finished all other tasks the gpio-poweroff driver drives the GPIO low.

As long as your circuit has reasonable glitch rejection I don't think you will need to do anything else.

For usage of GPIOs by the firmware see https://github.com/raspberrypi/firmware ... t-blob.dts .

aldobaldo
Posts: 11
Joined: Wed Sep 27, 2017 6:30 pm

Re: How-To Create a GPIO Halt Signal

Fri Sep 29, 2017 8:17 pm

thank you, this looks great :)

FM81
Posts: 518
Joined: Wed Apr 17, 2013 4:33 pm

Re: How-To Create a GPIO Halt Signal

Thu Oct 05, 2017 8:54 am

PhilE wrote:
Fri Sep 29, 2017 2:11 pm
Apologies, FM_81, I didn't see your reply until now. In case this is still of any interest to you:

The text I posted was the README entry for the now-standard gpio-poweroff overlay. It is present in all current firmware and Raspbian images. I believe that using this overlay you will be able to replace steps 1 and 5 in your scenario. All you should need to do is to make sure you are running a recent kernel and to add this your config.txt:

Code: Select all

dtoverlay=gpio-poweroff,gpiopin=2
From reading the code, at shutdown time it will driver GPIO2 high for 100ms, then low for 100ms, then high again. If power is cut off in the next 3 seconds you will get a kernel warning message.
Thank's again! I didn't hope for any reply after two years, but I will test it as soon as it is possible for me!

EDIT 12.10.17: Thank you very much, this works exactly as expected!

Greetings, FM_81
A: What does the command 'cat /dev/urandom', can you tell me please?
B: Yeah, that's very simple: It feeds your cat with radioactive material!

Return to “Other projects”