SPI Slave Driver

Use this forum for requesting small changes in ChibiOS. Large changes should be discussed in the development forum. This forum is NOT for support.
JSStabl
Posts: 78
Joined: Tue Feb 25, 2020 4:06 pm
Has thanked: 3 times
Been thanked: 2 times

Re: SPI Slave Driver

Post by JSStabl »

Any updates on the official support for the slave driver? Would make the chibiOS HAL only excellent for small slave devices.

I created a patch for the SPIv2 and the newest chibos 191. Does that seem right?

Code: Select all

Index: os/hal/include/hal_spi.h
===================================================================
--- os/hal/include/hal_spi.h   (revision 8ef845498982e9b5f2b8368ecd9af744c90ac6c6)
+++ os/hal/include/hal_spi.h   (date 1585150715934)
@@ -152,6 +152,10 @@
    */
   bool                      circular;
 #endif
+  /**
+    * @brief SPI Slave mode flag.
+    */
+    bool                      slave_mode;
+    /**
   /**
    * @brief Operation complete callback or @p NULL.
    */
   
   
Index: os/hal/ports/STM32/LLD/SPIv2/hal_spi_lld.c

===================================================================
--- os/hal/ports/STM32/LLD/SPIv2/hal_spi_lld.c   (revision 8ef845498982e9b5f2b8368ecd9af744c90ac6c6)
+++ os/hal/ports/STM32/LLD/SPIv2/hal_spi_lld.c   (date 1585150414264)
@@ -465,9 +465,17 @@
 
   /* SPI setup and enable.*/
   spip->spi->CR1 &= ~SPI_CR1_SPE;
-  spip->spi->CR1  = spip->config->cr1 | SPI_CR1_MSTR;
-  spip->spi->CR2  = spip->config->cr2 | SPI_CR2_FRXTH | SPI_CR2_SSOE |
+   if (spip->config->slave_mode) {
+        spip->spi->CR1  = spip->config->cr1;
+        spip->spi->CR2  = spip->config->cr2 | SPI_CR2_FRXTH |
+                    SPI_CR2_RXDMAEN | SPI_CR2_TXDMAEN;
+   }
+   else{
+       spip->spi->CR1  = spip->config->cr1 | SPI_CR1_MSTR;
+       spip->spi->CR2  = spip->config->cr2 | SPI_CR2_FRXTH | SPI_CR2_SSOE |
                     SPI_CR2_RXDMAEN | SPI_CR2_TXDMAEN;
+   }
+
   spip->spi->CR1 |= SPI_CR1_SPE;
 }
 

Last edited by JSStabl on Wed Mar 25, 2020 5:21 pm, edited 1 time in total.
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: SPI Slave Driver

Post by Giovanni »

Hi,

The current SPI driver will not support officially a slave mode, it is not designed for that.

A separate slave SPI driver will be eventually introduced, so far I have no clear idea of how it should be, there is a lot of variability on possible slave SPI uses.

Giovanni
JSStabl
Posts: 78
Joined: Tue Feb 25, 2020 4:06 pm
Has thanked: 3 times
Been thanked: 2 times

Re: SPI Slave Driver

Post by JSStabl »

Would you recommend using plyatovs solution from here: viewtopic.php?f=38&t=3213&start=20#p32291 (earlier in this thread) for a production system? Or is it more wise to create a complete Slave SPI solution without using chibiOS HAL?

Furthermore a driver that has a read/write buffer it transmits/receives on NSS and an interrupt/event for a incoming message probably would be very low level and expandable.
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: SPI Slave Driver

Post by Giovanni »

For a slave SPI application, my recommendation is to design a driver specifically for the protocol you need, I have not found yet a generic enough solution or I would have already implemented it in HAL.

The current SPI driver can be adapted (like you did) to implement very simple SPI slave scenarios, like exchanging fixed size buffers, more complex things like "idle frames" are more difficult to implement. See the SPI MMC/SD protocol, that would be very hard to implement without a specific driver.

Giovanni
JSStabl
Posts: 78
Joined: Tue Feb 25, 2020 4:06 pm
Has thanked: 3 times
Been thanked: 2 times

Re: SPI Slave Driver

Post by JSStabl »

Understood. Why not expose the the "very simple" scenario by just exposing the slave flag to the config?
As I understand the rest of the code would already allow for simple receive/sends (see example from plyatov).

Can you help me understand how the HAL API would behave in this slave scenario? As far as I understood, calling the Send/Receive/Exchange function "primes" the SPI to fill/empty the receive/transmit buffer when the master sends a signal, how is that triggered? Over the hardware NSS or simply when the clock starts running?

Here is my idea of things:
I'm planning to use the HAL without an RTOS, so I would call the exchange function and set a callback interrupt when a transmission was handled. In the callback I deal with my data, clear the buffer and call the exchange function again for the next transfer. I always know that my transmission is two bytes so I can set that beforehand.

Thanks!
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: SPI Slave Driver

Post by Giovanni »

The problem is long term support, I am not going to expose as an official feature a workaround.

Giovanni
mobyfab
Posts: 484
Joined: Sat Nov 19, 2011 6:47 pm
Has thanked: 21 times
Been thanked: 31 times

Re: SPI Slave Driver

Post by mobyfab »

For those who don't want to patch anything, you can simply do that:

Code: Select all

spiStart(&SPID1, &spicfg);
// Patch config for slave mode
SPID1.spi->CR1 &= ~SPI_CR1_SPE; // Disable
SPID1.spi->CR1 &= ~SPI_CR1_MSTR; // Remove master mode flag
SPID1.spi->CR2 &= ~SPI_CR2_SSOE; // Remove slave select output
SPID1.spi->CR1 |= SPI_CR1_SPE; // Enable


Tested OK on G431
Post Reply