Power & Source of Big Ideas

CM3588 NAS Kit - User Button

Moderators: chensy, FATechsupport

Does anyone know how to access user button programatically?
According to the doc it should be connected to GPIO0 D5 but I cannot see any change in returned data from

Code: Select all

sudo gpioinfo gpio0
when the button is pressed and when it's not.
First you need to identify the proper pin :

Code: Select all

GPIO0_D5 => Bank 1 pin
bank=0 => 0*32
group ∈ {(A=0), (B=1), (C=2), (D=3)} => group = 8*3 = 24

pin = 0*32 + 8*3 + 0 = 24


What is the output of :
cat /sys/class/gpio/gpio24/direction

if you have this not existing, you need to create the proper pin config:

Code: Select all

echo 24 > /sys/class/gpio/export
echo in > /sys/class/gpio/gpio24/direction
cat /sys/class/gpio/gpio24/value
=>when button is pressed and not pressed.

You can use my post for the fan as it is similar mechanism except that for button it is input whereas for fan it is output pin.
Thanks.

1. I believe you forgot to add 5 to the final result, so the pin is 29.
2. I'm using Armbian and there's no /sys/class/gpio directory (no sysfs for gpio), so the only way to access gpio is to use gpiod tool. It's miserable, I know :(

Long story short - I've found a way to read the status of the key.
First - this button is defined in device overlay as button@1 located in gpio-keys section. Its label is K1 which pointed me to the wrong direction because in the doc K1 is Power button and User button is named K5. I realized that this definition in dtb is actually for User button only because of the successful test (see below).
Second - every time I was trying to read the state of the button using gpioget, I was getting the message that the resource is busy and cannot be read. Well... I've tried a lot of different things, rebooted the server dozen of times and then... YES :)
I could read the state ONLY after unloading gpio_keys kernel module:

Code: Select all

sudo rmmod gpio_keys
while true; do sudo gpioget gpio0 29; sleep 1; done
1
1
...
1
0    <--- key pressed
0
...
0
1    <--- key released

1 - key released
0 - key pressed (and hold)

I wonder if there is any other method of getting the state without removing kernel module but rather to make use of them... From CLI at least...
Answering myself.
Yes, there is a quite nice method.
There's an event associated with this button.
/dev/input/by-path/platform-gpio-keys-event points to an event (number 5 in my case).
This is the script (Python3):

Code: Select all

import struct
with open(f'/dev/input/by-path/platform-gpio-keys-event', "rb") as f:
  while True:
    raw_data = f.read(24)
    data = struct.unpack('4IHHI', raw_data)
    if data[4] == 1:
      if data[6] == 1:
        print("Key pressed")
      elif data[6] == 0:
        print("Key released")

Who is online

In total there are 8 users online :: 0 registered, 0 hidden and 8 guests (based on users active over the past 5 minutes)
Most users ever online was 5185 on Wed Jan 22, 2020 1:44 pm

Users browsing this forum: No registered users and 8 guests