arduino/ArduinoCore-megaavr

SPI default configuration fails

JojoS62 opened this issue · 1 comments

The SPI configuration is using a SPISettings class from ArduinoCore-API, which is converted to a temporary object of type SPISettingsMegaAVR .
This conversion in

void config(SPISettings settings) {
config(SPISettingsMegaAVR(settings));
}
is using this constructor:
SPISettingsMegaAVR(SPISettings& x) { SPISettingsMegaAVR(x.getClockFreq(), x.getBitOrder(), x.getDataMode()); }

There the SPISettingsMegaAVR object is created on the stack, and then destroyed. It does not initialize the SPISettingsMegaAVR that is used in the config() call.
This problem is hidden because it works by sudden and is compiler version dependant. There is a discussion and some test code to reproduce the problem in https://forum.arduino.cc/t/houston-spi-has-a-problem-with-avr-gcc-11-1-0
The assumption that this is a compiler problem was false, its simply an uninitialized SPISettingsMegaAVR object.

A solution would be to use constructor delegation as explained here:
https://www.geeksforgeeks.org/constructor-delegation-c/

Hi,

Thanks Jojo for the description of the problem. This causes the SPI registers to be overwritten with random numbers in the begin() method. Likewise in the beginTransaction(...) method.

In April 2020 the API was changed. What was the reason for this? That it is better understood.
port to ArduinoAPI