Arduino Forum

Products => Arduino Zero => Topic started by: johnspeth on Jan 15, 2018, 03:57 pm

Title: SPi driver not configurable
Post by: johnspeth on Jan 15, 2018, 03:57 pm
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
SPI.beginTransaction(SPISettings(BIT_RATE,LSBFIRST,SPI_MODE0));
SPI.endTransaction();
SPI.begin();

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

Title: Re: SPi driver not configurable
Post by: johnspeth on Jan 16, 2018, 04:35 am
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.

JJS

const SPISettings DEFAULT_SPI_SETTINGS = SPISettings();

void SPIClass::begin()
{
 init();

 // 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);

 config(DEFAULT_SPI_SETTINGS);
}

Title: Re: SPi driver not configurable
Post by: johnspeth on Jan 16, 2018, 06:19 am
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.

JJS
Title: Re: SPi driver not configurable
Post by: dlabun on Jan 24, 2018, 07:04 pm
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.