Rock64 usb2.0 Power Control Floating GPIO Tutorial Files & Notes
#1
Brick 
Greetings,

   The purpose of this post is to document the rock64_usb_power_service.sh script , provide instructions and notes for its correct use (tutorial) , and document the installation of the script as a systemd automated service.  

   The problem that we are solving is that the usb2.0 power regulators are controlled by a hardware line (EN) (gpio2) which is in a floating state when the Rock64 boots.   This often results in the usb2.0 ports losing power ( at least momentarily ).  

   The solution to this problem is to create an executable script , as well an automated service (systemd) , that will export the gpio2 line to user space ( setting it low & turning ON the power regulators ) while also allowing the system admin (user) to control the gpio2 for power cycling devices plugged into the usb2.0 port.  This approach to fixing this problem is a win-win in that the usb2.0 power remains on permanently after  boot-up for those who want it, and it also allows advanced users the flexibility of being able to power cycle their usb2.0 ports as designed by the Rock64 engineers ( very handy in many use cases ).

   The user has complete freedom and control because the script may be run manually (as root) or it may be installed as an automated systemd service.  The following tutorial will provide the codes first , then I will discuss how to use them , and finally how to install the usb_gpio_on.service  in systemd.  After each code section I will give instructions for 1) where to place the script, 2) what permissions to give it, 3) what ownership to give it, and finally how to run it.

The Scripts


   Before beginning switch into root operating mode ( and permissions ) with  sudo -i  (important).

rock64_usb_power_service.sh

Code:
#!/bin/bash
# rock64_usb_power_service.sh
#
# Mark H. Harris
# v0.1b
#
#
PWRON=0
PWROFF=1
GP=2
GPOUT="out"
GPPATH="/sys/class/gpio"
GPVALUE="value"
GPMODE="direction"

## remove gpio if already exported
if [ -d $GPPATH/gpio$GP ]
then
   echo $GP > $GPPATH/unexport
   sleep 2
fi

# export the gpio, and set if ready
echo $GP > $GPPATH/export
sleep 2
if [ -e $GPPATH/gpio$GP/$GPMODE ]
then
   echo $GPOUT > $GPPATH/gpio$GP/$GPMODE
   sleep 2
   echo $PWRON > $GPPATH/gpio$GP/$GPVALUE
fi

   The  rock64_usb_power_service.sh  is the executable script.  Place this script in  /usr/local/sbin/

   Check the spelling very carefully;  or things will not work when it comes time to automate the service.

   set the ownership, group, and permissions:
   
   cd  /usr/local/sbin/
   chown  root  rock64_usb_power_service.sh
   chgrp  root  rock64_usb_power_service.sh
   chmod  0754  rock64_usb_power_service.sh

   To run the script manually :

   sudo  -i
   /usr/local/sbin/rock64_usb_power_service.sh

   To power cycle the usb2.0 port's 5v :

   sudo -i
   echo 1  >  /sys/class/gpio/gpio2/value
   echo 0  >  /sys/class/gpio/gpio2/value

   The above script exports the (EN)  gpio2 mux line to user space using the sysfs method,  sets the direction "out", and sets the value 0 ( pulling the line low turns ON the usb2.0 power regulators ).


usb_gpio_on.service

Code:
[Unit]
Description=set the gpio2 EN line low for usb2.0 power regulator ON

[Service]
ExecStart=/usr/local/sbin/rock64_usb_power_service.sh
Type=oneshot

