drmacro
Posts: 37
Joined: Tue Mar 08, 2016 4:26 pm

Override GPIO state at boot

Sat Oct 08, 2016 7:39 pm

HI,

I've read and even participated in some forum threads in the past about being able to force the state of the GPIO pins at boot time.

At different points device trees and at another point overlays were the preferred method. Lots of what I'm able to google is 2012 vintage, so, I'm guessing not necessarily correct for the latest OS versions.

So, my question is: what, if any, method should (can) be used to set specific GPIO pins to the desired use case and not the default at boot?

Basically I want 8 pins that are outputs and low when the PI boots. Is this possible?

Regards,
Mac

User avatar
joan
Posts: 14013
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Override GPIO state at boot

Sat Oct 08, 2016 8:03 pm

Not really. All the GPIO are set as inputs at power up. You will need to set them as outputs. You might be able to use device tree to do that fairly early in the boot process.

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

Re: Override GPIO state at boot

Thu Oct 13, 2016 12:54 pm

The firmware will look for a file called "dt-blob.bin" at boot time that allows pins functions, pulls, output values and drive strengths to be specified, but you have to make a copy of the entire configuration and change the bits you want to change, which can cause problems if the upstream default dt-blob (built into start.elf) changes.

Device Tree (as used by the kernel - not the "dt-blob" used by the firmware) allows pin functions and pulls to be specified (this is "pinctrl"), and it allows GPIOs to permanently set high or low ("gpio hogs"), but that is all.

How early in the boot process do you need these pins to be outputs? Would an input pin with pull down not suffice?

drmacro
Posts: 37
Joined: Tue Mar 08, 2016 4:26 pm

Re: Override GPIO state at boot

Thu Oct 13, 2016 1:30 pm

I did some messing around with the dt blob and the dev tree several months back. It was never clear which to use (for a given desired effect) and when either content took effect. (Read everything I could at that point and didn't come away with anything definitive in my mind.)

In this application, I'm controlling the electrical mains, so I need to make sure the controlled outputs are never out of my control and they need to come up in the known state. So, that means the driving pin needs to be below 3Vdc so the FET is turned off. My code drives it high to turn on the device.

At this point I've modified the external circuit to have one of the GPIO outputs that defaults to high control the enable-not pin on the buffer that drives the FET. This appears to be achieving the desired effect with the addition of code that sets the pin low and configures it as an output, thus enabling the buffer's outputs. Currently using GPIO BCM 2, header pin 3. It appears to come up in a high state. From what I've read, it defaults as input. I assume it is being pulled up at that point.

But, I'd still like to fully understand when and how to use the dt blob and the device tree. ;)

Mac

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

Re: Override GPIO state at boot

Thu Oct 13, 2016 1:36 pm

The safest (and easiest) way to get a known pin state from power-on is to choose pins which naturally have the correct pull state - work with the system, not against it. By default, GPIOs 0-8 pull high, and GPIOs 9-27 pull low. You can always add an external inverter if you need to.

gregeric
Posts: 1509
Joined: Mon Nov 28, 2011 10:08 am

Re: Override GPIO state at boot

Thu Oct 13, 2016 1:43 pm

Worth mentioning that GPIOs 2 & 3 additionally have strong hardware pullups (1k8 - those pins are used for the open drain IC2 interface).

ElEscalador
Posts: 667
Joined: Tue Dec 15, 2015 4:55 pm
Location: Detroit, MI USA
Contact: Website

Elescalador

Thu Oct 13, 2016 2:10 pm

Maybe a hack solution but it works: I have a pi that controls huge fireballs, so I can never let it be out of my control. My solution was to use a pair of relays with the contacts wired in series, but using the no contacts on one and the nc contacts on the other. The result is that the pins can switch from low to high while booting and it doesn't matter - one has to be high and one has to be low in order for the mains voltage to get past the relay pair.
My Autonomous Robot Project and a few of my other projects below.

https://lloydbrombach.wordpress.com/

drmacro
Posts: 37
Joined: Tue Mar 08, 2016 4:26 pm

Re: Override GPIO state at boot

Thu Oct 13, 2016 2:12 pm

PhilE wrote:The safest (and easiest) way to get a known pin state from power-on is to choose pins which have the correct pull state - work with the system, not against it. By default, GPIOs 0-8 pull high, and GPIOs 9-27 pull low. You can always add an external inverter if you need to.
In this case the system default can achieve the desired effect, but, there can be times when you need to bend the system to your needs. Hence the desire to understand blobs & trees...

The chart I found for Rev 2 B board is below, and it doesn't agree with your statement. And, if I remember correctly, my meter confirmed the chart. Is this because we're discussing different boards?

+----------+-Rev2-+------+--------+------+-------+
| wiringPi | GPIO | Phys | Name | Mode | Value |
+----------+------+------+--------+------+-------+
| 0 | 17 | 11 | GPIO 0 | IN | Low |
| 1 | 18 | 12 | GPIO 1 | IN | Low |
| 2 | 27 | 13 | GPIO 2 | IN | Low |
| 3 | 22 | 15 | GPIO 3 | IN | Low |
| 4 | 23 | 16 | GPIO 4 | IN | Low |
| 5 | 24 | 18 | GPIO 5 | IN | Low |
| 6 | 25 | 22 | GPIO 6 | IN | Low |
| 7 | 4 | 7 | GPIO 7 | IN | Low |
| 8 | 2 | 3 | SDA | IN | High |
| 9 | 3 | 5 | SCL | IN | High |
| 10 | 8 | 24 | CE0 | IN | Low |
| 11 | 7 | 26 | CE1 | IN | Low |
| 12 | 10 | 19 | MOSI | IN | Low |
| 13 | 9 | 21 | MISO | IN | Low |
| 14 | 11 | 23 | SCLK | IN | Low |
| 15 | 14 | 8 | TxD | ALT0 | High |
| 16 | 15 | 10 | RxD | ALT0 | High |
| 17 | 28 | 3 | GPIO 8 | IN | Low |
| 18 | 29 | 4 | GPIO 9 | IN | Low |
| 19 | 30 | 5 | GPIO10 | IN | Low |
| 20 | 31 | 6 | GPIO11 | IN | Low |
+----------+------+------+--------+------+-------+

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

Re: Override GPIO state at boot

Thu Oct 13, 2016 2:29 pm

1) Please use Broadcom GPIO and physical header pin numbers only - any other number scheme just confuses the matter. For example, I can't make sense of this line:

