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

ironhorse
Posts: 14
Joined: Mon Apr 06, 2015 9:05 pm

Drive a GPIO on a SAM9x25-EK via Linux

Mon Apr 13, 2015 5:04 am

I have the most basic Linux question in the world for you all. I have always used micros in bare metal fashion and have always had direct control to drive GPIO ports however I wanted. I'm using the SAM9x25-EK dev board now and will need to use Linux for the first time on one of my boards. I eventually will need to drive some GPIOs high or low depending on a condition (very slow binary status stuff). The SAM9x25-EK has a User LED on port line PD21. 

Is there a low level command in Linux that can access already known ports that will allow me to drive this port or a way to accomplish this without adding a new device with it's own Linux driver? Or is that really the only way to drive a darn GPIO port high and low? If I had to write an application, how would I control this port?  I guess a question can't get any more basic than this one.

Rain dance time,
IH
bizer
Posts: 32
Joined: Fri Aug 22, 2014 8:11 pm

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

Mon Apr 13, 2015 9:14 pm

Hi, right now i'm asking me the same question, only with a SAM9N12.
I did some GPIO Toggeling with raspberry, but it's not the same with Atmel. I've you find something let me know:

http://wiringpi.com/the-gpio-utility/
ironhorse
Posts: 14
Joined: Mon Apr 06, 2015 9:05 pm

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

Tue Apr 14, 2015 11:26 pm

Nice! Thanks for the PI link.
gpio write <pin>

That is exactly what I am looking for but just for the SAM9X25 instead of the PI. Glad I am not the only one trying to figure this out. I know there must be tons of people using embedded Linux for years that know the answer to this. I should have titled the subject, "The most basic Linux questions in the world" to get the most eyes.

Still searching...

IH
jcarrillo
Posts: 11
Joined: Thu Jun 19, 2014 9:37 am

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

Wed Apr 15, 2015 10:30 am

Hi,

With Linux versions v3.x the GPIOs are numbered as:
- PA (bank A) 0 .. 31 => GPIO_number = 0 .. 31
- PB (bank B) 0 .. 31 => GPIO_number = 32 .. 63
- PC (bank C) 0 .. 31 => GPIO_number = 64 .. 95
...

The process with Linux shell is the following:
1) Export the GPIO

Code: Select all

echo GPIO_number > /sys/class/gpio/export
# the file /sys/class/gpio/pioXY is created where X is the Bank number and Y the GPIO number.
2) Configure the GPIO as input or as output

Code: Select all

echo GPIO_direction > /sys/class/gpio/pioXY/direction # where GPIO_direction is in or out.
3) Write the value of the GPIO

Code: Select all

echo GPIO_value > /sys/class/gpio/pioXY/value
or read it

Code: Select all

cat /sys/class/gpio/pioXY/value
Example: if you want to configure the GPIO PC2 as output and write a LOW value:

Code: Select all

echo 66 > /sys/class/gpio/export # PC2 => 64 + 2 = 66 (/sys/class/gpio/pioC2 is created)
echo out > /sys/class/gpio/pioC2/direction
echo 0 > /sys/class/gpio/pioC2/value
If you use a Linux kernel v2.x, the path of the GPIO driver is

Code: Select all

/sys/class/gpio/gpioGPIO_number
if I remember correctly.

IMPORTANT: with recent kernel versions the GPIO driver (sysfs) is not enabled by default. To enable this feature, switch on the option within the kernel configuration tool (menuconfig): : Device drivers --> GPIO support --> /sys/class/gpio/... (sysfs interface) 

Hope this helps.

Regards!
Last edited by jcarrillo on Thu Apr 16, 2015 9:39 am, edited 1 time in total.
ironhorse
Posts: 14
Joined: Mon Apr 06, 2015 9:05 pm

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

Thu Apr 16, 2015 4:03 am

jcarrillo you rock! :D

That is exactly what I was looking for.
sds5096
Posts: 7
Joined: Sun Oct 05, 2014 4:01 pm

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

Fri Apr 17, 2015 3:53 am

How could this be performed programmatically from a C++ application?
ironhorse
Posts: 14
Joined: Mon Apr 06, 2015 9:05 pm

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

Mon Apr 20, 2015 3:44 am

Could the C++ application just open up a shell automatically and accomplish this using those commands? I would be interested in seeing some application implementation examples as well.

