Teensy 3 SPI Basic Clock Questions

Status
Not open for further replies.
Donziboy, where did you get these numbers from? I've been looking at the datasheet, but I still don't understand the correlation between
0x000f000 and 0x0004000.

Can you explain how you determined 0x0000f000
Code:
#define SPI_SR_TXCTR 0x0000f000


and how you got 0x00004000
Code:
#define SPI_WRITE_8(c) \
	do { \
		while ((SPI0_SR & SPI_SR_TXCTR) >= 0x00004000); \
		SPI0_PUSHR = ((c)&0xff) | SPI0_PUSHR_CTAS(0) | SPI0_PUSHR_CONT; \
	} while(0)

I did not come up with them read the rest of thread. It is Plovedays code not mine.
 
My mistake, but really I'm directing these questions to anyone who knows the answer. I see that the 0x0000f000 is used to as a mask to isolate the TXCTR bits, I just don't understand why you only PUSHR when the TXCTR is >= 4. Does it have to do with the minimum frame size, or am I missing some basic concept?
 
I spent all day trying to start a SPI slave sketch, and it's making me crazy. I can't even get any data to shift into the RX FIFO registers. At least I think it isn't shifting in, I can't increment the RXCTR (in the SPI0_SR register).

I've read through the SPI section of the datasheet many times now, and I'm just stuck.
I'm using Plovedays code for my master (modified, sending 2 bytes of 8 bits). It was working for another application.

I'm using SPI0_MCR=0x00000000 and SPI0_CTAR0_SLAVE=0x38000000. This should put me in slave mode and a FMSZ size of 7.
From what I get from the datasheet, (I thought) the data should shift in automatically if it is in SLAVE mode & getting the clock signal.
But it isn't doing that.
Using SS pin 10 active low.

Is there something I need in my sketch to get this going? I've tried pullup resistors, I've even tried clocking it by hand.
I'm sorry to keep asking stupid questions, I probably sound like a complete newb, but I'm ok with that.

Also, whats up with the "grayed out" SPI pins on the pinout card that comes with the Teensy3? I even tried those.

Here's what I'm working with...
Code:
//Globals/members:
uint32_t ctar0, ctar1, ctar_slave, mcr;
bool serialPrint_ONS=LOW;

void setup(){
  Serial.begin(57600);
  
  pinMode(SS, INPUT);
  pinMode(SCK, INPUT);
  pinMode(MISO, INPUT);
  pinMode(MOSI, INPUT);

  SPCR =0x40; // |= _BV(SPE);  //From arduino SPI...I still get error is this is missing?
  mcr = SPI0_MCR;
  SPI0_MCR=0x00000000;
  
  SPI0_CTAR0_SLAVE = 0x38000000;
}

void loop(){
  
  if(!serialPrint_ONS){ 
    Serial.print("SPIO_MCR:             ");
    Serial.println(SPI0_MCR,BIN);
    Serial.print("SPIO_CTAR0_SLAVE:     ");
    Serial.println(SPI0_CTAR0_SLAVE,BIN);
    Serial.print("SPI_SR:               ");
    Serial.println(SPI0_SR,BIN);
    Serial.print("SPI_RXFR              ");
    Serial.println(SPI0_RXFR0,BIN);
    Serial.println();
    delay (1000);
  }
}
 
Last edited:
Sadly I have no clue how to work with the SPI Slave. I am kind of in limbo with my project atm, from everything I have seen I probably wont be using a Teensy for much longer. I am also taking a hiatus from my project.
 
I spent a bunch of time messing with the SPI unit before finally getting it to work. I haven't tried to use it as a slave. Three things that helped me:

Make sure you enable the clock to the SPI unit:

Code:
// Enable Clock to SPI.   from page 241 of Kinetis K20 Reference Manual.
SIM_SCGC6 |= SIM_SCGC6_SPI0;
// wait for clocks to come up.
delay(1000);

Make sure that you re-enable the pins after configuring the SPI:

Code:
// do this after you've configured the SPI unit:
CORE_PIN11_CONFIG = PORT_PCR_DSE | PORT_PCR_MUX(2);
CORE_PIN12_CONFIG = PORT_PCR_MUX(2);
CORE_PIN13_CONFIG = PORT_PCR_DSE | PORT_PCR_MUX(2);

I found that doing this after configuring the unit was essential.

I also shifted the clock signal to pin 14 so I could use the LED to debug:

Code:
// It is VERY IMPORTANT that the pins be reassigned AFTER ALL
// SPI initialization INCLUDING datamode or bitorder etc.

// First reassign pin 13 to the default so that it is not SCK
CORE_PIN13_CONFIG = PORT_PCR_MUX(1);
pinMode(13, OUTPUT);
// and then reassign pin 14 to SCK
CORE_PIN14_CONFIG = PORT_PCR_DSE | PORT_PCR_MUX(2);

The code I wrote is for SPI master, but may be helpful.

If you have further issues, you may want to consider starting a new thread, as this one has wandered off somewhat. :)

-c
 
Last edited:
Unfortunately, no luck with those edits. I did try using the pull-down resistors on the clock too. And dropping the DSE since it isn't an output pin

For future reference, I'm moving to a more specific thread I found a little while ago:
Teensy 3.0 as SPI Slave
 
Last edited:
I'm receiving SPI data on my slave. I have a lot of cleanup to do with the logic, but I'm getting data.
Thanks CMASON & PLOVEDAY...I'm using big chunks of code that you two did before me.
I'll try post something more significant in the next couple of days.
 
Status
Not open for further replies.
Back
Top