thickwick
Posts: 2
Joined: Tue Feb 07, 2012 7:24 pm

Re: GPIO Programming Question

Tue Feb 07, 2012 7:33 pm

Hello!

I'm a beginer to embeded programming and electronics, and I have some different ideas of things I would like to do on the raspberry Pi. My understanding of the gpio pins, is that they can be controlled very much like an Arduino's pins and turned on and off?

My other question is what languages support GPIO on this debian environment? It would be my preference to find a Java or JVM based language that can make reads and writes to these pins.

Thanks!

davidgoodenough
Posts: 74
Joined: Wed Sep 21, 2011 11:55 am

Re: GPIO Programming Question

Tue Feb 07, 2012 7:55 pm

Not a problem, linux exports gpio pins as files under /sys/class/gpio, you can open them using normal file IO from Java and away you go.

User avatar
johnbeetem
Posts: 945
Joined: Mon Oct 17, 2011 11:18 pm
Location: The Mountains
Contact: Website

Re: GPIO Programming Question

Tue Feb 07, 2012 8:06 pm

For those who prefer programming down at the bare metal, it may be possible to mmap the GPIO registers and play with them directly.  It's going to be one of the first things I try when I get a RasPi, now that we have a datasheet with GPIO addresses.  There's a thread about various ways to access the BeagleBoard's similar GPIOs at the BeagleBoard community FAQ: http://elinux.org/BeagleBoardF....._GPIO_pins

thickwick
Posts: 2
Joined: Tue Feb 07, 2012 7:24 pm

Re: GPIO Programming Question

Tue Feb 07, 2012 8:25 pm

davidgoodenough said:


Not a problem, linux exports gpio pins as files under /sys/class/gpio, you can open them using normal file IO from Java and away you go.


Will each individual gpio pin be a separate file in the /sys/class/gpio folder? Also how does flushing these pins work? As soon as you close the handle does the information get transmitted to the pin?

User avatar
Gert van Loo
Posts: 2487
Joined: Tue Aug 02, 2011 7:27 am
Contact: Website

Re: GPIO Programming Question

Tue Feb 07, 2012 9:16 pm

Hummm… can't seem to find the front page gertboard video posts anymore. That uses them and somebody extracted the text from the screen (I have not got the text here).

Every 'putch' immediately effects the port as the I/O for those 'devices' is not buffered. So no need to flush.

Post edit: Found the code!

#include <stdio.h>

#define TIME 100000

int gpiotbl[17] = {0,1,4,7,8,9,10,11,14,15,17,18,21,22,23,24,25};

void test_motor();

int main()

{ int g,rep;

char string[128];

// Make the 17 GPIO file systems & set them to output mode

for (g=0; g<17; g++)

{

sprintf(string,"echo %d > /sys/class/gpio/export" ,gpiotbl[g]);

system(string);

sprintf(string,"echo out > /sys/class/gpio/gpio%d/direction",gpiotbl[g]);

system(string);

}

// light effect on buffers

for (rep=0; rep<5; rep++)

{

for (g=0; g<12; g++)

{

sprintf(string,"echo 1 > /sys/class/gpio/gpio%d/value",gpiotbl[g]);

system(string);

usleep(TIME);

}

for (g=0; g<12; g++)

{

sprintf(string,"echo 0 > /sys/class/gpio/gpio%d/value",gpiotbl[g]);

system(string);

usleep(TIME);

}

}

test_motor();

test_motor();

return 0;

} // main

void test_motor()

{ int mota,motb;

char string[128];

mota = gpiotbl[12];

motb = gpiotbl[13];

sprintf(string,"echo 1 > /sys/class/gpio/gpio%d/value",mota);

system(string);

sprintf(string,"echo 0 > /sys/class/gpio/gpio%d/value",motb);

system(string);

sleep(7);

sprintf(string,"echo 0 > /sys/class/gpio/gpio%d/value",mota);

system(string);

usleep(200000);

sprintf(string,"echo 1 > /sys/class/gpio/gpio%d/value",motb);

system(string);

sleep(7);

sprintf(string,"echo 0 > /sys/class/gpio/gpio%d/value",motb);

system(string);

usleep(200000);

} // test_motor

