User avatar
fulburncams
Posts: 72
Joined: Wed Nov 30, 2011 8:29 pm

GPIO: Register Addressing

Wed May 16, 2012 12:25 pm

Hi
I am struggling to understand how to write to the GPIO Hardware. Currently I have constructed a board with UART and GPIO Leds, which is all working fine using shell commands and a hacked version of Gerts software listing.
My intial question is that in the BCM2835 spec the registers for the GPIO bits is defined as 41 registers starting at 0x7e200000, whilst in the wiki example code the addresses are in ranges 2000000x e.g
#define BCM2708_PERI_BASE 0x20000000
#define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) /* GPIO controller */


Can I not just address memory direct as per the BSM2835 spec , although I am not sure how to do this in 'C' as I am currently a novice and trying to understand what the malloc and mmap code is doing in Gerts example is beyond me. Anyone else currently experimenting with the GPIO.
Many thanks
Steve

User avatar
gordon@drogon.net
Posts: 2023
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website Twitter

Re: GPIO: Register Addressing

Wed May 16, 2012 1:38 pm

I have an alternative way to drive the GPIO - it's not to everyones tastes, however it does work and it's in C and it's a library you can use - You can get it from:
https://projects.drogon.net/raspberry-pi/wiringpi/
if interested.

As for the base address difference - read page 6 of the peripherals manual and all will be clear...

Gordon
--
Gordons projects: https://projects.drogon.net/

User avatar
fulburncams
Posts: 72
Joined: Wed Nov 30, 2011 8:29 pm

Re: GPIO: Register Addressing

Wed May 16, 2012 2:06 pm

Gordon
Thanks, I missed page 6 , apologies. I will try with your wiringPi code and see how i get on.
Cheers
Steve

User avatar
gordon@drogon.net
Posts: 2023
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website Twitter

Re: GPIO: Register Addressing

Wed May 16, 2012 2:16 pm

fulburncams wrote:Gordon
Thanks, I missed page 6 , apologies. I will try with your wiringPi code and see how i get on.
Cheers
Steve
It took me a couple of readings too to find it!
I've had lots of fun with just the GPIO - not ventured into the other stuff yet - i2s/spi. More LEDs than I can shake a stick at:
Image

Gordon
--
Gordons projects: https://projects.drogon.net/

mabrowning
Posts: 18
Joined: Mon May 14, 2012 4:14 pm

Re: GPIO: Register Addressing

Wed May 16, 2012 3:59 pm

fulburncams wrote: My intial question is that in the BCM2835 spec the registers for the GPIO bits is defined as 41 registers starting at 0x7e200000, whilst in the wiki example code the addresses are in ranges 2000000x
I struggled with this recently as well, until I read a paragraph 1.2.3:
BCM2835 ¶1.2.3 wrote: Physical addresses range from 0x20000000 to 0x20FFFFFF for peripherals. The bus
addresses for peripherals are set up to map onto the peripheral bus address range starting at
0x7E000000. Thus a peripheral advertised here at bus address 0x7Ennnnnn is available at
physical address 0x20nnnnnn.

User avatar
fulburncams
Posts: 72
Joined: Wed Nov 30, 2011 8:29 pm

Re: GPIO: Register Addressing

Wed May 16, 2012 4:05 pm

Gordon

I copied the code to my Raspi.

I managed to compile the wiringPi code under a directory wiringcode and that created files called libwiringPi.a and wiringPi.o using make command.

Then i tried compiling the gpio code under the gpio folder using the make command and got the following

