unixcommando
Posts: 18
Joined: Sun Dec 04, 2016 6:08 pm

Online LED

Mon Apr 10, 2017 2:18 pm

Hello,

I'm working a project where I'm going to have a Pi Zero running in a headless stand alone configuration. Since I won't have a monitor and won't be online I need a way to insure that my Pi is shutdown before it's powered off. The first part is easy, I have a momentary switch I have configured to trigger a shutdown when pressed, this works really well. What I'm having issues with is a status LED to monitor OS state. I have an LED on GPIO #6 which is triggered when the OS reaches run level 4. This is the only run level I'm concerned with and my LED is working perfectly.

Where I'm running into trouble is I want the LED to remain on until the system is halted. I'm running this through rc config so I'm using a proper RC script in /etc/init.d and my links are being created correctly. What I'm seeing is that the moment I press the switch to trigger a shutdown my LED shuts off. Watching the shutdown on the console I can see that it takes 3 or 4 seconds AFTER the LED goes out before the system is really shutdown. I've tried changing when the script runs IE K09 vs K01, I've even remove the K scripts figuring that the LED will go dark when the Pi pulls voltage from the GPIOs, but nothing seems to make a difference. The LED goes dark before the system actually shuts down.

Is there a solution for me or am I shoveling against the tide? My guess is since even after removing K scripts the LED goes down first then there must be something in the shutdown process that pulls voltage from the GPIOs very early in the shutdown process. I've looked a the RC scripts and nothing jumps out at me as being responsible for disabling GPIO.

Thoughts?

Thanks
-Bob

User avatar
rpdom
Posts: 16151
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Online LED

Mon Apr 10, 2017 9:25 pm

Look at lower level. The dtoverlay params in /boot/config.txt have something that provides this feature outside of the Linux kernel. You can specify a GPIO to go high very early in the boot sequence and then go low when shutdown is complete. (or the other way round)

To make GPIO6 (physical pin 31) go high at boot and low after shutdown, add this line to /boot/config.txt

Code: Select all

dtoverlay=gpio-poweroff,gpiopin=6,active_low=1

unixcommando
Posts: 18
Joined: Sun Dec 04, 2016 6:08 pm

Re: Online LED

Tue Apr 11, 2017 12:23 am

Very nice, thank you. Works like a charm. It does turn the LED on earlier than I wanted, but it does keep it asserted until power off so given the choice I'll take the light coming on earlier, at least I'll have confidence the OS is down and the SD card is closed before I power off the device.

Thanks again.

-Bob

unixcommando
Posts: 18
Joined: Sun Dec 04, 2016 6:08 pm

Re: Online LED

Tue Apr 11, 2017 2:37 pm

After testing the above solution I found that it does work. Sort of. It has an interesting side effect that I'm not yet clear about whether is a problem or not. Reboot works just fine, but if I do a halt or shutdown -h it shuts down the OS, extinguishes the LED, but doesn't power off the Pi. After the OS shuts down and prints such to the console, it leaves power to HDMI and the USB bus as well as the main bus. After the OS shuts down I get a kernel panic.

Right now I'm assuming that since the OS is down, all file systems are unmounted and the SD card is closed, if that assumption is correct then I have no issue with what's going on. I'd prefer the internal poweroff routine run, but so long as my switch doesn't arc as I remove power it should cause no damage.

I think my next step is to upgrade the OS and Firmware and see if that helps and try to understand dtoverlay to a greater extent.

-Bob

User avatar
rpdom
Posts: 16151
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Online LED

Tue Apr 11, 2017 6:16 pm

That's probably because halt and shutdown -h don't do a full shutdown. Try poweroff instead.

Return to “Troubleshooting”