Multiple SPI devices, multiple implementations fighting over resources

Status
Not open for further replies.

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...)

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
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:
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 {
...
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:
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 do the same after TS is init (except HSPI -> SSPI)

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;
  }
  
}
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?
 
In a nut shell, my question is, is there a standard way for multiple SPI devices, using potentially multiple implementations to peacefully coexist?

Not yet. I am working on it. **

Cristian Maglie, Arduino's technical lead and ultimately the person who decides what contributions they will accept, recently provided some input and specific decisions, after a lengthy discussion and many initial starts on this work over the last couple months. Working with Arduino to get this accepted into the platform for all Arduino and Arduino compatible boards is a long, slow and difficult process, but in the long run everybody will benefit more if it's a standard part of Arduino's SPI library, rather than a Teensy-only extension.

Of course, I'll be developing this into a new version of Teensyduino just as soon as it's looking stable (unlikely to incompatibly change) and working.

The Adafruit 2.8 inch TFT shield is one of the pieces of hardware I have on my desk now for testing, since it's known to have compatibility troubles with other libraries.


** - Actually, at this moment I'm investigating several previously reported problems and fixing bugs, in preparation for Teensyduino 1.19, which is intended to be a no-new-feature, bug-fix-only release. But this SPI stuff is near the top of my priority list. It's a terrible problem that really must be fixed soon, not just for Teensy but for all Arduino compatible boards.
 
I am not sure if this will help, but here are a few screen shots showing the stuff using my logic analyzer (Saleae)...

First here shows a simple start up of the program:
LA-2.8TFT-1.jpg

Here is a close up showing how the fast hardware SPI code is controlling both the chip select as well as the Data/Command IO lines, which update for each byte (or word) that is output:
LA-2.8TFT-2.jpg

Finally here shows a case where I am moving my finger up and down on my ugly slider control, which is filling the rectangles inside of the slider as I move. You can see that both sets of SPI outputs are going and at different speeds...
LA-2.8TFT-3.jpg

Again not sure if it helps to show these up here after the fact, but it sure helped me localize what was going on.

Thanks
Kurt
 
Not sure how many others are wanting to play some with the multiple SPI issues I mentioned. But I know of at least one other up on Lynxmotion(Robotshop) forum who is also playing along with trying to get the display working at high speed and also have the touch screen to work. Yesterday he posted up there a simple start of a program that displays some rectangles and detects if you press within the rectangles and then draw a black outline around the rectangle that was touched. (Adafruit 2.8" TFT with touch shield). The current versions of my Adafruit_IlI9341 and Adafruit_STMPE libraries are up on my github projects (github/kurte)

When I tried it on my setup, the program init, but display would not draw, so I hacked it up to save and restore a few things that changed (SPI Register(s) and Pin Mode(s)), which got it to work again. So far I am only needing to save a restore two things:

Found I needed to restore pin 10 and the SPI0_CTAR0 as the SPI library mucks with these.

Code:
//---------------------------------------------------------
// Some hacks for switching SPI registers
uint8_t   g_icurSPIR = 0xff;  // Set to something that won't be
uint32_t  g_aluSPI0_CTAR0[SPIR_CNT];
uint32_t  g_alsuSPI0_PIN10_CONFIG[SPIR_CNT];  // pin 10 CS gets mucked with...

void SaveSPIRegs(uint8_t spir) {
  g_aluSPI0_CTAR0[spir] = SPI0_CTAR0;
  g_alsuSPI0_PIN10_CONFIG[spir] = CORE_PIN10_CONFIG;
}

void RestoreSPIRegs(uint8_t spir) {
  if (g_icurSPIR != spir) {  
    // Need to wait until all outputs are done... 
    // Wait until hardware queue is empty...
    uint16_t wTimeout = 0xffff;
    while (((SPI0.SR) & (15 << 12)) && (--wTimeout)) ; // wait until empty

    // May want to wait until actual output is done? also may need to turn off SPI before change?
    // But for now simply try restoring the register(s)
    SPI0_CTAR0 = g_aluSPI0_CTAR0[spir];
    CORE_PIN10_CONFIG = g_alsuSPI0_PIN10_CONFIG[spir];
    g_icurSPIR = spir;
  }
}
//-----------------------------------------------------------

void PrintPinsConfig(void) {
  Serial.print(CORE_PIN8_CONFIG, HEX);
  Serial.print(" ");
  Serial.print(CORE_PIN9_CONFIG, HEX);
  Serial.print(" ");
  Serial.print(CORE_PIN10_CONFIG, HEX);
  Serial.print(" ");
  Serial.print(CORE_PIN11_CONFIG, HEX);
  Serial.print(" ");
  Serial.print(CORE_PIN12_CONFIG, HEX);
  Serial.print(" ");
  Serial.print(CORE_PIN13_CONFIG, HEX);
  Serial.println(" ");
  //  Serial.print(SPI0.MCR, HEX);
  //  Serial.print(" ");
  //  uint32_t ulSR = SPI0.SR;
  //  Serial.println(SPI0.SR, HEX);
}
I included the updated program in case anyone wishes to try it out.

I still have my own questions about the best way to use the hardware SPI on the Teensy 3.1, things like:
a) WIth the above code I update the one register without stopping the stopped bit in the SPI MCR register. It appears to work, but documents make it sound like I should halt it first?

