I'm looking at some SPI master code that runs on an LPC2388FBD144 (NXP ARM processor) for a colleague. I'm doing the slave SPI code that runs fine on a PIC.
Somehow his code "misses" reading messages that I know were sent because I can see them in a hardware LA trace of the exchange. We're both quite puzzled how SPI master code can appear to "drop" messages. He's been adding delay loops to the code in an attempt to make it work which I don't think can be the right way to solve this problem
Here's a small snip of the code:
// Lag = Signed position error (int, transmitted as two values)
while (SSP0SR & BSY);
while (!(SSP0SR & TFE)); /* Wait if TNF cleared, Tx FIFO may still be full. */
for (rb=1;rb<10;rb++); // allow for a gap
SSP0DR = 0xEBEB; // send 7. poll
while (!(SSP0SR & RNE));
val = SSP0DR << 16; // read back 6th value: Lead/Lag, High int part
while (SSP0SR & BSY);
while (!(SSP0SR & TFE)); /* Wait if TNF cleared, Tx FIFO may still be full. */
for (rb=1;rb<10;rb++); // allow for a gap
SSP0DR = 0xEBEB; // send 8. poll
while (!(SSP0SR & RNE));
ram_var.Lag_X = val | SSP0DR; // read back 7. value: Lead/Lag, Low int part
// State
while (SSP0SR & BSY);
while (!(SSP0SR & TFE)); /* Wait if TNF cleared, Tx FIFO may still be full. */
for (rb=1;rb<10;rb++); // allow for a gap
SSP0DR = 0xFFFF; // send dummy byte for reading back the last value
while (!(SSP0SR & RNE));
ram_var.State_X = i = SSP0DR; // read back 8. value: Flags
// wait for end of transmission before deactivating SSEL/
while (SSP0SR & BSY);
while (!(SSP0SR & TFE)); /* Wait if TNF cleared, Tx FIFO may still be full. */
for (rb=1;rb<20;rb++); // allow for a gap
// condition handling
if (i & 0x4000) { // was a stall indicated?
for (rb=1;rb<200;rb++); // allow for a longer gap
SSP0DR = 0x8001; // Send STALL_ACK
while (SSP0SR & BSY);
while (!(SSP0SR & TFE)); /* Wait if TNF cleared, Tx FIFO may still be full. */
for (rb=1;rb<20;rb++); // allow for a gap
}
I believe that there must be a problem with how he is testing that it's:
- OK to transmit the next 16 bits, and
- the data xfer has completed.
I'm a complete beginner with ARM SPI - I only saw this code yesterday for the first time.
The LA trace is quite clear (image attached) that 0x4000 was received by the master when it sent the 0xFFFF message. And yet the code that's supposed to send the ACK never triggered (otherwise you'd see the master sending 0x8001 before *SSEL was raised - orange trace).
Any guidance on what's wrong will be gratefully received.
Thanks
David