pyA10S GPIO ports module is available at: https://pypi.python.org/pypi/pyA10S/0.1.2
Soon after we released A13-GPIO for Python http://olimex.wordpress.com/2013/03/08/a13-olinuxino-gpio-python-libraries/ we got requests to do same for iMX233.
iMX233_GPIO is released on PyPi now https://pypi.python.org/pypi/iMX233_GPIO/0.1.0
it works identical to pyA13 you can use same commands:
import iMX233_GPIO as GPIO
#init module
GPIO.init()
#configure module
GPIO.setinput(GPIO.PIN#)
GPIO.setoutput(GPIO.PIN#)
#set GPIO high
GPIO.output(GPIO.PIN#, 1)
#set GPIO low
GPIO.output(GPIO.PIN#, 0)
#read input
state = GPIO.input(GPIO.PIN#)
due to the GPIO multiplexing we add only these GPIOs in the base package:
PIN9, PIN10, PIN11, PIN12, PIN13, PIN14, PIN15, PIN16, PIN17, PIN18, PIN23, PIN24, PIN25, PIN26, PIN27, PIN28, PIN29, PIN31
you can use this as template, and if you for instance disable LCD in your linux image you can have access to all LCD pins too.
After you download the package from PyPi do as follows to install it:
#tar -zxvf iMX233_GPIO-0.1.0.tar.gz
#cd iMX233_GPIO-0.1.0.tar.gz
#sudo python setup.py install
after the installation you can use it this way:
#sudo python
>>> import iMX233_GPIO as GPIO
>>> GPIO.init() #init GPIOs
>>> GPIO.getcfg(GPIO.PIN31) #config PIN31 as GPIO
>>> GPIO.setcfg(GPIO.PIN31, GPIO.OUTPUT) #config PIN31 as output
>>> GPIO.output(GPIO.PIN31, GPIO.HIGH) #set PIN31 high
>>> GPIO.output(GPIO.PIN31, GPIO.LOW) #set PIN31 low
>>> GPIO.setcfg(GPIO.PIN31, GPIO.INPUT) #config PIN31 as input
>>> GPIO.input(GPIO.PIN31) #read PIN31
PINxx assignments is exactly as per imx233-OLinuXino-MAXI schematic
Maximum iMX233 GPIO toggle speed via Python is 15,6 kHz, which is kind of dissapointing, as /dev/mem C approach achieve about 5.7 6 Mhz, but Python is running slow on imx233 so this probably cause the slow GPIOs access.
On A13 is opposite /dev/mem is running slower at 1Mhz but pyA13 GPIO achieve up to 100 kHz as Python is running faster on A13.
Scratch is great visual environment very easy to understand by children.
I already blogged that Scratch is running fine on OLinuXino here http://olimex.wordpress.com/2013/03/12/a13-olinuxino-wifi-running-scratch-ide-perfect-platform-for-kids-programming-education/
What bothered us though was that we can’t use all GPIOs and hardware resource which OLinuXino offers and Scratch basic installation can only play music and make cool animations.
So we start searching info how to implement this.
Soon we found SNAP http://snap.berkeley.edu/ which is based BYOB (Build your own Blocks) which is based on Scratch but adds ability to make your own blocks, recursion etc new features.
SNAP is implemented with JavaScript so even better it runs on your Browser without need of installation. Based on SNAP there are few cool projects made by Technoboy10 (https://github.com/Technoboy10) interfacing Wii Nunchuk , Arduino and NXT-Lego robots.
I asked Technoboy10 is there documentation how to connect Hardware to SNAP and soon he published this info in the Wiki: https://github.com/Technoboy10/snap-server/wiki/How-to-create-a-Snap!-Extension which is very detailed.
So How cool is this? Kids are able to program robots with visual IDE!
Looking at Technoboy10 GitHub repository we figured out how to do this, but SNAP basic installation have no such great choice of sprites and animation examples, so I decided for the beginning to try to implement OLinuXino hardware features to SCratch.
So I posted question on Scratch forum and start to wait for reply http://scratch.mit.edu/forums/viewtopic.php?id=115821 . After few days waiting, 30+ views and none reply I did what I had to do at the beginning: searched github for “scratch c” then for “scratch python” and found what we needed:
https://github.com/pilliq/scratchpy.
This is cool python client for Scratch. When you enable remote sensors in Scratch it creates server, then with scratchpy you can pool for given variables and take their values or change them, this way you can connect sensors or actuators.
As opposite to SNAP (where SNAP i client and snap-server is made with pyton, so SNAP connects to the server) here Scratch makes server and the scratchpy client connects to it and interact through variables.
So here is our first “Hello World” blinking LED with Scratch: http://www.youtube.com/watch?v=qbTNWTa5tXQ
Instruction how to install the OLinuXino GPIO interface to Scratch is on GitHub: https://github.com/OLIMEX/OLINUXINO/tree/master/SOFTWARE/A13/A13-GPIO%2BSCRATCH
Our second project animate GPIO connector with Scratch based on real buttons inputs and LED outputs:
Here is video on the GPIO interaction with SCRATCH http://www.youtube.com/watch?v=DzmvqlQodac
Now when we have this powerful tool: scratchpy to interface Scratch to anything via python, just imagine what would happend if we connect Scratch to … OpenCV
From my previous blog I show you some videos which demonstrate the power of OpenCV: face recognition, face expression recognition, color objects tracking. You can make SCRATCH script for instance which recognize the face of the person in the fron of the web cam and run some animation to welcome it by name.
Or you can program your robot to chase this yellow pong ball which is in the front of it.
Or you can make different animations depend on the face expression of the person in the front of the camera… the applications and the fun will be endless!
Python is one of the most popular languages in the last years, it’s used by Google for their search engine and many other apps like game designs, databases etc.
Python combines the easy of use of the interpretive languages with powerful datastructures, so it replaces the old days BASIC but opens up lot of new possibilities.
If you want to learn more about Python you can read http://www.learnstreet.com/lessons/study/python or the official Python language web site http://python.org/
The best of all are the Python libraries you can see thousands of these at https://pypi.python.org/pypi
A13-OLinuXino as any Linux platform can run Python language, what we were missing was GPIO library which to allow Python users to have access to A13-OLinuXino GPIOs.
Now this library is done! You can download is from https://pypi.python.org/pypi/pyA13/ or from GitHub: https://github.com/OLIMEX/OLINUXINO/tree/master/SOFTWARE/A13/PYTHON-A13-GPIO
then install it:
#tar -zxvf pyA13-0.1.7.tar.gz #cd pyA13-0.1.7.tar.gz #sudo python setup.py install
after the installation you can use it this way:
#sudo python >>> import A13_GPIO as GPIO >>> GPIO.init() #init GPIOs >>> GPIO.getcfg(GPIO.PIN37) #config PIN37 as GPIO >>> GPIO.setcfg(GPIO.PIN37, GPIO.OUTPUT) #config PIN37 as output >>> GPIO.output(GPIO.PIN37, GPIO.HIGH) #set PIN37 high >>> GPIO.output(GPIO.PIN37, GPIO.LOW) #set PIN37 low >>> GPIO.setcfg(GPIO.PIN37, GPIO.INPUT) #config PIN37 as input >>> GPIO.input(GPIO.PIN37) #read PIN37
PINxx assignments is exactly as per A13-OLinuXino schematic
EDIT: Maximum GPIO toggle speed via Python is 101 kHz
RPI-UEXT allow Raspberry Pi to have access to different modules see original blog post we made about it: http://olimex.wordpress.com/2012/11/21/raspberry-pi-gpio-to-breadboard-and-uext-adapter/
MOD-IO2 allow RaspberryPi to control 2 relays and to read/write 7 additional GPIOs, including Analog inputs. On top of this MOD-IO2 is stackable and addressable so you can stack and connect to Raspberry Pi as many relays as you need for your project.
You have chance to win RPI-UEXT + CABLE26-pin + MOD-IO2 today if you answer correctly our quiz question!
Today at 17.00 o’clock our local Bulgarian time (GMT+2) we will post on Twitter our questions.
You have one hour to reply to our tweet with the correct answer.
At 18.00 o’clock we will count the correct answers and ask random.org to generate random number in range then announce the winner and ship the board by airmail in Monday.
Good Luck!
In the previous post I explained how to setup web server on iMX233-OLinuXino-MAXI now will explain how to control iMX233-OLinuXino resources through the web.
Clone WEB-IO from GitHub:
# mkdir web-io
# cd web-io
# git clone https://github.com/OLIMEX/olinuxino-web-io.git
Copy gpio into /etc/rc.d/
# cp -vf web-io/init/gpio /etc/rc.d/
Start the script
# /etc/rc.c/gpio start
Change the permission of i2c-o
# chmod 666 /dev/i2c-0
Copy web folder into /srv/http/
# cp -rvf web-io/web/io /srv/http/
Go to your browser and enter the IP-address of the board: 192.168.0.1/io/menu_1.html
you will see the GPIOs web page and with mouse click you can change GPIO state
Yesterday we got yet another amazing project by our friend Dimitar Gamishev.
He was playing with A13-OLinuXino-WIFI and decided to learn how to write Android applications, but he didn’t stop with “Hello world”
What he decided to try is to interface low level resources to his Android application like to implement I2C communication and control of MOD-IO connected to A13-OLinuXino UEXT connector.
The result is MODIO.apk which you can download from: https://github.com/OLIMEX/OLINUXINO/tree/master/SOFTWARE/A13/ANDROID-MOD-IO
You can see this application running in this video: http://www.youtube.com/watch?v=fBydZn8FylE
With it you can switch on/off relays on MOD-IO and read the Input status and ADC analog values (the trimmer potentiometer on the picture).
The sources are at GitHub: https://github.com/hehopmajieh/modio_android, what he did was to use Android NDK to implement the I2C communication in C then to export it and make it public for use by the Android SDK java applications. As you can see the result is Android APK which you can run and it communicates with MOD-IO connected to UEXT.
Using the same approach all GPIOs, SPIs, I2C and etc resources on A13-OLinuXino can be made available for native Android SDK applications and you can intereface real world hardware with pure Android application code.
Today I will show you how to access A13-OLinuXino GPIOs.
First make sure you have installed the GPIO drivers by
# lsmod
you will see the list of the installed kernel modules and if “sun4i_gpio” is there everything is OK
If you got A13-OLinuXino-SD with the pre-installed Debian everything is already setup for you, if not you can follow the instructions of this post and make one SD card yourself: http://olimex.wordpress.com/2012/10/12/building-bootable-sd-card-with-debian-linux-image-for-a13-olinuxino/ do not forget when compile the kernel to enable SUN4I_GPIO_UGLY = y inside .config , why UGLY? you will understand soon
Allwinner do not care too much for the GPIOs as they primary support Android and their major market is the tablet manufacturers. This SUN4I-GPIO is made by Tom Cubie who work at Allwinner and made available to community, this driver though is not so flexible as the sysfs and you can’t switch between input and output easily.
With this driver you permanently assign one port to be input or output in the script.bin and when this gpio kernel module is loading it reads from script.bin the assignments and make them permanently, so if you want to change some GPIO to input or output you have to unload the driver make change in the script.bin and load it again and this is ugly
Anyway with the card we ship we made these gpios defined:
pe4, pe5, pe6, pe7: inputs
pe8, pe9, pg10, pg11, pg9, pb10, pb3, pb4, pe10, pe11: outputs
pg9 is GPIO output with the green status LED connected to it
to switch LED on you can do:
# echo 1 > /sys/devices/virtual/misc/sun4i-gpio/pin/pg9
and to switch LED off you can do:
# echo 0 > /sys/devices/virtual/misc/sun4i-gpio/pin/pg9
Now let’s see how fast we can toggle LED from shell script:
# vi gpio
and enter this code:
while [ true]
do
echo 1 > /sys/devices/virtual/misc/sun4i-gpio/pin/pg9
echo 0 > /sys/devices/virtual/misc/sun4i-gpio/pin/pg9
done
# chmod +x gpio
#./gpio
with the oscilloscope is seen square wave with 220 uS period i.e. 45 454 Hz! this is quite more fast than the 300Hz we achieved on iMX233 GPIOs from shell http://olimex.wordpress.com/2012/09/07/imx233-olinuxino-working-with-gpios/
now let’s try same in C:
# vi gpioc.c
#include <stdio.h>
#include <fcntl.h>
#include <string.h>int main() {
char s_0[] = “0″;
char s_1[] = “1″;
int fd;//toggle pg9 LED gpio
if((fd=open(“/sys/devices/virtual/misc/sun4i-gpio/pin/pg9″,O_RDWR))<0) return(1);
else {while(1) {
if(write(fd,s_1,strlen(s_1))<0) return(1);
if(write(fd,s_0,strlen(s_0))<0) return(1);
};if(close(fd)<0) return(1);
}
}
# cc gpioc.c -o gpioc
# chmod +x gpioc
# ./gpioc
the oscilloscope show 4.2 uS period square wave i.e. about 238 095Hz not so fast than iMX233 140kHz. But obviously if we chase speed should not use file system.
Now next step is to make access via /dev/mem as mmgpio with iMX233 http://olimex.wordpress.com/2012/09/11/imx233-olinuxino-gpios-faster-and-faster/
Using directly /dev/mem iMX233 achieved up to 5.7 Mhz GPIO toggle, I do wonder how fast we can go with A13, but this will be done in my next blog about it.
In my previous post http://olimex.wordpress.com/2012/09/07/imx233-olinuxino-working-with-gpios/ I experimented with GPIOs trying to see how fast they could be changed and had in my TODO list to access them via /dev/mem directly writing to the registers, David Whittaker made this before me
: http://tech.groups.yahoo.com/group/olinuxino/message/2022
He made this wonderful tool gpio-mmap.h where he define several useful functions:
gpio_map(); // this function should be called to initialize and map the GPIOs to the /dev/mem
gpio_output( bank, pin); // initialize GPIO as output you can see which GPIO correspond to bank/pin in IMX233 user manual, I prepare cross reference for all GPIOs which will post soon here on the blog
GPIO_WRITE_PIN(pin,value); //this writes to the GPIO pin#
the quick code I did you try gpio-mmap.h tool was:
#include “gpio-mmap.h”
int main() {
gpio_map();
gpio_output(2,1); //bank 2 bit 1 = GPIO65 the LED on board
while (1) {
GPIO_WRITE_PIN(65,1);
GPIO_WRITE_PIN(65,0);
}
}
the oscolloscope connected to the GREEN LED show nice square with 175 nS period, i.e. the direct memory register write can achieve as high as 5.7 MHz wow
note that this code was running without other codes to interrupt it, in Linux which is multitasking OS you can run many processes, for instance if I add in background the led_blink script from the previous blog post, I can see with the oscilloscope LED glitch on ever second when the scrip toggle the LED, i.e. now I have 5.7Mhz frequency distrubed from time to time with the 1Hz led_blink script, so the more processed you run on iMX233 the less determinant will be this frequency and will slow down, this is like on your desktop when you run video for instance and start several other tasks like opening open office or copy some big file on the disk the video may freeze for a second when the video buffer goes empty and other tasks keep the CPU busy as this moment.
Nevermind gpio-mmap.h is one very useful tool, now we can implement software SPI with bit-banging as SPI is broken in imx233 kernel 2.6.35 and 5Mhz is pretty decent clock speed for most of the tasks, so we can now make some good code examples for our SPI UEXT modules.
You can download gpio-mmap.h source from GitHub: https://github.com/OLIMEX/OLINUXINO/tree/master/SOFTWARE/iMX233/gpio-mmap.h
I already blogged how to blink LED connected to OLinuXino GPIO through the shell script here http://olimex.wordpress.com/2012/04/23/blinking-led-with-linux-or-hello-world-with-imx233-olinuxino/
today I did experiement to see what is the fastest GPIO toggel I can achieve with this method and this code:
echo out > /sys/class/gpio/gpio0/direction
while [ 1 -eq 1 ]
do
echo 1 > /sys/class/gpio/gpio0/value
echo 0 > /sys/class/gpio/gpio0/value
done
makes nice 400Hz square wave on oscilloscope this means 1.25mS to set GPIO high and 1.25mS to set GPIO low.
Quite OK if you do some slow IO processes like switching ON OFF relays etc but slow if you want to do something faster, so I decided to see what will happend if I make same code in C.
For this purpose I decided to use the Green LED next to PWR jack which is connected to GPIO65:
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
int main()
{
char s_out[] = “out”;
char s_0[] = “0″;
char s_1[] = “1″;
int fd;
//define GPIO65 as output
if((fd=open(“/sys/class/gpio/gpio65/direction”,O_RDWR))<0)
return(1);
else {if(write(fd,s_out,strlen(s_out))<0)
return(1);
printf(“GPIO65 set as output\r\n”);
if(close(fd)<0)
return(1);
}
//toggle GPIO65
if((fd=open(“/sys/class/gpio/gpio65/value”,O_RDWR))<0)
return(1);
else {
while(1) {
if(write(fd,s_1,strlen(s_1))<0)
return(1);
if(write(fd,s_0,strlen(s_0))<0)
return(1);
};
if(close(fd)<0)
return(1);
}}
This code improved the speed significant now I can see 140 kHz on GPIO65 with 3.5 uS high and 3.5 uS low time.
Although about 350 times faster than shell script this still seems slow to me, there is another approach to access the GPIOs under Linux and this is through “/dev/mem” writing directly in memory and iMX233 registers.
So what would be the speed if we write directly to the memory/registers? There is nice documentation with about 50 pages explaining how to set GPIOs
I may find a time during the weekend to read and try to implement it.