How can I set up with the SPI library the second SPI port?
*shame*
It should be set up with the SPIClass(<port>), but it wont function:
Trying a simple “SPIClass(1).begin” (=equiv. to SPI.begin) crash the maple…
I looked at this a while ago.
I can’t remember why, I think its the way that arduino uses global constructors in libraries, which make it hard to change this
I’m sure I looked for a method (not sure I may have posted to the old Arduino forum thread)
Anyway. I think the best solution is just to duplicate the SPI lib and call its SPI2
e.f.
#include <SPI2.h>
But the question, is whether the global SPI variable then needs to be called SPI or whether we should call it SPI2
I think calling it SPI2 would probably be better, otherwise you can’t have 2 SPI channels at the same time
Alternatively, we have a version of SPI which doesnt have the global constructor, and you have to manually construct the class
Its a shame that Arduino chose to use the global constructor stuff.
Ah, I partially remember now.
I was trying to put a #ifdef into SPI to stop the global constuctor being called, so that I could manually construct it in the code, but I don’t think its possible
if you put a #define NO_GLOBAL_CONSTRUCTORS in the sketch, its not picked up by the library,
libs are compiled first
It would need to be a menu option, but those are bad news.
I’ve not tried including another header e.g. #include no_global_constructors.h which would define , NO_GLOBAL_CONSTRUCTORS
I guess thats worth a go.
But perhaps just versions of the libraries that don’t globally construct is better
ha ha ha…and I was really thinking, I’m mad
Ok: I see no problem in a duplicate SPI2 library (and think further: SPI3) so you can include easily different DMA settings
I was only testing all nucleo stuff, so I’m sure, I’m the first working with two separate SPI ports…but (how often do I write “but”?)
BUT:
Practically I have one scenario for using 2 SPI ports:
TFT screen
SD-card
Perhaps the SD-card is too slow with the high TFT clock rate, so you can choose a slower rate for SD-card without slowing down the whole thing and/or use different DMA settings.
No. you are not going mad ![]()
I recall someone else asked me the same question
Perhaps we can get a bit of community feedback
e.g.
should we have
#include <SPI2.h>
which has a global var of SPI2
#include <SPI3.h>
which has a global var of SPI3
Sounds like the best option, albeit code would be duplicated which means any change to SPI1 would need to be copied into SPI2 etc
I asked about SPI2, and I believe Roger said it could be used with HardwareSPI. I have not tried it thought.
Given that Arduino does not have a second port, we can choose whichever way to call it. I think it would be better to not have it instantiated, that way it does not take any memory unless you decide to use it in your sketch, in which case you instantiate it.
We should be able to do DMA transfers the exact same way, just need to change a couple of parameters.
That’s my 2 cents.
However I don’t think there is any way to put something in the sketch that would be picked up by the libraries that could be used to prevent the global var.
We could do it as menu, but I don’t think that’s a good idea, as when you open a particular sketch you would also need to change the menu settings.
Perhaps its a question for the programming section on the Arduino forum, but last time I posted the same question, people deemed it to be a non Arduino question, so I didn’t get any useful responses
Is it correct to setup SPI2 only in changing the last line (544 hard to find!) in SPI.cpp from
SPIClass SPI(1);EDIT2: Thinking further: Ok, Equal we handle to use two (or more) SPI ports simultaneously, there will be always the need to reconfigure the used libraries, because all of them are related to “SPI”. So maybe we “declare” SPI2 as storage SPI port (SD Cards, Flash RAM,….)?
We have to look into “Energia” and “chipkit” how they handle SPI with multiple ports.
https://github.com/energia/Energia/tree … raries/SPI – I don’t know how/if we get benefit out of it.
Edit: But mabye it’s a better thing to use one SPI port and use/implement transactions:
https://www.pjrc.com/teensy/td_libs_SPI.html
/// set up the speed, mode and endianness of each device
SPISettings settingsA(2000000, MSBFIRST, SPI_MODE1);
SPISettings settingsB(16000000, LSBFIRST, SPI_MODE3);
