enableSPI
Enable SPI interface
Syntax
Description
enableSPI(
enables the SPI bus. You can
then use the Raspberry Pi® hardware board pins SPI0_MOSI (GPIO 10), SPI0_MISO (GPIO 9), and SPI0_SCLK
(GPIO 11) as SPI pins and interface the Raspberry Pi hardware boards with any SPI devices to exchange data. The SPI bus is enabled
by default. To disable SPI, use mypi
)disableSPI
.
Examples
Connect and Read Data from SPI Device
Enable and disable the SPI interface on the Raspberry Pi kernel and use the Raspberry Pi hardware board pins for SPI functionality and GPIO, respectively. Write and read data from the EEPROM SPI IC 25AA080 interfaced with the Raspberry Pi hardware board and exchange data. For more information on IC 25AA080, refer to its datasheet. You can also exchange data between the Raspberry Pi and other SPI devices. For more information, refer to the datasheet of the specific SPI device. Perform these steps on the Raspberry Pi Linux® terminal to ensure that the SPI interface is enabled in the hardware kernel.
Run this command:
sudo raspi-config
Select Interfacing Options > SPI.
Select Yes when prompted to enable the SPI interface.
Select Yes when prompted to automatically load the SPI kernel module.
Select Finish.
Select Yes when prompted to reboot.
Tip
You can also enable the SPI interface using the Raspberry Pi Resource Monitor App.
To create a connection from MATLAB® to the Raspberry Pi board, execute this command at the MATLAB Command Window.
mypi=raspi;
DeviceAddress: 'raspberrypi-hysdu8X38o' Port: 18734 BoardName: 'Raspberry Pi 3 Model B+' AvailableLEDs: {'led0'} AvailableDigitalPins: [4 5 6 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27] AvailableSPIChannels: {‘CE0’,’CE1’} AvailableI2CBuses: {'i2c-1'} I2CBusSpeed: 100000 AvailabelWebCams: mmal service 16.1 (platform:bcm2835-v4l2)
MISO (GPIO 9)
, MOSI (GPIO 10)
, and
SCLK (GPIO 11)
are used for SPI functionality. GPIO pins 9, 10, and
11 are not listed in the AvailableDigitalPins
property as the SPI
functionality is enabled by default and the pins are currently being utilized by the SPI
functionality.To display the pin mapping of the Raspberry Pi hardware board, execute this command at the MATLAB Command Window.
showPins(mypi);

The revised pin map of the Raspberry Pi hardware board shows the location of the SPI pins, SPI_MISO (GPIO
9)
, SPI_MOSI (GPIO 10)
, and SPI_SCLK (GPIO
11)
, on the CE1
bus.
After physically connecting your SPI device to the three SPI pins of the Raspberry Pi hardware board, establish a connection to the SPI device through the
CE1
bus.
eeprom = spidev(mypi, 'CE1')
eeprom = spidev with properties: Channel: CE1 Mode: 0 BitsPerWord: 8 Speed: 500000
Enable write and read operation on EEPROM.
enableCmd = 6;
Write data to EEPROM with the precision of
uint8
.
writeCmd = 2; address = [0 0]; dataIn = [01 02 03]; dummyData = zeros(1,length(dataIn)); writeRead(eeprom, enableCmd, 'uint8'); dataOut = writeRead(eeprom, [writeCmd address dataIn], 'uint8');
dataOut = 1×6 uint8 row vector 0 0 0 0 0 0
Read data from EEPROM with the precision of uint8
.
readCmd = 3;
dataOut = writeRead(eeprom, [readCmd address dummyData], 'uint8');
dataOut = 1×6 uint8 row vector 0 0 0 1 2 3
Clear the active SPI device connection before disabling it. After you disable the SPI functionality, you can use the Raspberry Pi hardware board pins as GPIO pins.
clear eeprom;
disableSPI(mypi);
mypi=raspi;
DeviceAddress: 'raspberrypi-hysdu8X38o' Port: 18734 BoardName: 'Raspberry Pi 3 Model B+' AvailableLEDs: {'led0'} AvailableDigitalPins: [4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27] AvailableSPIChannels: {} AvailableI2CBuses: {'i2c-1'} I2CBusSpeed: 100000 AvailabelWebCams: mmal service 16.1 (platform:bcm2835-v4l2)
GPIO 7, 8, 9, 10,
and 11
pins are now available
and can be used as GPIO pins. No SPI buses are available for SPI interfacing.Before using SPI again, enable the SPI functionality.
enableSPI(mypi);
Input Arguments
mypi
— Connection to Raspberry Pi hardware board
raspi
object (default)
Connection to a Raspberry Pi hardware board, specified as a raspi
object.
Version History
Introduced in R2014a
Commande MATLAB
Vous avez cliqué sur un lien qui correspond à cette commande MATLAB :
Pour exécuter la commande, saisissez-la dans la fenêtre de commande de MATLAB. Les navigateurs web ne supportent pas les commandes MATLAB.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)