3

I'm trying to understand a few simple blocks about programming the Raspberry Pi. My questions are as follows....

  1. I've worked on a number of micro-controllers out there and have always used their language that the IDE supported. I understand that there are several libraries out there for the pi using C and C++. WiringPi is one that I initially downloaded and installed. However, is there a library that's already on the Pi that I don't have to install or download and what is it referred to as? I heard people say the Native library but I don't know what that means or if it's what i'm referring too. Also, if my board is Model B rev. 2, where can I find the specifications of the name of the ports or pins or registers that are associated with the GPIO and all the other headers on the pi? I know C but I know C++ better. To be able to know if there is a library already on there or just to know the pin names to where I can just straight up write a c++ program and use those names would be a lot of help.

  2. Is there any documentation or web link out there that would provide me assistance or a resource as I write my code using the pre-installed library on the Pi for the C and C++ Language?

  3. Are the C and C++ libraries the same? What are some things I need to be aware of when between a .c file and a .cpp file when I start to compile them and include the pre-installed libraries?

2

OK. Some good questions here. I'll try to take it in order since that's the easiest way to answer, but it may make the text a bit rambling.

As far as I know there isn't an inbuilt package for the GPIO other than the Python GPIO package. However, WiringPi has been packaged for easier installation. It hasn't yet been included into Rasbian. I think it will be at some point.

As for information about the GPIO itself, eLinux is the best source of information. The registers between the Rev 1 and Rev 2 boards are the same for the pins. I believe that GPIO 21 and 27 are the same pin between them is an annoying paradox for anyone who wants to specify GPIO pin by header number, i.e. by pin 13 in this case. Pin 13 on a Rev 1 is 21, on a Rev 2 is 27. But calling for BCM pin 21 will get you 13 on a Rev 1 and I'm not quite sure what on a Rev 2, but not physical pin 13. The best way to make sure of this unfortunately I think is to actually look at the code of WiringPi or one of the other libraries. As far as I know it's DMA writes shifted up from a base location of 0x20000000, but I've not looked at it in too much detail. Far easier to stick with a library here. Ultimate details of the spec are probably in some of the Broadcom documentation. Personally I use the bcm2835 library when I'm in C.

All of the really good documentation for using GPIO on the Pi, however, is for doing it in Python. The syntax can be a bit of a stretch if you're used to C, but you really want to get up and going quickly it's probably easier to get used to those differences and use all of the available tutorials. There are literally dozens.

C and C++ cross compatibility is one of those interesting things. In the case of the libraries for GPIO they're effectively compatible as they're all written in C and are linked as dynamic libraries.

Keep in mind that gcc is for C files and g++ is for C++ files. But you can compile a C++ file with gcc as long as you don't use any of the special C++ features. Like classes. I'd stick to C for now until you get used to things again. It's much easier to learn one thing at a time.

0

You can access the GPIO via the inbuilt memory mapped I/O.

As far as I am aware there is no pre installed library. I suggest you use the WiringPi library. This is reasonably documented (although you have to go to the web page for documentation). It is also instructive to examine the source code.

All of the functions are in c, but are usable from c++ because of the included

#ifdef __cplusplus
extern "C" {
#endif

There is some software by Gert van Loo (supplied with the gertboard) which enables direct access which I have used. See http://elinux.org/RPi_Low-level_peripherals#C_2 for an example

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.