Today I did my first Linux shell script with iMX233-OLinuXino. I used to use Windows for many years, then recently moved to Ubuntu but never had to deal with shell scripting till now.
We should make GPIO testing bench to do functional test of the iMX233-OLinuXino GPIO ports, so I had to learn some new stuff.
Raivis installed so called SYSFS package in the OLinuXino Linux kernel which allow you to access the GPIOs from the linux shell so I had to read some more about Linux shell scripting to may make the board GPIO tester.
After reading about SYSFS it seems pretty easy to manipulate the GPIOs from the shell.
To make GPIO1 output and set in “1” for instance this should be executed:
echo out > /sys/class/gpio/gpio1/direction
echo 1 > /sys/class/gpio/gpio1/value
and to make GPIO2 as input and read it could be done with this script:
echo in > /sys/class/gpio/gpio2/direction
echo cat /sys/class/gpio/gpio2/value
echo will return 0 or 1 depend on GPIO2 state
All gpios can be seen with
$ ls /sys/class/gpio
So my first thing was to attach LED to PIN9 (CONNECTOR.PIN19) which correspondents to gpio0 in Linux (yes I know it’s confusing, there is one cross table and Linux kernel assign the numbers not exactly as per our schematic 😮 )
Then run VI and created my first linux script:
echo out > /sys/class/gpio/gpio0/direction
while [ 1 -eq 1 ]
do
echo 1 > /sys/class/gpio/gpio0/value
sleep 1
echo 0 > /sys/class/gpio/gpio0/value
sleep 1
done
saved it as “gpio” and made it executable with
$chmod +x gpio
then executed the script with:
$./gpio
as expected the LED start blinking with 1Hz which you can see on the video:Â http://youtu.be/4FvNwmmj_X8
Apr 23, 2012 @ 17:15:25
Very cool, good progress!
By the way, you don’t need “[ 1 -eq 1]” as the conditional expression for infinite loops, just use “true”. 🙂
Apr 23, 2012 @ 17:17:01
I wrote this is my first script. I was thinking how to make true expression 🙂
Apr 23, 2012 @ 18:03:33
Nice!
Although the LED is blinking at 0.5 Hz 🙂
Apr 23, 2012 @ 18:06:06
correct!
Apr 23, 2012 @ 22:19:47
Are you sure it should read
echo cat /sys/class/gpio/gpio2/value
I would think the correct line would be
cat /sys/class/gpio/gpio2/value
Apr 24, 2012 @ 06:40:38
He is just learning the stuff and you are right. 🙂
Apr 24, 2012 @ 07:48:16
yep copy paste mistake 🙂
Apr 23, 2012 @ 22:22:05
And I /really/ have to get me one of these little beauties 🙂
Apr 24, 2012 @ 09:03:09
I am very interested in communication via I2C, you could show me some examples? What distribution of Linux you are using?
Apr 24, 2012 @ 09:39:31
please go to Freescale web and download the L2.6.31_10.05.02_ER_docs.tar.gz documents there is described LTIB and the Linux support package, all iMX233 peripherials have implemented drivers: SPI, I2C, UARTs, Video effects, etc etc.
Apr 27, 2012 @ 07:08:14
FYI there is a unix command “true” that is more typically used for infinite loops in shell scripts. It’s just a program (or shell intrinsic) that always returns 0. So a more idiomatic infinite loop would start “while true”
Aug 02, 2012 @ 14:11:31
Nice little sample. I installed the arch linux distribution and it is running nicely. But any hints on how to work with the i2c?
Aug 02, 2012 @ 14:14:01
Please check WEB-IO https://github.com/hehopmajieh/olinuxino-web-io it controls MOD-IO connected to iMX233-OLinuXino-Maxi via UEXT connector through I2C commands.
Oct 08, 2012 @ 17:44:42
I am interrested in connecting some Leds to the gpio pins. I read your post, you do’nt wrote where is the other leg of te led ? watching your photo, it appears to be 5V or Ground. For dummies; may be you will update to indicate pin 40.
I Also note that you do’nt talk about an in line resistor (as for LED1 integarted) ?
Regards
PO.
Oct 08, 2012 @ 17:49:23
if you look at the picture the LED have red and blue wires, the red is anode and connected to OLinuXino GPIO, the blue is cathode and connected to GND through resistor 330 ohm.
hope this helps!
Oct 08, 2012 @ 18:17:14
Yes, is will! Thanks.
PO.
Jan 30, 2013 @ 23:36:15
Thanks, very helpful post. I am having this problem, though:
I attached an LED to pin18 on the board, which corresponds to gpio17 in Linux. It’s value is currently 1, so the LED is on.
When I try to set it to 0, with:
echo 0 > /sys/class/gpio/gpio17/value
the board immediately crashes (shuts down). Do you have any idea what could be the problem? Thanks!
Feb 08, 2013 @ 20:22:26
Hi everyone,
I am interested in connecting more LED in another GPIOs and I am having a hard time to figure the LINUX GPIO numbers.
I know that:
CONNECTOR PIN9 = LINUX GPIO0 = iMX233 PIN26
CONNECTOR PIN18 = LINUX GPIO17 = iMX233 PIN14
What about the others GPIOs, does anyone knows what is the correspondence between LINUX GPIO number and iMX233 pin number?
Thanks
Feb 09, 2013 @ 00:20:07
Hi
the mapping between the iMX233 pin numbers and the Linux pin numbers is described on page 41 of the Olinuxino Maxi User Manual:
Click to access iMX233-OLINUXINO-MAXI.pdf