SPI : manual handling of CS pin

How do you set up SPI when you want to manually handle the CS pin ?

I have a working code when I set the SPI config like this :

    _spi = SpiController.FromName(socket.SpiBus).GetDevice(new SpiConnectionSettings()
                {
                    ChipSelectType = SpiChipSelectType.Gpio,
                    ChipSelectLine = GpioController.GetDefault().OpenPin(socket.Cs),
                    Mode = SpiMode.Mode0,
                    ClockFrequency = 8000000
                });

But if I use this :

    _spi = SpiController.FromName(socket.SpiBus).GetDevice(new SpiConnectionSettings()
                {
                    ChipSelectType = SpiChipSelectType.None,
                    Mode = SpiMode.Mode0,
                    ClockFrequency = 8000000
                });
    // Initialize Cs pin
                _cs = GpioController.GetDefault().OpenPin(socket.Cs);
                _cs.SetDriveMode(GpioPinDriveMode.Output);
                _cs.Write(GpioPinValue.High);

and I add _cs.Write(GpioPinValue.Low); and _cs.Write(GpioPinValue.High); before and after each transaction, then the same code does not work anymore.

What am I missing ?

Both are the same thing! Do you set and reset cs with every write?

You may want to look at the analyzer to see if there are any differences.

Is there a minimum delay required between CS change and the beginning of the write?

@Mike : I don’t think so. But, in a way, I would say that you were not that far.

I have the device (FT810) now working with manual handling of CS. But… not really entirely.
I have to use the “automatic” CS handling for its init sequence. Once this init sequence is over, I can use a manual CS.
And that’s why I think that you were probably not that far about timings, but it seems to be the other way round : manual handling seems to slow for the init sequence and the device does not initialize correctly, hence returning wrong values afterward.

By the way, disposing the SPI configuration does not dispose the SpiSelectLine in it. I had to dispose it manually and then dispose the config before being able to reuse the CS line.
I don’t know if it’s an expected behavior, but you have to know it otherwise you get a nice exception.

Yes, disposing SPI does not dispose CS. Spi take an opened pin, not open the pin. So Spi does not have responsible to close it.

2 Likes

Thank you. I understand that and that’s indeed the expected behaviour.