Normal mode, no debugger.
I am tempted so say I am doing exactly what you suggest,
but at this point, I dare not be sure of anything.
But, and here is where I believe the big difference is,
I don't think your code will work if called in rapid succession with CS asserted during
the entire procedure, IF your code execution speed is "fast" related to SPI baudrate.
What I think happens is this:
SPIF must be clear before sending a byte, IF you need to read rx data after ALL clocks are generated.
If you do not clear SPIF first, it may already be set before loading SPI2DR for transmission.
In that case the while(SPIF) loop falls through immediately even if clock pulses still remain to be generated.
In addition to the above, if you send the next character
(remember I need to send a "READ-Instruction first, THEN a dummy byte to sample the rx data)
as soon as the transmit register is empty (SPTEF=1) right after clearing SPIF,
there will be clocks remaining to be generated (if baudrate is low compared to exec time).
These remaing clocks, perhaps only 1 or 2, will then affect when SPIF is set,
(not sure about SPI internal details, perhaps something else goes on)
and this will happen earlier than wanted, while the last byte (dummy byte in my case)
is being clocked out. That is when I incorrectly read SPI2DRL which has not yet been
loaded with the incoming character, because clocking has not yet ended.
My hypothesis is:
When needing to read data back in a multibyte protocol,
do not send data back to back when SPTEF=1.
Instead, clear SPIF, send, wait for SPIF=1, THEN send the next byte, or read incoming data.
For send only, SPTEF will work.
End of hypothesis.
There may be flaws in my reasoning and verbosity,
but I'm relatively sure that this is more or less what is happening.
If nothing else my problems seem to be gone.
Comments are extremely appreciated,
Anders J