Infraviolet
Posts: 22
Joined: Wed Jan 30, 2019 10:47 pm

Shutting down and STAYING shut down

Sun May 05, 2019 2:39 am

I'm using a Pi zero attached to a custom PCB as part of a large project. The custom PCB has a "shut down" button on it and a "startup" button. The startup button temporarily enables a "relay"* while it is held down, during this time an ATMEGA328P microcontroller (almost an arduino uno but mounted differently) on the custom PCB powers on and turns on a GPIO output, by the time that the user releases the "startup" button the atmega's GPIO is being used to keep this "relay" is the "continue to power the board" state. The pi begins booting, the atmega runs other bits of code while keeping that GPIO output high. The Pi runs and does it's stuff with the board's analogue hardware and various realtime slave microcontrollers. When it comes to shuytting down the user presses the "shutdown button", this informs that atmega microcontroller, the atmega microcontroller sends a HIGH warning signal, through a 5V to 3v3 level shifter to keep the Pi's GPIOs safely isolated form 5V voltages, to one of the Pi's GPIO inputs. When the Pi sees this pin go high it does system("shutdown -h now") from within the C code it is running. About 14 seconds later the Pi has shutdown. The 5V supply going into the Pi's 5V pins stays high at 5V, it is afterall the same 5V rail as is used to power the atmega, and the ground of the board stays at ground. Also when the "shutdown" button was pressed the atmega started a countdown of its own, and after this countdown is finished it lets it's "enable the relay" GPIO drop LOW and the whole board is powered off, the 5V rail which powers Pi, atmega and everything else goes to ground as the relay turns off. For clarity I'll mention that, once the shutdown pin has been pressed for the necessary minimum time of half a second for the atmega to decide "this is a real shutdown request and not noise" then it doesn't matter whether the user releases the button or keeps it pressed, the atmega will warn the pi and do it's countdown to cutting of the relay either way.

But I've had some trouble. Due to the existance of a few emergency (low batetry and such) shutdown modes and other stfuf which the atmega does there can be cases where the atmega doesn't cut the relay's GPIO to LOW until perhaps 20 secodns or 30 seconds afetr the "shutdown" button is pressed. This is a deliberate and desired behaviour. However I've found that sometiems during one of these extended shutdowns the Pi will have shutdown but during the time between the Pi shutting down and the atmega turning of the relay somehow the pi begins to power up again. I don't know if it's electrical noise in the circuitry causing the Pi to think it has just been freshly powered on, or something else, but it seems that a Pi which has been shutdown but is still exposed to a 5V supply may spontaneously try to start again. Starting again like this is bad, I could quite likely corrupt the SD card of the pi or cause other damage to the Pi if power to it's 5V pins is cut during startup rather than when it is in a shutdown state.

What can I do to get the Pi to, upon turning off due to the shutdown -h call, stay off and not restart? I won't want the Pi to begin startup again until next time the whole board is powered on. Is there an alternative shutdown terminal command which I can call from my C program? What about use of the "run" pin pair beside GPIOs 37 and 39, although redesigning my PCB to accomodate extra header sockets in the region of my board which aligns to that section of the Pi could be rather tricky.

Thank you

*It's not quite a normal relay, but this description is the easiest way to dicuss it without going into long details, it's a voltage regulator which has an input GPIO pin which can be sent high or low to enable the regulator's output, everything on my board is powered from that output.

markost
Posts: 17
Joined: Mon Sep 17, 2018 4:45 am

Re: Shutting down and STAYING shut down

Sun May 05, 2019 5:59 am

...atmega's GPIO is being used to keep this "relay" is the "continue to power the board" state. The pi begins booting...

I think that's a problem here. Obviously GPIO resets Raspberry Pi.
Especially when using a voltage regulator that is still faster than the classic relay.

I use a similar logic for safe boot and shutdown Raspberry Pi.

When the shutdown command begins to run, there is only one check that allows immediate start of Raspberry Pi, otherwise Raspberry goes into total shutdown (no 5V on microUSB).
After this, only a re-emergence of the voltage (with delay) causes Raspberry Pi to restart.

Here is a flow chart if it can help.



Image

Infraviolet
Posts: 22
Joined: Wed Jan 30, 2019 10:47 pm

