Skip to content

Commit

Permalink
[stm32] Prevent SPI RX FIFO overflow in SPI master
Browse files Browse the repository at this point in the history
In full duplex mode (only mode supported by SpiMaster), every
transmitted byte will produce a corresponding received byte which is
pushed into the RX FIFO with some delay. The existing TX loop does not
limit outstanding transactions to ensure they will fit within the RX
FIFO. If the SPI PHY transmits a frame while the HAL is in its TX loop,
it will freely push more TX frames until the TX FIFO is full. These
extra frames may overflow the RX FIFO if the HAL hasn't gotten to
popping incoming frames by the time they are received.

This could be solved by checking for the SUSP (suspended to prevent
overflow) during TX or by using the DXP bit to coordinate transactions
instead of TXP/RXP. This commit instead chooses to track the number of
outstanding frames using the existing counters and prevent transmit if
there are more outstanding frames than would fit in the RX FIFO.

This implementation assumes 8-bit/one-byte SPI data size, which is an
existing limitation of the SpiMaster.
  • Loading branch information
WasabiFan authored and salkinium committed Nov 12, 2024
1 parent 7a9d378 commit 678fd9a
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/modm/platform/spi/stm32h7/spi_master.cpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ SpiMaster{{ id }}::transfer(
std::size_t txIndex = 0;

while (rxIndex < length) {
while ((txIndex < length) and !Hal::isTxFifoFull()) {
while ((txIndex < length) and !Hal::isTxFifoFull() and (txIndex - rxIndex) < Hal::FifoSize) {
Hal::write(tx ? tx[txIndex] : 0);
++txIndex;
}
Expand Down Expand Up @@ -110,7 +110,7 @@ SpiMaster{{ id }}::transfer(
while (rxIndex < length) {
default:
{
while ((txIndex < length) and !Hal::isTxFifoFull()) {
while ((txIndex < length) and !Hal::isTxFifoFull() and (txIndex - rxIndex) < Hal::FifoSize) {
Hal::write(tx ? tx[txIndex] : 0);
++txIndex;
}
Expand Down

0 comments on commit 678fd9a

Please sign in to comment.