From ae510fee0069356fda16d20c5eb169216186e727 Mon Sep 17 00:00:00 2001
From: Alberto Nidasio <alberto.nidasio@skywarder.eu>
Date: Tue, 30 May 2023 18:54:56 +0200
Subject: [PATCH] [SPI] Added asserts on RX FIFO

---
 src/shared/drivers/spi/SPIBus.h | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/src/shared/drivers/spi/SPIBus.h b/src/shared/drivers/spi/SPIBus.h
index fa58b7a8c..5e93f58c7 100644
--- a/src/shared/drivers/spi/SPIBus.h
+++ b/src/shared/drivers/spi/SPIBus.h
@@ -22,6 +22,7 @@
 
 #pragma once
 
+#include <assert.h>
 #include <interfaces/delays.h>
 #include <utils/ClockUtils.h>
 
@@ -489,6 +490,9 @@ inline void SPIBus::read(uint8_t* data, size_t nBytes)
 
 inline void SPIBus::read16(uint16_t* data, size_t nBytes)
 {
+    // At the start of the transfer we assume that the RX FIFO is empty
+    assert((spi->SR & SPI_SR_RXNE) == 0);
+
     // Set 16 bit frame format
     set16BitFrameFormat();
 
@@ -529,6 +533,9 @@ inline void SPIBus::write(const uint8_t* data, size_t nBytes)
 
 inline void SPIBus::write16(const uint16_t* data, size_t nBytes)
 {
+    // At the start of the transfer we assume that the RX FIFO is empty
+    assert((spi->SR & SPI_SR_RXNE) == 0);
+
     // Set 16 bit frame format
     set16BitFrameFormat();
 
@@ -555,6 +562,9 @@ inline void SPIBus::write16(const uint16_t* data, size_t nBytes)
 
 inline uint8_t SPIBus::transfer(uint8_t data)
 {
+    // At the start of the transfer we assume that the RX FIFO is empty
+    assert((spi->SR & SPI_SR_RXNE) == 0);
+
     // Wait until the peripheral is ready to transmit
     while ((spi->SR & SPI_SR_TXE) == 0)
         ;
@@ -572,6 +582,9 @@ inline uint8_t SPIBus::transfer(uint8_t data)
 
 inline uint16_t SPIBus::transfer16(uint16_t data)
 {
+    // At the start of the transfer we assume that the RX FIFO is empty
+    assert((spi->SR & SPI_SR_RXNE) == 0);
+
     // Set 16 bit frame format
     set16BitFrameFormat();
 
@@ -619,6 +632,9 @@ inline void SPIBus::transfer(uint8_t* data, size_t nBytes)
 
 inline void SPIBus::transfer16(uint16_t* data, size_t nBytes)
 {
+    // At the start of the transfer we assume that the RX FIFO is empty
+    assert((spi->SR & SPI_SR_RXNE) == 0);
+
     // Set 16 bit frame format
     set16BitFrameFormat();
 
-- 
GitLab