Strange SPI error on Teensy3

sumotoy

Well-known member
I come across this recently during a library development.

In brief, call any spi function without instantiate an SPI.begin() first, will cause a freeze of teensy board and a consequential "usb device not recognized..." OS system driver error!

here's an example:

Code:
#include <SPI.h>

const int slaveSelectPin = 10;

void setup() {
  // set the slaveSelectPin as an output:
  pinMode (slaveSelectPin, OUTPUT);
  // initialize SPI:
  //SPI.begin(); 
}

void loop() {
  digitalWrite(slaveSelectPin,LOW);
  SPI.transfer(0x00);
  SPI.transfer(0);
  digitalWrite(slaveSelectPin,HIGH); 
  delay(1000);
}

The code compile fine and just after transfer I got an "usb device not recognized..." error from windows. To "resuscitate" teensy 3 I just have to upload any working code and press 2 times reset.
In my system this is always repicable.

OS:Win7 32bit
IDE:beta8
Board:Teensy3


Anyone can confirm this?
 
Last edited:
I think I'm having this same issue. Trying to use SPI without SPI.begin() causes the teensy3 to hang and not enumerate, even if Serial.begin() precedes SPI access.
 
This is not a "strange error" its what happens when you try to use the SPI library without initializing it with SPI.begin().

The code will hang the Teensy and the USB comunications over USB dies so there is no enumeration.
There is not much use 'trying' to use SPI without SPI.begin(), you can be fairly sure that it wont work
and the Teensy will hang.

The SPI library and the Serial are using different hardware and software.

Most libraries must be properly intialized before use, that can usually be found in the library documentation.
 
Arduino not 'hang'' like teensy in this situation, serial stops but I'm able to send code as normal, teensy3 literally 'crash' and to get back to work sometimes I need to press reset repeatily 3,4 times and send corrected code even 2 or 3 times
 
Just a quick experiment with an arduino done now, surprisly not hang at all! No SPI of course but my Serial.printl("click"); I put in a loop still alive.
 
There are very big differences how the ATMega328 type chips in Arduino and Teensy 2 and Cortex M4 like Teensy 3 work.
The periperials like SPI on the Cortex chips are often unclocked, turned off, at power on so that even the control registers are not
available to read an write until the module has been powered up, and that is usually done in the begin() function.

In this case the Cortex M4 often jumps to a hardware fault handler, since illegal memory accesses have been done.
The AtMega chips carries on happily, pretending nothing bad has happened while the SPI is broken.

We can discuss what is better or worse, a system that stops working when things break bad, or a system that pretends all is
well when no communications will happen, and which is most confusing to the programmer/user.

Anyway, most libraries are coded to make things work if possible and not following the library documentations how to
initialize stuff is up to you but I would call it foolish. The fact that the Arduino doesnt hang but almost looks like its working
doesnt really help and resetting the Teensy 3 to bootloader is just a click away.

Best regards
Magnus
 
Thanks Magnus, that help me a lot to understand what's happen. First time I got this error I was in a 'panic like' situation since I was afraid that my USB or whatever had killed my Teensy3.
Just a note... When this happen a click on Teensy reset will be not enough, have to reset and send code, then reset again, send again until he's happy. Don't know if with current IDE something change
since I never forget SPI.begin() again.
 
Back
Top