As far as I can tell, the SPI::transfer16 method needs to be fixed. It crashes because (it seems) SPI_S has to be read before attempting to transfer data. Also reported here: https://forum.pjrc.com/threads/27689-Teensy-LC-Beta-Testing?p=64209&viewfull=1#post64209
The method's code was:
This fixed it:
Remarks:
The method's code was:
Code:
inline static uint8_t transfer16(uint16_t data) {
SPI0_C2 = SPI_C2_SPIMODE;
SPI0_DH = data >> 8;
SPI0_DL = data;
while (!(SPI0_S & SPI_S_SPRF)) ; // wait
uint16_t r = (SPI0_DH << 8) | SPI0_DL;
SPI0_C2 = 0;
return r;
}
This fixed it:
Code:
inline static uint16_t transfer16(uint16_t data) {
SPI0_C2 = SPI_C2_SPIMODE;
while (!(SPI0_S & SPI_S_SPTEF)) ; // wait for Tx empty
*((uint16_t*)&KINETISL_SPI0.DL) = data;
while (!(SPI0_S & SPI_S_SPRF)) ; // wait for Rx not empty
uint16_t r = *((uint16_t*)&KINETISL_SPI0.DL);
SPI0_C2 = 0;
return r;
}
- the code posted by Frank B (simply reading SPI0_S) didn't work for me, I had to keep the first loop checking for SPTEF;
- writes to both DLH at once (this is not mentioned in the datasheet, so I just tried it because I need that for 16-bit DmaSpi);
- my version returns a uint16_t.