The SPIPort class writes to and reads from the SPI bus On Chip Peripheral (OCP) device integrated into the Raspberry Pi 2 CPU via the SPIDev device driver access method. At this time there is no RPICSIO class of similar functionality which uses the Memory Mapped access method.
IMPORTANT NOTE: Before creating any RPICSIO Port class you will need to ensure that the RPICSIO Library knows which type of Raspberry Pi you are dealing with. This is done by executing a call (only once is necessary) like the one below somewhere in your code before you create any port.
RPICSIOConfig.Instance.RPIType = RPITypeEnum.RPITYPE_RPI2;
public class SPIPortFS : PortFS
public SPIPortFS(SPIPortEnum spiPortIn) : base(GpioEnum.GPIO_NONE)
public override void ClosePort()
public void Dispose()
public SPISlaveDeviceHandle EnableSPIGPIOSlaveDevice(GpioEnum gpioEnum)
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.
public SPISlaveDeviceHandle EnableSPISlaveDevice(SPISlaveDeviceEnum spiSlaveDeviceIn)
public uint GetDefaultSpeedInHz()
public SPIModeEnum GetMode()
public override PortDirectionEnum PortDirection()
public void SPITransfer(SPISlaveDeviceHandle ssHandle, byte[] txByteBuf, byte[] rxByteBuf, int numBytes)
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.
public void SetDefaultSpeedInHz(uint spiSpeedInHz)
public void SetMode(SPIModeEnum spiMode)
public SPIPortEnum SPIPort { get; }