Please enable javascript, or click here to visit my ecommerce web site powered by Shopify.
Jump to: navigation, search

Difference between revisions of "Rock/GPIO"

(Created page with "==pin numbers == <syntaxhighlight lang="c" enclose="div"> #define NUM_GROUP 32 #define PIN_BASE 160 #define RK30_PIN0_PA0 (0*NUM_GROUP + PIN_BASE + 0) #...")
 
Line 1: Line 1:
 +
= GPIO on the extension header =
 +
 +
 +
Below is programming in different languages to bit flipping the GPIO on RR.
 +
 +
== C ==
 +
<syntaxhighlight lang="c" enclose="div">
 +
 +
#include <stdio.h>
 +
 +
int main()
 +
{
 +
  FILE *fp = fopen("/sys/class/gpio/export", "w");
 +
  if( !fp )
 +
  {
 +
    printf("error open export file");
 +
    return 1;
 +
  }
 +
  fprintf(fp, "%d", 172);
 +
  fclose(fp);
 +
 +
  fp = fopen("/sys/class/gpio/gpio175/direction", "w");
 +
 +
  if( !fp )
 +
  {
 +
    printf("error open export file");
 +
    return 1;
 +
  }
 +
  fprintf(fp, "%s", "out");
 +
  fclose(fp);
 +
 +
  fp = fopen("/sys/class/gpio/gpio175/value", "w");
 +
 +
  if( !fp )
 +
  {
 +
    printf("error open export file");
 +
    return 1;
 +
  }
 +
  fprintf(fp, "%d", 1);
 +
 
 +
}
 +
 +
</syntaxhighlight>
 +
 +
== Python ==
 +
<syntaxhighlight lang="python" enclose="div">
 +
 +
import time
 +
 +
def initpin(pinnum, mode):
 +
  '''
 +
  pinnum: pin number, eg. 172, 175 etc.
 +
  mode: pin mode, valid values: in or out
 +
  '''
 +
  with open('/sys/class/gpio/export', 'w') as f:
 +
    f.write(str(pinnum))
 +
 +
  with open('/sys/class/gpio/gpio' + str(pinnum) + '/direction', 'w') as f:
 +
    f.write(str(mode))
 +
 +
def setpin(pinnum, value):
 +
 
 +
  with open('/sys/class/gpio/gpio' + str(pinnum) + '/value', 'w') as f:
 +
    f.write(str(value))
 +
 +
if __name__ == '__main__':
 +
  initpin(172, 'out')
 +
  while True:
 +
    setpin(172, 0)
 +
    time.sleep(1)
 +
    setpin(172, 1)
 +
    time.sleep(1)
 +
</syntaxhighlight>
 +
 +
== Shell ==
 +
<syntaxhighlight lang="bash" enclose="div">
 +
 +
#!/bin/sh
 +
 +
# enable the gpio 172 -> green led
 +
echo 172 > /sys/class/gpio/export
 +
 +
# set the direction to output
 +
echo "out" > /sys/class/gpio/gpio172/direction
 +
while true;
 +
do
 +
echo 0 > /sys/class/gpio/gpio172/value #led on
 +
sleep 1
 +
echo 1 > /sys/class/gpio/gpio172/value #led off
 +
sleep 1
 +
done
 +
 +
</syntaxhighlight>
 +
 
==pin numbers ==
 
==pin numbers ==
 
<syntaxhighlight lang="c" enclose="div">
 
<syntaxhighlight lang="c" enclose="div">

Revision as of 03:15, 18 September 2014

GPIO on the extension header

Below is programming in different languages to bit flipping the GPIO on RR.

C

#include <stdio.h>

int main()
{
  FILE *fp = fopen("/sys/class/gpio/export", "w");
  if( !fp )
  {
    printf("error open export file");
    return 1;
  }
  fprintf(fp, "%d", 172);
  fclose(fp);

  fp = fopen("/sys/class/gpio/gpio175/direction", "w");

  if( !fp )
  {
    printf("error open export file");
    return 1;
  }
  fprintf(fp, "%s", "out");
  fclose(fp);

  fp = fopen("/sys/class/gpio/gpio175/value", "w");

  if( !fp )
  {
    printf("error open export file");
    return 1;
  }
  fprintf(fp, "%d", 1);
 
}

Python

import time

def initpin(pinnum, mode):
  '''
  pinnum: pin number, eg. 172, 175 etc.
  mode: pin mode, valid values: in or out
  '''

  with open('/sys/class/gpio/export', 'w') as f:
    f.write(str(pinnum))

  with open('/sys/class/gpio/gpio' + str(pinnum) + '/direction', 'w') as f:
    f.write(str(mode))

