SPI Slave full duplex with DMA

Maple Mini, Maple Rev3, Maple Rev 5 and Maple Ret 6, iTead Maple etc
Post Reply
FCam1
Posts: 4
Joined: Wed Nov 08, 2017 5:22 pm
Location: France

SPI Slave full duplex with DMA

Post by FCam1 » Wed Nov 08, 2017 6:34 pm

Hello,

I'm working on a robotic project that implies fast bidirectional data exchange between a maple mini and a raspberry pi 3.

I would like to use DMA on maple mini side to act as SPI full duplex slave (dma rx + dma tx).

I found this example on this post (https://github.com/stevstrong/Audio-sam ... t.ino#L111)of the forum, but it only deals with rx part.
I also found out that (https://github.com/rogerclarkmelbourne/ ... I.cpp#L454) : there are methods in SPI lib (dmaTransfer() / dmaTransferRepeat() / dmaSendRepeat() / dmaSend()). Does this methods work for slave operation or do i need to manually set registers from my sketch ? Is there any examples that uses this function for slave full duplex ?

Is it even possible to do what i want to do with the mini ?

Thanks a lot !

stevestrong
Posts: 1824
Joined: Mon Oct 19, 2015 12:06 am
Location: Munich, Germany

Re: SPI Slave full duplex with DMA

Post by stevestrong » Wed Nov 08, 2017 8:02 pm

You cannot use both master and slave with the same interface. Reason is the clock signal, which is generated always by the master.
But you can configure one SPI interface as master (let's say SPI1) and SPI2 as slave.

Code: Select all

SPIClass spi1(1);
SPIClass spi2(2);

...
spi1.beginTarnsaction(SPISettings(36000000)); // spi1 as master, with 36MHz clock
spi2.beginTransactionSlave(); // spi2 as slave, clock is generated by the (external) master
...

knielsen
Posts: 7
Joined: Sat Nov 04, 2017 10:10 am

Re: SPI Slave full duplex with DMA

Post by knielsen » Wed Nov 08, 2017 11:27 pm

A full-duplex SPI slave always transmits one byte to the master in parallel
with one byte sent from (and initiated by) the master.

Thus, when transmitting from slave to master, you need to prepare the byte
to send, and then wait for whenever the master decides to initiate a
transfer.

This makes SPI slave usually not very useful with DMA. Only if you know in
advance on the slave exactly what data to transmit to the master the next
time, and how many bytes, can Tx DMA be set up.

If you want the usual kind of communication where the master sends a command
and the slave reacts to it with some reply, you are probably better off by
setting up an interrupt to trigger after slave reads one command byte from
the master. Then the interrupt can switch on the command type and prepare
the proper data to transmit back as reply (and then this reply could use
DMA, and probably only needs Tx DMA, not Rx DMA).

Hope this helps,

- Kristian.

stevestrong
Posts: 1824
Joined: Mon Oct 19, 2015 12:06 am
Location: Munich, Germany

Re: SPI Slave full duplex with DMA

Post by stevestrong » Thu Nov 09, 2017 9:21 am

OK, it seems that I misinterpreted the meaning of full-duplex SPI.
If it is really so as Kristian says, then the current SPI lib does this already, with DMA.

If you call

Code: Select all

SPI.dmaTransfer(txBuffer, rxBuffer, length);
this will send <length> bytes of data to SPI slave and will simultaneously receive in the rxBuffer the same amount of bytes from the same slave.

@FCam1, is this what you want?

FCam1
Posts: 4
Joined: Wed Nov 08, 2017 5:22 pm
Location: France

Re: SPI Slave full duplex with DMA

Post by FCam1 » Thu Nov 09, 2017 4:09 pm

Hi, Yes so to be more precise, I want to implement this dmaTransfer on the mini as a slave.
So whenever the master (R-PI) do a known size spi transfer (generating the CLK), the mini records incoming bytes in RX dma buffer, and simultaneously transfers the TX dma buffer (that has already been filled with data before the transfer happend).

So does this SPI.dmaTransfer(txBuffer, rxBuffer, length) works in slave mode?

It would be useful to have a way to transmit data very quickly in bidirectional mode with a computer !

In my robotic design, I want to read data from various sensors, and in the same time, send control command to apply to actuators. The mini would just be an interface between high level controller running on the PI and various sensors and low level control boards with different bus and protocol (UART, I2C...)

I will start to go deeper in the lib to understand if what i'm looking for is already implemented in SPI lib (i'm not an expert in programming such low level stuff)

Thanks again !

stevestrong
Posts: 1824
Joined: Mon Oct 19, 2015 12:06 am
Location: Munich, Germany

Re: SPI Slave full duplex with DMA

Post by stevestrong » Thu Nov 09, 2017 4:27 pm

Ah, I think what you need is a bidirectional slave configuration.
Currently in slave mode the STM32 device only receives data, but does not transmit anything.
And you want to send some data already written to the Tx buffer.

Afaik, we don't have yet this configuration, or at least I don't know anyone who implemented such structure.
Maybe the original poster here can help you:
http://stm32duino.com/viewtopic.php?f=9 ... ave#p34152
In the current version of the lib, for the slave mode the Rx mode only is set: https://github.com/rogerclarkmelbourne/ ... I.cpp#L164
So you have to figure out which DMA register settings are needed for you.

victor_pv
Posts: 1746
Joined: Mon Apr 27, 2015 12:12 pm

Re: SPI Slave full duplex with DMA

Post by victor_pv » Thu Nov 09, 2017 6:05 pm

The current DMA functions will likely work if you set the SPI peripheral in slave mode before calling them.
I do not remember if the code to set the SPI port in slave mode is there or not, you will need to check on that.

The DMA functions don't really care if the device generates the clock, or receives the clock. All they do is setup the DMA peripheral to serve the SPI peripheral DMA requests. As long as the SPI is in slave mode, and generates DMA requests when it needs to send date or has received some, I believe the DMA part setup should be exactly the same.
I haven't looked at the DMA or the SPI in a few months, so I may be forgetting something, but I think you should try to:
1.- Set SPI port in slave mode
2.- Use dmaSend to send data.
3.- Start RX in the Master SPI port, and see if it get's data in.

User avatar
mrburnette
Posts: 1885
Joined: Mon Apr 27, 2015 12:50 pm
Location: Greater Atlanta
Contact:

Re: SPI Slave full duplex with DMA

Post by mrburnette » Fri Nov 10, 2017 1:40 am

FCam1 wrote:
Thu Nov 09, 2017 4:09 pm
<...>
In my robotic design, I want to read data from various sensors, and in the same time, send control command to apply to actuators. The mini would just be an interface between high level controller running on the PI and various sensors and low level control boards with different bus and protocol (UART, I2C...)
<...>
While not directly what you want to accomplish, you may find this link interesting.

With any Raspberry Pi but you must keep in mind that you are running on multicore h/w with Noobs Linux, so there is a latency issue you must manage unless your Raspberry Pi code can be bound to near_real_time. See, this.

Ray

FCam1
Posts: 4
Joined: Wed Nov 08, 2017 5:22 pm
Location: France

Re: SPI Slave full duplex with DMA

Post by FCam1 » Thu Nov 30, 2017 6:49 pm

I come back with good news !

I succeeded to communicate in Full Duplex with a Maple mini as slave.
The configuration of the SPI was not easy but after hours it works !
The code of initialization of the SPI have to be cleaned in order to have an easier way to do set up this configuration.

I created a new class to transmit data as slave with a buffer.

PS : For the moment and with the the conclusion of the firsts tests I don't need to use the DMA (sufficient speed).


My repo : https://github.com/FCam1/Low_Level_Soft ... master/SPI
Attachments
transferSlave.h
(117 Bytes) Downloaded 6 times
transferSlave.c
(1.27 KiB) Downloaded 8 times
SPI_Slave_Full_Duplex.ino
(1.08 KiB) Downloaded 7 times

User avatar
RogerClark
Posts: 7481
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: SPI Slave full duplex with DMA

Post by RogerClark » Thu Nov 30, 2017 9:41 pm

Interesting

Perhaps the function should eventually be added to the LibMaple core.

However I'll leave it to Steve and Victor to recommend whether they think its OK, as they have become the DMA and SPI experts

Post Reply