Welcome Guest, you are in: Login

Netduino Wiki

RSS RSS

Navigation





Search the wiki
»

PoweredBy

SPI

RSS
Modified on 2012/06/12 15:18 by Kristoffer Categorized as Uncategorized

Introduction

SPI stands for "Serial Peripheral Interface". It's an interface on which you can connect multiple SPI supported devices. It uses a master/slave configuration where the Netduino is always the master. To connect a device to SPI on the Netduino it uses four pins. These pins are most commonly named SS, MOSI, MISO and SPCK.

SPI Pins

SS: Slave Select. There can be multiple SS-pins but only one should be active at the same time. By changing the active SS-pin, you change the active device. Netduino: Any GPIO-pin (Pins.GPIO_PIN_*).
MOSI: Master Out, Slave In. This is a serial transmit-pin. Multiple devices can be connected to this one. Netduino: Pin 11 (SPI_Devices.SPI1).
MISO: Master In, Slave out. This is a serial receive-pin. Multiple devices can be connected to this one. Netduino: Pin 12 (SPI_Devices.SPI1).
SCLK: Serial Clock. This defines the connection speed of the serial bus. Multiple devices can be connected to this one. Netduino: Pin 13 (SPI_Devices.SPI1).

Connect a SPI device

This illustration gives a perfect view of how to connect two SPI-devices to a single Netduino:

Image

Yellow is SCLK
Purple is MISO
Green is MOSI
Orange is SS1 (to first SPI device)
Brown is SS2 (to second SPI device)
Blue is SS3 (to third SPI device)


Some (but not all) SPI devices can also work in serie without using an additional SS-pin. For example, the 74HC595 bitshift IC can be placed in serie. This would be a bit like this:

Image

You can see that CLCK (yellow) and SS (orange) goes parallel to all IC's The MOSI (green) goes to the first IC and from there to the second IC and from there to the third IC. This way you don't need to use three SS-pins but only one. Also a small detail, it only uses three pins. This is because the 74HC595 IC is write-only.

Code samples

Intializing multiple SPI devices

The naming of SPI classes in .NET MF 4.1 is not very clear, but this how it works if you want to use multiple devices:

// Defines the first SPI slave device with pin 10 as SS
SPI.Configuration Device1 = new SPI.Configuration(
    Pins.GPIO_PIN_D10, // SS-pin
    false,             // SS-pin active state
    0,                 // The setup time for the SS port
     0,                 // The hold time for the SS port
    true,              // The idle state of the clock
    false,             // The sampling clock edge
    1000,              // The SPI clock rate in KHz
     SPI_Devices.SPI1   // The used SPI bus (refers to a MOSI MISO and SCLK pinset)
);

// Defines the second SPI slave device with pin 9 as SS
SPI.Configuration Device2 = new SPI.Configuration(
    Pins.GPIO_PIN_D9,  // SS-pin
     false,             // SS-pin active state
    0,                 // The setup time for the SS port
    0,                 // The hold time for the SS port
    true,              // The idle state of the clock
     false,             // The sampling clock edge
    1000,              // The SPI clock rate in KHz
    SPI_Devices.SPI1   // The used SPI bus (refers to a MOSI MISO and SCLK pinset)
);

// Initializes the SPI bus, with the first slave selected
 SPI SPIBus = new SPI(Device1);

/*
Here you can talk with Slave 1
*/

// We switch to the second slave
SPIBus.Config = Device2;

/*
Here we can talk with Slave 2
*/

SPIBus.Config = Device1;

/*
Here you can talk with Slave 1 again
*/

Writing to a SPI-device

// Writes a single byte with value 255 to the SPI device
byte[] WriteBuffer = new byte[1];
WriteBuffer[0] = 255;
SPIBus.Write(WriteBuffer);

Reading from a SPI-device

It's not possible to read without writing.

// Making an empty write buffer and empty read buffer
byte[] WriteBuffer = new byte[1];
byte[] ReadBuffer = new byte[1];
SPIBus.WriteRead(WriteBuffer, ReadBuffer);
Debug.Print(ReadBuffer[0].ToString());
  Name Size
- bitshift in serie.png 111.99 KB
- general spi.png 86.35 KB

ScrewTurn Wiki version 3.0.4.560. Some of the icons created by FamFamFam.