End Presentation


TWiki Slide Show
Next
Accessing the Real World

Lecture 7


Uli Raich


UCC semester 2017/2018

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 1 of 38





















TWiki Slide Show
Next
Access libraries
As we have already seen, the Raspberry Pi flat cable connector

and the cobbler + bread board, give access to external hardware though

  • gpio
  • I2C bus
  • SPI
  • serial port interface
Dedicated interfaces to camera and display

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 2 of 38





















TWiki Slide Show
Next
Software access
Of course you can access the external hardware through

the BCM-2837 interfaces and their registers directly, however,

this is not for the faint-hearted (read the 200 page manual first!)

The easier way to access these devices are ready made

libraries giving you a simpler API for access

To my knowledge there are 2 such libraries around

(at least these are the most popular ones):

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 3 of 38





















TWiki Slide Show
Next
How to download and install
Both libraries can be downloaded as git source archives,

which allows you to have the very latest version and

to keep your version up to date.

git is a revision control system allowing many developers

to work on the same project. You check out the current version,

work on it and you can upload to the git server

the modifications you made.

You can also create a new code branch where you implement

new functionality which may be specific to what you want

to use the code for, or it may be a try to implement new options

which may later be discarded or, in case everybody is interested

in this new functionality it may be merged back into the main branch.

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 4 of 38





















TWiki Slide Show
Next
Our use of git
We do not want to develop the code but just use it.

In this case we decide, where in our file system tree we want to

install the source code of the library and then we download it with

git clone git://git.drogon.net/wiringPi

This will download the source code which we will have to compile

which is done with ./build. This is a shell script!

Have a look to see if you can understand what it does.

In order to keep the library up to date we go to the wiringPi

directory just created and we type:

git pull

This will update the source code to the latest version

Here is the installation manual of wiringPi.

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 5 of 38





















TWiki Slide Show
Next
Getting the LED blink program to work
The wiringPi library has included a few example programs to show its use.

The most simple one is a program making a LED blink.

WiringPi has its own numbering system for the GPIO pins:

gpioNumbering.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 6 of 38





















TWiki Slide Show
Next
Creating blink.c
As you would expect, wiringPi has its own include file

which you must use in order to access the library:

#include <wiringPi.h>

On our systems we have installed this include file is in /usr/include

on the Pi file system while the library itself is in /usr/lib

As these are the standard positions for include files and libraries on a

Linux system all we have to do in the Makefile is to add

-lwiringPi to the LDLIBS macro.

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 7 of 38





















TWiki Slide Show
Next
The C code of blink.c
blink.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 8 of 38





















TWiki Slide Show
Next
The Makefile to build blink
blinkMakefile.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 9 of 38





















TWiki Slide Show
Next
Ohm's law
If you consider that the LED has no resistance and the Raspberry Pi

drives the GPIO pins with 3.3V and you connect as shown in the circuit diagram,

then what is the current flowing through the LED?

ledschematics.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 10 of 38





















TWiki Slide Show
Next
!WiringPi functions
The library has functions to

  • Setup and initialize the library
    This will open the needed device drivers and give access to them
  • Core functions: Functions to
    • set gpio pins to input or output and
    • to read and write from /to them
    • define pull-ups/pull-downs
  • Some Pi specific functions liking getting the version no
  • Timing functions (e.g. delays)

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 11 of 38





















TWiki Slide Show
Next
!WiringPi functions (2)
  • Priority, interrupts, threads
  • I2C bus functions
  • Serial line functions
  • SPI functions
  • Miscellaneous function

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 12 of 38





















TWiki Slide Show
Next
A bit more on bash scripts
As explained before bash provides it own programming language with

  • Assignments
  • Conditional statements
  • Loops

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 13 of 38





















TWiki Slide Show
Next
A bash script to setup the Pis
During the weekend I wanted to install the latest version of the

wiringPi library from git on all 15 Raspberry Pis in the lab.

I needed to

  • Create a file system structure with
    /opt/ucc/micros/raspberry into which I put the wiringPi sources
  • Change its owner to root:ucc such that members of the
    ucc group have access to the files
  • Change the permissions to allow members of the
    ucc group to also write into these directories

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 14 of 38





















