SPI between teensy 3.6 and arduino pro mini

Hello,

I try to figure the SPI transaction speed between arduino and teensy

teensy 3.6 runs at 180mhz, arduino mini pro is 16mhz/3.3v

sometimes the arduino responds the right data, most of the time it answers with the same value the teensy master sends through MOSI

I use Gammon arduino slave tutorial which works perfectly fine with a raspberry pi as SPI master

http://www.gammon.com.au/forum/?id=10892

Here is the arduino code:

ISR(SPI_STC_vect)
{
  byte c = SPDR;
  SPDR = c + 15; 
}

void setup()
{
  // SPI Slave mode
  SPI.setClockDivider(SPI_CLOCK_DIV2);
  
  // turn on SPI in slave mode
  SPCR |= bit (SPE);

  // turn on interrupts
  SPCR |= _BV(SPIE);
  
  // have to send on master in, *slave out*
  pinMode(MISO, OUTPUT);

}
  
loop()
{
}

here is the teensy code:

********* = I tried every logical possible value

SPISettings settings(*********, MSBFIRST, SPI_MODE0);

void setup() {
  pinMode (ARDUINO_CS, OUTPUT);
  digitalWrite(ARDUINO_CS,HIGH);
  
  SPI.setSCK(14);// cos 13 is led pin
  SPI.begin(); 
}

void loop() {
 
    digitalWrite(ARDUINO_CS,LOW);
    
    buffer[0]=0x55;
    buffer[1]=0;
    buffer[2]=0;
    buffer[3]=0;
    buffer[4]=0;
    
    SPI.beginTransaction(settings);
    digitalWrite(ARDUINO_CS,LOW);
    SPI.transfer(buffer, SPI_BUFF_SIZE);
    digitalWrite(ARDUINO_CS,HIGH);
    SPI.endTransaction();
    
}

any help appreciated

thanks

********* = I tried every logical possible value

Please explicitly define which values you tried. There are 180 million logically possible values so I seriously doubt that you tried all of them.
I would expect a value of 500000 to work with above code.

sometimes the arduino responds the right data, most of the time it answers with the same value the teensy master sends through MOSI

How do you know? In the code you don't check the returned value.

Where did you set the SPI clock divider on the Teensy? What's the default?

Where did you set the SPI clock divider on the Teensy? What's the default?

The magical asterisks (*) in the code which OP doesn't want to share with us, would set the SPI clock on the Teensy.

phil123456:
arduino mini pro is 16mhz/3.3v

That'd be a problem as the ATmega processors don't do 16 MHz reliably if at all at 3.3V.

"the OP doesnt want to share" .. come on, are you for real ? (yes it's obviously on the teensy side :wink: )

I mean I really tried tons of values and intermediate...which lead me to ask on this forum a less empirical way to do this

is there a way to predict the frequency I should use in the teensy SPI config to be compatible with the arduino (from it's cpu clock frequency ?) I mean teensy and arduino mini pro dont even have CPU freqs that are multiple of one another

would the SPI slave "adapt" to the SCK or does the frequency asolutely needs to be 100% precise ?

I honestly feel like I am gonna give up SPI on this one and use good ole regular/UART baud serial protocol...hoping they would be "platform independant"

You look up SPI clock divider on the PJRC site or on the SAM chip datasheet.

A slave does not "adapt" as such, it will just follow whatever clock speed is set on the CLK line. In contrast, UART Serial requires accurate timing from both sides to work reliably.

Why don't you post the complete code that gives your best results so far? The Arduino code you posted doesn't seem to produce any output so I don't see how you can tell whether it works or not.

“A slave does not “adapt” as such, it will just follow whatever clock speed is set on the CLK line”

quiet contradictory :slight_smile:

but you gave me an idea, I’ll put a debug pin HIGH/LOW in the interrupt routine

which should give me an idea of the max frequency I should use on the master CLK

I guess the MISO = MOSI is related to the fact the SPDR register is used both for input/output

I actualy had a working value, cant find it back anymore

Maximum frequency allowed is 1/2 of the CPU frequency, so 8 MHz for a 16 MHz Arduino.
This provided you can read the buffers fast enough, and wiring has low enough stray capacitance, etc.
Indeed 500 kHz as suggested above sounds like a good starting point.

phil123456:
"A slave does not "adapt" as such, it will just follow whatever clock speed is set on the CLK line"

quiet contradictory :slight_smile:

No, it is how SPI works. Shift registers running SPI don't have clocks, the SCLK line tells them when to accept data.

but you gave me an idea, I'll put a debug pin HIGH/LOW in the interrupt routine

which should give me an idea of the max frequency I should use on the master CLK

Or you could use the default AVR SPI speed which is CPU Clock/4... 4MHz on an Uno but it can go 8MHz and have less time to process what's coming in. Clock divider choices on AVR are powers of 2....

I guess the MISO = MOSI is related to the fact the SPDR register is used both for input/output

Master In Slave Out != Master Out Slave In

http://www.gammon.com.au/spi

GoForSmoke:
No, it is how SPI works. Shift registers running SPI don't have clocks, the SCLK line tells them when to accept data.

so it means that the external clock tells them when to sample so they "adapt" on it instead of requiring a particular frequency

I know MISO !=MOSI, but on the scope it says otherwise :slight_smile:

there is no short circuit on my board neither

I thought I went too fast on teensy side, but I tried really low freqs, thinking that it would give enough time to the arduino

video of the MOSI=MISO issue

https://streamable.com/v5ula

issue @ 0:11

no idea if I should worry about this, if it's gonna solve itself outside of the breadboard (PCB) or if it is SPI frequency related

phil123456:
so it means that the external clock tells them when to sample so they "adapt" on it instead of requiring a particular frequency

I know MISO !=MOSI, but on the scope it says otherwise :slight_smile:

there is no short circuit on my board neither

I thought I went too fast on teensy side, but I tried really low freqs, thinking that it would give enough time to the arduino

SPI is a circular bus, it writes and reads the same bit in the port register 1 bit at a time for 8 bits then data and port write are available. Some SPI slave devices can be daisy-chained, output shift regs and input shift regs can be chained so that every byte sent to the outputs reads a byte from the inputs. More usually it's output registers on MOSI and no connection to MISO.

Perhaps you have an echo program running?

Reality check: SPI spec wasted a pin.

I thought I went too fast on teensy side, but I tried really low freqs, thinking that it would give enough time to the arduino

This isn't a very exact number. What is a low frequency in your eyes?