Shut down OPi Zero using a push button on GPIO


Aditya
 Share

2 2

Recommended Posts

Armbian is a community driven open source project. Do you like to contribute your code?

12 hours ago, Aditya said:

So is there simple way to implement that??

Sure !

Take this overlay source and save it as "gpio-keys.dts" :

 

 

 


/dts-v1/;
/plugin/;

/ {
   compatible = "allwinner,sun8i-h3";

   fragment@0 {
      target = <&pio>;
      __overlay__ {
         keypad_pins: keypad_pins {
            allwinner,pins = "PA10";
            allwinner,function = "gpio_in";
            allwinner,pull = <1>;
         };
      };
   };

   fragment@1 {
      target-path = "/"; // "/soc";
      __overlay__ {
         mykeypad: mykeypad {
            compatible = "gpio-keys";
            #address-cells = <1>;
            #size-cells = <0>;
            pinctrl-names = "default";
            pinctrl-0 = <&keypad_pins>;
            status = "okay";
            button@10 {
               label = "GPIO KEY_POWEROFF";
               linux,code = <0x74>;
               gpios = <&pio 0 10 1>; // PA10, active_low
            };
         };
      };
   };

   __overrides__ {
      mykeypad =     <&mykeypad>,"status";
   };
};
 

 

Compile it as an overlay DTBO using this command :

git-work/dtc/dtc -@ -I dts -O dtb -o /boot/dtb/overlay/sun8i-h3-gpio-keys.dtbo gpio-keys.dts

Then, add this overlay either in /boot/armbianEnv.txt with "overlays=gpio-keys" or by loading it dynamically using :

mkdir /sys/kernel/config/device-tree/overlays/gpiokeys
cat /boot/dtb/overlay/sun8i-h3-gpio-keys.dtbo > /sys/kernel/config/device-tree/overlays/gpiokeys/dtbo

 

As soon as you connect the PA10 pin to GND, it will trigger a "shutdown" ...

Link to post
Share on other sites

17 minutes ago, martinayotte said:

Compile it as an overlay DTBO using this command :


git-work/dtc/dtc -@ -I dts -O dtb -o /boot/dtb/overlay/sun8i-h3-gpio-keys.dtbo gpio-keys.dts

I am sorry but I am really a noob. Do I need to install something for this purpose?? Because it says this
-bash: git-work/dtc/dtc: No such file or directory

 

Link to post
Share on other sites

1 minute ago, Aditya said:

-bash: git-work/dtc/dtc: No such file or directory

Right ! You need to install DT compiler from here : http://ftp.debian.org/debian/pool/main/d/device-tree-compiler/device-tree-compiler_1.4.7-3_armhf.deb

And do not provide this path "git-work/dtc/dtc" but simply "dtc -@ -I dts -O dtb -o /boot/dtb/overlay/sun8i-h3-gpio-keys.dtbo gpio-keys.dts" ...

Link to post
Share on other sites

9 minutes ago, martinayotte said:

Right ! You need to install DT compiler from here : http://ftp.debian.org/debian/pool/main/d/device-tree-compiler/device-tree-compiler_1.4.7-3_armhf.deb

And do not provide this path "git-work/dtc/dtc" but simply "dtc -@ -I dts -O dtb -o /boot/dtb/overlay/sun8i-h3-gpio-keys.dtbo gpio-keys.dts" ...

After installing that and compiling, it gave a couple of warnings
 

/boot/dtb/overlay/sun8i-h3-gpio-keys.dtbo: Warning (unit_address_vs_reg): /fragment@1/__overlay__/mykeypad/button@10: node has a unit name, but no reg property
/boot/dtb/overlay/sun8i-h3-gpio-keys.dtbo: Warning (avoid_unnecessary_addr_size): /fragment@1/__overlay__/mykeypad: unnecessary #address-cells/#size-cells without "ranges" or child "reg" property

Is that okay

Link to post
Share on other sites

1 hour ago, Aditya said:

Can you please take a look into the reboot button also.

I've look during more than an hour, it deosn't seems to be implemented ...

 

The only other way I would see is to run this python script and add a " os.system("reboot") " after the "button pressed" print :

 

 


#!/usr/bin/env python
"""Read button.

Make gpio input and enable pull-up resistor.
"""

import os
import sys
import time
import logging

if not os.getegid() == 0:
    sys.exit('Script must be run as root')

from pyA20.gpio import gpio
from pyA20.gpio import connector
from pyA20.gpio import port


button = port.PA10

"""Init gpio module"""
gpio.init()

"""Set directions"""
gpio.setcfg(button, gpio.INPUT)

"""Enable pullup resistor"""
gpio.pullup(button, gpio.PULLUP)
#gpio.pullup(button, gpio.PULLDOWN)     # Optionally you can use pull-down resistor


try:
    while True:
        state = gpio.input(button)      # Read button state
#        print (state)
        if state == 0:
            print("button pressed")
            logging.info("button pressed")
        time.sleep(0.2)

except KeyboardInterrupt:
    print ("Goodbye.")
 

 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

2 2