Raspberry Pi GPIO programming

DSCF6353 - Copy

GPIO – Starting from the pins up

During the last week of study I have spent a lot of time looking at many online references that relate to Coding for GPIO input and output on the Raspberry PI along with my other single board computers including (Radxa rock, banana Pi and Odroid U3+).

As many programmers will know there are many walls to be climbed while following a path of reference using the web, this has been the case during the last week of my reading for sure.

Just taking the Raspberry Pi alone for the moment, the study blocks relate to the fact that a lot of web pages contain details that are incomplete or are just simply not tested and thus working. You need a lot of time and patience to work through to the point where you yourself have collected enough details that actually work when coded in reality.

I have however now reached a point where I can at least post the details that I have collected, feeling that they work as I have now tested them to do so !!

There is simply too much detail relating to GPIO coding to place in this post alone so here today I am just going to pass on the base ground work that I am willing to use myself, in order to work outwards from them to each of my possible coding routes.

In My last post I introduced the basic concept of GPIO input/output and ended by saying that I would in this post talk about how GPIO is controlled via programming languages.

There are many programming languages that you can install on the Raspberry PI and other GPIO enabled development boards but not all of them are matched well to code for GPIO, I feel that the reason for this is as follows.

GPIO coding has many levels some falling at the hardware level of Machine code and Assembly Language, others abstracted far away in the realm of object orientated programming methods.

Here is an example of using assembly language to turn on an LED light :-

This example requires a simple wiring, using an LED and a resistor (1KOhm, typically) to 2 GPIO pins of the RaspPi 2 header. More information on the GPIO connector pinout can be found here. The black wire is connected to Pin 6 of the header (GND), and the red wire to Pin 7 (BCM 4)

RasPi2LEDWiring

Raspberry-Pi-GPIO-Layout-Model-B-Plus-rotated

Assembly Code by : D. Thiebaut

@ LINK !! blink.s
@ D. Thiebaut
@ based on the following C program:
@
@ #include
@ #include
@ #include
@
@ int main (void) {
@ int pin = 7;
@ printf(“Raspberry Pi wiringPi blink test\n”);
@
@ if (wiringPiSetup() == -1) {
@ printf( “Setup didn’t work… Aborting.” );
@ exit (1);
@ }
@
@ pinMode(pin, OUTPUT);
@ int i;
@ for ( i=0; i<10; i++ ) {
@ digitalWrite(pin, 1);
@ delay(250);
@
@ digitalWrite(pin, 0);
@ delay(250);
@ }
@
@ return 0;
@ }
@
@ To assemble, link, and run:
@
@ as -o blink.o blink.s
@ gcc -o blink2 blink.o -lwiringPi
@ sudo ./blink2

@ —————————————
@ Data Section
@ —————————————

.data
.balign 4
Intro: .asciz "Raspberry Pi wiringPi blink test\n"
ErrMsg: .asciz "Setup didn't work… Aborting…\n"
pin: .int 7
i: .int 0
delayMs: .int 250
OUTPUT = 1

@ —————————————
@ Code Section
@ —————————————

.text
.global main
.extern printf
.extern wiringPiSetup
.extern delay
.extern digitalWrite
.extern pinMode

main: push {ip, lr} @ push return address + dummy register
@ for alignment

@ printf( "blink…" ) ;
ldr r0, =Intro
bl printf

@ if (wiringPiSetup() == -1) {
@ printf( "Setup didn't work… Aborting." ) ;
@ exit (1) ;
@ }
bl wiringPiSetup
mov r1,#-1
cmp r0, r1
bne init
ldr r0, =ErrMsg
bl printf
b done

@ pinMode(pin, OUTPUT) ;
init:
ldr r0, =pin
ldr r0, [r0]
mov r1, #OUTPUT
bl pinMode

@ for ( i=0; i<10; i++ ) {
ldr r4, =i
ldr r4, [r4]
mov r5, #10
forLoop:
cmp r4, r5
bgt done

@ digitalWrite(pin, 1) ;
ldr r0, =pin
ldr r0, [r0]
mov r1, #1
bl digitalWrite

@ delay(250) ;
ldr r0, =delayMs
ldr r0, [r0]
bl delay

@ digitalWrite(pin, 0) ;
ldr r0, =pin
ldr r0, [r0]
mov r1, #0
bl digitalWrite

@ delay(250) ;
ldr r0, =delayMs
ldr r0, [r0]
bl delay

add r4, #1
b forLoop

done:
pop {ip, pc} @ pop return address into pc

———————————————————————

Follow the link at the top of the listing for more details !!

For sure Assembly Language is a very legitimate method of programming the Raspberry-pi or any computer of its type. It has the huge advantage of not even needing an operating system to be loaded when the raspberry pi boots, I will detail the methods of doing this when its appropriate to do so but for now just take it as a possibility for applications where only your machine code is needed, in projects that do not need an operating system, i.e. your TV has no OS! but plenty of code.

NB : As a related foot note – you can do the same as above using compiler based languages that can produce stand-alone machine code routines.

It is at this level of hardware programming that everything starts when it comes to coding for GPIO, the level of direct memory addressing for both input and output. Above this level you will find that all GPIO enabled programming languages follow two routes!

Firstly they can make use of the operating system to control any devices attached to the GPIO pins, Linux and on the Raspberry PI2 and 3, Windows 10 for IOT, are both configured to allow for GPIO pin control or device driver control for hardware devices that are attached as peripherals to the GPIO port.

Secondly languages can make use of code (libraries, units or modules ) that bypass the operating systems GPIO configuration in order to just like the Assembly language example here directly read or write to the pins on the GPIO header.

I feel this is just about the correct amount of information for this post today, my following posts will be a series of posts that will take the form of study notes that follow a route as below :

1.. Looking at any available GPIO information from Broadcom BCM2835-ARM-Peripherals Datasheet , Broadcom Publish a datasheet. The purpose of this datasheet is to provide documentation for all SOC peripherals in sufficient detail to allow a developer to port an operating system to BCM2835.

I have now posted relating to this area here….

I may also contrast and compare this information to that of datasheets available for the Radxa rock and Odroid U3 systems.

2.. A Look at how the Raspberry PI operating systems are configured to control the GPIO port, its pins and its interfaces. The reason for this post will be centred around the fact that you can use the operating system itself to code in the form of shell scripts. All GPIO enabled versions of Linux for the Raspberry Pi and other boards such as the Radxa rock use a file structure in the mounted file systems to pass read and write (input and output) signals to the shell, these signals can be used in a shell script.

Not to cloud issues but it is even possible to use none GPIO enabled programming languages to code for GPIO, if they can open and write to and read from these operating system files, I do however only want to make use of fully GPIO capable languages and their GPIO libraries and modules!

I have now posted relating to this area

3.. I want to construct a full review (that will involve more than one post!) of all available GPIO enabled programming languages starting with Python and followed by languages such as C++ and Java. I have found in the Radxa rock and some Raspberry Pi documentation that included coding examples, only involve – as I said above, opening and closing the related operating system files. To do this is most likely very slow and inefficient and also has the drawback of not producing stand-alone code. While this kind of code is more than ok for testing purposes ! why not work with the final aim of your code being fast and also having the ability to run itself in such away that it can be loaded in any environment with or without needing a particular OS or even one at all !!

Ok that really is enough for this post, back soon !!

Advertisements
This entry was posted in Development board hardware, Development Languages, Linux, Operating systems, Radxa rock, Raspberry pi, Uncategorized, Windows 10 IOT core and tagged , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s