def setpin(pinnum, value):
 
  with open('/sys/class/gpio/gpio' + str(pinnum) + '/value', 'w') as f:
    f.write(str(value))

if __name__ == '__main__':
  initpin(172, 'out')
  while True:
    setpin(172, 0)
    time.sleep(1)
    setpin(172, 1)
    time.sleep(1)

Shell

#!/bin/sh

# enable the gpio 172 -> green led
echo 172 > /sys/class/gpio/export

# set the direction to output
echo "out" > /sys/class/gpio/gpio172/direction
while true;
do
echo 0 > /sys/class/gpio/gpio172/value #led on
sleep 1
echo 1 > /sys/class/gpio/gpio172/value #led off
sleep 1
done

pin numbers

#define      NUM_GROUP 32
#define      PIN_BASE            160

#define RK30_PIN0_PA0           (0*NUM_GROUP + PIN_BASE + 0)
#define RK30_PIN0_PA1           (0*NUM_GROUP + PIN_BASE + 1)
#define RK30_PIN0_PA2           (0*NUM_GROUP + PIN_BASE + 2)
#define RK30_PIN0_PA3           (0*NUM_GROUP + PIN_BASE + 3)
#define RK30_PIN0_PA4           (0*NUM_GROUP + PIN_BASE + 4)
#define RK30_PIN0_PA5           (0*NUM_GROUP + PIN_BASE + 5)
#define RK30_PIN0_PA6           (0*NUM_GROUP + PIN_BASE + 6)
#define RK30_PIN0_PA7           (0*NUM_GROUP + PIN_BASE + 7)
#define RK30_PIN0_PB0           (0*NUM_GROUP + PIN_BASE + 8)
#define RK30_PIN0_PB1           (0*NUM_GROUP + PIN_BASE + 9)
#define RK30_PIN0_PB2           (0*NUM_GROUP + PIN_BASE + 10)
#define RK30_PIN0_PB3           (0*NUM_GROUP + PIN_BASE + 11)
#define RK30_PIN0_PB4           (0*NUM_GROUP + PIN_BASE + 12)
#define RK30_PIN0_PB5           (0*NUM_GROUP + PIN_BASE + 13)
#define RK30_PIN0_PB6           (0*NUM_GROUP + PIN_BASE + 14)
#define RK30_PIN0_PB7           (0*NUM_GROUP + PIN_BASE + 15)
#define RK30_PIN0_PC0           (0*NUM_GROUP + PIN_BASE + 16)
#define RK30_PIN0_PC1           (0*NUM_GROUP + PIN_BASE + 17)
#define RK30_PIN0_PC2           (0*NUM_GROUP + PIN_BASE + 18)
#define RK30_PIN0_PC3           (0*NUM_GROUP + PIN_BASE + 19)
#define RK30_PIN0_PC4           (0*NUM_GROUP + PIN_BASE + 20)
#define RK30_PIN0_PC5           (0*NUM_GROUP + PIN_BASE + 21)
#define RK30_PIN0_PC6           (0*NUM_GROUP + PIN_BASE + 22)
#define RK30_PIN0_PC7           (0*NUM_GROUP + PIN_BASE + 23)
#define RK30_PIN0_PD0           (0*NUM_GROUP + PIN_BASE + 24)
#define RK30_PIN0_PD1           (0*NUM_GROUP + PIN_BASE + 25)
#define RK30_PIN0_PD2           (0*NUM_GROUP + PIN_BASE + 26)
#define RK30_PIN0_PD3           (0*NUM_GROUP + PIN_BASE + 27)
#define RK30_PIN0_PD4           (0*NUM_GROUP + PIN_BASE + 28)
#define RK30_PIN0_PD5           (0*NUM_GROUP + PIN_BASE + 29)
#define RK30_PIN0_PD6           (0*NUM_GROUP + PIN_BASE + 30)
#define RK30_PIN0_PD7           (0*NUM_GROUP + PIN_BASE + 31)

