Tackling Graceful Shutdowns on the GBZ

Various user-contributed guides for software-related things
Post Reply
User avatar
Popcorn
Trailblazer
Trailblazer
Posts: 354
Joined: Thu May 05, 2016 12:58 am
Location: Berlin
Has thanked: 29 times
Been thanked: 71 times

Tackling Graceful Shutdowns on the GBZ

Post by Popcorn » Tue May 10, 2016 11:42 am

GBZ-Power-Monitor by Popcorn

UPDATE: There are multiple reports that [mention]Camble[/mention]'s Graceful Shutdown hardware solution works perfectly and with the ability to close the EN pin on the Powerboost. Something that the Pololu solution cannot do which was annoying to some. So, I would strongly consider using his hardware method if you have not already purchased a pololu switch. You can still use my monitor also with the low battery warning along with his solution (with a slight modification of one line of code). I will be testing the circuit and revising my instructions once I'm able to replicate everything and it's working well. Thanks!

My GBZ Monitor found here - https://github.com/thinkflo/GBZ-Power-Monitor

The original Pololu Solution below:

This is a Power Management utlity which is primarily built for the Gameboy Zero project. This adds graceful shutdowns from the main power switch and automatic low battery alerts and shutdowns when the battery level is low. This is meant to be used in concert with the provided list of required hardware and components within a Retropie 3.7+ environment.

This solution will work in any portable battery powered raspberry PI unit that uses a Powerboost 1000C. So not only can this be applied to the Gameboy Zero project, but also to the Adafruit Pi Grrl and Pi Grrl 2 projects as well as a wide variety of portable retro gaming systems or IoT solutions that use the Raspberry Pi and a Powerboost 1000C


Background

Currently in the GBZ, the power switch works like a normal On/Off switch. The only problem is when you cut the power, it's kinda hard on the system. There's no clean dismounting of the drives. It's really just like yanking the power out. And that's been known to cause corrupted files and disks.

My goal is to add an inexpensive and small electronic switch that will take care of the killing the power part, but only after a clean shutdown process. After some research, both the Pololu Mini Pushbutton LV or the Pololu Mini Slider LV will both work and fits our needs. They are basically identical in size and price ($5) with a couple extra features on the pushbutton version.

The main power switch will toggle ON the Pololu switch (but only ON, not off). Then, we need 3 GPIO pins mapped, one as output, and two as input. Once the unit is turned on, we use the built-in UART TX pin and send a signal to the ON pin of the electric switch which overrides it to remain on. (Alternatively, for the Pololu Mini Push Button switch, we use a GPIO pin mapped with the GPIO-Poweroff driver which maps to the OFF pin which tells the switch to shut off when we successfully power off). This ensures the system remains on until we are ready to shut it off. Another GPIO input pin will read the setting of the power switch, so we can tell when the user wants to shutdown. And lastly, the final GPIO input pin will go to the pin attached to the Low Battery LED of the Powerboost, so we can gracefully power down automatically when the battery is very low. A nice little add.


Required Hardware and Components
  • - Raspberry Pi Zero (or Model B+, Raspberry Pi 2 and Pi 3)
    - Adafruit Powerboost 1000C
    - Pololu Mini Slide Switch LV or Pololu Mini Push Button LV
    - 2N3904 NPN transistor
    - 47k resistor
    - A mini SPDT or DPDT latching push and hold switch (for emergency resets and prolonged storage)
    - Original DMG (or equivalant) SPDT Slide Power Switch

Dependencies
  • - Retropie 3.7+ or latest Raspbian
    - Python 2.7 and Python Module RPi.GPIO (comes installed with Retropie 3.7)
    - omxplayer (comes installed with Retropie 3.7)
    - Must be run as a sudoer user (the default Pi user on Retropie 3.7 is a sudoer)

Wiring Diagram

Image

Notes
  • - The built-in slide switch on the Pololu switch in the diagram must be flipped into the off position to work
    - If using the alternate Pololu Mini Push Button LV, instead of using UART TX to the ON pin, map physical pin 7/GPIO4 to the OFF pin of the Pololu Push Button LV and add the following to the /boot/config.txt file

    Code: Select all

    dtoverlay=gpio-poweroff,gpiopin=4
    - the 2nd VOUT & GND from the Pololu switch (labeled Video DC) can go to the power strip from Wermy's video guide 4
    - In Wermy's latest wiring video guide number 4, he wires the main power switch to be closed when OFF, this needs to be inverted for the Pololu switch. Use the other pin on the switch which closes when ON (or just turn the switch around). These will be mapped to the SW and GND pins of the Pololu instead. (or to A and B of the Pololu Push Button version)
    If the latching emergency reset doesn't work, try mapping it to a shared ground.

