Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 10 of 10

Thread: Irregular SPI Behavior

  1. #1
    Junior Member
    Join Date
    Jun 2019
    Posts
    14

    Irregular SPI Behavior

    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:
    Click image for larger version. 

Name:	RPjXYLh.jpg 
Views:	18 
Size:	37.7 KB 
ID:	23313

    Incorrect:
    Click image for larger version. 

Name:	07MYZ33.jpg 
Views:	20 
Size:	34.6 KB 
ID:	23314

    Click image for larger version. 

Name:	kSU48vL.jpg 
Views:	24 
Size:	37.6 KB 
ID:	23315

    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.

  2. #2
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    8,229
    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 by Frank B; 01-15-2021 at 10:31 PM.

  3. #3
    Junior Member
    Join Date
    Jun 2019
    Posts
    14
    Quote Originally Posted by Frank B View Post
    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.

  4. #4
    Senior Member PaulS's Avatar
    Join Date
    Apr 2015
    Location
    Netherlands
    Posts
    518
    Took your exact code, ran it on a Teensy LC and checked with the logic analyzer.
    Here is the setup:

    Click image for larger version. 

Name:	IMG_20210116_104846.jpg 
Views:	9 
Size:	66.0 KB 
ID:	23318

    And here is the logic analyzer output:

    Click image for larger version. 

Name:	DSView-210116-105236.png 
Views:	10 
Size:	31.6 KB 
ID:	23319

    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

  5. #5
    Senior Member
    Join Date
    Jul 2020
    Posts
    905
    Is this a layout issue? Cables too long? Missing or misrouted ground return wire?

  6. #6
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    8,229
    Or maybe a misbehaving ground loop.
    Is the LA connected to the same computer?

  7. #7
    Junior Member
    Join Date
    Jun 2019
    Posts
    14
    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)

    Click image for larger version. 

Name:	g3iDAZv.jpg 
Views:	15 
Size:	322.3 KB 
ID:	23323

    Quote Originally Posted by Frank B View Post
    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.

  8. #8
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    8,229
    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.

  9. #9
    Senior Member PaulS's Avatar
    Join Date
    Apr 2015
    Location
    Netherlands
    Posts
    518
    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.

    Click image for larger version. 

Name:	br_front.png 
Views:	9 
Size:	196.0 KB 
ID:	23325

    Paul

  10. #10
    Junior Member
    Join Date
    Jun 2019
    Posts
    14
    Quote Originally Posted by PaulS View Post
    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.

    Click image for larger version. 

Name:	br_front.png 
Views:	9 
Size:	196.0 KB 
ID:	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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •