General SPI Question: using SPI.begin();


I’m using three devices on the same SPI bus (SPI1). Do I need to call the device-specific begin() function each time I call a different device?

Here’s an example to illustrate my question (using only two of the devices):

Adafruit_STMPE610 ts = Adafruit_STMPE610(STMPE_CS);
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, -1);


while (ts.bufferEmpty()) {        // Wait in this while loop until touched
//    "Waiting..."

TS_Point p = ts.getPoint(); 
p.x = map(p.x, TS_MINX, TS_MAXX, 0, tft.width());     // Scale from ~0->4000 to tft.width using the calibration #'s
p.y = map(p.y, TS_MINY, TS_MAXY, 0, tft.height());

tft.setCursor(0, 12);                         // Set tft cursor
tft.setTextColor(ILI9341_WHITE);              // Set text color
tft.setTextSize(5);                           // Set text size
tft.print(p.x);                               // Print p.x on TFT
tft.print(p.y);                               // Print p.x on TFT

When I had the code running on Arduino, I didn’t need to do this…but for some reason, when I ported over to Photon I do.

One hypothesis is that each device is running on a different clock speed, so calling begin() sets the correct clock speed.

Another hypothesis is that it’s simply setting the CS for the correct device when I call begin()…but why wasn’t it doing that on Arduino?

Thanks for any help, in advance! :smile:


Normally libraries should control their own CS pins (and most do) and the clock speed should not be an issue, but to be sure, call the begin() function of the slowest device last.

A “bigger” problem is when the SPI modes or bit order are different. Arduino has a transaction scheme that alows for switching between these modes but Particle does not, so many libraries rely on the modes to be set correctly once for all times but if one begin() sets other modes than another device requires, you’d have to call that respective begin() again, with possibly unknown sideffects, depending on the other actions performed in begin().

There was a proposal of @peekay123 for adding SPI transactions a while back, but we haven’t heard anything back from Particle about that since.

Try SdFat, a Library for SD Cards

Got it, thank you @ScruffR. I’ll have a look at the libraries and see if they have differing bit orders, etc.

Should there any lag between switching devices? My touch screen isn’t initializing 100% of the time on initial boot up…wondering if I need a small delay(100) or so between?

@peekay123 Any progress on SPI transactions @ScruffR mentioned?