Sanity check; Using SPI pins for something else.

Status
Not open for further replies.

Cosford

Well-known member
Hi guys,

Working with a TLC5917 LED Driver, which in the past I'd had working fine by using 5 dedicated pins from the T3.1 (SDI, SDO, CLK, LE and OE). However, I'm using literally every pin on the T3.1, with the exception of pin 33 and I still need to find 2 more pins somewhere. I'd like to avoid a port expander if possible.
So, now I'm turning to the idea of putting the 5917 on the SPI bus I'm already using to communicate to a TFT display amongst other things.
Of course, actually getting the 5917 to drive LEDs won't be an issue using SPI (as it's essentially a shift register), however, getting into special mode to read error status' and set current adjustment, might not be so simple (see pages 17-19 here: http://www.ti.com/lit/ds/symlink/tlc5917.pdf).
In short, I need to be able to be able to control the clock line, outside of the SPI functions so as to toggle OE and LE as required to get it into special mode. Am I free to just manipulate it with digitalWriteFast provided, of course, it's not already conducting an SPI transaction at the time?

Thanks in advance,
Cosford.
 
Am I free to just manipulate it with digitalWriteFast provided, of course, it's not already conducting an SPI transaction at the time?

Not exactly.

First, you should begin a SPI transaction and keep it while you're meddling with the pins. Only end the transaction when you've finished and returned everything to normal.

The pins on Teensy 3.1 (and LC) are controlled by config registers, which control a 8-way mux. The mux selects which thing controls the pin. If you use digitalWriteFast, nothing will happen, because the mux is set to allow SPI to control the pin. You'll need to write to the config register, to put the pin under GPIO control. Then you can use digitalWriteFast. After you're done, you'll need to put the config register back the way it was, so SPI has control of the pin again.
 
Great, thanks for that.

Okay, I'm trying to work it back to the set-up of the control registers which map the SPI functions to pins. I followed the SPI function calls back to the config register for the SCK pin which is PORTC_PCR5, which maps to the register at 0x4004B014 (referenced as 4004_B014 in the manual), then I got a bit lost so went directly to the manual.
From page 228 in the manual, I should just need to set the MUX bits (8-10) to 001, which is 'Alternative 1', which should be the GPIO setting for the MUX? Then set that back to alternative 2 to set it back to SPI0_SCK? Does that sound right?

Based on that, does something like this look like it would work fine?

Code:
SPI.begin();
SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE0));  //chosen arbitrarily?
PORTC_PCR5 = (PORTC_PCR5 & 0xFFFFF8FF) | 256; //Set MUX to alternative 1 (GPIO)

digitalWriteFast(13, HIGH);
//etc.

PORTC_PCR5 = (PORTC_PCR5 & 0xFFFFF8FF) | 512; //Set MUX to alternative 2 (SPI0_SCK)
SPI.endTransaction();
 
Sorry to bump this up; does the code I've written above look about right? Just want to make sure I've understood you correctly.
 
Yeah, that looks about right.

If it's wrong, you'll know, because the digitalWrite stuff will have no effect on the pin if it's still in SPI mode, or because the SPI will stop working if you don't get the pin back to SPI mode.
 
Greats Thanks for that. Finishing up board designs this week, PCBs should be in hand in a few weeks and will report back. =)
 
Status
Not open for further replies.
Back
Top