TWiki Slide Show
Next
A bash script to setup the Pis (2)
  • Download the wiringPi source code into the /opt/ucc/micros/raspberry directory
  • Modify a Makefile in the wiringPi subdirectory
  • Compile the code and install using the build bash script supplied with wiringPi
  • Compile all the examples
  • Create the examples/solutions directory on /opt/ucc
    owned by uccstaff:uccstaff and only readable by this used
  • Transfer the solutions of the exercise associated to this lecture
  • Try out everything

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 15 of 38





















TWiki Slide Show
Next
... a lot of work
This is a lot of typing!

The solution: a shell script!

The shell script can only be executed by root because only the
root user can create directories in /opt

How to check if we are root?

A conditional statement in the script!

bashRoot.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 16 of 38





















TWiki Slide Show
Next
The rest of the script
restScript.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 17 of 38





















TWiki Slide Show
Next
Loops in bash
You can write

  • for loops
  • while loops
  • until loops
in bash.

Unfortunately I have not enough time to explain the full syntax of bash scripts.

You should have a look in the WEB for more information.

Often bash scripts are used for installations and you should be able

to understand what these scripts are doing before executing them.

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 18 of 38





















TWiki Slide Show
Next
Example of an endless loop in bash
loopBash.png

You can also read command line arguments just like you do it in C:

argsBash.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 19 of 38





















TWiki Slide Show
Next
The gpio command
There is the gpio command, which is part of wiringPi.

It has many options. Please have a look at its man page!

One option allows to read and write GPIO pins:

gpio write $PIN $ON_OFF

If PIN is 0 and ON_OFF is 1 then the LED connected to GPIO pin 0 will go on.

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 20 of 38





















TWiki Slide Show
Next
A bit more on C programming
We have just seen the very basics of C programming and we need to learn

a bit more about the libraries we can use

Here we will look at

  • String handling functions
  • Some bit handling function
  • Exiting a program gracefully

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 21 of 38





















TWiki Slide Show
Next
String handling functions
There is a series of functions to
  • Compare strings strcmp and strncmp
  • Copy strings strcpy and strncpy
  • Locate a sub-string strstr
are the most important important ones.

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 22 of 38





















TWiki Slide Show
Next
Treating command line arguments
Consider a wave generator where the user can choose the

type of waveform using command line arguments.

His options are:

  • sine
  • rectangular
  • triangular
  • sawtooth
How can be convert the strings into a variable that can be treated in a switch statement?
---++
First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 23 of 38





















TWiki Slide Show
Next
First check it the number of arguments is correct
argCountCheck.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 24 of 38





















TWiki Slide Show
Next
The test and assignment of the wave type
waveTypeTest.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 25 of 38





















TWiki Slide Show
Next
Bit handling functions
We need to learn a few bit handling functions before being able to prepare the data for the DAC:
| : bitwise or
& : bitwise and
data += 5 <=> data = data + 5
data |= 5 <=> data = data | 5
~data: invert all bits in data
data >> 4 all bits in data are shifted right by 4
data << 4 all bits in data are shifted left by 4

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 26 of 38





















TWiki Slide Show
Next
Writing data to a DAC
Imagine a 12 bit digital to analogue converter with the following register outline:

The 12 bit data must be written 4 bits at a time to registers

which are selected by the RS bits.

The lowest significant digit goes to register 0
(both RS bits zero)

The medium significant digit to register 1
(RS=01)

And the highest significant digit to register 2
(RS=10)

To strobe the data into the register the strobe line must see a low to high transition

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 27 of 38





















TWiki Slide Show
Next
The DAC
We have a 12 bit DAC, which creates signal levels from 0V to Vcc

What is the max. number this DAC can take in decimal and in hex?

What is the signal resolution in ‰

This is Physics!

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 28 of 38





















TWiki Slide Show
Next
Preparing data for the DAC
Remember the bit layout of the DAC register?


We must write the

  • lowest significant,
  • medium significant and
  • highest significant bits
separately.

We must “or” in the register select bits and read/write bits

(write is usually active low)

We must strobe in the data (a pulse on the strobe bit)

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 29 of 38





















TWiki Slide Show
Next
DAC preparation
Write a function which prepares a byte for the DAC

The function takes 3 arguments:

  • The data nibble (4 bits of data)
  • The register selection (0,1,2)
  • The read/write bit
