Main Site Documentation

Debuging SPI is Working Weirdly!


#1

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);
            }
        }        
    }
}


#2

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


#3

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);
            }
        }


#4
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);
            }
        }
    }
}

#5

Thanks Mr. Justin,

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


#6

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.


#7

@ PetaByte - got a link to the imu?


#8

Hi Mr. Brett,

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


#9

@ justin
yes here it is:
http://www.analog.com/en/mems-sensors/mems-inertial-measurement-units/adis16407/products/product.html

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 …


#10

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.


#11

Much was fixed on spi.


#12

@ 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?


#13

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


#14

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 !!


#15

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.


#16

The one that I found is:

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

Thnx


#17

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.