Hi Micka,
1. Could you please clarify the SPI bus configuration?
The interesting is mainly setting of SPI Clock Phase Bit (CPHA) and Transfer Width (XFRW).
The SS (CS) pin must go to a high level at the end of every transfer when CPHA=0.
Please check also minimum leading time before the first SCK edge, minimum trailing time after the last SCK edge and minimum idling time between transfers (minimum SS high time).
The slaves SPIDR register should be already written at least half SCK cycle prior new transfer begins.
So, if two bytes are transferred without any gap for SPI command processing or when we do not handle SS signal between these transfers (only when CPHA=0), two bytes shift will be probably a result.
2. You should be also careful with TX interrupt.
This is a typical issue: When we enable TX interrupt during SPI initialization, TX ISR will be executed “immediately” since SPTEF=1 by default and TX buffer will be filled with some “dummy” value. So, there is at least one byte additional shift in compare with expected result.
So, correctly we should initialize SPI with disabled TX interrupt. When we receive SPI command, we should write the first byte into SPIDR register and enable TX interrupt for rest of the message. We will disable the TX interrupt when we send the last byte from the message into SPIDR register.
I hope it helps you.
Have a great day,
RadekS
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------