SPI1 and Serial1 and pin 39 input together?

ctadlock

Active member
Setup
- Teensy 4.1
- TLC9547 LED driver which is a slave only SPI device; its connected via SPI1 with alternative pins (MOSI1:26, SCK1:27, CS1:38); MISO is not used or connected for this component as its not needed*
- Serial1 with pins (RX1:0, TX1:1)
- Digital input switch on pin 39

All three components work fine on their own. I had to use the alternate SPI1 pins as the normal SPI1 pins conflict with Serial 1 (0, 1).

Problem
The problem comes from an unexpected place. Since the TLC9547 is a slave I didnt wire up MISO for the purpose of SPI. I used pin 39 for a digital input switch. However pin 39 is the alternate MISO for SPI1. When I created a sketch that uses all three components it craps out, the Serial1 stops working. I found that since Im using the alternate pins for SPI1, I must also set the MISO alternate pin 39 so it stops interfering with Serial1 (pin 1). But Im using pin 39 as a digital input!!

Code:
SPI1.setMOSI(this->pinDat_);
SPI1.setMISO(39); // < issue
SPI1.setSCK(this->pinClk_);
SPI1.begin();

Questions
- Am I correct in my understanding of this?
- Is there any way to "turn off" MISO on SPI1 so it doesnt conflict with either Serial1 (pin 1) or my digital input (pin 39)?
- What if any side effects will happen if I just roll forward and set the alternate MISO pin 39 for SPI1 but continue to use it as a digital input? The code never reads from SPI1, but will it cause some internal issue?
 
Thanks for the link. I already have full control of the ordering of when the code is called so can setup my digital input pin after SPI1... but that doesn't really answer my question. Does that cause any issues?

If I set pin 39 input mode to input_pullup after it's been set as the miso for spi1 what state is that pin in? When that switch is triggered will it mess up spi1? Conversely will spi interfere with the switch?

I feel this is a question for Paul as it requires a very deep understanding of the chip registers and spi library. Maybe a way to disable miso if it's not used?

At minimum this should be in the docs as it's an easy mistake. I paid an expert to do a full design review of the board before manufacturing and we all missed it.
 
I personally have not done much with SPI in slave mode so maybe some who have can give more of a definitive answer.

Obviously. Paul would be a great choice to answer this. However, I am guessing he is probably still up to his eyeballs dealing with part shortages and the like.

Each of the actual IO pin (PAD) are mainly controlled by 2 registers:

One that controls which mode the pin is in: that is each pin can do several things. For example Pin39
Code:
39/A5	AD_B1_13 	FLEXSPIA_DATA00	ACMP_OUT01	LPSPI3_SDI	SAI1_TX_DATA00	CSI_DATA04	GPIO1_IO29	USDHC2_DATA5	KPP_COL01	ENET2_1588_EVENT2_IN	FLEXIO3_FLEXIO13

So far example calling: pinMode(39, INPUT_PULLUP);
Will set the register IOMUXC_SW_MUX_CTL_PAD_GPIO_AD_B1_13 to GPIO mode which is 5, and maybe the SION flag:
And it will also set the register: OMUXC_SW_PAD_CTL_PAD_GPIO_AD_B1_13 which controls the actual pad, with things like PULL UP, Drive strength...

And the last settings of these registers wins... (At least until something else sets them).

With the SPI mode, there is another setting associated with the PAD, and that is while that physical pin can be used for LPSPI3_SDI, there is another pin, that can also be used
for that signal, and so there is register that says which one should we use: IOMUXC_LPSPI3_SDI_SELECT_INPUT

And as the other thread mentioned, they were fine without SIN pin routed through to SPI at least in the master mode. And I have seen others do so in the past, but again
I don't typically do slave mode, so don't know if it could interfere or not. Again I suspect that it should work fine, but have been surprised before.
 
Thanks again. To be clear the Teensy is master and the TLC9547 is slave.

I think by the fact were down in the weeds looking at if register flags conflict or not is an indication this is a thing to avoid, hence why it should be more explicit in the docs.

For me, luckily I only put components on one board so time to get out the exacto knife and cut some traces and some wire to reconnect things. Ill set MISO1 to pin 39 and not connect it to anything.
 
At first glance at the title, I also thought this thread was about trying to use SPI1 in slave mode.

But this looks much simpler. Unless I've missed something (admittedly, I have limited time today and read all this quickly), all you probably need is something like this:

Code:
SPI1.begin();
Serial1.begin(9600);
pinMode(39, INPUT_PULLUP);

Or maybe this:

Code:
Serial1.begin(9600);
SPI1.setMISO(39);
SPI1.begin();
pinMode(39, INPUT_PULLUP);

The order is important.

In the first example, SPI1.begin() takes control of pins 1, 26, 27. Then Serial1.begin() takes control of pins 0, 1. SPI1 will no longer use pin 1. Then pinMode() takes control of pin 39.

In the second example, Serial1.begin() takes control of pins 0, 1. Then SPI1.begin() takes control of pins 39, 26, 27. Then the final pinMode() takes pin 39, leaving SPI1 without access to it.

The key point is to get the order correct. Every pin has a mux which assigns it to be controlled by a certain peripheral. GPIO isn't special, it's just another peripheral. If you initialize the hardware in an order where the later usage takes control of the pin in the way you wanted, it should work without needing to dive into low-level hardware registers.
 
Your 2nd code is what I have and I think it works. I just dont have the knowledge to look inside the SPI class to know if using pin 39 as INPUT_PULLUP would mess up SPI somehow. There is a lot of bitwise operations on the register flags going on in there.

I still think putting this in the SPI docs in the alternative pin usage section would be useful.
 
Every pin has a mux which assigns it to be controlled by a certain peripheral. GPIO isn't special, it's just another peripheral.

When you call pinMode(39, INPUT_PULLUP), control of the pin is given to GPIO and SPI1 can't access it any longer.
 
PS, I tried to change the post title to me more useful/searchable to other but I dont see that is possible.
 
Back
Top