Hi!
I'm using a Teensy 4.0, and I'm running into an issue where my code gets stuck when I try to communicate over SPI. This likely is due to some kind of hardware mistake on my end (see below); however, I can't figure out what I'm doing wrong and would love any suggestions anyone might have for debugging this.
Here's a minimal example:
When I run this on my Teensy 4.0 with nothing connected, I get the expected output:
As soon as I connect the Teensy to my PCB, I get through the first two prints, but never reach the third printed line - the code seems to hang. My output is just:
As the code seems to be hanging at the SPI.transfer call, I started throwing Serial.print statements into SPI.transfer(), lines 1549-1573 in SPI.cpp:
Now, with the Teensy plugged into my PCB the output is:
Somehow count_read was decremented past 0. I haven't quite reached the point of digging in to find out how SPI.transfer really works (like, what are port().RSR, port().SR, LPSPI_RSR_RXEMPTY, LPSPI_SR_TDF?), but I was hoping someone might have some idea based on this how I might have messed up.
Since this only happens when my Teensy is plugged into my PCB, I assume this is because I have misconfigured something on the SPI bus or with my SPI devices, but I'm at a loss as to what.
For reference, when I run the above minimal example on my Teensy 4.0 without it plugged into my board, I get:
I'm using a Teensy 4.0, and I'm running into an issue where my code gets stuck when I try to communicate over SPI. This likely is due to some kind of hardware mistake on my end (see below); however, I can't figure out what I'm doing wrong and would love any suggestions anyone might have for debugging this.
Here's a minimal example:
Code:
#include <SPI.h>
#define DAC_CS 10
void setup() {
Serial.begin(115200);
SPI.begin();
delay(3000);
Serial.println("Completed setup()");
}
void loop() {
delay(3000);
SPI.beginTransaction(SPISettings(1e6, MSBFIRST, SPI_MODE0));
digitalWrite(DAC_CS, LOW);
uint8_t send_buf[] = {0b10101010, 0b10101010, 0b10101010};
uint8_t recv_buf[] = {0,0,0};
Serial.println("Transferring");
SPI.transfer( send_buf, recv_buf, 3);
Serial.println("Transferred");
digitalWrite(DAC_CS, HIGH);
SPI.endTransaction();
}
When I run this on my Teensy 4.0 with nothing connected, I get the expected output:
"Completed setup()
Transferring
Transferred
Transferring
Transferred"
ad nauseum every 3 seconds.Transferring
Transferred
Transferring
Transferred"
As soon as I connect the Teensy to my PCB, I get through the first two prints, but never reach the third printed line - the code seems to hang. My output is just:
"Completed setup()
Transferring"
Transferring"
As the code seems to be hanging at the SPI.transfer call, I started throwing Serial.print statements into SPI.transfer(), lines 1549-1573 in SPI.cpp:
Code:
while (count > 0) {
Serial.print("first loop: count="); Serial.println(count);
Serial.print("first loop: count_read="); Serial.println(count_read);
// Push out the next byte;
port().TDR = p_write? *p_write++ : _transferWriteFill;
count--; // how many bytes left to output.
// Make sure queue is not full before pushing next byte out
do {
if ((port().RSR & LPSPI_RSR_RXEMPTY) == 0) {
uint8_t b = port().RDR; // Read any pending RX bytes in
if (p_read) *p_read++ = b;
count_read--;
}
} while ((port().SR & LPSPI_SR_TDF) == 0) ;
}
// now lets wait for all of the read bytes to be returned...
while (count_read) {
Serial.print("second loop: count_read="); Serial.println(count_read);
if ((port().RSR & LPSPI_RSR_RXEMPTY) == 0) {
uint8_t b = port().RDR; // Read any pending RX bytes in
if (p_read) *p_read++ = b;
count_read--;
}
}
Now, with the Teensy plugged into my PCB the output is:
Completed setup()
Transferring
first loop: count=3
first loop: count_read=3
first loop: count=2
first loop: count_read=3
first loop: count=1
first loop: count_read=1
second loop: count_read=4294967295
second loop: count_read=4294967295
second loop: count_read=4294967295
second loop: count_read=4294967295
second loop: count_read=4294967295
second loop: count_read=4294967295
second loop: count_read=4294967295
second loop: count_read=4294967294
second loop: count_read=4294967293
second loop: count_read=4294967293
second loop: count_read=4294967293
second loop: count_read=4294967293
second loop: count_read=4294967293
second loop: count_read=4294967293
second loop: count_read=4294967293
second loop: count_read=4294967292
second loop: count_read=4294967291
second loop: count_read=4294967291
second loop: count_read=4294967291
...
Transferring
first loop: count=3
first loop: count_read=3
first loop: count=2
first loop: count_read=3
first loop: count=1
first loop: count_read=1
second loop: count_read=4294967295
second loop: count_read=4294967295
second loop: count_read=4294967295
second loop: count_read=4294967295
second loop: count_read=4294967295
second loop: count_read=4294967295
second loop: count_read=4294967295
second loop: count_read=4294967294
second loop: count_read=4294967293
second loop: count_read=4294967293
second loop: count_read=4294967293
second loop: count_read=4294967293
second loop: count_read=4294967293
second loop: count_read=4294967293
second loop: count_read=4294967293
second loop: count_read=4294967292
second loop: count_read=4294967291
second loop: count_read=4294967291
second loop: count_read=4294967291
...
Somehow count_read was decremented past 0. I haven't quite reached the point of digging in to find out how SPI.transfer really works (like, what are port().RSR, port().SR, LPSPI_RSR_RXEMPTY, LPSPI_SR_TDF?), but I was hoping someone might have some idea based on this how I might have messed up.
Since this only happens when my Teensy is plugged into my PCB, I assume this is because I have misconfigured something on the SPI bus or with my SPI devices, but I'm at a loss as to what.
For reference, when I run the above minimal example on my Teensy 4.0 without it plugged into my board, I get:
Completed setup()
Transferring
first loop: count=3
first loop: count_read=3
first loop: count=2
first loop: count_read=3
first loop: count=1
first loop: count_read=2
second loop: count_read=1
second loop: count_read=1
second loop: count_read=1
second loop: count_read=1
second loop: count_read=1
second loop: count_read=1
second loop: count_read=1
second loop: count_read=1
second loop: count_read=1
second loop: count_read=1
second loop: count_read=1
second loop: count_read=1
second loop: count_read=1
Transferred
...
Transferring
first loop: count=3
first loop: count_read=3
first loop: count=2
first loop: count_read=3
first loop: count=1
first loop: count_read=2
second loop: count_read=1
second loop: count_read=1
second loop: count_read=1
second loop: count_read=1
second loop: count_read=1
second loop: count_read=1
second loop: count_read=1
second loop: count_read=1
second loop: count_read=1
second loop: count_read=1
second loop: count_read=1
second loop: count_read=1
second loop: count_read=1
Transferred
...