26.3.2.2.2 Buffer Mode
The SPI peripheral can be configured in Buffered mode by writing a ‘1
’ to
the Buffer Mode Enable (BUFEN) bit in the Control B (SPIn.CTRLB) register to avoid data
collisions.
This mode will enable two receive buffers and one transmit buffer. Both will have separate interrupt flags, transmit complete and receive complete. Figure 26-1 shows the extra buffers.
When Buffer mode is enabled it can work in two different ways. The Buffer Mode Wait for Receive (BUFWR) bit in the Control B (SPIn.CTRLB) register controls how the Buffer mode works. The details of how they work including timing diagrams are described below.
Client Buffer Mode with Wait for
Receive Bit Written to ‘0
’
In Client mode, if the Buffer mode Wait for Receive (BUFWR) bit in SPIn.CTRLB is written
to ‘0
’, a dummy byte will be sent before the transmission of user data
starts. Figure 26-3 shows a transmission sequence with this
configuration. Notice how the value 0x45
is written to the Data
(SPIn.DATA) register but never transmitted.
When the Wait for Receive (BUFWR) bit in SPIn.CTRLB is written to ‘0
’,
all writes to the Data (SPIn.DATA) register go to the Transmit Data Buffer register. The
figure above shows that the value 0x43
is written to the Data
(SPIn.DATA) register but not immediately transferred to the shift register, so the first
byte sent will be a dummy byte. The value of the dummy byte equals the values that were
in the shift register at the same time. After the first dummy transfer is completed, the
value 0x43
is transferred to the shift register. Then
0x44
is written to the Data (SPIn.DATA) register and goes to the
Transmit Data Buffer register. A new transfer is started, and 0x43
will
be sent. The value 0x45
is written to the Data (SPIn.DATA) register,
but the Transmit Data Buffer register is not updated since it is already full containing
0x44
and the Data Register Empty Interrupt Flag (DREIF) in
SPIn.INTFLAGS is low. The value 0x45
will be lost. After the transfer,
the value 0x44
is moved to the shift register. During the next
transfer, 0x46
is written to the Data (SPIn.DATA) register, and
0x44
is sent out. After the transfer is complete,
0x46
is copied into the shift register and sent out in the next
transfer.
The DREIF goes low every time the Transmit Data Buffer register is written and goes high after a transfer when the previous value in the Transmit Data Buffer register is copied into the shift register. The Receive Complete Interrupt Flag (RXCIF) in SPIn.INTFLAGS is set one cycle after the DREIF goes high. The Transfer Complete Interrupt Flag is set one cycle after the Receive Complete Interrupt Flag is set when both the value in the shift register and in the Transmit Data Buffer register has been sent.
Client Buffer Mode with Wait for
Receive Bit Written to ‘1
’
In Client mode, if the Buffer Mode Wait for Receive (BUFWR) bit in SPIn.CRTLB is written
to ‘1
’, the transmission of user data starts as soon as the
SS pin is driven low. Figure 26-4
shows a transmission sequence with this configuration. Notice how the value
0x45
is written to the Data (SPIn.DATA) register but never
transmitted.
All writes to the Data (SPIn.DATA) register go to the Transmit Data Buffer register. The
figure above shows that the value 0x43
is written to the Data
(SPIn.DATA) register, and since the SS pin is high, it is
copied to the shift register in the next cycle. The next write (0x44
)
will go to the Transmit Data Buffer register. During the first transfer, the value
0x43
will be shifted out. In the figure above, the value
0x45
is written to the Data (SPIn.DATA) register, but the Transmit
Data Buffer register is not updated since the DREIF is low. After the transfer is
completed, the value 0x44
from the Transmit Data Buffer register is
copied to the shift register. The value 0x46
is written to the Transmit
Data Buffer register. During the next two transfers, 0x44
and
0x46
are shifted out. The flags behave identically to the Buffer
Mode Wait for Receive (BUFWR) bit in SPIn.CTRLB set to ‘0
’.