Re: Shutting down and STAYING shut down

Sun May 05, 2019 10:42 pm

Could you just explain in text the meaning of your flowchart, your terminology on it "acc" and other such things is very different to mine, can you define briefly what those labels on your diagram mean in the context of the system components I have described. For clarity, I power my Pi off GPIO, not off the microUSB due to space constraints of where it is located when fitted to my PCB, and it really isn't practical to have a separate 5V rail on the PCB for the Pi than I have for the atmega. Thanks.

markost
Posts: 17
Joined: Mon Sep 17, 2018 4:45 am

Re: Shutting down and STAYING shut down

Mon May 06, 2019 5:11 am

Similarities in the design are:
using a microcontroller to control the process,
using delays, using GPIO for start up and as sense for shutdwon on Raspberry Pi side ...

There are two buttons in your set up (start&stop)
For me it is ACC, for both of them.

My design is designed to automate the startup & shutdown process when using
the car environment; (ACC key position in the car or may be any other swith that connect + V to the input of microcontroller).

In short, when ACC is present, the start up procedure starts (after the desired delay). <---your startup pushbutton
When the ACC is not present, the shutdown procedure is started (after the desired delay). <---your shutdown pushbutton
XY time after shutdown command started, countdown started (time for complete shutdown procedure).

After countdown time elapsed, the GPIO from microcontroller gives the command to break + 5V to Raspberry Pi.

You need this last step in your design.

Infraviolet
Posts: 22
Joined: Wed Jan 30, 2019 10:47 pm

Re: Shutting down and STAYING shut down

Mon May 06, 2019 3:12 pm

Unless you're describing something different I already have an element in my design where my microcontroller cuts off power to the pi after a delay period. But as the Pi's shutdown time can vary, and as can things coming after the "shutdown" button press but before the countdown begins, the Pi has often shut down before the relay is cut. And then in the several seconds between Pi having shut down and relay being cut the Pi begins starting up again. I'm afer a way to stop this "start again" from happening after the Pi has shutdown. I need some way to get the pi to startup when it sees a fresh 5V voltage on it's GPIOs, but to stay off if it has already shutdown and there has eben a 5V voltage on the GPIOs before during and afetr shutdown.

Logically the pi itself needs to do this to interface with the rest of my system:
1. Is there a 5V voltage on my 5V GPIO, if no then I am off, if yes continue to 2
2.Has it been there previously, if yes stay turned off, if no begin startup and continue to 3
3.I am now running, stay running as long a I can until a signal on another GPIO tells me to run the software command to shut down, continue to 4
4.I am shutting down, continue to 1


However what is actually happening is the Pi is treating noise on the 5V pin (I assume it must be noise) as being a fresh voltage to startup rather than being a period of constant 5V before power is cut.

markost
Posts: 17
Joined: Mon Sep 17, 2018 4:45 am

Re: Shutting down and STAYING shut down

Tue May 07, 2019 2:04 pm

I'm sorry, but I can not explain it easier.

You can look at the logic described at

https://www.tindie.com/products/picotou ... w-version/,

where it is described in detail how my circuit works.

Pay attention to the explanation of POWER ON (especially the last sentence) and POWER OFF (full part).

Infraviolet
Posts: 22
Joined: Wed Jan 30, 2019 10:47 pm

Re: Shutting down and STAYING shut down

Wed May 08, 2019 12:59 am

Thanks for the link, I can make much mroe sense of your flowchart when I see the rest of your system's details.
The more I think about it the more your system sounds exactly like what mine is. The real difference I see is only in the turning on phase, you have a deliberate delay after pressing the "on" switch before the pi begins booting* which isn't necessary for me, I never face situations of wanting to temporarily turn my system on without letting the Pi start. The turning off phase seems the same, aren't you suffering the problem that sometiemes the time when the Pi's power is cut off is substantially long enough after the Pi has finished shutting down that the Pi may begin to restart? As you turn off you wait a time after having sent a GPIO "turn off now" message to the Pi, then you cut off power to the Pi, and then you cut off power to the microcontroller. But if you misjudge that time it takes for the Pi to shutdown do you end up potentially cutting power prematurely (if the judge time is shorter than the reality)? And if you misjudge with too high a time what stops the Pi just trying to turn on again, it seems from my experience that by default a Pi wired to 5V on the GPIO power pins may try to start up again at any moment (perhaps from little noise spikes and dips on the power line?), and then being cut off while it is booting? I can see your device has a user adjustable time delay for all those 4 different times you've talked about, have you mainly tried to avoid doing anything to "sense" if the Pi is off and tried to avoid the risk of the pi beginning to start itself up again through simply trying to cut the 5V to the Pi precisely on cue for when, given the time you sent the "turn off" message over GPIO, you thik the Pi will have shut down and not give it any extra moments in which to risk beginning to start itself again?

