Hello,
According to AN2717 "M68HC08 to HCS08 Transition", one of the enhancements of the HCS08 SPI is
• The HCS08 has a double-buffered receiver; the M68HC08 does not.
This statement seems to have been made to justify that the overrun flag has been eliminated in the later device.
• The M68HC08 Family has an interruptible error flag for receiver overruns (when a new byte is received before the previous byte has been read). The HCS08 Family has a double-buffered receiver and therefore does not require a flag or interrupt for this condition.
However, AN2717 does not attempt to explain any details, but relies on reference to individual data sheets.
Examination of relevant data sheets leads me to believe that the first statement is misleading, or wrong, with respect to the HC08. I can see little difference between the SPI buffer arrangements of the two devices. The data sheets I have used for comparison are those for the MC9S08QG8 and the MC908QB8.
MC908QB8 -
Paragraph 15.2 refers to "Double-buffered operation with separate transmit and receive registers". This is supported by Fig. 15.2, which shows transmit data register, shift register, and receive data register blocks. This would meet my understanding of the term "double buffering", with the shift register being the primary buffer, and the other registers the secondary buffers.
MC9S08QG8 -
Paragraph 15.1.1 refers to "Double-buffered transmit and receive", and Fig. 15.3 shows Tx buffer, SPI shift register, and Rx buffer blocks. This is remarkably similar to the other device. Of course the Rx buffer might actually be a two-level FIFO (which I would consider to provide "triple-buffering"), but I don't think this is the case. Section 15.4 of the data sheet provides the following explanation -
Because the transmitter and receiver are double buffered, a second byte, in addition to the byte currently being shifted out, can be queued into the transmit data buffer, and a previously received character can be in the receive data buffer while a new character is being shifted in. The SPTEF flag indicates when the transmit buffer has room for a new character. The SPRF flag indicates when a received character is available in the receive data buffer. The received character must be read out of the receive buffer (read SPID) before the next transfer is finished or a receive overrun error results.
In the case of a receive overrun, the new data is lost because the receive buffer still held the previous character and was not ready to accept the new data. There is no indication for such an overrun condition so the application system designer must ensure that previous data has been read from the receive buffer before a new transfer is initiated.
The final paragraph would seem very good advice to prevent receive overrun, and loss of data, for either MCU type, even where an overrun flag is provided. Of course, this will apply to the SPI master only, but would be valid whether polling or using receive interrupt, and for all SPI clock rates. The method of handling a SPI data transaction should not need to differ between the two types, so it would seem.
However, if two bytes ever need to be sequentially loaded into the transmit buffer for any reason, there is potential for the second returned byte to be lost due to receive buffer overrun, unless the following situations can apply -
- There is no interest in any returned data - for a send only requirement, or
- Use polling of the SPRF flag, rather than interrupt operation, unless the SPI clock rate is very low. If polling is used, mask all interrupts from prior to the second byte entering the transmit buffer, until the first returned byte is read from the receive buffer.
- If SPI interrupt operation must be used for the received data, the clock rate would need to be reduced so that the total bus cycles needed to enter the receive ISR, read the receive buffer and clear the interrupt flag, plus the maximum ISR cycles for all other interrupts that may potentially delay processing of the receive interrupt, must not exceed the bus cycles required for the SPI send process for a single byte.
I would be interested to hear if others agree or disagree with my analysis, and whether I have correctly interpreted the data sheet information.
Regards,
Mac