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.
}
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!
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!
<…>
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. <…>
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.
SPIClass SPI_2(2); //Create an SPI2 object.
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
Thanks.
The example here (using SPI_2(2)) should work without any lib modification.
Have you tried it out?
The example here (using SPI_2(2)) should work without any lib modification.
Have you tried it out?