树莓派2上面启用spi的方法和树莓派1不太一样。网上关于1代的设置方法很多,好不容易找到一个适合2代用的设置方法,相比起来简单了不少。最简单的方法当然是raspi-config!
树莓派2上开启SPI
The Raspberry Pi has a Serial Peripheral Interface (SPI) bus which can be enabled on Pins 19,21,23,24 & 26. It is a synchronous serial data link standard and is used for short distance single master communication between devices. As far as the Pi is concerned this is usually relevant to certain sensors and add-on boards.
There is more technical information about SPI on the SPI Wikipedia page if you are interested.
The default Raspbian image disables SPI by default so before you can use it the interface must be enabled. This can be done uses either of two methods. I’ll describe both methods but the first one is probably easier and quicker.
Method 1 – Enabling Using Raspi-config
Start by running the following command :
sudo raspi-config
This will launch the raspi-config utility. Select option 8 “Advanced Options”.
Select the “SPI” option.
Set the option to “Yes”.
Select “OK”.
Select “Finish”.
Reboot for the changes to take effect :
sudo reboot
SPI is now enabled.
Method 2 – Enabling By Editing A File Manually
This process assumes you are using the latest Raspbian build from the official downloads page. You can update your current image using :
sudo apt-get update
sudo apt-get upgrade
To enable hardware SPI on the Pi we need to make a modification to a system file :
sudo nano /boot/config.txt
Add the following line at the bottom :
dtparam=spi=on
Use CTRL-X, then Y, then RETURN to save the file and exit. Reboot using the following :
sudo reboot
Checking If SPI Is Enabled (Optional)To check if the SPI module is loaded by the system run the following command :
lsmod
You should see “spi_bcm2708″ listed in the output. You can use the following command to filter the list and make it easier to spot the spi entry :
lsmod | grep spi_
SPI is now enabled.
Install Python SPI Wrapper (Optional)In order to read data from the SPI bus in Python we can install a library called ‘py-spidev’. To install it we first need to install ‘python-dev’ :
sudo apt-get install python2.7-dev
Then to finish we can download ‘py-spidev’ and compile it ready for use :
wget https://github.com/Gadgetoid/py-spidev/archive/master.zip
unzip master.zip
rm master.zip
cd py-spidev-master
sudo python setup.py install
cd ..
You should now be ready to either communicate with add-on boards using their own libraries (e.g. the PiFace) or other SPI devices (e.g. the MCP3008 ADC).