BBBCSIO

The SPIPortFS Class

 

The SPIPortFS Class

The SPIPort class writes to and reads from the one of the two SPI bus On Chip Peripheral (OCP) devices integrated into the Beaglebone Black CPU via the SPIDev device driver access method. At this time there is no BBBCSIO class of similar functionality which uses the Memory Mapped access method.

 

Source Code

The source code for this class is available online for download and also in browseable form.

 

Example Code

Example code which demonstrates the use of this class can be found on the SPI Port Example page.

 

Syntax

public class SPIPortFS : PortFS

Provides Serial Peripheral Interconnect (SPI) port functionality for a BeagleBone Black (SPIDev Version).

 

Public Constructors

public SPIPortFS(SPIPortEnum spiPortIn) : base(GpioEnum.GPIO_NONE)

Constructor.

Parameters
spiPortIn - The SPI port to access.

 

Public Methods

public override void ClosePort()

Closes the port. Because we do not open the port at the PortFS level we cannot close it there either. This overrides the PortFS.ClosePort() and does what is necessary internally to close things up.

 

public void Dispose()

Implements IDisposable. Should be called after the class is closed to release resources.

 

public SPISlaveDeviceHandle EnableSPIGPIOSlaveDevice(GpioEnum gpioEnum)

Enables the SPI slave device which uses a GPIO pin as a slave select.

When using a GPIO pin as a slave select line we still need to open a SPIDev device because we need a device to send the data to. The way SPIDev works each spidev device is fundamentally associated with a particular slave select line and this cannot be changed. The GPIO line will be used as a separate slave select but the spidev device specific slave select will also be asserted whenever the device is writtent to.

In order to use a GPIO as a slave select you must ignore and not electrically attach anything to the slave select pin the SPIDEV device uses as it will be asserted on each write.

In other words, the SPIDev device is needed to shift the data in and out, however you must ignore its internal slave select line entirely if you wish to use GPIO based slave selects. Otherwise any device attached to it will be receive every write to the SPIPort no matter which GPIO slave select is also asserted.

Parameters
gpioEnum, The GPIO of the pin we use as the slave select.

Returns
The handle for the Slave Device or null for fail.

 

public SPISlaveDeviceHandle EnableSPISlaveDevice(SPISlaveDeviceEnum spiSlaveDeviceIn)

Enables the SPI slave device. Will throw exceptions if it cannot open the device

Parameters
spiSlaveDeviceIn, The SPI Slave Device to configure.

Returns
The handle for the Slave Device or null for fail.

 

public uint GetDefaultSpeedInHz()

Gets the Default SPI speed in Hertz. This value can be overridden on a per slave basis by setting the SpeedInHz in the SPISlaveDeviceHandle

Returns
The spi port speed in Hertz.

 

public SPIModeEnum GetMode()

Gets the SPI mode. This is used for all Slave Devices on the port It cannot be individually set on a per slave basis. At least one non-GPIO slave must be enabled

Returns
The spi mode.

 

public override PortDirectionEnum PortDirection()

Gets the PortDirection

Returns
A PortDirectionEnum value. This value is always PortDirectionEnum.PORTDIR_INPUTOUTPUT.

 

public void SPITransfer(SPISlaveDeviceHandle ssHandle, byte[] txByteBuf, byte[] rxByteBuf, int numBytes)

Writes/Reads a buffer out/in to/from an SPI Slave Device.

Note that the SPI protocol always reads a byte for every byte sent. If you send a byte you get a byte. If you do not sent a byte you will never receive a byte since this code operates as an SPI Master. Thus if you only wish to receive you must send an equivalent number of bytes. The bytes you send are determined by the Slave. Sometimes this is just 0x00 and sometimes it represents an address to read - exactly what you, send is entirely slave device implementation dependent. If you only wish to transmit, not receive, just use NULL for your rxBuffer. The SPI port will still receive, of course, but you will not be bothered by it.

The Slave Select is set when you opened the port. The rule is one slave to one SPISlaveDeviceHandle.

Parameters
ssHandle, The SPI Slave Device handle to write to.
txByteBuf, The buffer with bytes to write.
rxByteBuf, The buffer with bytes to receive. Can be NULL.
numBytes, The number of bytes to send/receive.

 

public void SetDefaultSpeedInHz(uint spiSpeedInHz)

Sets the Default SPI speed in Hertz. This value can be overridden on a per slave basis by setting the SpeedInHz in the SPISlaveDeviceHandle

Parameters
spiSpeedInHz, The speed in Hertz to set.

 

public void SetMode(SPIModeEnum spiMode)

Sets the SPI mode. This is used for all Slave Devices on the port It cannot be individually set on a per slave basis. At least one non-GPIO slave must be enabled

Parameters
spiMode, The spi mode to set.

 

Public Properties

public SPIPortEnum SPIPort { get; }

Gets the SPI Port. There is no Set accessor this is set in the constructor

value
The working SPIPortEnum.