SPI setBitOrder is default set to LSBFIRST

Vassilis
Mon Jun 22, 2015 8:14 pm
I enabled the SPI_DEBUG in SPI.cpp file (Arduino_STM32\STM32F1\ libraries\SPI\src\SPI.cpp) and I noticed that is enabled by default the LSBFIRST.
spi_master_enable(0,0,896)

When I sent the command: SPI.setBitOrder(MSBFIRST); the the Debug output was:
Bit order set to 1
spi_master_enable(0,0,768)

The VS1003B (VS1053B), the Ethercard library, the ILI9341 LCD and the SSD1306 OLED use the MSBFIRST.

Do you think is a good idea to set the setBitOrder(MSBFIRST) as default into the SPI library during the SPI initialization <SPI.begin()> ?


RogerClark
Mon Jun 22, 2015 11:32 pm
Whats the default for AVR ?

Vassilis
Tue Jun 23, 2015 4:10 am
The ATmega328 SPI by default uses the MSB first.
Register SPCR, bit DORD = 0 = MSB

ATmega328p page 172


RogerClark
Tue Jun 23, 2015 4:37 am
Thanks

In that case we should probably change it to match the AVR (I’ve no idea why LeafLabs would have set the default to the other value)


PaulRB
Tue Jun 23, 2015 1:39 pm
I second that, Roger. Caught me out until Vassilis helped me on this thread.

Vassilis
Tue Jun 23, 2015 3:10 pm
I found a simple solution. I don’t know if this is the correct file to modify but it worked.

The file is the Arduino_STM32\STM32F1\cores\maple\wirish_constants.h
I swapped the two lines in enum BitOrder . That makes default the MSBFIRST on SPI ports.

#ifndef _WIRING_CONSTANTS_
#define _WIRING_CONSTANTS_

#ifdef __cplusplus
extern "C"{
#endif

enum BitOrder {
//LSBFIRST = 0,
//MSBFIRST = 1
LSBFIRST = 1,
MSBFIRST = 0
};

#ifdef __cplusplus
}
#endif

#endif


madias
Tue Jun 23, 2015 3:30 pm
I found a simple solution.

Vassilis
Tue Jun 23, 2015 5:25 pm
Hello Mathias
I will do some further tests on I2C

Where else do you think we will have problem on source code (libraries) ?


madias
Tue Jun 23, 2015 6:11 pm
A quick github code search (I love github for this feature!):
https://github.com/rogerclarkmelbourne/ … q=BitOrder
not so less files are using BitOrder….

Vassilis
Tue Jun 23, 2015 6:59 pm
I tried the RTClib library for DS1307 I2C Real Time Clock and it works ok. No malfunction.
I will do some more I2C devices tests.

victor_pv
Tue Jun 23, 2015 7:48 pm
madias wrote:A quick github code search (I love github for this feature!):
https://github.com/rogerclarkmelbourne/ … q=BitOrder
not so less files are using BitOrder….

madias
Tue Jun 23, 2015 8:11 pm
On arduino wiring_constants: https://github.com/arduino/Arduino/blob … onstants.h

enum BitOrder {
LSBFIRST = 0,
MSBFIRST = 1
};


ahull
Tue Jun 23, 2015 8:27 pm
madias wrote:On arduino wiring_constants: https://github.com/arduino/Arduino/blob … onstants.h

enum BitOrder {
LSBFIRST = 0,
MSBFIRST = 1
};


victor_pv
Tue Jun 23, 2015 9:08 pm
madias wrote:On arduino wiring_constants: https://github.com/arduino/Arduino/blob … onstants.h

enum BitOrder {
LSBFIRST = 0,
MSBFIRST = 1
};


RogerClark
Tue Jun 23, 2015 9:38 pm
i think its safer just to fix uninitialised values in SPI rather than making global changes, as global changes often have unexpected consequences.

Edit.

I need to test this to make sure the SD lib still works etc,

But as far as I can see, we just need to initialise the bitorder in the constructor to MSBFIRST
e.g. add a line at the bottom of the constructor to set it. We may also want to explicitly set the clock divider and the mode, even though the initialised values may be OK already. It just makes it easier for anyone reading the code to work out what the default values are

(Scroll to the bottom of this code window)
SPIClass::SPIClass(uint32 spi_num) {
switch (spi_num) {
#if BOARD_NR_SPI >= 1
case 1:
this->spi_d = SPI1;
break;
#endif
#if BOARD_NR_SPI >= 2
case 2:
this->spi_d = SPI2;
break;
#endif
#if BOARD_NR_SPI >= 3
case 3:
this->spi_d = SPI3;
break;
#endif
default:
ASSERT(0);
}

bitOrder=MSBFIRST;
}


Vassilis
Wed Jun 24, 2015 6:33 am
RogerClark wrote:i think its safer just to fix uninitialized values in SPI rather than making global changes, as global changes often have unexpected consequences.

RogerClark
Wed Jun 24, 2015 6:39 am
Vasillis

Thanks for testing.

I’ve not had time to test it yet.

I will wait for a bit more feedback, and if no one objects to the change, I will push the change a bit later…


RogerClark
Wed Jun 24, 2015 11:36 pm
I’ve now updated the repo with that change

Leave a Reply

Your email address will not be published. Required fields are marked *