Installation

You will need to connect the PI Zero to Wifi and from another computer on the same WiFI network, SSH in (or use Putty on PCs):

Code: Select all

ssh pi@retropie.local
Default password is 'raspberry'. Next at the command prompt, copy this monitor and the video assets with the following command:

Code: Select all

cd ~;git clone https://github.com/NullCorn/GBZ-Power-Monitor.git
Now, launch the Monitor manually and test that it's working properly

Code: Select all

python ~/GBZ-Power-Monitor/gbz_power_monitor.py
Once you are satified that the monitor behaves properly, add the monitor to the startup process to complete the installation and then reboot to make it live.

Code: Select all

echo "@reboot     /usr/bin/nice -n 19 /usr/bin/python ~/GBZ-Power-Monitor/gbz_power_monitor.py" >> mycron; crontab mycron;rm mycron

Keeping Up-to-Date

I'm always tinkering with the script to fix bugs and improve it's stablity. There's a bunch of changes I'm planning to add including the ability to detect when the power is plugged back in, which would cancel the shutdown process. Presently, once the shutdown process is activated, it will shutdown even if you had rushed to go plug in the unit. I also want to add logging and implement proper threaded processes. Right now, it's a bit uglier than I would want.

So, as I continue to tinker and add these things, you can make sure you have the latest updates by issuing this command

Code: Select all

cd ~/GBZ-Power-Monitor;git pull origin master

FAQ

What is the purpose of the Latching Switch? Since we are changing the way the PI powers down, by software, in case your Pi has a kernel panic or, for example, the monitor unexpectedly crashes, your pi will be stuck on and you'd have to get your screwdriver out to reset it. How annoying! So this was added as a way to do that. It's also used when you want to store the unit for longer periods of time, this lets you disconnect the batteries. The button should be recessed so it's not easily hit. ie: Mount it internally and drilling a pin hole where a paperclip is needed to hit it.

Do I have to use the Powerboost 1000C and not another type? Maybe. You could use other power supplies, however, if they do not have a Low Battery indictor like an LED or dedicated pinout, then you will lose the automatic Low Battery warnings and shutdowns, which one of the core functions of this monitor. But even so, you could still use this for the dedicated power switch which would still gracefully shut down. You might have to reconfigure your emergency latching switch though. Let us know about your success or failure stories!


Video Examples
https://www.youtube.com/watch?v=TRkEfD04unk
Low Battery Warning

https://www.youtube.com/watch?v=nRJ42oSrIg4
Power Switch test


Contact

Questions, Comments, Kudos, Free Beer to abandonedemails@gmail.com. Please put "sudomod" somewhere in the subject or your message will not be received.



Original Post below:

Hi all,

Ok, I know this topic has been touched upon in a variety of threads previously (mostly on the old board). But I thought I'd start a dedicated thread for this here.

Currently in the GBZ, the power switch works like a normal On/Off switch. The only problem is when you cut the power, it's kinda hard on the system. There's no clean dismounting of the drives. It's really just like yanking the power out. And that's been known to cause corrupted files and disks.

My goal is to add an inexpensive and small electronic switch that will take care of the killing the power part, but only after a clean shutdown process. After some research, both the Pololu Mini Pushbutton LV or the Pololu Mini Slider LV will both work and fits our needs. They are basically identical in size and price ($5) with a couple extra features on the pushbutton version.

The main power switch will toggle ON the Pololu switch (but only ON, not off). Then, we need 3 GPIO pins mapped, one as output, and two as input. Once the unit is turned on, a GPIO pin will be configured as output and send a signal to the ON pin of the electric switch which overrides it to remain on. Basically ensuring the system remains on until we are ready to shut it off. Another GPIO input pin will read the setting of the power switch, so we can tell when the user wants to shutdown. The other GPIO input pin will go to the low pin of the Powerboost, so we can gracefully power down automatically when the battery is very low. A nice little add.

Last year, the Raspberry Foundation added a driver patch to support what we are trying to do called GPIO-Poweroff. This allows you to assign changes to GPIO state once the shutdown process is completed. So what we do is assign GPIO-Poweroff to the GPIO output pin that is feeding the ON signal on the Pololu switch. Once the shutdown is completed, the system itself will flip the state to low and that will shut the switch off.