[CC] gpio.c
gpio.c:10:22: error: wiringPi.h: No such file or directory
gpio.c: In function ‘doMode’:
gpio.c:41: error: ‘NUM_PINS’ undeclared (first use in this function)
gpio.c:41: error: (Each undeclared identifier is reported only once
gpio.c:41: error: for each function it appears in.)
gpio.c:51: warning: implicit declaration of function ‘pinMode’
gpio.c:51: error: ‘INPUT’ undeclared (first use in this function)
gpio.c:53: error: ‘OUTPUT’ undeclared (first use in this function)
gpio.c:55: error: ‘PWM_OUTPUT’ undeclared (first use in this function)
gpio.c: In function ‘doWrite’:
gpio.c:84: error: ‘NUM_PINS’ undeclared (first use in this function)
gpio.c:94: warning: implicit declaration of function ‘digitalWrite’
gpio.c:94: error: ‘LOW’ undeclared (first use in this function)
gpio.c:96: error: ‘HIGH’ undeclared (first use in this function)
gpio.c: In function ‘doRead’:
gpio.c:115: error: ‘NUM_PINS’ undeclared (first use in this function)
gpio.c:121: warning: implicit declaration of function ‘digitalRead’
gpio.c: In function ‘doPwm’:
gpio.c:146: error: ‘NUM_PINS’ undeclared (first use in this function)
gpio.c:155: warning: implicit declaration of function ‘pwmWrite’
gpio.c: In function ‘main’:
gpio.c:167: warning: implicit declaration of function ‘wiringPiSetup’
make: *** [gpio.o] Error 1


I also had to copy the wiringPi.h file to this directory or else i got cannot find file message.

I assume I am doing something incorrect, how should i build this. Many thanks
Steve

User avatar
gordon@drogon.net
Posts: 2023
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website Twitter

Re: GPIO: Register Addressing

Wed May 16, 2012 4:11 pm

fulburncams wrote:Gordon

I copied the code to my Raspi.

I managed to compile the wiringPi code under a directory wiringcode and that created files called libwiringPi.a and wiringPi.o using make command.
cd wiringPi
make
then
sudo make install

That will copy wiringPi.h into /usr/local/include and libwiringPi.a into /usr/local/lib

If you check the Makefile in the examples and gpio directorys they're referenced from there.

Gordon
--
Gordons projects: https://projects.drogon.net/

User avatar
fulburncams
Posts: 72
Joined: Wed Nov 30, 2011 8:29 pm

Re: GPIO: Register Addressing

Wed May 16, 2012 4:18 pm

Realised my mistake, now moved wiringPi.h to right place.. but now hit following problem

[CC] gpio.c
[link]
gcc -o gpio gpio.o -L/usr/local/lib -lwiringPi
/usr/bin/ld: cannot find -lwiringPi
collect2: ld returned 1 exit status
make: *** [gpio] Error 1


PLEASE IGNORE _ CODE NOW BUILT
Last edited by fulburncams on Wed May 16, 2012 4:27 pm, edited 1 time in total.

User avatar
gordon@drogon.net
Posts: 2023
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website Twitter

Re: GPIO: Register Addressing

Wed May 16, 2012 4:26 pm

fulburncams wrote:Realised my mistake, now moved wiringPi.h to right place.. but now hit following problem

[CC] gpio.c
[link]
gcc -o gpio gpio.o -L/usr/local/lib -lwiringPi
/usr/bin/ld: cannot find -lwiringPi
collect2: ld returned 1 exit status
make: *** [gpio] Error 1
It still looks like you've not run the

sudo make install

in the wiringPi directory.

Gordon
--
Gordons projects: https://projects.drogon.net/

User avatar
fulburncams
Posts: 72
Joined: Wed Nov 30, 2011 8:29 pm

Re: GPIO: Register Addressing

Wed May 16, 2012 4:29 pm

ALL working now, thanks

User avatar
gordon@drogon.net
Posts: 2023
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website Twitter

Re: GPIO: Register Addressing

Wed May 16, 2012 4:45 pm

fulburncams wrote:ALL working now, thanks
Great!

The big down-side (for now) with this method is that you need to run the program as root, so it can directly access the /dev/mem and poke the GPIO via there. Accessing via /sys/gpio is probably safer, but I think it's a bit of a fiddle, however I understand there is a nice Python library that uses it.

