KurtE
Senior Member+
This is probably a subset of the larger SPI questions that are talked about in the thread: http://forum.pjrc.com/threads/25582...-main-program?highlight=SPI+interrupt+program which I need to reread (probably several times) and also read through the Arduino email lists mentioned in that thread. But I am in this case I am not talking about interrupts (yet)
Also some of this information is from another thread about using the Adafruit 2.8" TFT display (http://forum.pjrc.com/threads/25718...-for-Arduino-for-Teensy-3-1?p=48458#post48458)
In a nut shell, my question is, is there a standard way for multiple SPI devices, using potentially multiple implementations to peacefully coexist?
Background: I have been playing around with one of the Adafruit 2.8" TFT shields that is mounted on my Teensy 3.1 breakout board. My first pass, I updated the Adafruit_ILI9341 driver to work on the Teensy using the SPI library. I got it to work, but it is pretty slow. I then worked on making the touch screen code to work (Adafruit_STMPE610), which again I used the SPI library. Code for these are up on github. I got this to work, but still was not happy with the display speed. so using Pauls updates for the Adafruit_ST7735 library, I merged these into my version of the Adafruit_ILI9341 library. I then tested it using the Adafruit graphics test and it was a lot better
So then I tried it with my test program that included touch and the display completely stopped working, but the touch still worked. I thought maybe I should also try to convert the STMPE610 driver to use the Fast SPI code, but the CS for this on the shield is on pin 8 which is not one of the pins on the Teensy that that the hardware SPI can drive...
First thought was maybe the IO pins were being changed between the two SPI implementations. So I printed out the core pin information for pins 9-13, before I init the display, after I init the display and after I init the touch screen (Serial.print(CORE_PIN9_CONFIG, HEX)... (9=Command/data, 10=CS...)
And yes the init of the Touch screen changed the settings... So I then had the idea, that I would save the CORE_PIN... settings for those IO pins after the display was init, likewise after the TS was init, and then have a function that would restore them, depending on which device I was going to talk to next... Still no luck... Then reading through both code bases, I find that both of them are using the hardware registers SPI0.CTAR0 SPI0.MCR (and in the case of the display CTAR1), and at least in the case of the display, this is done at the begin call and the rest of the code assumes this. Example:
The CTAS(0) uses SPI0.CTAR0... There is a writedata16, which write a word instead and uses CTAR1... Again those two registers were setup in the init.
Side question: in the above code I was wondering about switching the code, to have the while loop above the PUSHR? that is why wait after the push for it not to be full, instead of check before... That way your code continues to run and hopefully by the next call maybe one of the spi outputs completed... Probably not important.
So I hacked my Init code and restore function, to remember these values as well. Example:
I do the same after TS is init (except HSPI -> SSPI)
I then have a pretty stupid function:
I then added calls to this. In particular I bracketed all of my TS calls...
This appears to be working. I know I should be doing other stuff as well, like maybe I should change SPIO_MCR first to halt before updating CTAR0...
But the question is, is there (or should there be) a more generalized fix for this. For example suppose I did update the STMPE library to also work using the FAST SPI code? It too may want it's on CTAR0 or CTAR1 register settings...
Suggestions?
Also some of this information is from another thread about using the Adafruit 2.8" TFT display (http://forum.pjrc.com/threads/25718...-for-Arduino-for-Teensy-3-1?p=48458#post48458)
In a nut shell, my question is, is there a standard way for multiple SPI devices, using potentially multiple implementations to peacefully coexist?
Background: I have been playing around with one of the Adafruit 2.8" TFT shields that is mounted on my Teensy 3.1 breakout board. My first pass, I updated the Adafruit_ILI9341 driver to work on the Teensy using the SPI library. I got it to work, but it is pretty slow. I then worked on making the touch screen code to work (Adafruit_STMPE610), which again I used the SPI library. Code for these are up on github. I got this to work, but still was not happy with the display speed. so using Pauls updates for the Adafruit_ST7735 library, I merged these into my version of the Adafruit_ILI9341 library. I then tested it using the Adafruit graphics test and it was a lot better
First thought was maybe the IO pins were being changed between the two SPI implementations. So I printed out the core pin information for pins 9-13, before I init the display, after I init the display and after I init the touch screen (Serial.print(CORE_PIN9_CONFIG, HEX)... (9=Command/data, 10=CS...)
Code:
Before TFT:0 0 0 0 0
Check for Hardware SPI
Hardware SPI
After TFT:200 200 200 0 240
After TS:200 144 240 200 240
Code:
void Adafruit_ILI9341::writedata(uint8_t c)
{
if (hwSPI) {
SPI0.PUSHR = c | (pcs_data << 16) | SPI_PUSHR_CTAS(0);
while (((SPI0.SR) & (15 << 12)) > (3 << 12)) ; // wait if FIFO full
} else {
...
Side question: in the above code I was wondering about switching the code, to have the while loop above the PUSHR? that is why wait after the push for it not to be full, instead of check before... That way your code continues to run and hopefully by the next call maybe one of the spi outputs completed... Probably not important.
So I hacked my Init code and restore function, to remember these values as well. Example:
Code:
tft.begin();
Serial.print("After TFT:");
PrintPinsConfig();
// real hack remember pin configs
g_u32_CORE_PIN9_CONFIG_HSPI = CORE_PIN9_CONFIG;
g_u32_CORE_PIN10_CONFIG_HSPI = CORE_PIN10_CONFIG;
g_u32_CORE_PIN11_CONFIG_HSPI = CORE_PIN11_CONFIG;
g_u32_CORE_PIN12_CONFIG_HSPI = CORE_PIN12_CONFIG;
g_u32_CORE_PIN13_CONFIG_HSPI = CORE_PIN13_CONFIG;
g_u32_SPI0_CTAR0_HSPI = SPI0_CTAR0;
g_u32_SPI0_MCR_HSPI = SPI0_MCR;
I then have a pretty stupid function:
Code:
void OLEDSGC::SetSPIMode(boolean fHardwareSPI){
// Very large hack
if (fHardwareSPI) {
CORE_PIN9_CONFIG = g_u32_CORE_PIN9_CONFIG_HSPI;
CORE_PIN10_CONFIG = g_u32_CORE_PIN10_CONFIG_HSPI;
CORE_PIN11_CONFIG = g_u32_CORE_PIN11_CONFIG_HSPI;
CORE_PIN12_CONFIG = g_u32_CORE_PIN12_CONFIG_HSPI;
CORE_PIN13_CONFIG = g_u32_CORE_PIN13_CONFIG_HSPI;
SPI0_CTAR0 = g_u32_SPI0_CTAR0_HSPI;
SPI0_MCR = g_u32_SPI0_MCR_HSPI;
// SPI0.MCR = SPI_MCR_MSTR | SPI_MCR_PCSIS(0x1F) | SPI_MCR_CLR_TXF | SPI_MCR_CLR_RXF;
} else {
// Wait until hardware queue is empty...
uint16_t wTimeout = 0xffff;
while (((SPI0.SR) & (15 << 12)) && (--wTimeout)) ; // wait until empty
CORE_PIN9_CONFIG = g_u32_CORE_PIN9_CONFIG_SSPI;
CORE_PIN10_CONFIG = g_u32_CORE_PIN10_CONFIG_SSPI;
CORE_PIN11_CONFIG = g_u32_CORE_PIN11_CONFIG_SSPI;
CORE_PIN12_CONFIG = g_u32_CORE_PIN12_CONFIG_SSPI;
CORE_PIN13_CONFIG = g_u32_CORE_PIN13_CONFIG_SSPI;
SPI0_CTAR0 = g_u32_SPI0_CTAR0_HSPI;
SPI0_MCR = g_u32_SPI0_MCR_HSPI;
}
}
This appears to be working. I know I should be doing other stuff as well, like maybe I should change SPIO_MCR first to halt before updating CTAR0...
But the question is, is there (or should there be) a more generalized fix for this. For example suppose I did update the STMPE library to also work using the FAST SPI code? It too may want it's on CTAR0 or CTAR1 register settings...
Suggestions?