Image

So far, I've successfully tested all of the hardware side of things, and can confirm that everything works as documented. Right now, I'm just powering an LED light with the Pololu switch. But the switch turns on when I flip the power switch. And it also stays on, when I'm driving a GPIO high to the On pin, even with the power switch in OFF. And then, it shuts off when I switch that state to low over GPIO. So we know that this will work for us.

The only thing left is that we will need to write some software to listen for those input GPIO for changes and trigger shutdown for either the a) power switch being flipped or b) the low battery signal being tripped from the Powerboost. Either of those will launch the shutdown process, and gracefully shutdown your system.

So, next, I need to get the GPIO-Poweroff driver configured and working and the related GPIO listener daemons for case changes. I'm excited that it's working great so far and it's looking like this will be doable! Will keep you guys updated. More updates to follow
Last edited by Popcorn on Mon Jul 09, 2018 8:12 am, edited 1 time in total.

User avatar
Popcorn
Trailblazer
Trailblazer
Posts: 354
Joined: Thu May 05, 2016 12:58 am
Location: Berlin
Has thanked: 29 times
Been thanked: 71 times

Re: Tackling Graceful Shutdowns on the GBZ

Post by Popcorn » Tue May 10, 2016 3:20 pm

So, I took a quick stab at testing out GPIO-Poweroff, and what it does is that it seems to set the GPIO pin HIGH when you shutdown, even if you set the active_low flag, which is weird. So this is the opposite of what we want... BUT.. I then tried it without GPIO-Poweroff, and.. eureka. It did exactly what it was suppose to. When I "shutdown now", after it completes, all the power to the GPIO is cut, and then that flips the switch Off, like we want! So, basically, we don't even have to do anything here.

My question is does anyone know if we can rely on this behaviour? If a GPIO output is set high on boot up, does the shutdown process always kill it or set it to low after it's completed? Can we depend on this? Any GPIO experts here?

So far, because of this, I'd say we are a complete green light on these switches working the way that we want them too. This is gonna work!

As a footnote, GPIO on high after shutdown, but then mapped to the OFF pin of the other Pololu Mini Pushbutton LV switch will work exactly as intended. The issue that I described above seems to only affect the Pololu Mini Slide Switch LV (One of the extra features of the Mini Pushbutton model is that it has an OFF pin)

ktechelonbreak
Posts: 78
Joined: Wed May 04, 2016 10:07 pm
Been thanked: 7 times

Re: Tackling Graceful Shutdowns on the GBZ

Post by ktechelonbreak » Tue May 10, 2016 4:02 pm

So does this mean when you flip the switch from ON to OFF, it will send a signal to the LV switch to run a command to shutdown? What happens on power up? Sorry tried to read the posts but I'm not too knowledgeable on this and a lot of foreign words to me.

User avatar
Popcorn
Trailblazer
Trailblazer
Posts: 354
Joined: Thu May 05, 2016 12:58 am
Location: Berlin
Has thanked: 29 times
Been thanked: 71 times

Re: Tackling Graceful Shutdowns on the GBZ

Post by Popcorn » Tue May 10, 2016 4:14 pm

When you flip the main power switch from ON to OFF, mechanically nothing will happen. But, because that switch is mapped as a GPIO input, and there will be a daemon running on bootup, listening for edge changes to that GPIO port, it will be triggered in software. Then, it will instruct the RPI0 to shutdown gracefully. Once it finishes, the Pololu switch gets shut off because the power to the GPIO gets cut, as we discovered.

On power up, it is purely mechanical. Once you flip it from OFF to ON, the pins on the Pololu switch are closed which instructs the electronic switch to turn on. So it works like normal.

ktechelonbreak
Posts: 78
Joined: Wed May 04, 2016 10:07 pm
Been thanked: 7 times

Re: Tackling Graceful Shutdowns on the GBZ

Post by ktechelonbreak » Tue May 10, 2016 4:23 pm

Wow that's a pretty clean execution! Might in the market for one of these switches then.

Masato
Posts: 6
Joined: Fri May 06, 2016 4:40 pm
Location: Canada
Been thanked: 4 times

Re: Tackling Graceful Shutdowns on the GBZ

Post by Masato » Tue May 10, 2016 4:57 pm