Leave the strobe bit zero!

What will be the result if you want to write 5 to register to the middle nibble?

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 30 of 38





















TWiki Slide Show
Next
Solution: DAC preparation
sendToHWInclude.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 31 of 38





















TWiki Slide Show
Next
Now the test program generating data for the DAC
dacPrep.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 32 of 38





















TWiki Slide Show
Next
Strobe
Now create a strobe function which takes DAC data (including the register bits)

and generates a strobe signal on the

STR line without touching the other bits

With a correctly prepared data and register byte this will

write the data to the correct register within the DAC

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 33 of 38





















TWiki Slide Show
Next
Solution: Strobe
strobe.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 34 of 38





















TWiki Slide Show
Next
Exiting a program gracefully
When we have a program like our blink program,

then the only way to exit the endless loop is a ^C

which will leave the LED in an unknown state.

Is there a way to exit the program gracefully and to

make sure the LED is always off once the program exits?

Yes, there is!

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 35 of 38





















TWiki Slide Show
Next
Killing the program
When we kill the program with ^C, a signal (SIGINT) is sent

to it which normally results in a brutal stop of the program.

It is however possible to capture the signal and

do some cleanup before the program finally exits.

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 36 of 38





















TWiki Slide Show
Next
Signal handler
signalHandler.png

First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 37 of 38





















TWiki Slide Show
Next
Catching signals
signal.png


First slide Previous Next Last slide
COPYRIGHT © 2024 by the contributing authors
Slide 38 of 38





















First slide Previous End Presentation






























-- Uli Raich - 2017-09-27

Comments

Preparing data for the DAC
I Attachment History Action Size Date Who Comment
PNGpng argCountCheck.png r1 manage 19.9 K 2017-10-16 - 11:17 UnknownUser  
PNGpng argsBash.png r1 manage 9.0 K 2017-10-16 - 11:17 UnknownUser  
PNGpng bashRoot.png r1 manage 7.1 K 2017-10-16 - 11:16 UnknownUser  
PNGpng blink.png r1 manage 48.9 K 2017-09-27 - 14:26 UnknownUser  
PNGpng blinkMakefile.png r1 manage 6.6 K 2017-09-27 - 16:36 UnknownUser  
PNGpng dacPrep.png r1 manage 42.0 K 2017-10-16 - 12:18 UnknownUser  
PNGpng enum.png r1 manage 20.0 K 2017-10-16 - 11:19 UnknownUser  
PNGpng gpioNumbering.png r1 manage 157.9 K 2017-09-27 - 14:26 UnknownUser  
Unknown file formatodp lecture_7.odp r1 manage 548.1 K 2017-10-16 - 12:37 UnknownUser  
PNGpng ledschematics.png r1 manage 7.2 K 2017-09-27 - 16:48 UnknownUser  
PNGpng loopBash.png r1 manage 5.7 K 2017-10-16 - 11:16 UnknownUser  
PNGpng piRemoteDesktop.png r1 manage 378.8 K 2017-09-27 - 14:53 UnknownUser  
PNGpng reminna.png r1 manage 3.9 K 2017-09-27 - 14:53 UnknownUser  
PNGpng restScript.png r1 manage 28.4 K 2017-10-16 - 11:16 UnknownUser  
PNGpng sendToDAC.png r1 manage 29.7 K 2017-10-16 - 12:11 UnknownUser  
PNGpng sendToHWInclude.png r1 manage 28.4 K 2017-10-16 - 12:11 UnknownUser  
PNGpng signal.png r1 manage 19.0 K 2017-10-16 - 11:17 UnknownUser  
PNGpng signalHandler.png r1 manage 24.4 K 2017-10-16 - 11:17 UnknownUser  
PNGpng ssh-X.png r1 manage 293.5 K 2017-09-27 - 15:07 UnknownUser  
PNGpng strobe.png r1 manage 45.1 K 2017-10-16 - 12:11 UnknownUser  
PNGpng waveTypeTest.png r1 manage 32.8 K 2017-10-16 - 11:19 UnknownUser  

This topic: Embedded_Systems > WebHome > LectureSlides > Lecture7:AccessingTheRealWorld
Topic revision: r6 - 2017-11-01 - uli
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2024 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback