Quantcast
Channel: neophob.com
Viewing all articles
Browse latest Browse all 36

Raspberry Pi: Enable the SPI device

$
0
0

I want to use the SPI Hardware of the Raspberry PI (Led Modules you know…). I wrote down a step-by-step guide.

1) Install Raspbian “wheezy” (2012-07-15-wheezy-raspbian.zip)
SSHd is enabled, login as user “pi”, password: “raspberry”

2) Update the system packages:

1
2
$ sudo apt-get update
$ sudo apt-get upgrade

This took more than an hour.

3) Update the Firmware
Thanks to http://hexxeh.net/?p=328117855 and
http://www.brianhensley.net/2012/07/getting-spi-working-on-raspberry-pi.html

1
2
3
$ sudo wget http://goo.gl/1BOfJ -O /usr/bin/rpi-update
$ sudo chmod +x /usr/bin/rpi-update
$ sudo rpi-update

A reboot is needed now. Hint: The firmware update is safe and painless and cannot brick your Pi.

4) Update the Kernel
The stock kernel (3.1) does not support the SPI device, thanks to Chris Boot we can easily use a bleeding edge kernel (3.2.23-rpi1+).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ wget http://apt.bootc.net/debian/pool/main/l/linux-source-3.2.23-rpi1+/linux-image-3.2.23-rpi1+_1_armel.deb

$ sudo dpkg -i linux-image-3.2.23-rpi1+_1_armel.deb
dpkg: error processing linux-image-3.2.23-rpi1+_1_armel.deb (--install):
 package architecture (armel) does not match system (armhf)
Errors were encountered while processing:
 linux-image-3.2.23-rpi1+_1_armel.deb

$ sudo dpkg --force-architecture -i linux-image-3.2.23-rpi1+_1_armel.deb
dpkg: warning: overriding problem because --force enabled:
 package architecture (armel) does not match system (armhf)
Selecting previously unselected package linux-image-3.2.23-rpi1+.
(Reading database ... 55321 files and directories currently installed.)
Unpacking linux-image-3.2.23-rpi1+ (from linux-image-3.2.23-rpi1+_1_armel.deb) ...

5) Remove SPI from the Blacklist

1
2
3
4
$ cat /etc/modprobe.d/raspi-blacklist.conf
# blacklist spi and i2c by default (many users don't need them)
#blacklist spi-bcm2708
blacklist i2c-bcm2708

Reboot now

6) Enjoy

1
2
3
$ ls -al /dev/spi*
crw------T 1 root root 153, 0 Jan  1  1970 /dev/spidev0.0
crw------T 1 root root 153, 1 Jan  1  1970 /dev/spidev0.1

7) Test SPI
Download the spidev_test.c source and compile it:

1
2
3
4
5
6
7
8
9
10
11
12
13
$ gcc spidev_test.c -o spidev_test
$ sudo ./spidev_test -D /dev/spidev0.0
spi mode: 0
bits per word: 8
max speed: 500000 Hz (500 KHz)

00 00 00 00 00 00
00 00 00 00 00 00
00 00 00 00 00 00
00 00 00 00 00 00
00 00 00 00 00 00
00 00 00 00 00 00
00 00

The tool should transfer this byte sequence:

1
2
3
4
5
6
7
8
    uint8_t tx[] = {
        0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
        0x40, 0x00, 0x00, 0x00, 0x00, 0x95,
        0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
        0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
        0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
        0xDE, 0xAD, 0xBE, 0xEF, 0xBA, 0xAD,
        0xF0, 0x0D,

So shorten the MISO (GPIO 9) and MOSI (GPIO 10) pins of your RPI (loopback):

(source).
1
2
3
4
5
6
7
8
9
10
11
12
pi@raspberrypi ~ $ sudo ./spidev_test -D /dev/spidev0.0
spi mode: 0
bits per word: 8
max speed: 500000 Hz (500 KHz)

FF FF FF FF FF FF
40 00 00 00 00 95
FF FF FF FF FF FF
FF FF FF FF FF FF
FF FF FF FF FF FF
DE AD BE EF BA AD
F0 0D

With the “-s” switch you can define the SPI Speed, in my test case I could go up to 62MHz, after that the transferred bytes were incorrect. Here is an image of my “installation”:

Some random facts:

  • The GPIO pins use a 3V3 logic level and are not tolerant of 5V levels, such as you might find on a 5V powered Arduino!
  • You don’t need an USB keyboard for this howto, just SSH into the box
  • The Raspberry Pi proprietary libraries, headers, and utilities, included in /opt/vc in the Debian image
  • The kernel, GPU firmware, start-up scripts, and proprietary libraries/headers/demo source are all provided by RPM packages.
  • Oracle just released Java for ARM. This will not work for this Distribution: “java: error while loading shared libraries: libjli.so: cannot open shared object file: No such file or directory”. However Oracle wrote “One caveat is that the current binary is softfloat ABI only, so it won’t work with (for example) the Raspbian distribution which uses the hardfloat ABI. We are planning to add hardfloat support in an upcoming JDK release, as well as support for JavaFX on ARM.”

    What is this softfloat vs hardfloat thing? When will the Oracle JDK support hardfloat? Some ARM chips have hardware support for floating point (hardfloat), and some do floating point through software (softfloat). An operating system running on an ARM chip that supports hardware floating point can use the floating point registers for parameter passing during function calls, which improves performance. This parameter passing is a contract between the OS, libraries and applications (such as the JVM) called the ABI or Application Binary Interface. In the simple case, an OS exposing the softfloat ABI requires all libraries and applications to be compiled against softfloat, and an OS exposing hardfloat requires libraries and applications to be compiled against hardfloat. There is a special case where a hardfloat OS can provide a compatibility layer and therefore enable softfloat applications to work. Until recently, almost all Linux distributions were softfloat. Lately, Linux distributions have aggressively moved to hardfloat. Some – I believe Ubuntu 12.04 is a good example – also provides softfloat compatibility. Raspbian on the other hand is hardfloat only. The initial release of the Oracle JDK for ARM uses the softfloat ABI and so works on softfloat distributions, or hardfloat with softfloat compatibility, but not on hardfloat. This is just a matter of timing – we will provide a hardfloat JDK at some point in the future. It will likely be done iteratively, so we may for instance deliver ARMv7 first and ARMv6 later, and the initial release may be headless so no Swing/AWT. We will produce public early access builds as soon as we are able and make them available on java.net. We don’t have any dates to share yet, but will hopefully be able to provide a roadmap at JavaOne 2012.

Viewing all articles
Browse latest Browse all 36

Trending Articles