Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

Using SPI

Cai Biesinger edited this page Jul 10, 2018 · 4 revisions

Unfamiliar with SPI? We have a whole guide on it.

Scarlet supports SPI usage on the Beaglebone Black and Raspberry Pi, but all SPI buses have the same core methods as required by the Scarlet.IO.ISPIBus interface (shown below).

ISPIBus Interface Methods

  • byte[] Write(IDigitalOut DeviceSelect, byte[] Data, int DataLength)
    • Writes (and simultaneously reads) given data (and Data Length) on the SPI bus using the DeviceSelect digital output as the chip/device select.
  • void Dispose()
    • Used to dispose of the object and free resources when the bus is no longer needed.

Usage on BeagleBone Black

After system initialization, we will need to set the pins for the SPI bus, check out this chart for help choosing pins. For example, we will use pins P9_21 for MISO, P9_22 as clock, and no pin for MOSI (This means that we only have one-way communication from slave to master).

It is important that these pins come from the same bus.

BBBPinManager.AddMappingsSPI(BBBPin.P9_31, BBBPin.NONE, BBBPin.P9_22);

Now, we need to select Chip Select pins. These can be any IDigitalOut device, but for this example we'll use other GPIO pins, so we need to add a mapping for this as well:

BBBPinManager.AddMappingSPI_CS(BBBPin.P9_12);

As always, apply the pin settings before trying to use the systems. See here.

Once that is all set up, we can define the bus as such:
Note that, like I2C, on the BBB we don't instantiate the bus ourselves, instead we use an instance provided by Scarlet.

ISPIBus SPI0 = SPIBBB.SPIBus0; // Bus 0 since the pins we defined are on bus number 0

After we define the bus, we must define a chip select by creating the GPIO object:

IDigitalOut DeviceSelect = new DigitalOutBBB(BBBPin.P9_12);

Now you have the required setup to use the SPI buses provided by the BeagleBone! Most sensors simply need these two items passed into their constructor, then they are ready for use.


Usage on Raspberry Pi

Usage on Raspberry Pi is very simple. Just choose the SPI bus you'd like to use, and instantiate it.

ISPIBus SPI0 = new SPIBusPi(0);

Also instantiate your chip select lines. Once again, any kind of IDigitalOut is supported. Here I'll just use the Pi's GPIO:

IDigitalOut ChipSelect = new DigitalOutPi(31);

Now you can use SPI devices the same as on the BeagleBone, just pass these two objects into the constructor.

Logo

Quick Links:
NuGet
Pin Diagrams: RPi | BBB
Developers: CaiB, Baldstrom

General Info:
Home
Common Issues
Getting Started
Supported Devices

Sections:
Logging
DataLog
Filters
Hardware I/O:
- BeagleBone Black
- Raspberry Pi
- Pin Diagrams: RPi | BBB
- GPIO: Using | For Beginners
- PWM: Using | For Beginners
- ADC: Using | For Beginners
- I2C: Using | For Beginners
- SPI: Using | For Beginners
- UART: Using | For Beginners
- CAN: Using | For Beginners
Networking
Sensors
StateStore

Other: Interesting Case Studies

Clone this wiki locally