System.ArgumentException using SPI

Whats wrong with this?
I get this error:

The thread '<No Name>' (0x2) has exited with code 0 (0x0).
    #### Exception System.ArgumentException - 0xfd000000 (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] ####
    #### VepelTest.Program::Main [IP: 0085] ####
Exception thrown: 'System.ArgumentException' in GHIElectronics.TinyCLR.Devices.Spi.dll
An unhandled exception of type 'System.ArgumentException' occurred in GHIElectronics.TinyCLR.Devices.Spi.dll

  class Program
    {
        static GpioController gpio;
        static SpiDevice spiDevice;
        static SpiController spiController3;
        static GpioPin oe;
        static GpioPin la;

        static void Main()
        {
            gpio = GpioController.GetDefault();
            spiController3 = SpiController.FromName(SC20100.SpiBus.Spi3);

            SpiConnectionSettings spiConnectionSettings = new SpiConnectionSettings()
            {
                ChipSelectType = SpiChipSelectType.None,
                ChipSelectActiveState = false,
                Mode = SpiMode.Mode0,
                ClockFrequency = 1000,
                DataFrameFormat = SpiDataFrame.LsbFirst
            };

            spiDevice = spiController3.GetDevice(spiConnectionSettings);

            oe = gpio.OpenPin(SC20100.GpioPin.PA15);
            oe.SetDriveMode(GpioPinDriveMode.Output);
            la = gpio.OpenPin(SC20100.GpioPin.PA14);
            la.SetDriveMode(GpioPinDriveMode.Output);

            la.Write(GpioPinValue.High);
            spiDevice.Write(new byte[] { 170, 120 });
            la.Write(GpioPinValue.Low);

            while (true)
            {
                oe.Write(GpioPinValue.High);
                Thread.Sleep(1000);
                oe.Write(GpioPinValue.Low);
                Thread.Sleep(1000);
            }



        }


    }

What line in the code causes the error?

spiDevice.Write(new byte[] { 170, 120 });

Unable to test your code at the moment, but theres two things that I could suspect from causing this.

  1. Your spi clock is very low, only 100hz, are you sure this low of a frequency is supported?
  2. You are setting the cs setting to none, but then you also set the activestate to false. This seems a bit counterintuitive, have you tried removing the ChipSelectActivateState = false line from the settings?
1 Like

Yes, the clock is very low.

It was the clock, strangely this clock frequency worked in TinyCLR 1

That was on a completely different hardware