Rock Pi 4 Power Button

I am using Rock Pi 4B for a project. In the project there is no display & mouse to press the shutdown button in UI or to write a terminal command.

Has anyone achieved power button functionality on Rock Pi 4B? How to implement it?

I was just disconnecting the power cable, but in the end my Armbian OS got into a bad state, and I had to reinstall everything from scratch.

1 Like

I also need to add a button for graceful shutdown of my headless Rock Pi 4 project.

I believe we need to configure GPIO appropriately, have a script running to check button state, and perform gracious shutdown when it is changed. I’m leaning towards using a locking button, or switch, over momentary button.

I’ll post my solution once I figure it, but it is not a priority for me right now, busy with internal design and exposing IO ports. Would appreciate to see yours if you implement it earlier.

Hi,

Nice to hear about it. I found this post: Reboot script via GPIO armbian

It uses mraa library to set ISR for a button. Not sure if it would be possible to power up after shutdown with this method. Anyway I am trying this approach at the moment and will update if I achieve any progress.

1 Like

I also have a power switch in my design, so for me the process would be use a button for graceful shutdown first, then cut the power completely.

Opened another issue on Armbian forum.

1 Like

For my use case of locking mechanical switch, can I just use some pin with internal pull-up resistor, e.g. pin 5, and use the switch to tie it to GND without any additional resistors?

This is the script I’ve come up with:

import mraa
import time
import os
mygpio = mraa.Gpio(5)
mygpio.dir(mraa.DIR_IN)
while True:
	time.sleep(10)
	val = mygpio.read()
	time.sleep(10)
	if val > 0:
		 os.system("shutdown now -h")

Are my ideas correct, or do I need to adjust something?

This is how I did it:

  1. Clone MRAA to your system
    git clone https://github.com/eclipse/mraa
  2. Install dependencies (removed nodejs-dev because it was giving error)
    sudo apt-get install git build-essential swig3.0 python-dev cmake libjson-c-dev
  3. Modify gpio_advanced.c example to use your pin and your command
cd mraa/examples/c
sudo nano gpio_advanced.c

Modify #define GPIO_PIN 6
to pin that you want (not all pins work, pins 13, 22, 23, 24 seem to work).
Now lets modify interrupt code to use your command:

int_handler(void* args)
{
    fprintf(stdout, "ISR triggered\n");
    // Add your command here, I used: system("shutdown now");
}

In the bottom of the file, there is following line:

    /* wait 30 seconds isr trigger */
    sleep(30);

Modify it to be

while(1) {
   sleep(30);
}
  1. Build mraa by following commands:
    cd mraa
    mkdir build
    cd build
    cmake ..
    make
  1. Enable the pin that you used in the gpio_advanced.c example by doing following:
cd /sys/class/gpio
su root
echo 23 > export

after this command, when you type ls, pin23 should be visible.

  1. Run the program
su rock
cd mraa/build/examples/c
sudo ./gpio_advanced

If it gives you error, try other pin.

If it works, let me know. I can also show how to automate it inside shell script/service.

Start this way:
https://www.armbian.com/bugs/

@igorp Thank you for your reply. But I am not sure what you mean with the link you provided because there is no Armbian bug. I wrote to the forum to get help on the topic.

I understand. Just giving you few hints to have better chances of success. There are a lot of technical issues / challenges and very little people that can do something about.

It is valuable and useful that you shared your research but people tend to skip if they don’t see logs from the actual device. If you manage to get this working, you are welcome to integrate it into Armbian. This step is also important as many solutions people find and share are otherwise lost in black hole called forums …

Ok, I get it now :slight_smile:

Thank you. I will try through that link too. If it would work in the end, at least I will try to make some kind of tutorial for the button functionality.

As I have understood from this post, in order to add wake-up functionality, we can convert dtb file to dts, modify device tree, and then convert dts to dtb again via device tree compiler.

In Armbian, go to

/boot/dtb/rockchip

Here you can see many dtb files for various devices, in my case:

rk3399-rock-pi-4b.dtb

is used. There is also a generic file rk3399-rock-pi-4.dtb, but it is not being used (I moved it to another location and device was booting, while when I removed file ending with 4b, system stopped working).

If you run:

dtc -I dtb -O dts rk3399-rock-pi-4b.dtb -o rk3399-rock-pi-4b.dts

As I understood, dts file will be generated based on the dtb file. We can modify
device settings by editing the dts file. After we are done, we can run following command to generate dtb file again:

dtc -I dts -O dtb rk3399-rock-pi-4b.dts -o rk3399-rock-pi-4b.dtb

and then reboot.

Next step is to learn what modifications to make on the dts file and find out if any other system modifications will be required for the changes to take effect. Still working on it, let me know if you can find out anything useful.

@jack Could you please help? What should be done in order to wake the system (from shutdown or low power state) by a button press? We are able to put the system into shutdown/low power state with a method mentioned above.

I checked button implementation in RockPro64 dts file, and tried to replicate it in rock 4: https://github.com/RafigRzayev/RockPi4B_wake_button_dts/commit/046c5b81c39756e8cd0e8b69a243539e01429c39

Then did:

sudo systemctl suspend

And then tried to short pin 0x0f (pin 21 on 40-pin GPIO) with GND. Didn’t work.