possible bug in spi & spififo

Frank B

Senior Member
Hi,

i found, that the initialization for non-native CS is in the wrong order in (minimum) two spi libs:

Example:
if you try:
Code:
const int ledPin = 13;
void setup() { 
  digitalWrite(ledPin, HIGH);   // set the LED on  
  pinMode(ledPin, OUTPUT);
}
void loop() {}
the led stays off.
this, instead works:
Code:
const int ledPin = 13;
void setup() { 
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, HIGH);   // set the LED on  
}
So.. the order of setting pin-direction and value is important.
Let's look at flash_spi.cpp:
Code:
  digitalWrite(10, HIGH);
  pinMode(10,OUTPUT);

  // Set up the chip select pin (6)
  digitalWrite(f_cs, HIGH);
  pinMode(f_cs,OUTPUT);
..so this does not work, i think.

SPIFIO:
Code:
			*reg = 1;
			pinMode(pin, OUTPUT);
is the wrong order, too.

Maybe there are more places that need to be fixed ?
Together with missing pullups, i think this causes problems at the time when the very first initialzations are done, because more than one SPI-Device is selected.


Edit: tested with Teensy 3.1
Edit: could this be an explanation for some strange effects ?
 
Last edited:
Wrong:
untitled2.png
OK:
untitled.png
 
so re SPIFIFO -- would the fix be to just reverse the order? like so?

Code:
pinMode(pin, OUTPUT);
*reg = 1;

i've just tried that. SD.h still freezes up when used together with SPIFIFO + non-native CS, ie as reported here
 
so re SPIFIFO -- would the fix be to just reverse the order? like so?

Code:
pinMode(pin, OUTPUT);
*reg = 1;

i've just tried that. SD.h still freezes up when used together with SPIFIFO + non-native CS, ie as reported here

It's only a a problem _before_ the first transmission to a chip. CS is floating and the chip may interprete data which are intended for other chips. But i think it's a only a problem under very special circumstances. Nevertheless, it's wrong.
SD.h still freezes up
yes, i tried that too.
 
Last edited:
paul, i tried to do a pullrequest for flash_spi.cpp inside the libraries/audio folder (regarding this little bug), but surprisingly, it is not in the github version ?
 
Back
Top