User avatar
ukscone
Forum Moderator
Forum Moderator
Posts: 4260
Joined: Fri Jul 29, 2011 2:51 pm
Contact: Website

Re: GPIO Programming Question

Tue Feb 07, 2012 9:24 pm

John Beetem said:


For those who prefer programming down at the bare metal, it may be possible to mmap the GPIO registers and play with them directly.  It's going to be one of the first things I try when I get a RasPi, now that we have a datasheet with GPIO addresses.  There's a thread about various ways to access the BeagleBoard's similar GPIOs at the BeagleBoard community FAQ: http://elinux.org/BeagleBoardF....._GPIO_pins


i built devmem2 for the raspi earlier today just to check that things work as expected and everything was fine (on a real raspi -- not mine just an ssh session) so i'll be mmap()'ing for my gpio accessing

User avatar
meltwater
Posts: 1015
Joined: Tue Oct 18, 2011 11:38 am

Re: GPIO Programming Question

Wed Feb 08, 2012 10:13 am

@ukscone

Any details for us novice bods would be welcome, as and when you have some available.

I hope to get some example code up in my Easy GPIO tutorial on the wiki (even if it requires tweaking once tested on a RPi, it should give an idea of how it works) when I've got the basics of the hardware together (building up a selection of little test modules at the moment).

At the moment I have Gerts LED code which is fine, some details on reading inputs if available will be handy (yet to look into that bit yet).
______________
http://www.themagpi.com/
A Magazine for Raspberry Pi Users
Read Online or Download for Free.

My new book: goo.gl/dmVtsc

Meltwater's Pi Hardware - pihardware.com

Like the MagPi? @TheMagP1 @TheMagPiTeam

User avatar
ukscone
Forum Moderator
Forum Moderator
Posts: 4260
Joined: Fri Jul 29, 2011 2:51 pm
Contact: Website

Re: GPIO Programming Question

Wed Feb 08, 2012 4:56 pm

@meltwater devmem2 is a handy little thing and used quite often in embedded devices (especially arm based). basically if you have rw access to /dev/mem (you are root) you can read/write to any memory location within the cpu's address space. as the gpio in the bcm2835 are in registers in known locations you can fiddle with them using devmem2.

I have another program (i ripped about 80% of the code from devmem2, pxaregs, gpio-val and gpio-dir) that lets you do the same thing but uses the register names rather than memory location that I am currently modding using the info from the datasheet.

you can find the devmem2 manpage here http://man.cx/devmem2(1)

and my binary here http://russelldavis.org/Raspbe.....Pi/devmem2

and http://russelldavis.org/Raspbe.....em2.static

built from this source http://russelldavis.org/Raspbe...../devmem2.c

obviously most people will just use system() and the sysfs files but some might prefer the mmap() way especially if the gpio isn't exposed in /sys or they also want to do something to a non-gpio memory location

User avatar
meltwater
Posts: 1015
Joined: Tue Oct 18, 2011 11:38 am

Re: GPIO Programming Question

Wed Feb 08, 2012 9:14 pm

Thank you!  Handy tool.

Not that I ever knew how to use them back in the day, but sounds like PEEK and POKE.

To me it sounds a more natural way to access the GPIO, the file read/write Linux side is new to me, but will see how it pans out when it comes to using it.
______________
http://www.themagpi.com/
A Magazine for Raspberry Pi Users
Read Online or Download for Free.

My new book: goo.gl/dmVtsc

Meltwater's Pi Hardware - pihardware.com

Like the MagPi? @TheMagP1 @TheMagPiTeam

