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: 54
Joined: Tue Feb 25, 2020 4:06 pm
Been thanked: 1 time

Re: SPI Slave Driver

Postby JSStabl » Wed Mar 25, 2020 3:37 pm

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: 12943
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 729 times
Been thanked: 609 times
Contact:

Re: SPI Slave Driver

Postby Giovanni » Wed Mar 25, 2020 4:56 pm

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: 54
Joined: Tue Feb 25, 2020 4:06 pm
Been thanked: 1 time

Re: SPI Slave Driver

Postby JSStabl » Wed Mar 25, 2020 5:24 pm

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: 12943
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 729 times
Been thanked: 609 times
Contact:

Re: SPI Slave Driver

Postby Giovanni » Wed Mar 25, 2020 10:35 pm

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: 54
Joined: Tue Feb 25, 2020 4:06 pm
Been thanked: 1 time

Re: SPI Slave Driver

Postby JSStabl » Thu Mar 26, 2020 10:11 am

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: 12943
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 729 times
Been thanked: 609 times
Contact:

Re: SPI Slave Driver

Postby Giovanni » Thu Mar 26, 2020 12:16 pm

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

Giovanni


Return to “Small Change Requests”

Who is online

Users browsing this forum: No registered users and 2 guests