Code: Select all

| wiringPi | GPIO | Phys | Name   | Mode | Value |
|    20    |  31  |  6   | GPIO11 |  IN  |  Low  |
Physical pin 6 has been a GND since Pis began.

2) Older firmware used to rewrite all pin states - now it will only change the pins it is asked to by the dt-blob (compiled in or external), and later by the kernel.

This is the result of running "raspi-gpio get" on my Pi3 - you'll get similar output on a Model B:

Code: Select all

[email protected]:~$ sudo raspi-gpio get
BANK0 (GPIO 0 to 27):
  GPIO 00: level=1 fsel=0 alt=  func=INPUT
  GPIO 01: level=1 fsel=0 alt=  func=INPUT
  GPIO 02: level=1 fsel=0 alt=  func=INPUT
  GPIO 03: level=1 fsel=0 alt=  func=INPUT
  GPIO 04: level=1 fsel=0 alt=  func=INPUT
  GPIO 05: level=1 fsel=0 alt=  func=INPUT
  GPIO 06: level=1 fsel=0 alt=  func=INPUT
  GPIO 07: level=1 fsel=0 alt=  func=INPUT
  GPIO 08: level=1 fsel=0 alt=  func=INPUT
  GPIO 09: level=0 fsel=0 alt=  func=INPUT
  GPIO 10: level=0 fsel=0 alt=  func=INPUT
  GPIO 11: level=0 fsel=0 alt=  func=INPUT
  GPIO 12: level=0 fsel=0 alt=  func=INPUT
  GPIO 13: level=0 fsel=0 alt=  func=INPUT
  GPIO 14: level=0 fsel=2 alt=5 func=TXD1
  GPIO 15: level=1 fsel=2 alt=5 func=RXD1
  GPIO 16: level=0 fsel=0 alt=  func=INPUT
  GPIO 17: level=0 fsel=0 alt=  func=INPUT
  GPIO 18: level=0 fsel=0 alt=  func=INPUT
  GPIO 19: level=0 fsel=0 alt=  func=INPUT
  GPIO 20: level=0 fsel=0 alt=  func=INPUT
  GPIO 21: level=0 fsel=0 alt=  func=INPUT
  GPIO 22: level=0 fsel=0 alt=  func=INPUT
  GPIO 23: level=0 fsel=0 alt=  func=INPUT
  GPIO 24: level=0 fsel=0 alt=  func=INPUT
  GPIO 25: level=0 fsel=0 alt=  func=INPUT
  GPIO 26: level=0 fsel=0 alt=  func=INPUT
  GPIO 27: level=0 fsel=0 alt=  func=INPUT
Note that I have the UART enabled (ttyS0 on a Pi3), hence TXD1 and RXD1 on 14 & 15.

Use a chart like this to convert between GPIO numbers and physical pins.

drmacro
Posts: 37
Joined: Tue Mar 08, 2016 4:26 pm

Re: Override GPIO state at boot

Thu Oct 13, 2016 4:21 pm

Hmm...interesting, by sheer coincidence the pins I'm using are correct in both charts. (In the linked chart, the pins in the chart I referred to are shown as GPIO_GENx.) It appears whoever made the chart I used totally screwed up everything that was added for the 40 pin header.

