Jump to content

Accessing the GPIO pins through sysfs with mainline kernel


nedoskiv

Recommended Posts

Hello I'm using OPI PC+

and tried to figure  out some free gpio to play with it. Taking a look at forums/etc there are formula to calculate current GPIO number according to this https://linux-sunxi.org/GPIO and some posts here, there is a formula:

 

(position of letter in alphabet - 1) * 32 + pin number

 

so can someone explain me how pin 38 (PG6) becomes GPIO 198?

 

calculation back from the formula point letter F

 

 

 

Link to comment
Share on other sites

When you are doing frequently such calculations and getting bored, you simply use python script to help you out ...

#!/usr/bin/python3

import sys
import string

def convert(value):
	value = value.upper()
	alp = value[1]
	idx = string.uppercase.index(alp)
	num = int(value[2:], 10)
	res = idx * 32 + num
	return res

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

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

Link to comment
Share on other sites

sry if this is a stupid question: in which format should <pin> be given to your script?

I only get these:

cg@poisson:/usr/local/bin$ GpioCalc.py pg6
Traceback (most recent call last):
  File "/usr/local/bin/GpioCalc.py", line 22, in <module>
    print("%d" % convert(args[0]))
  File "/usr/local/bin/GpioCalc.py", line 11, in convert
    idx = string.uppercase.index(alp)
AttributeError: 'module' object has no attribute 'uppercase'

cg@poisson:/usr/local/bin$ GpioCalc.py g6
Traceback (most recent call last):
  File "/usr/local/bin/GpioCalc.py", line 22, in <module>
    print("%d" % convert(args[0]))
  File "/usr/local/bin/GpioCalc.py", line 11, in convert
    idx = string.uppercase.index(alp)
AttributeError: 'module' object has no attribute 'uppercase'

cg@poisson:/usr/local/bin$ GpioCalc.py g 6
Traceback (most recent call last):
  File "/usr/local/bin/GpioCalc.py", line 22, in <module>
    print("%d" % convert(args[0]))
  File "/usr/local/bin/GpioCalc.py", line 10, in convert
    alp = value[1]
IndexError: string index out of range

cg@poisson:/usr/local/bin$ GpioCalc.py 38
Traceback (most recent call last):
  File "/usr/local/bin/GpioCalc.py", line 22, in <module>
    print("%d" % convert(args[0]))
  File "/usr/local/bin/GpioCalc.py", line 11, in convert
    idx = string.uppercase.index(alp)
AttributeError: 'module' object has no attribute 'uppercase'

cg@poisson:/usr/local/bin$ 

Link to comment
Share on other sites

Sorry, I wasn't the original author of that script.

 

Although it headed with #!/usr/bin/python3, your issues seems that it is not python3 compatible, try it with python2.7 ...

 

(Or if you really python3, change the "uppercase" which is now "ascii_uppercase")

 

EDIT : it is better to change the "uppercase" to "ascii_uppercase", since it works on both python2.7 and python3

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

Terms of Use - Privacy Policy - Guidelines