User avatar
croston
Posts: 716
Joined: Sat Nov 26, 2011 12:33 pm
Location: Blackpool
Contact: Website

Re: GPIO Programming Question

Wed Feb 08, 2012 9:27 pm

I have written a Python module for GPIO (announced in another thread).  It is under a MIT licence.  Note that I don't think it will be suitable for time-critical or very fast switching - the lower level methods mentioned earlier in this thread are more suitable in these cases.

To save you from doing a search of the forum, check:

http://code.google.com/p/raspb.....io-python/

or

http://pypi.python.org/pypi/RPi.GPIO/

The examples there are about as simple to understand and use as it is possible to get!

User avatar
meltwater
Posts: 1015
Joined: Tue Oct 18, 2011 11:38 am

Re: GPIO Programming Question

Wed Feb 08, 2012 9:45 pm

Yep I'd booked marked and fav'd it on google code already...

Great that I/O will be do able from day one.

I probably won't try to write much RPi code until I have one to test with, since I'm not too familiar with it (currently building test circuits and testing with a launchpad kit instead - hopefully electrically not too dis-similar).
______________
http://www.themagpi.com/
A Magazine for Raspberry Pi Users
Read Online or Download for Free.

My new book: goo.gl/dmVtsc

Meltwater's Pi Hardware - pihardware.com

Like the MagPi? @TheMagP1 @TheMagPiTeam

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4257
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: GPIO Programming Question

Wed Feb 08, 2012 11:01 pm

ukscone said:


obviously most people will just use system()


I'm curious. Why use system(3) which has to fork a shell process, when you could just open the file with open(2), write a value and either flush or close the file? Is there some deep reason I'm not understanding?

8+5, oh dear.

User avatar
jojopi
Posts: 3354
Joined: Tue Oct 11, 2011 8:38 pm

Re: GPIO Programming Question

Thu Feb 09, 2012 4:21 am

rurwin said:

I'm curious. Why use system(3) which has to fork a shell process, when you could just open the file with open(2), write a value and either flush or close the file? Is there some deep reason I'm not understanding?
You really would not use system(3) unless you planned to port to shell in the future, or had just started porting from shell.  Or it was a didactic example where you had already omitted such niceties as error checking, and simply wished to make the code as short and easy as possible.

Incidentally, there is no concept of flushing the file if you used open(2).  write(2) takes effect immediately.  You should not need to close(2) or lseek(2) between writes either, so each change is exactly one syscall.

roli
Posts: 13
Joined: Mon Feb 13, 2012 12:54 pm
Contact: Website

Re: GPIO Programming Question

Mon Feb 13, 2012 1:05 pm

I like the python module it should be very useful. I need something to replace the Arduino (something that has more memory and more power) and RPi looks like a very good choice. And I like it that somebody supported python. I don't mind Java but for rapid development Python is a much nicer choice. But I have a small question about that "timing critical operations" warning. What is considered to be timing critical operation?

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4257
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: GPIO Programming Question

Mon Feb 13, 2012 1:31 pm

Linux is complex operating system and a lot is going on inside it. As a result the operations you carry out may be delayed. It is difficult to know exactly what the effect of that will be without having tried it, but I would guess that 100ms accuracy should be achievable without too much difficulty and that 1ms accuracy would require you to be careful.

User avatar
croston
Posts: 716
Joined: Sat Nov 26, 2011 12:33 pm
Location: Blackpool
Contact: Website

Re: GPIO Programming Question

Mon Feb 13, 2012 6:01 pm

roli said:


I like the python module it should be very useful. I need something to replace the Arduino (something that has more memory and more power) and RPi looks like a very good choice. And I like it that somebody supported python. I don't mind Java but for rapid development Python is a much nicer choice. But I have a small question about that "timing critical operations" warning. What is considered to be timing critical operation?



