GPIO LED blinker using SYSFS on the Rock64
#1
   

In the above pic I'm blinking an LED at physical pin(16) of the Rock64, GPIO3_A5, on the PI-2 bus;

... to use the sysfs method of setting the pin on|off one must first calculate the gpio# using the following:

( bank_num  * 32 )  + ( pad_name * 8 ) + pad_num

where pad names = {A:0, B:1, C:2, D:3}

In the above example the bank_num is 3 , pad_name is 0*8,  and the pad_num is 5 ,  so:
 ( 3 * 32 ) + ( 0 * 8 ) + 5  =  101

The following steps are used to set the gpio pin on|off:

1) sudo -i
2) echo 101 > /sys/class/gpio/export
3) echo out > /sys/class/gpio/gpio101/direction
4) echo 1 > /sys/class/gpio/gpio101/value          <--ON
5) echo 0 > /sys/class/gpio/gpio101/value          <--OFF

The following codes will blink gpio101 at pin(16), GPIO3_A5:  blinker.sh and sysled.sh are closely related scripts. The former is a controlled loop, and the latter is a forever running loop;  both are useful. Compare the two script and notice that they are almost identical.

blinker.sh

Code:
#!/bin/sh

echo $1 > /sys/class/gpio/export
echo out > /sys/class/gpio/gpio$1/direction

COUNTER=0
while [ $COUNTER -lt $2 ]; do
   echo 1 > /sys/class/gpio/gpio$1/value
   sleep .35
   echo 0 > /sys/class/gpio/gpio$1/value
   sleep .65
   COUNTER=$(($COUNTER+1))
done

echo $1 > /sys/class/gpio/unexport

To run blinker enter two parameters;  the first the gpio#, and the second the number of times you want it to blink!

sudo  ./blinker.sh  101  7

(will blink gpio101 seven times)


sysled.sh

Code:
#!/bin/sh

echo $1  > /sys/class/gpio/export
echo out > /sys/class/gpio/gpio$1/direction

COUNTER=0
while [ $COUNTER -lt 10 ]; do
   echo 1 > /sys/class/gpio/gpio$1/value
   sleep .35
   echo 0 > /sys/class/gpio/gpio$1/value
   sleep .65
done


To run sysled: 

sudo  -b  ./sysled.sh  101

(will strobe gpio101 forever)

