Go Down

Topic: SPi driver not configurable (Read 1 time) previous topic - next topic


Hi folks-

I'm seeing the SPI driver on the zero is not configurable.  In other words, it will not operate on any SPI settings I select.  The same code works correctly on a Mega 2560.  Can anyone offer suggestions on how to make it work?

Here's my code:

#define   BIT_RATE   250000

If I change any SPISettings values, there is no change.  I'm seeing the default 4MHz clock.  LSB vs MSB always MSB first.

Thanks - JJS


I figured out the problem.  Source code review tells me it's a bug.  Here's how I know:

Look at SPI.cpp (relevant snippets below).  Find the begin() method of SPIClass.  Note the config() call at the end of the method.  It overrides all of whatever SPI settings are passed when SPI.beginTransaction() is called.  The workaround is to call SPI.setClockDivider() _after_ SPI.begin().  That's what worked for me.

I'm REALLY surprised this bug is not documented nor even findable on google.


const SPISettings DEFAULT_SPI_SETTINGS = SPISettings();

void SPIClass::begin()

 // PIO init
 pinPeripheral(_uc_pinMiso, g_APinDescription[_uc_pinMiso].ulPinType);
 pinPeripheral(_uc_pinSCK, g_APinDescription[_uc_pinSCK].ulPinType);
 pinPeripheral(_uc_pinMosi, g_APinDescription[_uc_pinMosi].ulPinType);



The Zero SPI driver is more buggy than I determined earlier.

The next bug I found is that calling transfer16() before calling transfer(uint8_t) will occasionally cut short the 8 bit SPI transactions.  When I changed to all transfer(uint8_t) calls, the shortcut transaction went away.

Beware!  The Zero SPI driver is crappy.



It's not a bug, simply your code... In your first line of code you're setting up the SPI connection with your desired settings but by calling SPI.begin() you're resetting everything back to the defaults. If you want to set your own SPI settings there's no need to use SPI.begin() at all.

Go Up