SPI documentation?

I’m confused by the sample for SPI at https://docs.ghielectronics.com/software/tinyclr/tutorials/spi.html. The variable cs is of type GpioPin, but the ChipSelectLine in the SpiConnectionSettings expects an integer. Since there doesn’t seem to be detailed documentation for TinyCLR v1.0 (at least I can’t find it), I am guessing it should be Pins.SC20260.GpioPin.PE4.

Also my device (the HOLTEK HT1632C) sometimes needs to write 10 bits and other times 12 or 14 bits. Can I create two devices, where they only differ by the DataBitLength?

Also, if I have two devices sharing the one SPI interface, just with different CS lines, how do I set that up? Do I simply create two devices? And finally, what exactly is the calling format for TransferSequential and for TransferFullDuplex ?

Thanks!

It was int and now GPIO. Old docs are here (unsupported) https://github.com/ghi-electronics/Docs

SPI by nature is 8bit. Always use 8bit and only 8bit is supported. If your device needs 12 bit, it must support you sending 2 8bit transactions (16bits) and then your device will ignore the extra 4 bits.

Create 2 instances and each one has a different chip select.

Always use Full Duplex. The other one is advance use. That is how SPI works, it swaps the bytes between master and slave, always full duplex.

So I’m trying to use SPI to write to the device. I’m trying to write 2 bytes to the device (though it only wants 12 bits, it might be able to ignore leading 0). I get an Invalid Operation Exception deep in the device.Write:

#### Exception System.InvalidOperationException - CLR_E_INVALID_OPERATION (1) ####
#### Message: 
#### GHIElectronics.TinyCLR.Devices.Spi.Provider.SpiControllerApiWrapper::SetActiveSettings [IP: 0000] ####
#### GHIElectronics.TinyCLR.Devices.Spi.SpiController::SetActive [IP: 000b] ####
#### GHIElectronics.TinyCLR.Devices.Spi.SpiDevice::Write [IP: 000b] ####
#### GHIElectronics.TinyCLR.Devices.Spi.SpiDevice::Write [IP: 0009] ####
#### HT1632.HT1632::sendCommand [IP: 003c] ####
#### HT1632.HT1632::init [IP: 0040] ####
#### ClockMk2.Program::Main [IP: 0009] ####

Exception thrown: ‘System.InvalidOperationException’ in GHIElectronics.TinyCLR.Devices.Spi.dll
An unhandled exception of type ‘System.InvalidOperationException’ occurred in GHIElectronics.TinyCLR.Devices.Spi.dll

I set up the device as follows:

var cs = FEZ.GpioPin.D4;
var csPin = new GpioController.GetDefault().OpenPin(cs);
csPin.SetDriveMode(GpioPinDriveMode.Output);
csPin.Write(GpioPinValue.High);
var controller = SpiController.FromName(FEZ.SpiBus.Spi1);
var device = controller.GetDevice(new SpiConnectionSettings() {
        ChipSelectType = SpiChipSelectType.Gpio,
        Mode = SpiMode.Mode0,
        ClockFrequency = 1_000,
        ChipSelectLine = cs
});
byte[] byteBuffer = new byte[2];
byteBuffer[0] = 0x08;
byteBuffer[1] = 0x02;
csPin.Write(GpioPinValue.Low);
device.Write(byteBuffer);
csPin.Write(GpioPinValue.High);

I assume I didn’t initialize the device properly. What am I doing wrong?

Did I mention that the device is not really an SPI device? It has two clock inputs, one for READ/ and one for WRITE/. The data pin can be input or output depending on which clock is active. I never have to read, so I hope I can just drive the write clock with SCK and have the data pin connected to MOSI.

The number of bits to be transferred is variable and depends on the first 3 bits being transferred which determine how many bits are following. Thankfully the first bit is always high, so I hope it will ignore leading low bits…

If it’s not really an SPI device, then it’s probably a good target for bit-banging. What device is it?

(my reason for thinking bit-banging is the correct thing to do here is that SPI really likes to send in 8-bit chunks, and a device expecting 12-bits may not handle extra clk cycles)