*I assume this means that the microcontrolelr is able to get a separate 5V supply of it's own without exposing the Pi's pins to 5V? You have two separate regulated 5V supplies on there, a "low" current one (that 100mA LM2931) for the microcontroller and then a second unmentioend one for giving perhaps 2A to the Pi?

markost
Posts: 17
Joined: Mon Sep 17, 2018 4:45 am

Re: Shutting down and STAYING shut down

Wed May 08, 2019 6:47 pm

Try something like this for your solution (not a real code)

Code: Select all

void loop()

int state=1

if (state==1)
    {
    digitalWrite(GPIO_X, HIGH); // bypass start push button&arduino power supply and start raspberry
    delay xx seconds/minutes (raspberry full boot time)
    state=0 
    }
    
if (digitalRead(GPIO_Z) == LOW) // stop push button pressed
    {
    digitalWrite(GPIO_Y, LOW);    // start shutdown command in raspberry
    delay xx seconds/minutes (raspberry full shutdown time)
    digitalWrite(GPIO_X, LOW):
    }
Attachments
example (1).jpg
example (1).jpg (46.78 KiB) Viewed 1089 times

Infraviolet
Posts: 22
Joined: Wed Jan 30, 2019 10:47 pm

Re: Shutting down and STAYING shut down

Wed May 15, 2019 10:18 pm

That code is pretty much what mine does, but somehow the Pi starts to power up again during the "delay xx seconds/minutes (raspberry full shutdown time)" part. The pi begins powering down as soon as the previous line, "digitalWrite(GPIO_Y, LOW); // start shutdown command in raspberry" , is called, but I keep finding that after it has done so it often spontaneously starts up again, hence it is starting up right at the moment when "digitalWrite(GPIO_X, LOW):" gets called.

klricks
Posts: 6609
Joined: Sat Jan 12, 2013 3:01 am
Location: Grants Pass, OR, USA
Contact: Website

Re: Shutting down and STAYING shut down

Thu May 16, 2019 3:26 am

Infraviolet wrote:
Wed May 15, 2019 10:18 pm
That code is pretty much what mine does, but somehow the Pi starts to power up again during the "delay xx seconds/minutes (raspberry full shutdown time)" part. The pi begins powering down as soon as the previous line, "digitalWrite(GPIO_Y, LOW); // start shutdown command in raspberry" , is called, but I keep finding that after it has done so it often spontaneously starts up again, hence it is starting up right at the moment when "digitalWrite(GPIO_X, LOW):" gets called.
Do you have anything connected to GPIO 3 (pin 5)? By default, when the RPi is shutdown, shorting GPIO 3 to GND will cause a restart.
If you are also using the same pin for something else after the RPi is booted, then you may need to add a pullup. (Or use a different GPIO).

Note that by adding a special overlay to config.txt, GPIO 3 (or another GPIO), can be configured to do a safe shutdown when grounded...... without the need of a running script. (This feature can't be used on GPIO 3 if you are also using I2C).
Unless specified otherwise my response is based on the latest and fully updated Raspbian Buster w/ Desktop OS.

Infraviolet
Posts: 22
Joined: Wed Jan 30, 2019 10:47 pm

Re: Shutting down and STAYING shut down

Tue May 21, 2019 7:18 pm

Physical pin 5 is being used for i2c to a bunch of slaves. The slaves stay turned on until the whole system is shut off when the relay is turned off. I have got some pullup resistors on this pin, and the slaves should in theory not pull low when the master is not i2cing them. So I would not expect physical pin 5 to get pulled low at any time after the pi had received the "you should start shutting down" gpio signal (I use physical pin 36 for this).

Return to “Interfacing (DSI, CSI, I2C, etc.)”