Transcript Slide 1

Workshop
GPIO I2C SPI
Panic1
dinsdag 21 juli 2015
Installing the Raspberry Pi
 NOOBS
 NOOBS is an installer that packs a number of
RPi ready images, easy to use, but you need
an SD card of at least 8Gb to have enough
room left for Raspbian
 Raspbian
 Debian image for Raspbery Pi
© Sioux Embedded Systems 2011 | Confidential | 2
Installing NOOBS
 Format the SD card into one FAT32 partition
 Extract the files in the tar ball onto the SD
card
 Pop the SD card into the RPi and turn it on
 Select the image to install and you are ready
 Also easy to reinstall an image after it has
been messed up, by pressing Shift during
startup of the RPi and redo the installation
© Sioux Embedded Systems 2011 | Confidential | 3
Installing Raspbian
 Format the SD card into one FAT32 partition
 Use an image writing tool to write the
downloaded image to the SD card
 Plug the SD card into the RPi and start it up
© Sioux Embedded Systems 2011 | Confidential | 4
Documentation on RPi
 http://www.raspberrypi.org/documentation/
 http://elinux.org/RPi_Low-level_peripherals
© Sioux Embedded Systems 2011 | Confidential | 5
GPIO via command line
> sudo su
> echo "22" > /sys/class/gpio/export
> echo "out" > /sys/class/gpio/gpio22/direction
> echo "1" > /sys/class/gpio/gpio22/value
> echo "0" > /sys/class/gpio/gpio22/value
> while true; \
> do echo "1" > /sys/class/gpio/gpio22/value; \
> echo "0" > /sys/class/gpio/gpio22/value; \
> done
> echo "22" > /sys/class/gpio/unexport
Source: http://www.instructables.com/id/Control-Stuff-withyour-Raspberry-Pi-GPIO/
© Sioux Embedded Systems 2011 | Confidential | 6
GPIO in a C program
 Download and unpack the cross compiler
(https://github.com/raspberrypi/tools)
 Download and unpack Eclipse (www.eclipse.org)
 Create new C Project
 Executable, Empty Project, Cross GCC
 Cross compiler prefix: “arm-bcm2708hardfp-linuxgnueabi-”
 Path to cross compiler: ~/workshop\ rasberry\ pi/crosscompiler/tools-master/arm-bcm2708/armbcm2708hardfp-linux-gnueabi/bin
 New source file, copy-paste from http://elinux.org/RPi_Lowlevel_peripherals
 Ctrl-B to Build the project
© Sioux Embedded Systems 2011 | Confidential | 7
About the GPIO program
 This piece of C code doesn’t use a library,
instead it mmaps the device memory, and
manipulates the GPIO registers directly
 Must be run as root, to activate the root
account on Raspbian: sudo passwd
 Don’t change the ssh config, make sure the
root account stays permitted to login via ssh,
we will need this in the later steps when
running applications from Eclipse on the RPi
target machines
© Sioux Embedded Systems 2011 | Confidential | 8
Running the GPIO program
 Selec the menu Run
 Run configurations
 C/C++ Remote Applications
 Create a new connection to your RPi board
 Don’t forget to generate a ssh key and install it on
the RPi

Go to menu Window, Preferences, General, Network
Connections, SSH2
 If all is well, you can now instruct Eclipse to
copy and run the cross compiled program
on the target machine
© Sioux Embedded Systems 2011 | Confidential | 9
GPIO via CLI, two threads running
concurrently on different GPIO pins
© Sioux Embedded Systems 2011 | Confidential | 10
I2C background
Source: http://www.robot-electronics.co.uk/acatalog/I2C_Tutorial.html
© Sioux Embedded Systems 2011 | Confidential | 11
I2C by using bcm2835 library
 Download to the RPi:
http://www.airspayce.com/mikem/bcm2835/bcm2835-1.36.tar.gz
 Untar the file, and cd into the folder
 ./configure
 sudo make check
 sudo make install
 Getting these files into your cross compiler
 Copy bcm2835-1.36/src/bcm2835.o and libbcm2835.a from
RPi to ~/workshop\ rasberry\ pi/cross-compiler/toolsmaster/arm-bcm2708/arm-bcm2708hardfp-linux-gnueabi/armbcm2708hardfp-linux-gnueabi/sysroot/lib
 You will also need the header file (untar the same tar ball
somewhere on your machine locally)
© Sioux Embedded Systems 2011 | Confidential | 12
I2C example from bcm2835 lib
 We’ll be using the examples provided in the bcm2835 library
package
 Create a new Eclipse project, same cross compiler settings as
before
 To be able to use the lib in Eclipse, right click on Project, select
Properties, C/C++ General, Paths and Symbols, Libraries tab,
add "bcm2835", and in the Includes Tab, add the path to the
bcm2835.h file
 Copy the i2c.c file into the project, build the project, and run the
application like before on the RPi
 This time the application won’t work without arguments, try:
./I2cWorkshop -s85 -dw -ib 1 0x01
 What are looking at on the scope? Either use the hold function,
or put the program in a while(1) loop to see the signal.
 Don’t forget to run the application with option –ie to restore the
I2C lines back to GPIO at the end
© Sioux Embedded Systems 2011 | Confidential | 13
I2C: Trying to write to address
0x55, but no response
© Sioux Embedded Systems 2011 | Confidential | 14
SPI background
Typical use:
Typical use with three slave devices:
Daisy chained with three slave devices:
Source: http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus
© Sioux Embedded Systems 2011 | Confidential | 15
SPI via bcm2835 library
 Use the example spi.c file from the bcm2835
library package
 Same as before, create a new cross gcc
project in Eclipse, add the library to the
project options, copy the file into the project
 Connect the MOSI and the SCLK signals to
the scope. The CE0 signal is also interesting
© Sioux Embedded Systems 2011 | Confidential | 16
PWM via the bcm2835 library
 Use the example pwm.c file from the library
package
 Same as before
 Connect pin 12 (GPIO18) to the scope for the
PWM signal
© Sioux Embedded Systems 2011 | Confidential | 17
Thank you!
© Sioux Embedded Systems 2011 | Confidential | 18