'Timing critical' would be when you are concerned about the switching speed (real-time applications).  For example, if you needed a pulse exactly 10ms long once a second.  As Linux is a multitasking operating system, the CPU could be held at 100% by other processes out of your control and your Python code would not be able to switch at the correct time.

lb
Posts: 276
Joined: Sat Jan 28, 2012 8:07 pm

Re: GPIO Programming Question

Mon Feb 13, 2012 7:04 pm

Python is completely unsuitable for anything that requires predictable timing. Use C and avoid dynamic memory allocations for better results.

However, if you have hard realtime requirements, the RPi is no good at all. Even with Linux RT extensions (which aren't ported at this time, I suppose) you'll see 50 µs or so jitter/latency. A simple 8-bit MCU (like Arduino) on the other hand can easily achieve sub-µs latency, and can do frequency generation and PWM that is accurate to ns!

So the way to go for realtime applications is to use an MCU for them, and interface to RPi through I2C or SPI.

roli
Posts: 13
Joined: Mon Feb 13, 2012 12:54 pm
Contact: Website

Re: GPIO Programming Question

Mon Feb 13, 2012 7:50 pm

Interesting. That actually never crossed my mind. Somehow you would expect an operating system like linux would be capable of real time applications. But if I understand the whole thing correctly, the Gertboard will provide the option of timing critical applications (at least it seems logical since it has a PIC onboard). But to be honest, I am not sure yet how you will program the Gertboard (and its onboard PIC) and RPi.

What I need is something that can be used for real time applications and can be programmed with high level programming language (preferably Python or Java or Objective C - I probabably won't be seeing that in an non-Apple device soon).

I want to redo my home automation project ( http://r00li.com/rolihome ) so I won't need an external computer for the UI (small touch screen would be very nice) and web server. I needed two arduinos for my project (well two MCUs - not two actual arduinos) plus a computer (well the device can work as a standalone controller). With this the application is split into the MCU part and the computer part which is not nice and I want something that can do everything - the IO and high level stuff like the web server. What I want to do is get rid of the middleman (the Arduino) and controll the IO ports directly. And the RPi seems like a good choice for that but I don't want to get to the low level development. I have worked with ARMv4 assembler before but I am not realy a fan of low level development.

So is something like this doable with RPi, or should I be looking for a ARM development board that can be programmed in C or even .NET?

Themroc
Posts: 44
Joined: Fri Jan 06, 2012 9:35 am

Re: GPIO Programming Question

Mon Feb 13, 2012 9:17 pm

roli said:


Interesting. That actually never crossed my mind. Somehow you would expect an operating system like linux would be capable of real time applications.


It depends... back in the 2.4 times, they were struggling to make it fit for "multimedia". With some patches, they achieved a latency of 1ms IIRC - with one exception: switching virtual consoles spoiled it. So nowadays, a semi-relieable 1ms shouldn't be too hard to get - if that satisfies your need, then linux is real time capable for you.

roli said:


What I need is something that can be used for real time applications and can be programmed with high level programming language (preferably Python or Java or Objective C - I probabably won't be seeing that in an non-Apple device soon).


There is at least some RT support for Java:

http://en.wikipedia.org/wiki/R....._time_Java

kasperl
Posts: 90
Joined: Fri Jan 06, 2012 6:20 pm

Re: GPIO Programming Question

Mon Feb 13, 2012 9:21 pm

Depends on how real-time you need. If you're bit-banging a PWM for something (turning a pulse on and off in a loop written in your software to control an average value) there's no way anything written in Python (or Java) will be accurate enough and still respond to web requests. However, if you are simply setting the PWM value on an analogue output by turning the right GPIO's on and off, or sending a serial byte, you should be fine.

I have no experience with the real-time Linux kernel, so you might have some luck there. For home automation, you should be fine though, but you might need some external DA (digital->analog) converters or PWM (pulse width modulators).

roli
Posts: 13
Joined: Mon Feb 13, 2012 12:54 pm
Contact: Website

Re: GPIO Programming Question

Mon Feb 13, 2012 9:50 pm

Well I did a bit of thinking. I could still use the Arduino as an IO bridge (use it to connect to actual hardware and sensors) but do all the processing on the RPi. This is one option but I don't realy like it since I still need an Atmega chip with arduino bootloader, USB to serial adapter (or an FTDI chip) and special code written for the arduino. But I don't see any other way since I need an option of generating IR pulses (microsecond acuracy is a must) and responding to interrupts at the same time. Arduino can handle this quite nicely. But I needed another arduino that controlled the character display, worked with relays, received commands from an IR remote, did al the processing and sent the information to the computer. It is interesting that a device with a 16MHz clock was more than capable of doing that, while a much faster device like RPi isn't.

Themroc
Posts: 44
Joined: Fri Jan 06, 2012 9:35 am

Re: GPIO Programming Question

Mon Feb 13, 2012 10:16 pm

roli said:


But I don't see any other way since I need an option of generating IR pulses (microsecond acuracy is a must) and responding to interrupts at the same time.


If you need only one such output and they can be programmed for 1MHz clock (too tired to look it up now...), maybe you could abuse the SPI, I2C or UART.

User avatar
Gert van Loo
Posts: 2487
Joined: Tue Aug 02, 2011 7:27 am
Contact: Website

Re: GPIO Programming Question

Mon Feb 13, 2012 10:50 pm


It is interesting that a device with a 16MHz clock was more than capable of doing that, while a much faster device like RPi isn't.


You can take a 3GHz X86 processor and still find it is not capable of doing that. It is all a matter of processor load, timing, interrupts, interrupt processing, priorities and what else you want to processor to do. That is why, for example, we as HW engineers add all kind of interfaces in hardware, preferable with DMA support or big FIFO's. Unfortunately the BCM2835 does not have IR hardware support. It is interesting to see the demand for that. If I would have know that I would have added to 100-200 gates to support that. As Themroc says: for IR transmit you might mis-use the SPI stream for that.

roli
Posts: 13
Joined: Mon Feb 13, 2012 12:54 pm
Contact: Website

Re: GPIO Programming Question

Tue Feb 14, 2012 1:31 am

I know that the problem isn't the HW itself. After all computers use all sorts of controllers for standard IO so that the CPU can do more important work and not be bothered by I don't know talking to the hard drive. Proabably the only way to achieve this on a RPi would be to ditch the linux and programm the whole thing from the start. Or simply get an MCU which is made for stuff like this.

Well I kind of got over that after I thought about it for a little. I see that you can connect a DSI display to it which is great – at least what I got from this topic: http://www.raspberrypi.org/for.....lcd/page-3 . And I can still use the arduino for IO. It is not as good as I would want but it is kind of nice. And with a small touch display and a little custom application it should be more than suitable for a smart home controller and that coupled with the XBMC running on the HTPC could do all sorts of magic. If I could just get some free time and money.

Hmmm I have been thinking – could you connect the arduino (I mean the actual MCU not the board) to the RPi directly? Without USB of course? And speed isn't an issue here. Something like a standard character serial connection. Which is easy to read on arduino and in any other high level programming language.

hzrnbgy
Posts: 106
Joined: Mon Dec 26, 2011 10:55 pm

Re: GPIO Programming Question

Tue Feb 14, 2012 3:18 am

The only problem is that the typical Arduino mcu (AVR atmega) runs at 5V but the RPi at 3V3. You would need a level converter. But yes, you can connect them via serial, SPI, i2c, bit banged parallel bus, etc. It all depends on how you intend to make the two communicate, master-slave, master-master, command-response, etc.

There are however Atmel chips that can run at 3V3 and are more powerful than the typical Arduino chips. They are called xmega but they are programmed differently via a specialized tool. Though, you probably can download a suitable bootloader online and use serial to reprogram it.

Return to “General discussion”