( remember to cd into the working directory where you place the script   )
( also, don't forget to make the script executable: chmod 0754 sysled.sh )

   

In the pic above I have attached the grey wire at ground pin(14),  and the orange wire at pin(16).

The anode of the LED is attached at the orange wire, and the cathode of the LED is attached to the grey wire through the ballast resistor , which is 330 ohms. The LED is a low power (5-7ma) 3mm standard LED.

I have so far tested the blue pins ( see chart ) and the green pins.  DO NOT use the blue pins if you're using the micro SD card !  If you're booting from the eMMC module (as I am) then the blue pins and most of the green pins are available to you;
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
   

In the above pic I've changed the configuration of the LED a bit (not important) and I've selected a new gpio pin#;

... this time I'm going to select physical pin(22) which is (from the chart) GPIO3_A7.  We could calculate this manually; however, longsleep wrote a small script for the PineA64 which I have modified for the Rock64 which will calculate the gpio number from the BANK_REG value-- in this case from GPIO3_A7.

./name2gpio.sh  gpio3_a7

103


name2gpio.sh

Code:
#!/usr/bin/python3

import sys
import string
pads={"A":0,"B":1,"C":2,"D":3}

def convert(value):
    value = value.upper()
    bank_num = int(value[4:5], 10)
    pad_nam = pads[value[6:7]]
    pad_num = int(value[7:], 10)
    gpionum = (bank_num * 32) + (pad_nam * 8) + pad_num
    return gpionum

if __name__ == "__main__":
    args = sys.argv[1:]
    if not args:
        print("Usage: %s <bank_reg>  ie; GPIO3_A5" % sys.argv[0])
        sys.exit(1)

    print("%d" % convert(args[0]))


Note:  The name  GPIO3_A7  is the bank (3) (one of four) and the register offset (7) (one of eight), of pad zero (0).  This Python3 script pulls the numbers from the string and converts them to integers for calculation.
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
#3
This chart shows the pins ( see chart ) that have been tested, and whether they work;

Green PI-2 Bus 
pin( 7)    GPIO1_D4    NO       ( this pin will crash the Rock64, you've been warned )
pin(12)    GPIO2_A3    NO       ( this pin should work, but does not ) (note: it does work as input!)
pin(13)    GPIO0_A0    NO       ( this pin is for toggling power to the usb3.0 port, per p19 of Rock64! )
pin(31)    GPIO0_A2    NO       ( pin(31) should not be used, but for completeness is for the usb2.0 ports )
pin(15)    GPIO3_A4    YES     gpio100
pin(16)    GPIO3_A5    YES     gpio101
pin(18)    GPIO3_A6    YES     gpio102
pin(22)    GPIO3_A7    YES     gpio103

Green PI P5+  Bus 
pin( 3)    GPIO2_C1    YES     gpio81
pin( 4)    GPIO2_C2    YES     gpio82
pin( 5)    GPIO2_C7    YES     gpio87
pin( 6)    GPIO2_C3    YES     gpio83
pin( 9)    GPIO2_C0    YES     gpio80
pin(10)    GPIO2_B7    YES    gpio79
pin(11)    GPIO2_C5    YES    gpio85
pin(12)    GPIO2_C4    YES    gpio84
pin(14)    GPIO2_C6    YES    gpio86

Yellow PI P5+  Bus 
pin(21)    GPIO2_D1    YES    gpio89
pin(22)    GPIO2_D0    YES    gpio88

Blue PI-2 Bus      ( use the blue pins ONLY if you are booting from eMMC and NOT using micro SD card )
pin(33)    GPIO1_A0    YES    gpio32
pin(35)    GPIO1_A1    YES    gpio33
pin(37)    GPIO1_A2    YES    gpio34
pin(40)    GPIO1_A3    YES    gpio35
pin(38)    GPIO1_A4    YES    gpio36
pin(36)    GPIO1_A5    YES    gpio37
pin(32)    GPIO1_A6    YES    gpio38

The Rock64 seems to have a minor design flaw for the maker community, in that the available gpio hardware pins are already used internally by the system and only a very few of them are available for programming. This may require expanding the gpio bus with external shift registers|latches or SPI | I2C expanders. 
This is somewhat mitigated by the fact that some services like I2S or ethernet 10|100 may not be in use and would theoretically be available.

WARNING:  DO NOT experiment with any of the other pins at this time;  they will crash the Rock64 or damage the existing image on the SD card or damage the image on the eMMC module!
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
Important Update !

   

The previous posts in this thread have been updated ( including the scripts|codes ) to make the gpio# calculation from the pad names general and applicable to all the gpio pad names!  If you have copied the  name2gpio.sh script above,  please update your code.  These scripts will be included in a new release soon.

In the pic above the example being shown is gpio physical pin(10)  on the PI P5+ bus;  GPIO2_B7;  which happens to be the I2S MCLK.  This is important to note, that if you're not going to be using I2S, then you have at your disposal nine(9) additional gpio(s) (Green)  on the PI P5+  bus!

./name2gpio.sh  gpio2_b7

79

physical pin(10),   gpio2_b7,   is gpio79



name2gpio.sh

Code:
#!/usr/bin/python3

import sys
import string
pads={"A":0,"B":1,"C":2,"D":3}

def convert(value):
   value = value.upper()
   bank_num = int(value[4:5], 10)
   pad_nam = pads[value[6:7]]
   pad_num = int(value[7:], 10)
   gpionum = (bank_num * 32) + (pad_nam * 8) + pad_num
   return gpionum

if __name__ == "__main__":
   args = sys.argv[1:]
   if not args:
       print("Usage: %s <bank_reg>  ie; GPIO3_A5" % sys.argv[0])
       sys.exit(1)

   print("%d" % convert(args[0]))
Important Notice:
It is your responsibility to ensure that your pad calculation is accurate (as in all calculations) every effort has been made to ensure accuracy and accountability;  however, if you should make an error,  the calculation may affect a mux outside the intended target register causing adverse effects, including but not limited to destruction of data and temporary loss of the system. Particularly make sure the spelling of your gpio name designation is correct (garbage in garbage out);  also be careful -- measure twice, cut once ( and any other sayings your can remember from your youth ).

marcus
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
Here is a chart so far of the GPIOs that I have tested. As of now, I have five of the general GPIOs working on the Pi-2 Bus, have marked one off as a NC as it is shared with switching power to the USB3 on and off, and have been unable to get any response from the other as yet.

https://docs.google.com/spreadsheets/d/1...Q/pubhtml#

I have also added the latest version of the name2gpio.py script to my rock64 script collection here.
  Reply
#6
(07-06-2017, 10:43 PM)pfeerick Wrote: Here is a chart so far of the GPIOs that I have tested. As of now, I have five of the general GPIOs working on the Pi-2 Bus, have marked one off as a NC as it is shared with switching power to the USB3 on and off, and have been unable to get any response from the other as yet.

https://docs.google.com/spreadsheets/d/1...Q/pubhtml#

I have also added the latest version of the name2gpio.py script to my rock64 script collection here.


Thank you Pete;   post #3 of this thread is my own chart of tested gpio# including both bus;  please corroborate those findings when you have time.  You're chart will be good for long-term documentation on-line once a few folks have been able to verify the testing.  Thanks again.

Smile
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
#7
(07-06-2017, 11:19 PM)MarkHaysHarris777 Wrote: Thank you Pete;   post #3 of this thread is my own chart of tested gpio# including both bus;  please corroborate those findings when you have time.  You're chart will be good for long-term documentation on-line once a few folks have been able to verify the testing.  Thanks again.

No problem Marcus. The chart is a spreadsheet, so click on the tab at the top to see the second bus Wink

I have finished checking the I/Os on the Pi P5+ bus, and all the expected I/Os work. i.e. all the pins are controllable except for power, gnd, the ethernet ones and the SPDIF pin, so a total of 9 unique I/Os, and the bottom two shared with the Pi-2 bus header.

I threw this quick script together to do the tests... just give it the GPIO export number, and it will attempt to set the pin up, and toggle it five times (led blinky Wink ). I suppose I should be nice and build the python script functionality into it, and also a 'capture current state and restore when finished' process... maybe... someday Wink
  Reply
#8
   

In this installment of the gpio blog I've included an input pin; a gpio input pin is fun and useful. In the pic above I have replicated the gpio_LED_lab on the Rock64 and I'm using both the PI-2 bus, and the PI P5+ bus as well.  

All of the codes in this section are snippets of bash scripts, using the sysfs exclusively; in later installments we'll switch over to Python3 and then finally to C|C++. 

The following PI-2 bus pin resources are used :

pin(15)   gpio3_a4    gpio100   output
pin(16)   gpio3_a5    gpio101   output
pin(18)   gpio3_a6    gpio102   output
pin(22)   gpio3_a7    gpio103   output

The following PI-P5+ bus pin resources are used :

pin(6)    gpio2_c3    gpio83    input
pin(21)   gpio2_d1    gpio89    output
pin(22)   gpio2_d0    gpio88    output

After exporting a gpio# to set the pin as input set the direction to "in":

  echo in > /sys/class/gpio/gpio83/direction

Doing so places two additional file elements in gpio83/ called "active_low" and "edge". Edge is used if you were to want to use the pin as an interreupt (all gpios can be used as interrupt pins). The "active_low" (default 0) sets the pullup or pulldown of the pin.  The default is to pullup the pin to "1" normally.  To make the pin active, the hardware (your button) pulls the pin down to ground potential and while holding gives the pin a value of zero "0". If you set the active_low to "1" the opposite is true;  the pin is pulled down to "0" and your button then would need to pull the pin up to 3v3 to set it "active" "1".

To read the input pin in our bash script we set a variable using back-ticks (`) and then 'cat' the value to the bash variable:

  VARIABLE=`cat /sys/class/gpio/gpio83/value`

Note:  the back-ticks (`) are not single quotes;  the back-tick is the character under the tilde (~) on most keyboards.  The back-ticks signal bash to run the 'cat' command to get the value, and then place the value in the VARIABLE.

In the following hardcoded snippet I'm going to be running our friend the blinker.sh code to run a controlled loop that will blink pin gpio100;  only this time the code will run until the input pin gpio83 is triggered by pressing our button on the gpio_LED_lab.  

Code:
COUNTER=1
while [ $COUNTER -gt 0 ]; do
  echo 1 > /sys/class/gpio/gpio100/value
  sleep .35
  echo 0 > /sys/class/gpio/gpio100/value
  sleep .65
  COUNTER=`cat /sys/class/gpio/gpio83/value`
done


The code can be run in the foreground or background, regardless; the code will run continuously until such time as we press and hold the button which 'grounds' pin gpio83 ( its value will be read as a zero 0 causing the while loop to terminate ).

The code works because once every time through the blink loop the value of gpio83 is read, and then if $COUNTER is set to zero 0 the while loop exits. If we read the value at the bottom of the loop the LED will be "left" off.  If we read the value in the middle of the loop (after the set ON) then the LED will be "left" ON.

Note:  It is often handy to have even a simple gpio_LED_lab while developing gpio projects (once you get the LEDs right, you can move on to the real objects of your project-- like relays or motors;



Here is the real sysled.sh that runs on my Rock64 as a heart-beat :


sysled.sh

Code:
#!/bin/sh

COUNTER=1
while [ $COUNTER -gt 0 ]; do
  echo 1 > /sys/class/gpio/gpio$1/value
  sleep $3
  echo 0 > /sys/class/gpio/gpio$1/value
  sleep $4
  COUNTER=`cat /sys/class/gpio/gpio$2/value`
done

To run the above codes:

./sysled.sh  103  83  .40  .75  &

The code will blink forever gpio103 with timing values (.40 ON) (.75 OFF) in the background (&)  until the button is pressed on the gpio83 input pin !

This code snippet is useful for knowing that the system is running;  the LED slowly blinks indicating that the processor is active.

Note:  Its fun to start more than one sysled.sh process active (yes, that's possible) each on a different LED !  Then set different timing values for each LED.

Note2:  Of course, the above code snippet assumes you have setup the gpio pins ahead of time;  I have a routine in my startup that activates the gpio pins that I will be using for input and output.

   

Note3:  you don't need the variable in this code;  the read of the input pin can be placed in the while condition directly without the use of an intermediate variable:

sysled2.sh

Code:
#!/bin/sh

while [ `cat /sys/class/gpio/gpio$2/value` -gt 0 ]; do
  echo 1 > /sys/class/gpio/gpio$1/value
  sleep $3
  echo 0 > /sys/class/gpio/gpio$1/value
  sleep $4
done
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
#9
Hi Marcus,

Lets create ROCK64 GPIO tutorial guide in PDF format and post at wiki site.
  Reply
#10
(07-12-2017, 06:10 PM)tllim Wrote: Hi Marcus,

Lets create ROCK64 GPIO tutorial guide in PDF format and post at wiki site.

Yes;  I'm actually working on that !  ... I'll let you know when its ready.  It will also include the Python and C|C++ methods as well.
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


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Hardware issues with Rock64 grobbs 10 556 04-08-2021, 05:24 AM
Last Post: t4_4t
  Python GPIO Library for the Rock64 (R64.GPIO) Leapo 35 33,251 03-21-2021, 11:14 PM
Last Post: Impaled
  Rock64 Long Term stability ramprasad 4 1,206 03-16-2021, 07:23 PM
Last Post: Rocklobster
  Rock64 No Audio - Solved wbecks 11 13,894 03-15-2021, 03:15 PM
Last Post: lowry
  Safest way to send shutdown signal to headless Rock64 SMB server? bmurphr1 3 700 03-14-2021, 06:01 PM
Last Post: clach04
  Rock64 as a router (OpenWRT,etc) bob-anon 2 1,123 03-12-2021, 01:16 AM
Last Post: arkadione
  Rock64 enable 1-wire to read DS18B20 or Dallas temperature sensor Perry 2 853 02-12-2021, 08:02 PM
Last Post: Perry
  Will Mobian Run On Rock64? Porcupine 1 394 01-13-2021, 12:39 PM
Last Post: tophneal
  Rock64 v2 as Openmediavault server - buffers / shutdown problems helpmerock 2 720 12-29-2020, 09:46 AM
Last Post: helpmerock
  rock64, compile problems "illegal instruction", "memory fault" -> ddr_333Mhz? hunderteins 10 4,550 11-17-2020, 08:00 AM
Last Post: Johannes

Forum Jump:


Users browsing this thread: 1 Guest(s)