How to initiallize the two SPI ports of STM32

Vassilis
Thu Jun 04, 2015 8:18 am
In case you want to use two SPI devices simultaneusly to achieve the maximum speed performance the you can connect the first device to SPI-1 port and the second device to SPI2 port.

For example, If you want to make a project just like my WebRadio player that uses two SPI modules (ENC28J60 Ethernet module and the VS1053B MP3 decoder) then it’s better to connect the ENC28J60 to SPI-1 port and VS1053B to the SPI-2 port. That connection will maximize the data transfer from the ENC28J60 to the VS1053B.

Moreover, The ENC28J60 works at maximum 20 MHz SPI speed and VS1053B at 7.8 MHz (max). So, you can set only once the speed of each SPI port (SPI.setClockDivider).

With the great help of Roger finally I managed to use the second SPI port of my STM32F103C8T board.
I would like to present you two small example codes.

Using the first SPI port (SPI)

SS <–>PA4
SCK <–>PA5
MISO <–>PA6
MOSI <–>PA7

#include <SPI.h>

byte data;

void setup() {

SPI.begin(); //Initiallize the SPI 1 port.
//SPI.setBitOrder(MSBFIRST); // Set the SPI-1 bit order (*)
//SPI.setDataMode(SPI_MODE0); //Set the SPI-1 data mode (**)
//Set the SPI speed
SPI.setClockDivider(SPI_CLOCK_DIV16); // Slow speed (72 / 16 = 4.5 MHz SPI speed)

}

void loop() {
data = SPI.transfer(0x55); //Send the HEX data 0x55 over SPI-1 port and store the received byte to the <data> variable.
delayMicroseconds(10); //Delay 10 micro seconds.

}


pico
Sun Jun 07, 2015 4:42 am
A version of these example should probably find their way to the “example” folder. Useful stuff — I had resorted started looking at the lib source to try to figure out what was shown in a few lines here.

I realize that recently a lot of extended work on SPI w/ DMA has been done too — that all should all have some example programs added too, when the dust has settled.

A couple of suggestions for backwards compatibility with the original libmaple lib:

a) Bring back the defines like “SPI_4_5MHZ” as equivalents to “SPI_BAUD_PCLK_DIV_16” etc., just so the old sketches will compile. No harm in being to able to use either! Similarly define the older “SPI_MODE_0” as well as “SPI_MODE0”.

b) It was possible to pass all the config parameters in the begin() method as follows:

SPI.begin(SPI_4_5MHZ, MSBFIRST, SPI_MODE_0);

instead of:

SPI.begin();
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE0);
SPI.setClockDivider(SPI_BAUD_PCLK_DIV_16); // 72 / 16 = 4.5 MHz speed

as is now required. Syntactic sugar, to be sure, but it makes for more succinct (and readable) code IMHO. As well as making the new lib backward compatible with the old sketches!


mrburnette
Sun Jun 07, 2015 3:17 pm
pico wrote:<…>
as is now required. Syntactic sugar, to be sure, but it makes for more succinct (and readable) code IMHO. As well as making the new lib backward compatible with the old sketches!

pico
Mon Jun 08, 2015 4:11 pm
mrburnette wrote:Leaflabs was stuck in the pre-1.0 Arduino world.

mrburnette
Mon Jun 08, 2015 4:29 pm
pico wrote:
<…>
Actually, Leaflabs was always lightyears ahead of the Arduino world, and in many ways still is, even now. And I’m not just talking about their hardware. If you dig into the core files, you will see they chucked out almost everything Arduino, and rewrote things to a much higher standard. <…>

pico
Tue Jun 09, 2015 1:01 pm
mrburnette wrote:
I think you have missed my point: the use of “stuck” was intended on meaning they took open source Arduino GUI, and completely rewrote it to suit their own purposes: This was pre-Arduino 1.0 GUI.

rexnanet
Wed Jun 22, 2016 12:57 pm
SPIClass SPI_2(2); //Create an SPI2 object.

RogerClark
Wed Jun 22, 2016 10:02 pm
You can already do this.

The SPI library automatically instantiates just one SPI instance, which is on SPI1 by default.

You can instantiate another SPI instance in your code and assign it in the constructor to SPI 2

The problem is that most libraries dont allow you to pass an SPI imstance to the, and they are hard coded to use then”SPI”‘ class instance i.e SPI1
hence the need for setModule


rexnanet
Wed Jun 29, 2016 9:05 am
RogerClark wrote:You can already do this.

rexnanet
Thu Jul 14, 2016 12:57 pm
I’ve managed to do this but only by modifying the library’s SPI class to SPI_2 for example (SPIClass SPI_2(2);).

Thanks.


stevestrong
Thu Jul 14, 2016 1:48 pm
@rexnanet
The example here (using SPI_2(2)) should work without any lib modification.
Have you tried it out?

rexnanet
Thu Jul 14, 2016 4:33 pm
stevestrong wrote:@rexnanet
The example here (using SPI_2(2)) should work without any lib modification.
Have you tried it out?

RogerClark
Thu Jul 14, 2016 9:49 pm
The setModule function is the method we added to allow old libraries to work on SPI2 etc

stevestrong
Fri Jul 15, 2016 11:00 am
RogerClark wrote:The setModule function is the method we added to allow old libraries to work on SPI2 etc

rexnanet
Fri Jul 15, 2016 12:46 pm
stevestrong wrote:RogerClark wrote:The setModule function is the method we added to allow old libraries to work on SPI2 etc

Leave a Reply

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