#define RK30_PIN1_PA0           (1*NUM_GROUP + PIN_BASE + 0)
#define RK30_PIN1_PA1           (1*NUM_GROUP + PIN_BASE + 1)
#define RK30_PIN1_PA2           (1*NUM_GROUP + PIN_BASE + 2)
#define RK30_PIN1_PA3           (1*NUM_GROUP + PIN_BASE + 3)
#define RK30_PIN1_PA4           (1*NUM_GROUP + PIN_BASE + 4)
#define RK30_PIN1_PA5           (1*NUM_GROUP + PIN_BASE + 5)
#define RK30_PIN1_PA6           (1*NUM_GROUP + PIN_BASE + 6)
#define RK30_PIN1_PA7           (1*NUM_GROUP + PIN_BASE + 7)
#define RK30_PIN1_PB0           (1*NUM_GROUP + PIN_BASE + 8)
#define RK30_PIN1_PB1           (1*NUM_GROUP + PIN_BASE + 9)
#define RK30_PIN1_PB2           (1*NUM_GROUP + PIN_BASE + 10)
#define RK30_PIN1_PB3           (1*NUM_GROUP + PIN_BASE + 11)
#define RK30_PIN1_PB4           (1*NUM_GROUP + PIN_BASE + 12)
#define RK30_PIN1_PB5           (1*NUM_GROUP + PIN_BASE + 13)
#define RK30_PIN1_PB6           (1*NUM_GROUP + PIN_BASE + 14)
#define RK30_PIN1_PB7           (1*NUM_GROUP + PIN_BASE + 15)
#define RK30_PIN1_PC0           (1*NUM_GROUP + PIN_BASE + 16)
#define RK30_PIN1_PC1           (1*NUM_GROUP + PIN_BASE + 17)
#define RK30_PIN1_PC2           (1*NUM_GROUP + PIN_BASE + 18)
#define RK30_PIN1_PC3           (1*NUM_GROUP + PIN_BASE + 19)
#define RK30_PIN1_PC4           (1*NUM_GROUP + PIN_BASE + 20)
#define RK30_PIN1_PC5           (1*NUM_GROUP + PIN_BASE + 21)
#define RK30_PIN1_PC6           (1*NUM_GROUP + PIN_BASE + 22)
#define RK30_PIN1_PC7           (1*NUM_GROUP + PIN_BASE + 23)
#define RK30_PIN1_PD0           (1*NUM_GROUP + PIN_BASE + 24)
#define RK30_PIN1_PD1           (1*NUM_GROUP + PIN_BASE + 25)
#define RK30_PIN1_PD2           (1*NUM_GROUP + PIN_BASE + 26)
#define RK30_PIN1_PD3           (1*NUM_GROUP + PIN_BASE + 27)
#define RK30_PIN1_PD4           (1*NUM_GROUP + PIN_BASE + 28)
#define RK30_PIN1_PD5           (1*NUM_GROUP + PIN_BASE + 29)
#define RK30_PIN1_PD6           (1*NUM_GROUP + PIN_BASE + 30)
#define RK30_PIN1_PD7           (1*NUM_GROUP + PIN_BASE + 31)

#define RK30_PIN2_PA0           (2*NUM_GROUP + PIN_BASE + 0)
#define RK30_PIN2_PA1           (2*NUM_GROUP + PIN_BASE + 1)
#define RK30_PIN2_PA2           (2*NUM_GROUP + PIN_BASE + 2)
#define RK30_PIN2_PA3           (2*NUM_GROUP + PIN_BASE + 3)
#define RK30_PIN2_PA4           (2*NUM_GROUP + PIN_BASE + 4)
#define RK30_PIN2_PA5           (2*NUM_GROUP + PIN_BASE + 5)
#define RK30_PIN2_PA6           (2*NUM_GROUP + PIN_BASE + 6)
#define RK30_PIN2_PA7           (2*NUM_GROUP + PIN_BASE + 7)
#define RK30_PIN2_PB0           (2*NUM_GROUP + PIN_BASE + 8)
#define RK30_PIN2_PB1           (2*NUM_GROUP + PIN_BASE + 9)
#define RK30_PIN2_PB2           (2*NUM_GROUP + PIN_BASE + 10)
#define RK30_PIN2_PB3           (2*NUM_GROUP + PIN_BASE + 11)
#define RK30_PIN2_PB4           (2*NUM_GROUP + PIN_BASE + 12)
#define RK30_PIN2_PB5           (2*NUM_GROUP + PIN_BASE + 13)
#define RK30_PIN2_PB6           (2*NUM_GROUP + PIN_BASE + 14)
#define RK30_PIN2_PB7           (2*NUM_GROUP + PIN_BASE + 15)
#define RK30_PIN2_PC0           (2*NUM_GROUP + PIN_BASE + 16)
#define RK30_PIN2_PC1           (2*NUM_GROUP + PIN_BASE + 17)
#define RK30_PIN2_PC2           (2*NUM_GROUP + PIN_BASE + 18)
#define RK30_PIN2_PC3           (2*NUM_GROUP + PIN_BASE + 19)
#define RK30_PIN2_PC4           (2*NUM_GROUP + PIN_BASE + 20)
#define RK30_PIN2_PC5           (2*NUM_GROUP + PIN_BASE + 21)
#define RK30_PIN2_PC6           (2*NUM_GROUP + PIN_BASE + 22)
#define RK30_PIN2_PC7           (2*NUM_GROUP + PIN_BASE + 23)
#define RK30_PIN2_PD0           (2*NUM_GROUP + PIN_BASE + 24)
#define RK30_PIN2_PD1           (2*NUM_GROUP + PIN_BASE + 25)
#define RK30_PIN2_PD2           (2*NUM_GROUP + PIN_BASE + 26)
#define RK30_PIN2_PD3           (2*NUM_GROUP + PIN_BASE + 27)
#define RK30_PIN2_PD4           (2*NUM_GROUP + PIN_BASE + 28)
#define RK30_PIN2_PD5           (2*NUM_GROUP + PIN_BASE + 29)
#define RK30_PIN2_PD6           (2*NUM_GROUP + PIN_BASE + 30)
#define RK30_PIN2_PD7           (2*NUM_GROUP + PIN_BASE + 31)

