using SPI in slave mode

stevestrong
Tue Feb 02, 2016 1:22 pm
Hi all,
I just wanted to know whether someone already tested the SPI in slave mode and I am interested in the used code snippet if available.
Currently I am working on a project which involves data transfer between two maple minis over SPI in 16 bit mode, the one is master the other is slave also involving DMA access from SPI Rx to RAM.
I tried to implement some basic functions to setup the SPI in slave mode, similar to the routines of SPI master initialization, but it doesn’t seem to work.
I can see the clock, data and NSS coming out correctly from the master, but the DMA tube on the slave device doesn’t get fired at all, I don’t know whether SPI is receiving data or not.
Any working example could help me a lot.
Thanks.
Steve

mrburnette
Tue Feb 02, 2016 1:43 pm
@steve,

There was a good amount of noise in the forum last year about the SPI select, but I could not find anything about SPI slave-mode in our forum. I did find this article in the old Leaflabs forum:

Summary
…. to the point in my checking where it seems that the Maple in slave mode SPI simply doesn’t pay attention to the slave select line. My Seleae logic analyzer shows good clear data going into the Maple on all the right pins. For testing I’m using an Arduino 2009 as an SPI master to generate data, and have been slowing it down trying to find what is going wrong.

Ray


RogerClark
Tue Feb 02, 2016 8:36 pm
To summarise the NSS stuff.

STMs implementation inside the STM32 of what the NSS does is very odd.
It doesn’t operate in the way most people expect a Slave Select line to operate in terms of controlling external SPI devices.
There are plenty of postings on STMs own forum about this, basically complaining it doesn’t work.

So for SPI master, we gave up on using the hardware Slave Select pin (NSS).

Also. like Ray has already said… I dont recall anyone attempting SPI slave functionality before with the Stm32 libmaple core i.e what we use.


stevestrong
Tue Feb 02, 2016 9:18 pm
OK, so it seems I am kind of pioneer, right?
Hope I’ll manage alone, just that it will take some time.

stevestrong
Wed Feb 03, 2016 10:25 am
Managed to get DMA fired by SPI Rx.
The DMA-access had to be enabled before the SPI periphery gets enabled, which is not compatible with the actual “spi_reconfigure()”…
So far only 0xFFFFs got stored, but I’m still working on it.

RogerClark
Wed Feb 03, 2016 7:41 pm
thanks for the update

stevestrong
Fri Feb 05, 2016 1:50 pm
UPDATE
It looks like the problem was somewhere else, not in the SPI data communication, which is working as expected!
The SPI slave seems to work stable in 16 bit data mode.
Have to think about the most convenient change to check in to the repo, my actual quick’n-dirty solution may not like to everyone :)

RogerClark
Fri Feb 05, 2016 8:05 pm
Thanks Steve

If you dont have time to write a nice clean solution…

Even your prototype solution could be beneficial to anyone else needing this.

It doesnt need to go into the repo, a code snippet here would be better than nothing

Thanks

Roger


stevestrong
Fri Feb 05, 2016 11:01 pm
The thing is that I had to write new functions for slave settings in SPI.c, although direct register settings could have been used in INO file.
Anyway, I wanted to have it clean in the repo compatible format.
In the next week I’ll be in holiday, from tomorrow on, will continue when I’m back.
Cheers,
Steve

RogerClark
Sat Feb 06, 2016 1:14 am
Thanks Steve

I appreciate the sentiment. I often write stuff that I’m happy to use for my own testing, but would not publish as its not tidy or optimised ;-)

Cheers

Roger


WereCatf
Wed Apr 27, 2016 10:39 am
I was just thinking if there are any examples here on how to use the STM32F1 as an SPI-slave, has stevestrong gotten his code cleaned up yet? I’d like to see how it’s done.

stevestrong
Thu May 05, 2016 7:24 pm
OK, here they are, the master and the slave example.

gevard
Wed Jun 22, 2016 7:55 am
Thanks for sharing the codes.
I got an error about SPI.beginTransactionSlave, compiler claim is not member of ‘Class SPIClass’.
Do you use standard stm32duino SPI library?

stevestrong
Wed Jun 22, 2016 8:19 am
Do you use standard stm32duino SPI library?
Yes, but I think I didn’t checked in my local changes.
I have to send a PR to Roger.

RogerClark
Mon Nov 21, 2016 3:22 am
I think the SPI changes PR may still be pending, I’ll need to take a look.

From what I recall, the SPI and USB were in the same PR, and I manually merged the USB changes, but I can’t recall if I did the SPI changes or not.

I’ll need to check


stevestrong
Mon Nov 21, 2016 9:37 am
The examples should work using my repo, which contains these functions.
I think these changes are already initiated in a PR related to another topic due to my incompetency to correctly handle PRs on github :oops:

synfinatic
Mon Jul 03, 2017 3:25 am
Any chance of merging these changes into the mainline git repo?

stevestrong
Tue Jul 04, 2017 6:23 pm
The changes are all in the main repo master branch now.

synfinatic
Wed Jul 05, 2017 1:37 am
Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *