|
META TOPICPARENT |
name="LectureSlides" |
Slide 1: Accessing the Real World |
| in this new functionality it may be merged back into the main branch.
|
|
> > | 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.
|
| Getting the LED blink program to work
The wiringPi library has included a few example programs to show its use. |
|
|
|
> > |
|
| Creating blink.c
As you would expect, wiringPi has its own include file |
|
- SPI functions
- Miscellaneous function
|
|
> > | A bit more on bash scripts
As explained before bash provides it own programming language with
- Assignments
- Conditional statements
- Loops
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
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
... 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!
The rest of the script
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.
Example of an endless loop in bash
You can also read command line arguments just like you do it in C:
|
| The gpio command |
|
< < | In addition to the library functions a new command: gpio is provided. This can be used to set gpio pins from bash It has however many more options. Look them up in its man page. |
> > | 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.
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
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.
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 check it the number of arguments is correct
The test and assignment of the wave type
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
---++
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=01)
To strobe the data into the register the strobe line must see a low to high transition
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!
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)
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?
Solution: DAC preparation
Now the test program generating data for the DAC
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
Solution: Strobe
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!
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.
Signal handler
Catching signals
 |
|
%SLIDESHOWEND%
-- Uli Raich - 2017-09-27
Comments |
|
< < | |
> > | Preparing data for the DAC |
|
META FILEATTACHMENT |
attachment="gpioNumbering.png" attr="" comment="" date="1506522380" name="gpioNumbering.png" path="gpioNumbering.png" size="161701" user="uli" version="1" |
META FILEATTACHMENT |
attachment="blink.png" attr="" comment="" date="1506522380" name="blink.png" path="blink.png" size="50055" user="uli" version="1" |
|
|
META FILEATTACHMENT |
attachment="ssh-X.png" attr="" comment="" date="1506524862" name="ssh-X.png" path="ssh-X.png" size="300537" user="uli" version="1" |
META FILEATTACHMENT |
attachment="blinkMakefile.png" attr="" comment="" date="1506530162" name="blinkMakefile.png" path="blinkMakefile.png" size="6723" user="uli" version="1" |
META FILEATTACHMENT |
attachment="ledschematics.png" attr="" comment="" date="1506530936" name="ledschematics.png" path="ledschematics.png" size="7367" user="uli" version="1" |
|
|
> > |
META FILEATTACHMENT |
attachment="bashRoot.png" attr="" comment="" date="1508152594" name="bashRoot.png" path="bashRoot.png" size="7293" user="uli" version="1" |
META FILEATTACHMENT |
attachment="restScript.png" attr="" comment="" date="1508152595" name="restScript.png" path="restScript.png" size="29128" user="uli" version="1" |
META FILEATTACHMENT |
attachment="loopBash.png" attr="" comment="" date="1508152595" name="loopBash.png" path="loopBash.png" size="5824" user="uli" version="1" |
META FILEATTACHMENT |
attachment="argCountCheck.png" attr="" comment="" date="1508152652" name="argCountCheck.png" path="argCountCheck.png" size="20416" user="uli" version="1" |
META FILEATTACHMENT |
attachment="argsBash.png" attr="" comment="" date="1508152652" name="argsBash.png" path="argsBash.png" size="9219" user="uli" version="1" |
META FILEATTACHMENT |
attachment="signal.png" attr="" comment="" date="1508152652" name="signal.png" path="signal.png" size="19505" user="uli" version="1" |
META FILEATTACHMENT |
attachment="signalHandler.png" attr="" comment="" date="1508152653" name="signalHandler.png" path="signalHandler.png" size="24970" user="uli" version="1" |
META FILEATTACHMENT |
attachment="enum.png" attr="" comment="" date="1508152778" name="enum.png" path="enum.png" size="20456" user="uli" version="1" |
META FILEATTACHMENT |
attachment="waveTypeTest.png" attr="" comment="" date="1508152778" name="waveTypeTest.png" path="waveTypeTest.png" size="33580" user="uli" version="1" |
META FILEATTACHMENT |
attachment="strobe.png" attr="" comment="" date="1508155863" name="strobe.png" path="strobe.png" size="46229" user="uli" version="1" |
META FILEATTACHMENT |
attachment="dacPrep.png" attr="" comment="" date="1508156316" name="dacPrep.png" path="dacPrep.png" size="42958" user="uli" version="1" |
META FILEATTACHMENT |
attachment="sendToHWInclude.png" attr="" comment="" date="1508155863" name="sendToHWInclude.png" path="sendToHWInclude.png" size="29093" user="uli" version="1" |
META FILEATTACHMENT |
attachment="sendToDAC.png" attr="" comment="" date="1508155863" name="sendToDAC.png" path="sendToDAC.png" size="30373" user="uli" version="1" |
META FILEATTACHMENT |
attachment="lecture_7.odp" attr="" comment="" date="1508157461" name="lecture_7.odp" path="lecture_7.odp" size="561204" user="uli" version="1" |
|