b) How to know when I should update the registers. The above code simply waits until the queue is empty, but not sure if that implies the last queue entry has been completely shifted in/out. In some other code I am experimenting with, that allows me to read the registers. I added some additional code that looks like:
Code:
    while (((SPI0.SR) & (15 << 12)) && (--wTimeout)) ; // wait until empty
    
    // Make sure the last frame has been sent...
    SPI0.SR = SPI_SR_TCF;   // clear it out;
    wTimeout = 0xffff;
    while (!((SPI0.SR) & SPI_SR_TCF) && (--wTimeout)) ; // wait until it says the last frame completed
Again not sure if that is the right/best way to do this?

c) How not to overflow the output queue. Currently the code has
Code:
		SPI0.PUSHR = c | (pcs_command << 16) | SPI_PUSHR_CTAS(0);
		while (((SPI0.SR) & (15 << 12)) > (3 << 12)) ; // wait if FIFO full
But I wonder if instead of finding out we put in the 4th element and wait for the system to shift out one entry, we should instead check first that the queue is not full and then do the output, that way user code would have time to do stuff and maybe in most cases never have to wait. More like:
Code:
		while (((SPI0.SR) & (15 << 12)) > (3 << 12)) ; // wait if FIFO full
		SPI0.PUSHR = c | (pcs_command << 16) | SPI_PUSHR_CTAS(0);
Or can we not use the TFFF flag,
Code:
		while (!((SPI0.SR) & SPI_SR_TFFF)) ; // wait if FIFO full
		SPI0.PUSHR = c | (pcs_command << 16) | SPI_PUSHR_CTAS(0);
Maybe the best thing to do is simply try them out...

d) How to reasonably do read a register from the display (and potentially) read display memory. Currently the code I have
Code:
uint8_t Adafruit_ILI9341::readcommand8(uint8_t c, uint8_t index) {
  if (hwSPI) {
    uint16_t wTimeout = 0xffff;
    uint8_t r;
    while (((SPI0.SR) & (15 << 12)) && (--wTimeout)) ; // wait until empty
    
    // Make sure the last frame has been sent...
    SPI0.SR = SPI_SR_TCF;   // dlear it out;
    wTimeout = 0xffff;
    while (!((SPI0.SR) & SPI_SR_TCF) && (--wTimeout)) ; // wait until it says the last frame completed

    // clear out any current received bytes
    wTimeout = 0x10;    // should not go more than 4...
    while ((((SPI0.SR) >> 4) & 0xf) && (--wTimeout))  {
        r = SPI0.POPR;
    }
    
    //writecommand(0xD9); // sekret command
	SPI0.PUSHR = 0xD9 | (pcs_command << 16) | SPI_PUSHR_CTAS(0) | SPI_PUSHR_CONT;

    // writedata(0x10 + index);
	SPI0.PUSHR = (0x10 + index) | (pcs_data << 16) | SPI_PUSHR_CTAS(0);

    // writecommand(c);
   	SPI0.PUSHR = c | (pcs_command << 16) | SPI_PUSHR_CTAS(0) | SPI_PUSHR_CONT;

    // readdata
	SPI0.PUSHR = 0 | (pcs_data << 16) | SPI_PUSHR_CTAS(0);
        
    // Now wait until completed. 
    wTimeout = 0xffff;
    while (((SPI0.SR) & (15 << 12)) && (--wTimeout)) ; // wait until empty

    // Make sure the last frame has been sent...
    SPI0.SR = SPI_SR_TCF;   // dlear it out;
    wTimeout = 0xffff;
    while (!((SPI0.SR) & SPI_SR_TCF) && (--wTimeout)) ; // wait until it says the last frame completed

    wTimeout = 0x10;    // should not go more than 4...
    // lets get all of the values on the FIFO
    while ((((SPI0.SR) >> 4) & 0xf) && (--wTimeout))  {
        r = SPI0.POPR;
    }
    return r;  // get the received byte... should check for it first...

  }
...
Again this appears to work some, but feels like a hack, as I am trying to figure out when the commands are completed, to know which byte of data on the Recv queue will be my answer... Probably a better way!

Again this is simply me, having fun, trying to figure stuff out...
 

Attachments

  • Philtkp_Screen_Test-140610a.zip
    1.8 KB · Views: 157
Status
Not open for further replies.
Back
Top