In any case, after running raspi-get the naming confusion is just worse. The output I get is shown below. GPIO 00 func shows as SDA0. But the charts referenced don't show an SDA0, in the charts SDA is associated with GPIO 02 (physical pin 3). ("raspi-gpio help" doesn't specify what "GPIO 00" refers to...). Confused... :?

Code: Select all

BANK0 (GPIO 0 to 27):
  GPIO 00: level=1 fsel=4 alt=0 func=SDA0
  GPIO 01: level=1 fsel=4 alt=0 func=SCL0
  GPIO 02: level=1 fsel=0 alt=  func=INPUT
  GPIO 03: level=1 fsel=0 alt=  func=INPUT
  GPIO 04: level=1 fsel=0 alt=  func=INPUT
  GPIO 05: level=0 fsel=1 alt=  func=OUTPUT
  GPIO 06: level=1 fsel=1 alt=  func=OUTPUT
  GPIO 07: level=1 fsel=0 alt=  func=INPUT
  GPIO 08: level=1 fsel=0 alt=  func=INPUT
  GPIO 09: level=0 fsel=0 alt=  func=INPUT
  GPIO 10: level=0 fsel=0 alt=  func=INPUT
  GPIO 11: level=0 fsel=0 alt=  func=INPUT
  GPIO 12: level=0 fsel=0 alt=  func=INPUT
  GPIO 13: level=0 fsel=0 alt=  func=INPUT
  GPIO 14: level=0 fsel=0 alt=  func=INPUT
  GPIO 15: level=1 fsel=0 alt=  func=INPUT
  GPIO 16: level=1 fsel=1 alt=  func=OUTPUT
  GPIO 17: level=0 fsel=0 alt=  func=INPUT
  GPIO 18: level=0 fsel=0 alt=  func=INPUT
  GPIO 19: level=0 fsel=0 alt=  func=INPUT
  GPIO 20: level=0 fsel=0 alt=  func=INPUT
  GPIO 21: level=0 fsel=1 alt=  func=OUTPUT
  GPIO 22: level=0 fsel=0 alt=  func=INPUT
  GPIO 23: level=0 fsel=0 alt=  func=INPUT
  GPIO 24: level=0 fsel=0 alt=  func=INPUT
  GPIO 25: level=0 fsel=0 alt=  func=INPUT
  GPIO 26: level=0 fsel=0 alt=  func=INPUT
  GPIO 27: level=0 fsel=0 alt=  func=INPUT

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

Re: Override GPIO state at boot

Thu Oct 13, 2016 4:41 pm

Each GPIO pin (by which I mean one of the pins on the BCM2835-family device that can carry a GPIO signal) can have one of several functions, and each of the internal interfaces can usually be mapped to one of several GPIO pins.

SDA0/SCL0 are the signals for I2C0, which are available on GPIO0 and GPIO1. SDA1/SCL1 are for I2C1, and can be accessed on GPIO2 and GPIO3. On the rev1 Model B, I2C0 was available for general use and I2C1 was reserved for the VPU, so GPIO0 was connected to pin 3 and GPIO1 on pin 5. On all later Pis, the I2C allocation was reversed, so I2C0 is reserved for the VPU while I2C1 is available to the ARM. On those boards, GPIO2&3 are brought out on pins 3&5 - GPIO0&1 are not.

You can see the comparative pin assignments here.

drmacro
Posts: 37
Joined: Tue Mar 08, 2016 4:26 pm

Re: Override GPIO state at boot

Thu Oct 13, 2016 5:04 pm

So, if I understand correctly:

The output of raspi-get lists GPIO##.
GPIO## refers to the BCM GPIO number.

So, referring to GPIO07 is the same as seen elsewhere as BCM7?

And, one needs to take a close look at how these are referred to in the programming language of choice.
And, if available as in Python, use:

Code: Select all

GPIO.setmode(GPIO.BCM)
.

(At least this way gives me the least brain cramp... )

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

Re: Override GPIO state at boot

Thu Oct 13, 2016 5:07 pm

Yes, that's all correct.

drmacro
Posts: 37
Joined: Tue Mar 08, 2016 4:26 pm

Re: Override GPIO state at boot

Thu Oct 13, 2016 5:13 pm

Thanks!

Now someday, in my copious spare time, I'll go figure out blobs and trees. ;)

Mac

User avatar
Burngate
Posts: 5887
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK Tralfamadore
Contact: Website

Re: Override GPIO state at boot

Fri Oct 14, 2016 5:58 pm

Code: Select all

| wiringPi | GPIO | Phys | Name   | Mode | Value |
|     17   |  28  |   3  | GPIO 8 | IN   | Low   |
|     18   |  29  |   4  | GPIO 9 | IN   | Low   |
|     19   |  30  |   5  | GPIO10 | IN   | Low   |
|     20   |  31  |  6   | GPIO11 | IN   |  Low  |
These refer to P5 on B Rev 2.0 (P6 on the B Rev.2.1)

Return to “Device Tree”