Main Site Documentation

TINYCLR - How to use more than one SPI device on a bus?


#1

I have two SPI devices on the same bus on a Quail (STM32F427) and I get an exception on spi2 declaration when I run the following code :

private static void TestSPI()
        {
            var spiConfig1 = new SpiConnectionSettings(3)
            {
                ClockFrequency = 12000000,
                DataBitLength = 8,
                Mode = SpiMode.Mode0,
                SharingMode = SpiSharingMode.Shared
            };
            var spi1 = SpiDevice.FromId("GHIElectronics.TinyCLR.NativeApis.STM32F4.SpiProvider\\0", spiConfig1);

            var spiConfig2 = new SpiConnectionSettings(65)
            {
                ClockFrequency = 12000000,
                DataBitLength = 8,
                Mode = SpiMode.Mode0,
                SharingMode = SpiSharingMode.Shared
            };
            var spi2 = SpiDevice.FromId("GHIElectronics.TinyCLR.NativeApis.STM32F4.SpiProvider\\0", spiConfig2);
        }

The thread '<No Name>' (0x2) has exited with code 0 (0x0).
    #### Exception System.Exception - CLR_E_FAIL (1) ####
    #### Message: 
    #### GHIElectronics.TinyCLR.Devices.Spi.Provider.DefaultSpiDeviceProvider::InitNative [IP: 0000] ####
    #### GHIElectronics.TinyCLR.Devices.Spi.Provider.DefaultSpiDeviceProvider::.ctor [IP: 0029] ####
    #### GHIElectronics.TinyCLR.Devices.Spi.Provider.DefaultSpiControllerProvider::GetDeviceProvider [IP: 0008] ####
    #### GHIElectronics.TinyCLR.Devices.Spi.SpiDevice::FromId [IP: 004c] ####
    #### TinyCLRApplication1.Program::TestSPI [IP: 0053] ####
    #### TinyCLRApplication1.Program::Main [IP: 0004] ####
Exception thrown: 'System.Exception' in GHIElectronics.TinyCLR.Devices.dll

Each one is running fine alone and both are running fine as well when the second one is on another bus.

I thought that SpiSharingMode.Shared would allow that. Am I wrong ?


#2

then it may be a bug on our end. Any suggestions for a setup with a simple example code?


#3

An empty Quail board and the above code is the simplest setup you can use to reproduce the issue, in fact. As it does not pass the second SPI device creation.

By the way, how is data sent to the device when you select 16 bits length ? MSB or LSB first ?


#4

@ Bec a Fuel - It’s a known issue in the current release. MSB/LSB is determined by the specific SPI provider. So for STM32F4, the order is whichever the datasheet for the specific processor specifies. The HAL driver in the ports repo just writes/reads the entire 8/16 bit value to/from the DR register.