g8tr1522
Posts: 1
Joined: Tue Sep 12, 2017 3:27 pm

GPIO: should I learn sysfs or direct register access?

Tue Sep 12, 2017 5:17 pm

Hello everyone, I'm new to the forums

I plan on making silly IoT stuff just to get experience. Right now, I'm building an automatic trading card sorter, with camera, motors, price database APIs and everything. I figured I'd start with the motors, and from there, start with simple GPIO programs.

I've seen two main ways for configuring GPIO pins: using "direct register access", and using sysfs 'calls'. Question is, what should I learn? And what do you recommend? Keep in mind that I'm also asking for what is preferred in the industry, in case I include my projects on a resume or in an interview. Also, I'd prefer to stick with C/C++ for various reasons.
I don't expect every question to be answered. I'd rather get more detailed answers for individual questions and eventually, all will be answered by different people. Bold questions are priority questions.

I'm experienced with programming Atmel atXmega chips using C/C++ in Atmel Studio. Thus, I'm very comfortable with the "direct register access" method for configuring GPIO pins. But the process isn't as straightforward on the BCM2835.
Questions about this method:
(1) Is it as easy as including a header file?
- The .h would have define statements that have hardware nicknames assigned to register addresses (in the I/O space). No functions. Just writing a combination of bits to the I/O addresses. (this is what I'm used to).
- Eg, PORTF_OUT_SET = 0x80; would make pin 7 turn on by assigning 0x80 to address 0x12B (or whatever).
(2) Do I need a library that does what I described in (1)? Specifically, I need special functions to write to the I/O space.
- If so, what advantages does this have over method (1)? Eg, Linux OS permissions prevent direct access, library functions prevent misuse, etc.
(3) Is this method acceptable for general IoT stuff? I'm more concerned about the security. Let's say I made a Wi-Fi light switch. What (dis/)advantages does this method have over others?
- I'm not wanting to, say, flash pin states at 25MHz, so I don't need the advantage of speed.
(4) Any recommendations (eg, libraries or tutorials) for methods (1) or (2) in C/C++?
(5) Are there any IDEs/applications I can use to view the ram contents (ie, so I can view the I/O space) for debugging purposes?

I've read about the sysfs method. Seems more useful down the road, but it also seems like I'll need to do some homework to take full advantage of it.
(6) What does this do that is different than method (1)? (maybe see question (9)).
(7) What (dis/)advantages does this have over direct register access? I know that sysfs accounts for user permissions and contradictory mis-configurations.
(8) For IoT applications in mind, is this method more secure? Or more preferred in the industry?
(9) My understanding of the Linux OS and Unix stuff is pretty much nothing. I have no idea how permissions work, what a root user is, how to use the terminal, how to call to the terminal in a program, etc. Do I need this knowledge to fully take advantage of (or just scrape by) the sysfs method? Do I need this knowledge to excel in the industry?
(10) Any recommendations of resources where I can quickly learn the stuff described in (9)? Or maybe a textbook to get an in depth education?
(11) Recommendations for resources on learning the sysfs method in C/C++?

General Questions:
(12) Are there other methods that are more preferred?
(13) In tutorials that I'll be watching, is there any reason why I can't watch in one language and then translate to C/C++?
- Eg, (if a library can be used for both, then) I can watch Python tutorials and translate the concepts to C/C++?
(14) Any mistakes in my assumptions?

KnarfB
Posts: 158
Joined: Wed Dec 14, 2016 10:47 am
Location: Germany

Re: GPIO: should I learn sysfs or direct register access?

Tue Sep 12, 2017 9:47 pm

Using sysfs is easy, e.g. from terminal:

Code: Select all

sudo echo 18 > /sys/class/gpio/export
sudo echo out > /sys/class/gpio/gpio18/direction 
sudo echo 1 > /sys/class/gpio/gpio18/value
This can be translated to many prog. languages including C.
The advantage compared to WiringPi and other libs is: sysfs works for most embedded Linux systems, it is not Raspi specific.

A drawback is: you don't have tight control over the timing because it is a long way from a user app/command line to the kernel module which does the real work. Therefore, you may want to use direct register access, especially when you want to set several pins simultaneously (precisely at the same clock tick).

You may do so from user code. But, the timing between two statements cannot be precisely controlled (e.g. if you want to do PWM or emulate some fast HW protocol in software by "bit-banging"). So you might end up with writing a dedicated kernel module which gives you more precise control over the timing.
hth Frank

User avatar
rpdom
Posts: 11709
Joined: Sun May 06, 2012 5:17 am
Location: Essex, UK

Re: GPIO: should I learn sysfs or direct register access?

Wed Sep 13, 2017 6:46 am

KnarfB wrote:
Tue Sep 12, 2017 9:47 pm
Using sysfs is easy, e.g. from terminal:

Code: Select all

sudo echo 18 > /sys/class/gpio/export
sudo echo out > /sys/class/gpio/gpio18/direction 
sudo echo 1 > /sys/class/gpio/gpio18/value
The use of sudo here is pointless. The pi user has access to /sys/class/gpio without it, and "sudo echo blah" just runs the echo command as root. It doesn't run the redirect ">" as root.

Code: Select all

echo 18 > /sys/class/gpio/export
echo out > /sys/class/gpio/gpio18/direction 
echo 1 > /sys/class/gpio/gpio18/value
should be all you need.

Although if programming in C I would use a library like WiringPi or pigpio (actually I use my own code, but that's just my choice and because I wanted to know how to do it)

User avatar
gordon@drogon.net
Posts: 1970
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: GPIO: should I learn sysfs or direct register access?

Wed Sep 13, 2017 2:31 pm

Make life easy for yourself and use a library - there are a couple to choose from. wiringPi (mine) and PiGPIO are the popular ones.

both include command-line tools to let you do stuff without even writing a program.

e.g. in wiringPi - blink an LED on physical pin 11:

gpio -1 blink 11

(the -1 tells it to use physical pin numbers)

and so on. http://wiringpi.com/ however it's installed with raspbian by default. (as is pigpio IIRC)

wiringPi is a pin based library and has stuff like pinMode(), digitalWrite() and so-on.

-Gordon
--
Gordons projects: https://projects.drogon.net/

Return to “C/C++”

Who is online

Users browsing this forum: No registered users and 4 guests