IH
blue_z
Location: USA
Posts: 1676
Joined: Thu Apr 19, 2007 10:15 pm

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

Mon Apr 20, 2015 8:47 pm

sds5096 wrote:How could this be performed programmatically from a C++ application?
Use open(), write() and close() syscalls.

Regards
blue_z
Location: USA
Posts: 1676
Joined: Thu Apr 19, 2007 10:15 pm

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

Mon Apr 20, 2015 9:00 pm

ironhorse wrote:The SAM9x25-EK has a User LED on port line PD21.
ironhorse wrote:That is exactly what I was looking for.
Modern versions of Linux (assuming proper DT configuration) make a distinction between GPIO and LEDs (e.g. the LED can have a trigger). This topic is not Atmel specific, so search the web for info.

Regards
ironhorse
Posts: 14
Joined: Mon Apr 06, 2015 9:05 pm

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

Fri Apr 24, 2015 4:44 am

Thanks for the info blue_z

IH
ironhorse
Posts: 14
Joined: Mon Apr 06, 2015 9:05 pm

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

Tue Jun 16, 2015 3:46 pm

I just found this option jcarrillo wrote about in the kernel 3.18.6 menuconfig

Device Drivers > GPIO Support

[*] /sys/class/gpio/...(sysfs interface) :D

Posting it in case it helps someone else.
IH
eug_proon
Posts: 3
Joined: Wed May 25, 2016 3:20 pm

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

Tue Aug 16, 2016 2:42 pm

Hello, dear sam9x5 gurus...
I understand, that the post topic is old and nobody might not look at my posting, byt yet nonetheless... Hope my problem is close to the ogirin topic of the post, and there are many developers who would find useful the problem solution I need.
It is clear for me how to control GPIO pins through pseudo files in /sys/class/gpio. I try to debug a GPIO interface at the at91sam9x25-ek device and some sensor that should fire '0' at a GPIO pin/ The problem is that reading of the /sys/class/gpio/gpio50 returns always 1 without respect of the sensor's behavior. And now I don't know, who is to blame, the hardware or may be the driver.
I am going to write a kernel module, say, test_gpio50.ko to check directly, if the corresponding GPIO register "gets" the signal from sensor, but I don't know, what to check and where to look. Unfortunately, I cannot find info on memory mapping of GPIO in sam9x5, or sample program examples in C that would show how to control GPIO interface directly.
I would kindly appreciate any help in the issue.
Regards,
Evgenii Prun,
Moscow, Russia.
blue_z
Location: USA
Posts: 1676
Joined: Thu Apr 19, 2007 10:15 pm

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

Wed Aug 17, 2016 2:32 am

eug_proon wrote:I try to debug a GPIO interface at the at91sam9x25-ek device and some sensor that should fire '0' at a GPIO pin/ The problem is that reading of the /sys/class/gpio/gpio50 returns always 1 without respect of the sensor's behavior. And now I don't know, who is to blame, the hardware or may be the driver.
Given the lack of details and the pitfalls in using the sysfs GPIO interface, looking to place blame instead of being introspective seems vain.
eug_proon wrote:I am going to write a kernel module, say, test_gpio50.ko to check directly, if the corresponding GPIO register "gets" the signal from sensor, but I don't know, what to check and where to look.
Adding another unknown (i.e. an untested kernl module) to the mix is not a sane method of debugging.
There are many legitimate reasons for writing kernel modules.
But to verify if your setup is correct or because you think sysfs is broken are reasons rarely used.
IOW you probably haven't done a thorough job in verifying that your setup is correct and that you're using the sysfs interface correctly.
eug_proon wrote:Unfortunately, I cannot find info on memory mapping of GPIO in sam9x5, or sample program examples in C that would show how to control GPIO interface directly.
All the salient information is in the SoC datasheet, specifically the SoC memory map and the PIO register descriptions.

Regards
AleFachini
Posts: 15
Joined: Tue May 30, 2017 7:41 pm

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

Fri Nov 03, 2017 5:39 pm

Hello:
I'm sorry to bring up this topic, but it's very similar to what I need so i'll just add my question here.
I am working with sam9x35 EK and i need to run some init scripts setting some GPIO before running my app. Short explanation: the hardware is done so the pins are predefined so i just need to use those, because this migrated from 9g45 to 9x35.
Before Asking I've reviewed these documents:

