AnsweredAssumed Answered

Strange behavior with SPI on S12NE, Codewarrior 5.9

Question asked by Patrick Thomas on Aug 10, 2012
Latest reply on Aug 12, 2012 by Patrick Thomas

This should be a VERY simple task but for some reason when I send data to SPI, unexpected data shows up on the bus in certain situations.

 

Init routine:

void spiInit(void)  {//TODO: Currently set up as slave, will have to//      change over to master once we're ready  SPICR1=0b01011110;  // No interrupts (yet), enable SPI,                      //   master mode, clock idles high                      //   sample on even (rising) edges                      //   slave select active,                      //   MSB first  SPICR2=0x10; // unidirectional, run in wait mode, use SS  SPIBR=0x24; //260khz for now}

 

SPI writing routine:

void spiWrite(unsigned char x)  {  char junk;    while(!(SPISR_SPTEF));  SPIDR=x;  while(!(SPISR_SPIF));  junk=SPIDR;   // discard the junk byte to clear SPIF  return;}

 

 ...and finally a wrapper to call the above three times with one convenient function:

void spiWriteReg(unsigned char chan, unsigned char addr, unsigned char data)  {  char tempchan;    tempchan=chan&0b00011111;  spiWrite(0b00100000+tempchan);  // byte 1 is binary 001 + last 5 bits of "chan"  spiWrite(addr);  spiWrite(data);  return;}

When I call spiWriteReg(0,5,0) for example, I expect the output to SPI to be hex 20 05 00.

 

Instead, I sometimes get 20 05 1F... or 3F 05 00... I've even seen a 7F xx xx from another function that sends binary 010+chan.  In other words, it seems like the bus goes high and stays there for the remainder of the byte.  This behavior "seems" to change based on stepping through vs going at full speed, but I can't nail down a specific pattern.  It also "seems" to be more likely to happen if the prior byte was odd (ended in a 1).

 

Initially I thought maybe I was running into problems with interference from my SCI ISR, but the problem persisted even with the SCI calls commented out and interrupts disabled.

 

If anyone has any insight PLEASE let me know, I've been banging my head against this on and off for a couple weeks... it seems like the kind of thing where I'm overlooking something really obvious.

 

Thanks!!!

Outcomes