@jensa
I keep meaning to mention, that for example reading one byte at a time code like this:
Code:
uint32_t spiread32B(void) {
uint8_t buf[4] = {0,0,0,0};
uint8_t buf2[4] = {0,0,0,0};
SPI.beginTransaction( SPISettings(10000000, MSBFIRST, SPI_MODE0) );
digitalWriteFast(csPin,LOW);
SPI.transfer(buf2, buf, 1);
lastDataRead = buf[0];
lastDataRead <<= 8;
SPI.transfer(buf2, buf, 1);
lastDataRead |= buf[0];
lastDataRead <<= 8;
SPI.transfer(buf2, buf, 1);
lastDataRead |= buf[0];
lastDataRead <<= 8;
SPI.transfer(buf2, buf, 1);
lastDataRead |= buf[0];
digitalWriteFast(csPin,HIGH);
SPI.endTransaction();
return lastDataRead;
}
can be simplified by simply using the main SPI.transfer method... Something like:
I am trying to remember if the buffer call like the above is short circuited anyway...
Code:
uint32_t spiread32B(void) {
SPI.beginTransaction( SPISettings(10000000, MSBFIRST, SPI_MODE0) );
digitalWriteFast(csPin,LOW);
lastDataRead = SPI.transfer(0);
lastDataRead <<= 8;
lastDataRead |= SPI.transfer(0);
lastDataRead <<= 8;
lastDataRead |= SPI.transfer(0);
lastDataRead <<= 8;
lastDataRead |= SPI.transfer(0);
digitalWriteFast(csPin,HIGH);
SPI.endTransaction();
return lastDataRead;
}
Or you can do it all in one...
Code:
uint32_t spiread32B(void) {
SPI.beginTransaction( SPISettings(10000000, MSBFIRST, SPI_MODE0) );
digitalWriteFast(csPin,LOW);
lastDataRead = (SPI.transfer(0) << 24) | (SPI.transfer(0) << 16) | (SPI.transfer(0) << 8) | (SPI.transfer(0);
digitalWriteFast(csPin,HIGH);
SPI.endTransaction();
return lastDataRead;
}
But that probably does not help out your case here.
SPI.beginTransaction(...) stuff.
Currently assuming that you always call the beginTransaction stuff with same settings. The code always still sets the SPI configuration registers with the values.
Like:
Code:
port().CR = 0;
port().CFGR1 = LPSPI_CFGR1_MASTER | LPSPI_CFGR1_SAMPLE;
port().CCR = _ccr;
port().TCR = settings.tcr;
port().CR = LPSPI_CR_MEN;
It used to bypass this code if the settings had not changed from the last call to here. However ran into a few issues. including user code could and did change
which clock was used for SPI... Also some code sets these registers themselves.. Which we were missing.
Including code that called:
void setBitOrder(uint8_t bitOrder);
void setDataMode(uint8_t dataMode);
void setClockDivider(uint8_t clockDiv) {
But wondering if it might work better if we check the registers like maybe:
Code:
if ((port().CCR != _ccr) || (port().TCR != settings.tcr) {
port().CR = 0;
port().CFGR1 = LPSPI_CFGR1_MASTER | LPSPI_CFGR1_SAMPLE;
port().CCR = _ccr;
port().TCR = settings.tcr;
port().CR = LPSPI_CR_MEN;
}
Again I have not tried this out, but might later if I get a chance