Atmel-11115- Sam9x35 EK from where I got at page 9 the Pinout of SODIMM200 Interface from SoC.
Then i looked for the linuxkernel in my buildroot directorie and read these files:
path: ../buildroot/output/build/linux-linux4sam_5.6/arch/arm/boot/dts
  • at91sam9x5.dtsi
  • a91sam9x35.dtsi
here I have found these relevant lines:
#include "skeleton.dtsi"
#include <dt-bindings/dma/at91.h>
#include <dt-bindings/pinctrl/at91.h>
#include <dt-bindings/interrupt-controller/irq.h>
#include <dt-bindings/gpio/gpio.h>
#include <dt-bindings/clock/at91.h>

/ {
model = "Atmel AT91SAM9x5 family SoC";
compatible = "atmel,at91sam9x5";
interrupt-parent = <&aic>;

aliases {
serial0 = &dbgu;
serial1 = &usart0;
serial2 = &usart1;
serial3 = &usart2;
gpio0 = &pioA;
gpio1 = &pioB;
gpio2 = &pioC;
gpio3 = &pioD;
tcb0 = &tcb0;
tcb1 = &tcb1;
i2c0 = &i2c0;
i2c1 = &i2c1;
i2c2 = &i2c2;
ssc0 = &ssc0;
pwm0 = &pwm0;
};
And these
#include "at91sam9x5.dtsi"
#include "at91sam9x5_lcd.dtsi"
#include "at91sam9x5_macb0.dtsi"
#include "at91sam9x5_can.dtsi"

/ {
model = "Atmel AT91SAM9X35 SoC";
compatible = "atmel,at91sam9x35", "atmel,at91sam9x5";

ahb {
apb {
pinctrl@fffff400 {
atmel,mux-mask = <
/* A B C */
0xffffffff 0xffe03fff 0xc000000c /* pioA */
0x000406ff 0x00047e3f 0x00000000 /* pioB */
0xfdffffff 0x00000000 0xb83fffff /* pioC */
0x003fffff 0x003f8000 0x00000000 /* pioD */
>;
};
};
};
};
And Also for example:
pioD: gpio@fffffa00 {
compatible = "atmel,at91sam9x5-gpio", "atmel,at91rm9200-gpio";
reg = <0xfffffa00 0x200>;
interrupts = <3 IRQ_TYPE_LEVEL_HIGH 1>;
#gpio-cells = <2>;
gpio-controller;
#gpio-lines = <22>;
interrupt-controller;
#interrupt-cells = <2>;
clocks = <&pioCD_clk>;
};
here #gpio-lines is the same as ngpio? or at least means that D block has 0 to 21 gpios available?

what i cant find is what Mr jcarillo said
jcarrillo wrote: Hi,

With Linux versions v3.x the GPIOs are numbered as:
- PA (bank A) 0 .. 31 => GPIO_number = 0 .. 31
- PB (bank B) 0 .. 31 => GPIO_number = 32 .. 63
- PC (bank C) 0 .. 31 => GPIO_number = 64 .. 95
...

How do i find or where is this table definig the banks?
Or, if not neccesary i should just follow the schematic Soddimm Interface and use those for my scripts? Example for PD5

echo 101 > /sys/class/gpio/export # PD5 => 96 + 5 = 101 (/sys/class/gpio/pioD5 is created)

Regards and Thank you!
blue_z
Location: USA
Posts: 1676
Joined: Thu Apr 19, 2007 10:15 pm

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

Fri Nov 03, 2017 9:47 pm

You've quoted numerous DT lines (and destroyed the formatting) for what purpose?
The Device Tree is for configuring the board, and are used by the device drivers in the kernel.
Typically there is not much information in the DT that is transferable to userspace APIs, such as the sysfs interface for GPIOs.

AleFachini wrote:How do i find or where is this table definig the banks?
The pin mapping scheme described by jcarrillo is accurate for Atmel PIO (Parallel I/O). I don't know "where is this table" (since I doubt that one exists), but you're welcome to inspect the sysfs code for the mapping (since "code is the documentation"). Yet you seem to doubt the validity of this mapping for unspecified reasons.

Regards

Return to “LINUX”

Who is online

Users browsing this forum: Bing [Bot] and 1 guest