Debuging SPI is Working Weirdly!

Hi, I am sending (0x1A00) to the IMU in order to read the temperature value (which I expect it to be encoded as 207), but somehow I got sometimes 1 value, 2 values and sometimes nothing !!

Here is the output:

Here is the code:


using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;

using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using GTI = Gadgeteer.Interfaces;
using Gadgeteer.Modules.GHIElectronics;
using System.IO;
using Microsoft.SPOT.IO;

using GHI.OSHW.Hardware;
using Microsoft.SPOT.Hardware;


namespace GadgeteerApp7
{

    public partial class Program
    {

        
        //SPI
        static GT.Socket _socket;
        static GTI.SPI.Configuration _config;
        static GTI.SPI _spi;

        
        // This method is run when the mainboard is powered up or reset.   
        public void ProgramStarted()
        {

            // SPI Config
            _socket = GT.Socket.GetSocket(1, true, null, null);
            _config = new GTI.SPI.Configuration(true, 0, 0, true, true, 1000);
            _spi = new GTI.SPI(_socket, _config, GTI.SPI.Sharing.Exclusive, _socket, GT.Socket.Pin.Five, null);

            while (true)
            {
                // SPI Reading
                byte[] BytesWrite = new byte[] { 0x1A , 0x00 };
                byte[] SPIBuffer = new byte[2];
                _spi.WriteRead(BytesWrite, SPIBuffer);

                // SPI writing to SD
                Debug.Print("IMU Message: ");
                Debug.Print(SPIBuffer[0].ToString());
                Debug.Print(SPIBuffer[1].ToString());
                Thread.Sleep(1000);
            }
        }        
    }
}

You should not have any long running code in ProgramStarted()

Put

while (true)
            {
                // SPI Reading
                byte[] BytesWrite = new byte[] { 0x1A , 0x00 };
                byte[] SPIBuffer = new byte[2];
                _spi.WriteRead(BytesWrite, SPIBuffer);
 
                // SPI writing to SD
                Debug.Print("IMU Message: ");
                Debug.Print(SPIBuffer[0].ToString());
                Debug.Print(SPIBuffer[1].ToString());
                Thread.Sleep(1000);
            }

in a separate thread

Thanks @ justin

I separated this in another function but I think I still have to put it in separated thread. I am looking for resources for that.


        // This method is run when the mainboard is powered up or reset.   
        public void ProgramStarted()
        {
            ReadSPI();

        }

        public void ReadSPI()
        {

            // SPI Config
            _socket = GT.Socket.GetSocket(1, true, null, null);
            _config = new GTI.SPI.Configuration(true, 0, 0, true, true, 1000);
            _spi = new GTI.SPI(_socket, _config, GTI.SPI.Sharing.Exclusive, _socket, GT.Socket.Pin.Five, null);

            while (true)
            {
                // SPI Reading
                byte[] BytesWrite = new byte[] { 0x1A, 0x00 };
                byte[] SPIBuffer = new byte[2];
                _spi.WriteRead(BytesWrite, SPIBuffer);

                // SPI writing to SD
                Debug.Print("IMU Message: ");
                Debug.Print(SPIBuffer[0].ToString());
                Debug.Print(SPIBuffer[1].ToString());
                Thread.Sleep(1000);
            }
        }

using System.Threading;
using Microsoft.SPOT;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using GTI = Gadgeteer.Interfaces;


namespace GadgeteerApp43
{
    public partial class Program
    {
        //SPI
        static GT.Socket _socket;
        static GTI.SPI.Configuration _config;
        static GTI.SPI _spi;
        byte[] _bytesWrite;
        byte[] _spiBuffer;
 
        void ProgramStarted()
        {
            _socket = GT.Socket.GetSocket(1, true, null, null);
            _config = new GTI.SPI.Configuration(true, 0, 0, true, true, 1000);
            _spi = new GTI.SPI(_socket, _config, GTI.SPI.Sharing.Exclusive, _socket, GT.Socket.Pin.Five, null);
            Thread spiThread = new Thread(ImuThread);
            spiThread.Start();
        }

        private void ImuThread()
        {
            for(;;)
            {
                _bytesWrite = new byte[] { 0x1A, 0x00 };
                _spiBuffer = new byte[2];
                _spi.WriteRead(_bytesWrite, _spiBuffer);
 
                // SPI writing to SD
                Debug.Print("IMU Message: ");
                Debug.Print(_spiBuffer[0].ToString());
                Debug.Print(_spiBuffer[1].ToString());
                Thread.Sleep(1000);
            }
        }
    }
}

Thanks Mr. Justin,

but it seems that we have the same issue even with threads :S

away from a VS session, but does WriteRead() return a value? If so can you check the return code? Edit: no it doesn’t. Scratch that.

@ PetaByte - got a link to the imu?

Hi Mr. Brett,

Unfortunately it is a “void” function, so it returns nothing, Regards …

@ justin
yes here it is:

BTW the SPI settings for the IMU are:
CPOL(Clock Polarity) = 1, CPHA(Clock Phase) = 1
Because of that we configured the Cerbuino SPI as:

_config = new GTI.SPI.Configuration(true, 0, 0, true, true, 1000);

Regards …

Which board are you using? The Cerberus firmware had broken SPI for a long time. Not sure if they ever fixed it. I submitted a patch on the forum, but I’m not sure it ever made it into the firmware. The problem I was having was that the clock line would start running before the cs pin asserted. Simple fix in the firmware, but it obviously involves compiling the firmware from the porting kit: a daunting task for most people.

Much was fixed on spi.

@ jay - @ Gus

Thanks guys,

I am using FEZ Cerbuino Bee with .NET MF SDK (QFE2) and VS 2010.
I am not sure weather SPI is configured fine in the firmware or not?

what SDK / firmware version do you have? MFDeploy ->device capabilities

Hi Mr. @ Brett,

MFDeploy didn’t recognize it although it’s mentioned as (GHI NETMF Debug Interface) in the Device Manager -> Universal Serial Bus controllers !!

Try MFDeploy from the “other” version. If you ran MFDeploy from 4.1 directory, run the one in the 4.2 directory.

Also check the GHI SDK release notes from the SDK directory. On my PC:

C:\Program Files (x86)\GHI Electronics\GHI OSHW NETMF v4.2 SDK

is the directory and my release notes show:

[quote]SDK Version v1.0.5
April 30, 2013
[/quote]

If you’re not running the latest, you definitely want to move to that and update the firmware on all your devices.

The one that I found is:

So you think this Firmware has a corrupted SPI configuration and I have to update it?

Thnx

I think that there’s no point testing any further. Gus tells you there were many SPI fixes in the 4.2 series of framework updates. The one I point to is the newest. You aren’t using that. Unless you want to manually go through the release notes and see where it’s mentioned that there was a fix, but even then you’re still going to need to move forward, so go to the latest.