Irregular SPI Behavior

Status
Not open for further replies.

Jay8ee

Member
Hello.

I'm using Teensy as an SPI master for a project I am working on, but I am having some trouble getting reliable data transmission working. I am seeing the transmitted byte corrupted approximately 50% of the time. I have run the below test on my Teensy LC and a 3.6 and I see the same on both.

I have connected the Teensy to a logic analyzer to see what's coming down the wires. On the occasions that the transmit fails, I can see some ripples on the slave select, SPI clock and MOSI signals where there ought not to be.

Here are some captures from my FPGA based logic analyzer where I am sending the number 9 as the byte (00001001). The first image is a successful data transmission, the other two are bad ones. Note that ssel = SS in the images.

Correct:
RPjXYLh.jpg

Incorrect:
07MYZ33.jpg

kSU48vL.jpg

My Arduino Code and Setup
The Teensy is on a piece of breadboard, powered by USB.

My pins are setup as:
SS: 10
SCK: 13
MOSI: 11
MISO: 12

The pins are connected to the FPGA logic analyzer by < 10cm solidcore hookup wire.

Here is my teensy sketch, which sends a number that you type in the serial monitor every second:
Code:
#include <SPI.h>

const int slavePin = 10;

SPISettings spiSettings(5000000, MSBFIRST, SPI_MODE0);

void setup() {
  digitalWrite(slavePin, HIGH);
  pinMode (slavePin, OUTPUT);  
  
  SPI.begin();
  Serial.begin(9600);
}

void loop() {
  if (Serial.available() > 0) // is a character available?
  {    
    char serial_byte = Serial.read();
  
    // check if a number was received
    if ((serial_byte >= '0') && (serial_byte <= '9')) 
    {
      Serial.print("Number received: ");
      Serial.println(serial_byte);

      byte num = serial_byte - '0'; 

      while(true)
      {
        SPI.beginTransaction(spiSettings);
        digitalWrite(slavePin, LOW);
        SPI.transfer(num);
        digitalWrite(slavePin, HIGH);
        SPI.endTransaction();
        
        delay(1000);
      }
    }
  }
}

Other Notes
I've tried this with various different SPI clock speeds and I generally see the same behavior.

Because I've seen this on two different Teensys models, I suspect it must be something I have done wrong, but I cannot see atypical with my spi setup or test code.

Any help or advice appreciated.
Thanks.
 
I have personally used SPI with 60MHZ (reproduced by many users) and thousands use the displays or other hardware, it is a bit unlikely that something is wrong.
Maybe it is a problem with your hardware.. ?!?
Can you post a full screenshot? WHat is the sampling frequency of the LA?
I've seen some guys here, trying so sample I2S with a cheap 2MHz analyzer... ;) (Sorry if this is not the case here )

Edit: Can you post a photo?
 
Last edited:
I have personally used SPI with 60MHZ (reproduced by many users) and thousands use the displays or other hardware, it is a bit unlikely that something is wrong.
Maybe it is a problem with your hardware.. ?!?
Can you post a full screenshot? WHat is the sampling frequency of the LA?
I've seen some guys here, trying so sample I2S with a cheap 2MHz analyzer... ;) (Sorry if this is not the case here )

Edit: Can you post a photo?

Thanks for the reply. Those are pretty much the full picture, there's not really anything omitted from them. I'll try to snap some pics of the hw setup in the morning when I have better light.

The FPGA is sampling at 100MHz so I think I am all good there.
 
Took your exact code, ran it on a Teensy LC and checked with the logic analyzer.
Here is the setup:

IMG_20210116_104846.jpg

And here is the logic analyzer output:

DSView-210116-105236.png

I watched the output for a few minutes [so 100+ frames] but never saw any irregularity.
By the way, did you notice that the actual SCK is 4 MHz instead of 5 MHz?

How does your setup look like?

Regards,
Paul
 
Here is the picture of the connections between my Teensy and the FPGA dev board I am using as a logic analyzer (it is an Alchitry AU)

g3iDAZv.jpg

Or maybe a misbehaving ground loop.
Is the LA connected to the same computer?

So I just put a wire between my FPGA and the Teensy and now it works almost always :S. I didn't think that would be necessary since they are connected to the same computer. I wonder if it is because they are connected to a USB Hub?

I watched it for a minute and I saw borked wave only once.
 
No, a GND connecton is needed in this case, too.

Don't know how better LAs do it - maybe they have a USB Isolator builtin.
 
Are you sure you need to feed 3V3 to the breakout board?
From what I can tell from the photo is that you connect Teensy's 3V3 output to a V+ pin on the breakout board. Is the V+ on the breakout board also an output? That may collide than.

br_front.png

Paul
 
Are you sure you need to feed 3V3 to the breakout board?
From what I can tell from the photo is that you connect Teensy's 3V3 output to a V+ pin on the breakout board. Is the V+ on the breakout board also an output? That may collide than.

View attachment 23325

Paul

Thanks for the reply. The pin is actually one pin further to the right (B8), but the photo kinda makes it seem like it is in the +3.3v out (which is not a port).

Thanks for the heads up though.
 
Status
Not open for further replies.
Back
Top