Drive a GPIO on a SAM9x25-EK via Linux

This forum is for users of Microchip MPUs and who are interested in using Linux OS.

Moderator: nferre

Posts: 22
Joined: Tue May 30, 2017 7:41 pm

Re: Drive a GPIO on a SAM9x25-EK via Linux

Mon Nov 06, 2017 5:54 pm


Well it seems is hard to understand me, but is no problem! I figured it out using all these files I mentioned above, and online documentation that is also inside a compiled kernel for an architecture.
In short for other people who need understand sysfs in this case for sam9x35 this is what learnt.

If you are able to go through the kernel's filesystem, navigate to cd /sys/class/gpio
1- use ls comand
2-you'll see this:
export gpiochip0 gpiochip32 gpiochip64 gpiochip96 unexport
The "gpiochipX" are these mentioned "Banks" in this case A,B,C,D
3-If you refer to the file located at: yourkernel/documentation/gpio/sysfs.txt , you will find this text:
Paths in Sysfs
There are three kinds of entries in /sys/class/gpio:

- Control interfaces used to get userspace control over GPIOs;

- GPIOs themselves; and

- GPIO controllers ("gpio_chip" instances).

That's in addition to standard files including the "device" symlink.

The control interfaces are write-only:


"export" ... Userspace may ask the kernel to export control of
a GPIO to userspace by writing its number to this file.

Example: "echo 19 > export" will create a "gpio19" node
for GPIO #19, if that's not requested by kernel code.

"unexport" ... Reverses the effect of exporting to userspace.

Example: "echo 19 > unexport" will remove a "gpio19"
node exported using the "export" file.

GPIO controllers have paths like /sys/class/gpio/gpiochip42/ (for the
controller implementing GPIOs starting at #42) and have the following
read-only attributes:


"base" ... same as N, the first GPIO managed by this chip

"label" ... provided for diagnostics (not always unique)

"ngpio" ... how many GPIOs this manages (N to N + ngpio - 1)

Board documentation should in most cases cover what GPIOs are used for
what purposes. However, those numbers are not always stable; GPIOs on
a daughtercard might be different depending on the base board being used,
or other cards in the stack. In such cases, you may need to use the
gpiochip nodes (possibly in conjunction with schematics) to determine
the correct GPIO number to use for a given signal.
4-The above means that you should pick your Gpios for each bank with the offset of the gpiochipX as a base.

5-My example: for PD18

*Bank D starts at gpiochip96
*gpiochip96 is PD0
*if you refer to at91sam9x5.dtsi file at line 861 you will notice that D block has 22 pins, #gpio-lines = <22>; (not all available). (I picked 18 just for the example). and the calculation and command is as follows:

PD18 === 96+18 = 114

command: echo 114 > /sys/class/gpio/export

6-Now you can check with: ls /sys/class/gpio and you will see:
export gpiochip32 gpiochip96 unexport
gpiochip0 gpiochip64 pioD18
Well, i used the dtsi files for my architecture inside the kernel directories to find the extension (number of gpios per block) of each block. This can be done using the Datasheets, but the best way to understand is navigating to the gpio folder and listing the "gpiochipX"

In my case the "ngpio" command is showing Permission denied when I type:
# /sys/class/gpio/gpiochip64/ngpio
-sh: /sys/class/gpio/gpiochip64/ngpio: Permission denied
Edit: For fixing the permission denied msg. What I did:
# su root /sys/class/gpio/gpiochip64/ngpio
/sys/class/gpio/gpiochip64/ngpio: line 1: 32: not found

There you can see that block C has 32 pins/gpios.

You can refer to this link to ffind used and not used gpios.

Regards and I hope this help someone.

And Thanks Again Mr Blue_z

Return to “LINUX”

Who is online

Users browsing this forum: No registered users and 3 guests