[Install]
WantedBy=default.target

   The above  usb_gpio_on.service  script is called a systemd  "service unit file".  This file describes the service to systemd so that systemd knows how to find the executable, and further knows that the service executable only needs to be run once.  Again, it is very critical that all the spelling is precise;  miss spellings will result is chaos;  and of course no fix.

   Place this file  usb_gpio_on.service  in directory  /etc/systemd/system/

   Set the ownership, group, and permissions for this file :

   cd  /etc/systemd/system/
   chown  root  usb_gpio_on.service
   chgrp  root  usb_gpio_on.service
   chmod  0664  usb_gpio_on.service

   Inform systemd that a change has been made to the unit files on disk       (very critical)

   systemctl  daemon-reload
   systemctl  enable  usb_gpio_on.service

   Congratulations!   At this point the service has successfully run;  the gpio2 has been exported, and the gpio2 pin has been pulled low turning on the usb2.0 power regulators.  

   We are now done;  all that is left to do is to reboot your Rock64 ! 

   You can check that this worked after reboot by looking in  /sys/class/gpio/  for an export directory  called  gpio2!   

   At this point you may go about life on your Rock64 normally -- the usb2.0 power should not drop because the EN line is no longer floating;  however,  if you like you still have the option to power cycle the usb2.0 ports using the method I detailed above.


Caveat Emptor

If you want to try this tutorial follow two simple rules , or do so at your own risk.  Either wait for me to post on the forum that this has been tested multiple times and is 1) safe,  and 2) works;   or become a beta tester ( a guinea pig ).  That means that you contact me on irc and tell me you're testing and that you will be gracious to provide feedback ( including loss of time and|or hair )  so that I know who is using it so far, and whether I need to fix anything or make anything clearer.

Every effort has been made to ensure that this is accurate and free from defects;  but we all know the best laid plans of mice and of programmers.

We may at some future point fix this in the dts|dtb file and place in a future image;  the problem with that is that users who wish to have the advanced ability to power cycle the usb ports as designed would have to hack the dts first!  So we're going to try this and see how it goes.

This method and the script which have been provided have been tested according to this tutorial on my Rock64 production test board, and everything works on my board as described in this tutorial;  again, if I need to make anything more clear, or if something is broken ( I certainly hope not ) please let me know asap , preferably on irc.

Thanks much !    Rolleyes




marcushh777    Cool

please join us for a chat @  irc.pine64.xyz:6667   or ssl  irc.pine64.xyz:6697