Have a look at the 'gpio' program included - if you

sudo make install

that then installs as suid-root, and you can then poke the GPIO pins from the command line when logged in as a normal user - it's good for simple testing from the command line before writing a proper program. It's not well documented (yet - read the source and see the test.sh script ;-), but if you have LEDs on pins 0 and 1, then:

gpio mode 0 out
gpio mode 1 pwm
gpio write 0 1
gpio pwm 1 500

Lets you do some quick fiddling...

Gordon
--
Gordons projects: https://projects.drogon.net/

hande89
Posts: 19
Joined: Mon Jun 18, 2012 2:06 pm
Location: Finland

Re: GPIO: Register Addressing

Fri Jul 13, 2012 8:10 am

fulburncams wrote:Realised my mistake, now moved wiringPi.h to right place.. but now hit following problem

[CC] gpio.c
[link]
gcc -o gpio gpio.o -L/usr/local/lib -lwiringPi
/usr/bin/ld: cannot find -lwiringPi
collect2: ld returned 1 exit status
make: *** [gpio] Error 1
I have the same problem with Eclipse and C++ on Debian desktop PC using the arm-bcm2708-linux-gnueabi compiler. I run the necessary commands to install wiringPi and I'm sure that libraries and paths are correctly specified in project settings.

Actually it says "skipping incompatible /usr/local/lib/libwiringPi.a when searching for -lwiringPi"...
So what is the right way to access wiringPi functions from C++?
Distribution Debian 19.4.2012

User avatar
gordon@drogon.net
Posts: 2023
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website Twitter

Re: GPIO: Register Addressing

Fri Jul 13, 2012 8:27 am

hande89 wrote:
fulburncams wrote:Realised my mistake, now moved wiringPi.h to right place.. but now hit following problem

[CC] gpio.c
[link]
gcc -o gpio gpio.o -L/usr/local/lib -lwiringPi
/usr/bin/ld: cannot find -lwiringPi
collect2: ld returned 1 exit status
make: *** [gpio] Error 1
I have the same problem with Eclipse and C++ on Debian desktop PC using the arm-bcm2708-linux-gnueabi compiler. I run the necessary commands to install wiringPi and I'm sure that libraries and paths are correctly specified in project settings.

Actually it says "skipping incompatible /usr/local/lib/libwiringPi.a when searching for -lwiringPi"...
So what is the right way to access wiringPi functions from C++?
Sounds like your cross-compiling? If so, I'm really not sure how it all handles pulling in external libraries and so on. I develop and compile all my Pi stuff directly on a Pi itself where it all seems to work fine (at least for me!)

So if you are cross-compiling, I'm wondering if the compile it somehow re-interpreting the -L flag to specify the library location.. (and of-course make sure the library was compiled with the cross compiler too (thinking that after seeing the "incompatable" message above - wondering if you compiled wiringPi with the native x86 gcc than the cross compiler?)

One thing you could do is to simply put the sources for wiringPi.c and wiringPi.h in your own project directory and compile them all as one rather than try to use it as a library - that may help with the cross compiling scenario, but I've never used anything like Eclipse before so really don't know how it handles stuff like that.

I do have a cross-compiler setup on my desktop though, so maybe I'll give it a go there and see how it might be made to work in that environment (but I've only ever used it to build a kernel for the Pi!)

-Gordon
--
Gordons projects: https://projects.drogon.net/

hande89
Posts: 19
Joined: Mon Jun 18, 2012 2:06 pm
Location: Finland

Re: GPIO: Register Addressing

Fri Jul 13, 2012 9:10 am

Nevermind, I had installed wiringPi on RPi also and when I moved my source code to there and compiled with g++, it worked!

But yes I'm cross-compiling and think you got the point what was wrong...
Distribution Debian 19.4.2012

User avatar
gordon@drogon.net
Posts: 2023
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website Twitter

Re: GPIO: Register Addressing

Fri Jul 13, 2012 9:39 am