#define RK30_PIN3_PA0           (3*NUM_GROUP + PIN_BASE + 0)
#define RK30_PIN3_PA1           (3*NUM_GROUP + PIN_BASE + 1)
#define RK30_PIN3_PA2           (3*NUM_GROUP + PIN_BASE + 2)
#define RK30_PIN3_PA3           (3*NUM_GROUP + PIN_BASE + 3)
#define RK30_PIN3_PA4           (3*NUM_GROUP + PIN_BASE + 4)
#define RK30_PIN3_PA5           (3*NUM_GROUP + PIN_BASE + 5)
#define RK30_PIN3_PA6           (3*NUM_GROUP + PIN_BASE + 6)
#define RK30_PIN3_PA7           (3*NUM_GROUP + PIN_BASE + 7)
#define RK30_PIN3_PB0           (3*NUM_GROUP + PIN_BASE + 8)
#define RK30_PIN3_PB1           (3*NUM_GROUP + PIN_BASE + 9)
#define RK30_PIN3_PB2           (3*NUM_GROUP + PIN_BASE + 10)
#define RK30_PIN3_PB3           (3*NUM_GROUP + PIN_BASE + 11)
#define RK30_PIN3_PB4           (3*NUM_GROUP + PIN_BASE + 12)
#define RK30_PIN3_PB5           (3*NUM_GROUP + PIN_BASE + 13)
#define RK30_PIN3_PB6           (3*NUM_GROUP + PIN_BASE + 14)
#define RK30_PIN3_PB7           (3*NUM_GROUP + PIN_BASE + 15)
#define RK30_PIN3_PC0           (3*NUM_GROUP + PIN_BASE + 16)
#define RK30_PIN3_PC1           (3*NUM_GROUP + PIN_BASE + 17)
#define RK30_PIN3_PC2           (3*NUM_GROUP + PIN_BASE + 18)
#define RK30_PIN3_PC3           (3*NUM_GROUP + PIN_BASE + 19)
#define RK30_PIN3_PC4           (3*NUM_GROUP + PIN_BASE + 20)
#define RK30_PIN3_PC5           (3*NUM_GROUP + PIN_BASE + 21)
#define RK30_PIN3_PC6           (3*NUM_GROUP + PIN_BASE + 22)
#define RK30_PIN3_PC7           (3*NUM_GROUP + PIN_BASE + 23)
#define RK30_PIN3_PD0           (3*NUM_GROUP + PIN_BASE + 24)
#define RK30_PIN3_PD1           (3*NUM_GROUP + PIN_BASE + 25)
#define RK30_PIN3_PD2           (3*NUM_GROUP + PIN_BASE + 26)
#define RK30_PIN3_PD3           (3*NUM_GROUP + PIN_BASE + 27)
#define RK30_PIN3_PD4           (3*NUM_GROUP + PIN_BASE + 28)
#define RK30_PIN3_PD5           (3*NUM_GROUP + PIN_BASE + 29)
#define RK30_PIN3_PD6           (3*NUM_GROUP + PIN_BASE + 30)
#define RK30_PIN3_PD7           (3*NUM_GROUP + PIN_BASE + 31)