Total silly question about second SPI port

madias
Tue May 05, 2015 9:28 pm
Ok, it’s late and I’m momentary confused:
How can I set up with the SPI library the second SPI port?
*shame*

madias
Tue May 05, 2015 10:01 pm
Ok, meanwhile I do not found my question “silly”
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…

RogerClark
Tue May 05, 2015 10:16 pm
Matthias

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


madias
Tue May 05, 2015 10:25 pm
Roger, thank you for the answer,
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.

RogerClark
Tue May 05, 2015 11:35 pm
Matthias

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


victor_pv
Wed May 06, 2015 12:09 pm
Madias I thought on the same scenario, SPI1 for a TFT and SPI2 for an SDCard.

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.


RogerClark
Wed May 06, 2015 9:29 pm
I agree we could do with libraries that don’t do global instantiation

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


madias
Wed May 06, 2015 9:32 pm
I’m playing around to get SPI Port#2 working on my nucleo, I think I’ve to mind several things that are not correct at the moment, so I’ll be sure to setup the SPI library correctly:
Is it correct to setup SPI2 only in changing the last line (544 hard to find!) in SPI.cpp from
SPIClass SPI(1);

mrburnette
Wed May 06, 2015 10:59 pm
madias wrote:<…>

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,….)?


madias
Thu May 07, 2015 6:07 am
Ok, i think, there might be some possibilities:
We have to look into “Energia” and “chipkit” how they handle SPI with multiple ports.

RogerClark
Thu May 07, 2015 6:57 am
How do they do it ???

madias
Thu May 07, 2015 9:04 am
Ok, chipkit is using a separate library (DSPI), and energia (on stellaris/tiva) is using the “select Module” parameter:
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);


Leave a Reply

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