hande89 wrote:Nevermind, I had installed wiringPi on RPi also and when I moved my source code to there and compiled with g++, it worked!

But yes I'm cross-compiling and think you got the point what was wrong...
Glad you're going now!

I might still look at the cross-compiling environment though - you never know!

-Gordon
--
Gordons projects: https://projects.drogon.net/

rownyr
Posts: 41
Joined: Wed Jul 11, 2012 1:25 am

Re: GPIO: Register Addressing

Fri May 03, 2013 6:29 pm

FYI, I finally made Eclipse CDT to cross compile with libwiringPi. I was getting the same error (cannot find -lwiringPi). I found the solution here: http://www.crc64.com/rpi-wiringpi-ve-cr ... pilir.html

The problem lies in the file name of shared library. It should be named libwiringPi.so, because cross-ld does not 'see' libwiringPi.so.1.

Type this to make a valid symbolic link:

Code: Select all

sudo ln -s /usr/local/lib/libwiringPi.so.1 /usr/local/lib/libwiringPi.so
Static library works out of the box with cross compiler.

ravantech
Posts: 1
Joined: Tue Jul 02, 2013 1:56 pm

Re: GPIO: Register Addressing

Tue Jul 02, 2013 2:04 pm

hi rownyr,

I'm also trying for wiringPi to work for eclipse cross compiling, appreciate if you could give me some guidelines how did you resolve it.

I did try as per the link you have mentioned http://www.crc64.com/rpi-wiringpi-ve-cr ... pilir.html

but still have issue.

thanks in advance

User avatar
drkblog
Posts: 2
Joined: Thu Oct 03, 2013 1:04 pm
Location: Buenos Aires, Argentina
Contact: Website

Re: GPIO: Register Addressing

Sat Oct 05, 2013 8:37 pm

Same here:

Code: Select all

GPIO Utility
[Compile] gpio.c
gpio.c:85:12: warning: ‘decodePin’ defined but not used [-Wunused-function]
gpio.c: In function ‘doLoad’:
gpio.c:208:12: warning: ignoring return value of ‘system’, declared with attribute warn_unused_result [-Wunused-result]
gpio.c:214:12: warning: ignoring return value of ‘system’, declared with attribute warn_unused_result [-Wunused-result]
[Compile] extensions.c
[Compile] readall.c
[Link]
/usr/bin/ld: skipping incompatible /usr/local/lib/libwiringPi.so when searching for -lwiringPi

User avatar
gordon@drogon.net
Posts: 2023
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website Twitter

Re: GPIO: Register Addressing

Sat Oct 05, 2013 8:41 pm

FWIW: I've still not looked at cross compiling wiringPi. I'm sticking to compiling it on a Pi for now.

-Gordon
--
Gordons projects: https://projects.drogon.net/

jiwon21c
Posts: 3
Joined: Fri Mar 27, 2015 2:36 am

Re: GPIO: Register Addressing

Fri Mar 27, 2015 2:39 am

Hi,

I also ran into the same problem in Eclipse cross-compiling environment.
I hope to see a solution as soon as possible.

Thanks in advance,
John

jiwon21c
Posts: 3
Joined: Fri Mar 27, 2015 2:36 am

Re: GPIO: Register Addressing

Fri Mar 27, 2015 4:44 am

Hi,

To use this wiringPi under Eclipse CDT environment for now,
the related source and header files should be imported to the 'Project Explorer' instead of including the static(.a) library or dynamic(.so) in /usr/local/lib. My guess is that the ld issue with respect to the above libraries would be solved later.

And Eclipse should be run as sudo.
And also, to use the remote debugging approach in Eclipse, 'default user ID' should be set to 'root' for remote connection to RaspberryPi on 'Debug Configurations'. if not, if (geteuid () != 0) in int wiringPiSetup (void) returns error.

After above settings, I can now debug the wiringPi and GPIO handling codes.

John

Return to “Troubleshooting”