Go Down

Topic: Strange SPI bahviour (at least to me!) (Read 63 times) previous topic - next topic

weird_dave

I've been running a SPI device at 42MHz for a while, no problems. I've decided it's a bit too fast so I simply changed the speed to 21MHz. Lo and behold, it no longer works!

I've attached a pic of the results after lowering to 14MHz (pic 500v1) and at 42MHz (pic 500v2), the 3rd pic is also at 42MHz but zoomed in a bit more. The blue trace is data, red is the clock, yellow is the CS line, which went low somewhere to the left and is my trigger for the scope.
On the 14MHZ pic, the byte where the data changes to a '1', is being read in as a 6 instead of 3! I couldn't find a hold time in the datasheet for the data to be held after the clock has risen, the connected device changes its output for the next bit on a rising edge as the assumption is that it won't have actually changed before the master has read it in (the traces do agree with that assumption).

So, anyone some ideas on what's really going on? Is the sam3x datasheet telling porkies and it's actually sampling on the falling edge?


Below is the test code I used to reproduce the problem:

Code: [Select]
#include <SPI.h>

#define Serial SerialUSB

void setup() {
  pinMode(87, OUTPUT);
  Serial.begin(115200);
    while (!Serial);
Serial.println("Go!");
SPI.begin();
}

void loop() {
uint8_t SPI_Buffer[320];
uint8_t x;
int c;
SPI.beginTransaction(SPISettings(14000000, MSBFIRST, SPI_MODE0));
digitalWrite(87, LOW);
delayMicroseconds(3);
SPI.transfer (&SPI_Buffer[0], sizeof(SPI_Buffer));
digitalWrite(87, HIGH);
SPI.endTransaction();
c=0;
for (int i = 0; i < sizeof(SPI_Buffer); i++)
{
  x = SPI_Buffer[i];
  if (x<100) Serial.print("0");
  if (x<10) Serial.print("0");
  Serial.print(x);
  Serial.print(" ");
  c++;
  if (c == 64) {
    c=0;
    Serial.println("");
  }
}
Serial.println("");
delayMicroseconds(1000000);
}

Go Up