Aditya Posted January 27, 2020 Posted January 27, 2020 Hi there. I want to use a push button on a GPIO to shutdown O Pi Zero safely. Though the acpid way runs fine on an O Pi One, I wonder if there is a way to make it work on a O Pi Zero. Please direct me if its documented somewhere already. Thanks
martinayotte Posted January 27, 2020 Posted January 27, 2020 Beware that even if you trigger event from GPIO to execute "halt" command using a "gpio-key" node in DT, the OPiZero will still be connected to PSU since there is no PMIC to really turn off the power... 1
Aditya Posted January 29, 2020 Author Posted January 29, 2020 Yeah thats fine for me. Just want to implement that for safe shutdown as I would want to disconnect power after that. So is there simple way to implement that??
martinayotte Posted January 29, 2020 Posted January 29, 2020 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" ... 1
Aditya Posted January 29, 2020 Author Posted January 29, 2020 Great ! I would try that right away. A small query though. Can another button be hooked similar to this for a simple reboot also? By the way thanks a lot for this. You guys just rock.
Aditya Posted January 29, 2020 Author Posted January 29, 2020 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
martinayotte Posted January 29, 2020 Posted January 29, 2020 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" ... 1
martinayotte Posted January 29, 2020 Posted January 29, 2020 21 minutes ago, Aditya said: Can another button be hooked similar to this for a simple reboot also? The <0x74> is KEY_POWER, I've tried <0x198> which seem to be for KEY_RESTART, but it doesn't seem to work ... I will try to dig kernel sources to figure out ... 1
Aditya Posted January 29, 2020 Author Posted January 29, 2020 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
Aditya Posted January 29, 2020 Author Posted January 29, 2020 I short the PA10 pin to ground and its not shutting down. I have installed wiring pi and by giving command gpio readall I can see that PA10 is an input and voltage is high(pullup). But not shutting down. What could be the reason?
martinayotte Posted January 29, 2020 Posted January 29, 2020 21 minutes ago, Aditya said: Is that okay It is Okay 1
martinayotte Posted January 29, 2020 Posted January 29, 2020 10 minutes ago, Aditya said: I short the PA10 pin to ground and its not shutting down. Strange ... Which kernel are you using ? Did you loaded the overlay ? If you try "evtest /dev/input/event0", what are you seeing while putting PA10 to GND ?
martinayotte Posted January 29, 2020 Posted January 29, 2020 3 minutes ago, Aditya said: My bad. It does the job. Thanks a lot. What was the issue ? Did you forgot to load the overlay ?
Aditya Posted January 29, 2020 Author Posted January 29, 2020 I did but it had asked for permission which I missed somehow. I mean I used sudo but it again said permission denied for half of the command. Later used su command first and then gave loaded it easily.
martinayotte Posted January 29, 2020 Posted January 29, 2020 Just now, Aditya said: Later used su command first and then gave loaded it easily. Right ! I didn't mentioned it, loading overlay dynamically requires root privileges ... 1
Aditya Posted January 29, 2020 Author Posted January 29, 2020 Thanks a lot for helping me out. Can you please take a look into the reboot button also.
martinayotte Posted January 29, 2020 Posted January 29, 2020 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.") 1
Aditya Posted January 29, 2020 Author Posted January 29, 2020 Wow. Thanks a lot lot again. You are a life saver.
Recommended Posts