( I regret that I am not able to respond to personal messages;  let's meet on irc! )
  Reply
#2
It worked fine for me here on xenial-mate-rock64-0.4.16-83-arm64.

Just make sure the #!/bin/bash line is the very first one in the rock64_usb_power_service.sh file. I tripped myself up on that... It is correct in the posting.
  Reply
#3
(08-24-2017, 02:56 AM)KnReLe Wrote: It worked fine for me here on xenial-mate-rock64-0.4.16-83-arm64.

Thank you KnReLe !    Shy


I too have tested now on both xenial-minimal, and xenial-mate.  Both worked, gpio2 is set and held low, and the usb2.0 can be power cycled.

Caveat:  depending on timing ( when the user logs on vs how long it takes for rc.local to run ) there may be a very brief second or two where the usb 5v power drops before the automation pulls the gpio2 low turning on the usb power regulators.  If this happens to you , please let me know , and let me know how frequent this occurs (PM me, or send a private message, or stop by the irc).  Typically this is going to occur if the user does the logon from the gui before  the boot process has completed;  in other words, this will likely not occur if the user waits until the boot-up is complete before logging on.  This is something that can be seen while monitoring the serial console, for instance.  So, if the user waits a few seconds to logon after boot to give the boot process time to complete fully (serial console is at a logon prompt) it is unlikely that this caveat will occur.

Please review the entire tutorial , and notice that we're enabling the service in systemd 'earlier' which eliminates the timing problem with the legacy rc.local;  so I removed the rc.local step.


Thanks
marcushh777    Cool

please join us for a chat @  irc.pine64.xyz:6667   or ssl  irc.pine64.xyz:6697

( I regret that I am not able to respond to personal messages;  let's meet on irc! )
  Reply
#4
I have updated the tutorial;  please review and make changes as necessary;

If you edited the rc.local file in /etc please remove the line from rc.local and issue this command as root:

cd  /etc/systemd/system/
sudo  systemctl  enable  usb_gpio_on.service


<sorry for inconvenience>

Blush
marcushh777    Cool

please join us for a chat @  irc.pine64.xyz:6667   or ssl  irc.pine64.xyz:6697

( I regret that I am not able to respond to personal messages;  let's meet on irc! )
  Reply
#5
(08-24-2017, 04:40 AM)MarkHaysHarris777 Wrote: I have updated the tutorial;  please review and make changes as necessary;

If you edited the rc.local file in /etc please remove the line from rc.local and issue this command as root:

cd  /etc/systemd/system/
sudo  systemctl  enable  usb_gpio_on.service


<sorry for inconvenience>

Blush

Thanks!  Smile
It worked fine for me on Stock Debian LXDE (Rock64 SDCard) Build 20170809. This is a production Rock64 with 4GB that I received recently. Checked successfully that the directory gpio2 exists in /sys/class/gpio/.

Has worked three times so far:
- After reboot finishing the fix.
- After Shutdown and full power down. Starting from applying power to the Rock64.
- After Shutdown and pressing the Power button on the board.
3 for 3 is the best I've seen so far. 

Followed directions carefully and had no problems. Linux is still not my comfort zone. I've lived mainly in Windows going all the way back to MS-DOS days.

Next problem is to fix is the changing MAC address after reboot. I have a gazillion entries in my router thanks to this problem.

moravecz
  Reply
#6
The upcoming 0.5.3 will have those problems fixed.
Homepage: https://ayufan.eu

Releases:
Rock/Pro 64/Pinebook Pro: LinuxChromium OS
So/Pine A64/Pinebook: LinuxAndroid 6.0Android 7.1

Buy me a Beer
  Reply
#7
Hi! I'm having kind of the opposite problem. I would like to switch/power off the USB power of both USB 2.0 and USB 3.0 as an efficient means to preserve energy on a battery powered system (I want to dump once a day stuff onto an attached hdd and power it on just for that occasion). I read the other thread as well, but did not find further information. My board is a version 2.0 Rock64.

Running gpioinfo
gpiochip0 - 32 lines:
       line   0:      unnamed       unused   input  active-high  
       line   1:      unnamed       unused   input  active-high  
       line   2:      unnamed "vcc-host-5v-regulator" output active-low [used]
       line   3:      unnamed       unused   input  active-high


shows me the expected GPIO line I read about to switch the USB ports in this thread. When trying gpioset gpiochip0 2=1
gpioset: error setting the GPIO line values: Device or resource busy


I get an error message (trying the same on the other GPIOs on that chip works as expected). As it was possible to change those settings in earlier times I'm wondering how to achieve that again. I was changing the boards DTB file for other systems (Orange Pi) to switch from OTG to normal USB ports following some tutorial. But I'm lost what to do for this board if that is actually required.

If I got it right from this (or the other post) the USB 3.0 cannot be switched off?

Thank you for any hints! Dirk
  Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Are HW design files available for ROCK64? irenek 3 4,429 12-11-2023, 09:31 PM
Last Post: tllim
  Rock64 is unreliable after 3 years of service - power problem? ReleaseTheGeese 0 263 11-23-2023, 05:05 AM
Last Post: ReleaseTheGeese
  Rock64 PoE compatbility with Pi4 Hatt recent Single Board Computer offering from PINE kharak 1 947 04-26-2023, 11:38 PM
Last Post: tllim
  Case for the rock64 that supports the POE hat. o1CRiMSON1o 0 562 03-21-2023, 03:48 PM
Last Post: o1CRiMSON1o
  power connector type please AlexS 4 4,480 01-11-2023, 07:00 AM
Last Post: dmitrymyadzelets
  rock64 totally brick dakobg 2 1,613 11-07-2022, 05:45 PM
Last Post: olivercfc
  Alternative power supply ju0n 0 957 09-06-2022, 02:46 AM
Last Post: ju0n
  3D-Printable Button Pegs for the ROCK64 Aluminium Case CounterPillow 2 3,367 08-04-2022, 01:31 AM
Last Post: Vicky Weimann PhD
  Where can I find the ROCK64 POE HAT Zoz 2 2,460 06-08-2022, 12:44 AM
Last Post: Zoz
Smile wooden case for ROCK64 killor 13 16,105 03-04-2022, 06:56 AM
Last Post: killor

Forum Jump:


Users browsing this thread: 1 Guest(s)