SPI bus malfunctioning

StefanB

Member
Hey guys, I'm trying to use a teensy 4.0 for a SPI project.

C++:
#include <Arduino.h>
#include <SPI.h>  // include the SPI library:

const int slaveSelectPin = 10;

void setup() {
  // set the slaveSelectPin as an output:
  pinMode (slaveSelectPin, OUTPUT);
  // pinMode (LED_BUILTIN, OUTPUT);

  // initialize SPI:
  SPI.begin();
  SPI.beginTransaction(SPISettings(2000000, MSBFIRST, SPI_MODE1));
  SPI.endTransaction();
  //start serial:
  // Serial.begin(9600);
}

SPISettings settingsA(2000000, MSBFIRST, SPI_MODE1);
uint8_t stat, val1, val2, result;

void loop() {

  delay(1);
  digitalWrite(slaveSelectPin, LOW); // take the SS pin low to select the chip:

  stat = SPI.transfer(0x00);
  // delay(200);
  digitalWrite(slaveSelectPin, HIGH); // take the SS pin high to de-select the chip:

  delay(1);
 
  // //print stat
  // Serial.print("stat: ");
  // Serial.println(stat, BIN);
}

However, when I read my pin 13 (Which if I'm not mistaken is the SPI SCK pin) it appears like this:


which does not look like a SPI clock sync signal

Any idea what I'm doing wrong?

Thanks for the help in advance.

EDIT: for reference, there is nothing plugged in except USB, the O-scope ground wire to the pin next to pin 0, and the o-scope signal wire to pin 13.
 
Last edited:
Does the trace look any different if you arrange your code like this

Code:
  SPI.beginTransaction(SPISettings(2000000, MSBFIRST, SPI_MODE1));
  digitalWrite(slaveSelectPin, LOW); // take the SS pin low to select the chip:
  stat = SPI.transfer(0x00);
  digitalWrite(slaveSelectPin, HIGH); // take the SS pin high to de-select the chip:
  SPI.endTransaction();
 
This is what your code from message #1 shows on the logic analyzer:

1701597799889.png


Agree with @thebigg that using SPI transactions around every transfer is the correct way. Also described here at paragraph Transactional SPI configuration.

Paul
 
Last edited:
I also ran the code from msg #1 on a Teensy 4.0, using Arduino IDE 2.2.1 with Teensyduino 1.59-beta4 (0.59.4 in Boards Manager).

This is what my oscilloscope sees on pins 10, 11, 13:

file.png
 
I've tested various configurations and haven't seen anything resembling 8 cycles.

Something must be wrong with the way you're testing. If you show us what you're doing, maybe we could spot the problem. Recommend starting with a photo of your test setup. It could be a "simple" mistake or misunderstanding which you could look at over and over without seeing, but might be easy to see when more people get to look.

Also, if you're using other software, at least for the sake of testing please try Arduino IDE 2.2.1 with Teensyduino stalled by Boards Manager. Even if you don't like using Arduino IDE, it definitely does work and could help remove 1 source of uncertainty while trying to fix whatever is giving such wrong results.
 
Back
Top