RPi4 and Power Hat

  • Hello,

    I have LE 9.2.1 (RPi4.arm) running on a RPi4. I put it in a case with a power button (this one). I can start it with the push on the button and the "forced shutdown" is also working (press the button > 10s). What I cannot have is a graceful shutdown. Following the instructions with the case, I appended to /flash/config.txt :

    Code
    dtoverlay=gpio-poweroff,gpio_pin=18,active_low=0
    dtoverlay=gpio-shutdown,gpio_pin=17,active_low=1,gpio_pull=up

    Now, if I do a short press on the power button, the power options dialog is blinking on the screen. :-/

    According to this thread, I tried :

    Code
    LibreELEC:~ # cat /storage/.kodi/userdata/keymaps/keyboard.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <keymap>
    <global>
    <keyboard>
    <key id="61662">Shutdown</key>
    </keyboard>
    </global>

    But it doesn't work. I can still see

    Code
    2020-05-11 22:51:50.944 T:3011641360   DEBUG: HandleKey: power (0xf0de) pressed, action is ActivateWindow(ShutdownMenu)

    in the log.

    How can I shut it down gracefully ? I also want to add that if I switch it off in Kodi, the power led on the case remains green (I still have to do a force shutdown to be able to boot it again).

    Thanks !

  • Hi,

    I tried the first step : Power Button Scripting Part. It worked great, thank you ! :)

    I did the following steps, just changing the led_gpio_number from 13 to 18 like this :

    And now, when I start it up, it switch itself down directly. So the operation definitely did something but maybe the polarity is inverted (or something else is wrong in the script). I was able to go back to a manageable situation by removing the Hat and renaming autostart.sh.

    So, what do I need to do to make it work ?

    Thank you !

    • Official Post

    Take care about pin numbering. You use GPIO 18, which is the 6'th pin at the outer side of the board, like here.

    Unfortunately you haven't said anything about your LED wiring, so I can't tell you the right code.

    Your power hat has red and green LED's, so I assume red is for off, and green for on.

    That means, at least two pins must be in use for the LED's. Please tell me the pins, connected to the LED's.

    PS: The data sheet doesn't tells the wiring, but the photo shows that only the first 12 pins (two rows) have connections to the power hat.

    Chances are they use GPIO 17 and GPIO 18 for red and green LED's. Follow the wiring on the board to proof that theory.

  • You can see a picture of the power hat here, it's not obvious how things are wired and I event think that the LEDs are managed by a MCU (the STM8S001J3).

    If we consider that the case instructions are correct, and that we need to reproduce the behavior made by the line:

    Code
    dtoverlay=gpio-poweroff,gpio_pin=18,active_low=0

    How can this be done?

    Thank you!

    • Official Post

    Sorry, according to your first code lines of your first post, GPIO 17 and GPIO 18 are exclusively used for the power button, not for LED's.

    What you want is to switch the LED's from green to red after forced shutdown (long button press) and power off (short button press), right?

    PS: Theory behind my question: Some GPIO pins have to be set back to default state to activate the red LED's. So long / short button press may lead to different results. If you know other device states that let the red LED's shine, let me know. The photo doesn't provide useful infos about the wiring.

  • Here is the current situation:

    If I rely on the power push-button to switch the RPI off :

    I expect the light to go red after some time but it never happen. The solution for now is to keep the power button for 10s (forced shutdown), so that the light goes back to red.

    If I take the power menu in Kodi, this is the scenario:

    [Light is red, RPi is off] -- Push on the power button --> [Light is green, RPi is on] -- Select "Power off system" --> [RPi goes off, but light remains green]

    For both scenario, I guess that the power hat is not notified about that the RPi is actually off.

    As said, there is a MCU on the power hat. I don't think that the LED are controlled directly by the RPi. I think that it is the power hat MCU that drives them.

    Thank you once more for your support !

    • Official Post

    Thanks for the detailed explanation. Probably the MCU does time measurement and triggers GPIO 17 (shutdown) for a short button press and GPIO 18 (power off) for a long button press. That keeps my theory alive. I think GPIO 17 has to be set to the default state after shutdown to switch the LED's red. I'll do some research what's the default state (low or high). Stay tuned.

    • Official Post

    Here we go.

    1) Install the add-on Raspberry Pi Tools (probably already installed in your case)

    2) Remove this line from config.txt

    Code
    dtoverlay=gpio-shutdown,gpio_pin=17,active_low=1,gpio_pull=up

    3) Copy this /storage/.config/autostart.sh

    Bash
    #!/bin/sh
    
    python /storage/scripts/power_button.py

    4) Create a folder for a Python script: mkdir /storage/scripts

    5) Copy this /storage/scripts/power_button.py

    6) Make both files executable:

    chmod +x /storage/.config/autostart.sh

    chmod +x /storage/scripts/power_button.py

    The script will wait for a "low" signal at GPIO 17, like the original config.txt line did. On "low" it will shutdown, but additionally it sets all GPIO pins back to default state after shutdown. That's probably the important part for your LED's to switch to red.

  • Hello,

    I tried your method and the fist result was that the system was stuck at boot time with a message (I guess that it comes from systemd) saying that "A start job is running for kodi user autostart script..." with a counter like [22s/no limit].

    I adapted autostart.sh to execute it in a non-blocking mode. I tried:

    Bash
    #!/bin/sh
    
    nohup python /storage/scripts/power_button.py &

    and

    Bash
    #!/bin/sh
    
    (
            python /storage/scripts/power_button.py
    ) &

    according to example given here.

    And for both attempts, it was not able to see a difference in the shutdown process.

    Should I try in a different way ?

    Thank you !

  • After a lot of search, it is finally working properly. My issue was that I thought there was a typo in the instructions while it was totally correct.

    I had to had to the end of /flash/config.txt :

    Code
    dtoverlay=gpio-poweroff,gpiopin=18,active_low=0
    dtoverlay=gpio-shutdown,gpio_pin=17,active_low=1,gpio_pull=up

    As I said, I thought I had to replace "gpiopin=18" by "gpio_pin=18" to make it consistent with the next line (and according to my rapid search on the web) but I was wrong.

    Thank you for your great help (without our exchanges, I think I would already have bought another case :D ) !