SPI5 not working second time around

I have two MAX31855 thermocouple IC’s connected to SPI5 MISO and SCK with 2 GPIO for the chip select. I am using an SCM210260D module. The MAX13855 is read only, 32 bits.

The first time I run the code, it reads back the internal cold junction temp on only one of the devices and then second read gets back all zero’s from both devices. Then all reads are now zero. If I swap the reads around, and read the second device first, it works and then all zero’s for subsequent reads so it doesn’t appear to be a hardware issue. I’ll hook up the scope shortly to check it out. The following is the code I am using to setup and read the sensors.

init code

private static SpiController spiController = SpiController.FromName(SC20260.SpiBus.Spi5);

private static GpioPin csLower = GpioController.GetDefault().OpenPin(SC20260.GpioPin.PA0);
private static GpioPin csUpper = GpioController.GetDefault().OpenPin(SC20260.GpioPin.PC2);

private static SpiConnectionSettings lowerSpiSettings = new SpiConnectionSettings()
{
    DataFrameFormat = SpiDataFrame.MsbFirst,
    ChipSelectType = SpiChipSelectType.Gpio,
    ChipSelectLine = csLower,
    Mode = SpiMode.Mode0,
    ClockFrequency = 4000000,
    ChipSelectHoldTime = TimeSpan.FromMilliseconds(5),
};
private static SpiConnectionSettings upperSpiSettings = new SpiConnectionSettings()
{
    DataFrameFormat = SpiDataFrame.MsbFirst,
    ChipSelectType = SpiChipSelectType.Gpio,
    ChipSelectLine = csUpper,
    Mode = SpiMode.Mode0,
    ClockFrequency = 4000000,
    ChipSelectHoldTime = TimeSpan.FromMilliseconds(5),
};

static SpiDevice lowerSpiDevice = spiController.GetDevice(lowerSpiSettings);
static SpiDevice upperSpiDevice = spiController.GetDevice(upperSpiSettings);

Code to read the sensor. Adapted from Adafruit Arduino code. This is the first sensor. There is another function that reads the second sensor. No optimisation yet to reduce duplication. I need it working first.

private static float readLower()
{
    byte[] data = new byte[4];

    lowerSpiDevice.Read(data);

    ulong value = (ulong)((data[0] << 24) + (data[1] << 16) + (data[2] << 7) + data[3]);

    ulong valueInternal = value >> 4;

    float temp = valueInternal & 0x7FF;

    if((valueInternal & 0x800) == 0x800)
    {
        Int16 tmp = (short) (0xF800 | (valueInternal & 0x7FF));

        temp = tmp;
    }
    internalTemp1 = temp * 0.0625F;

    ulong valueExternal = value;

    if((valueExternal & 0x80000000) == 0x80000000)
    {
        valueExternal = 0xFFFFC000 | ((valueExternal >> 18) & 0x00003FFF);
    }
    float temp1 = valueExternal * 0.25F;

    bool error = (value & (ulong) 0x1000) == 0x1000;

    bool open = (value & (ulong) 0x01) == 0x01;

    return temp1;
}

Looking forward to seeing the scope capture

1 Like

I hooked up the SCOPE to the CS, SCK and DATA lines and on the first read I get the data but the CS is not working. It is always low. I would expect this to be HIGH at reset but it remains low.

I updated the source to set the GPIO pin direction as output and set the pin for HIGH but both CS pins remain low.

I am using PA0_C and PC2_C on the SCM20260D module and checking the datasheet for the STM32 processor shows these as analog inputs only. :frowning: This needs to be marked on the pinout for the module that these are ADC INPUT only. It’s confusing using PA0 as part of the pinout name as there is also another PA0 pin but not GHI fault but ST fault :grinning: Same for the PC2_C pin.

Can GHI also add a link to the processor datasheet and save us having to look this up by reading the part code from the device? :wink:

Can you explain why did you look at the processor datasheet? There is no gpio feature listed on those pins in pinout and they are not included in pins class.

Hi Gus,

They are on the pinout drawing for the SCM2026D and also on the schematic. I had mistakenly assumed this was PA0 but it wasn’t. I had to add 2 links to the board and the SPI is now working.

pa0_c

@Greg_Norris can we please clarify this? I thought this was clear but it is obviously not.

1 Like

Docs (pinout) has been clarified, thanks for pointing this out.

1 Like