Popcorn wrote:When you flip the main power switch from ON to OFF, mechanically nothing will happen. But, because that switch is mapped as a GPIO input, and there will be a daemon running on bootup, listening for edge changes to that GPIO port, it will be triggered in software. Then, it will instruct the RPI0 to shutdown gracefully. Once it finishes, the Pololu switch gets shut off because the power to the GPIO gets cut, as we discovered.

On power up, it is purely mechanical. Once you flip it from OFF to ON, the pins on the Pololu switch are closed which instructs the electronic switch to turn on. So it works like normal.
I'm thinking that this, combined with what was mentioned in another thread , specifically this link:

Image

Assuming you're also using an Adafruit PowerBoost (500 or 1000), I think that schematic could be modified with a DPDT slide switch in place of the pushbutton, and the other pole intercepting the low battery signal either completing the circuit (ON) or sending pin 15 to ground (OFF).

So how it would work is the switch goes to the "ON" position, enabling the PowerBoost, which turns on the RPI0, with output 14 going HIGH, thus keeping the PowerBoost enabled through the 10K resistor. At the same time, the low battery output from the PowerBoost would complete its circuit (it goes HIGH when battery level is good; LOW when low battery occurs).

When the switch gets flipped to the "OFF" position, the PowerBoost would remain enabled thanks to the HIGH pin 14 from the RPI0, but the switch would break the low battery circuit and send pin 15 to ground, thus looking like a low battery. This is the pin your daemon would be looking at for the safe shutdown. Once the shutdown is complete, pin 14 goes LOW, sending the PowerBoost enable to ground through the 100K resistor.

Overall, saves the need for 1 GPIO pin, gives you low battery shutdown as well as safe power down, and you don't need a fancy MOSFET switch, just a plain old $0.10 DPDT slide switch. All assuming you're using the PowerBoost of course.

Hopefully that makes sense, or that I didn't make some fatal error in my logic. If you write up a clean daemon that can take care of this, please post it! I believe you're the one that mentioned the github solution was dirty code lol.

User avatar
Ganreizu
Posts: 552
Joined: Thu May 05, 2016 8:20 am
Has thanked: 168 times
Been thanked: 97 times

Re: Tackling Graceful Shutdowns on the GBZ

Post by Ganreizu » Tue May 10, 2016 8:06 pm

So my question is can we manage a safe shutdown solution as well as a battery indicator light, or is it a one or the other type situation?

I was going to use the LBO control pin on the powerboost 1000c to make a battery indicator. Can i still do that as well as implementing the safe shutdown once we figure out a guided way to add it?

User avatar
Popcorn
Trailblazer
Trailblazer
Posts: 354
Joined: Thu May 05, 2016 12:58 am
Location: Berlin
Has thanked: 29 times
Been thanked: 71 times

Re: Tackling Graceful Shutdowns on the GBZ

Post by Popcorn » Tue May 10, 2016 10:37 pm

@Masato Nice. Thanks for this. Will give it a try too since I bought some DPDT switches last week as well. Just waiting for them. And come on, you have to agree that any solution that depends on a cronjob is the opposite of clean. Hehe Imagine using that to detect the power switch, and potentially waiting up to one whole minute before it realized that it was suppose to be 'off'

@Ganreizu yep, this solution doesn't interfere with the battery function. That remains the same. This just replaces the on/off switch with an Electronic one.

User avatar
Popcorn
Trailblazer
Trailblazer
Posts: 354
Joined: Thu May 05, 2016 12:58 am
Location: Berlin
Has thanked: 29 times
Been thanked: 71 times

Re: Tackling Graceful Shutdowns on the GBZ

Post by Popcorn » Tue May 10, 2016 11:58 pm

I just dug into this more and now I remember reading about this method before. It certainly could work but one problem I've heard about is that mapping over the UART transmit ports causes some unusual behavior, ESP on bootup. Where the console messages are written, it causes the TX port to flicker high and low. This is because the console messages themselves are what the high signal is. I guess this is why the resistors are necessary to pull the signal completely high or low.

Depending on what you go with, you'll either need DPDT switch or an electric one. And with this method, yes you save a GPIO port, but you need to build this little circuit now. So maybe it's a moot point. I'll still play around with it though and see.

User avatar
Fleder
Posts: 848
Joined: Thu May 05, 2016 9:04 am
Location: Germany
Has thanked: 182 times
Been thanked: 257 times

Re: Tackling Graceful Shutdowns on the GBZ

Post by Fleder » Wed May 11, 2016 12:32 am

@Popcorn
Sweet! Y'all got any more of them Honolulus? :D

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest