S12XEP100 MSCAN loopback not working?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

S12XEP100 MSCAN loopback not working?

Jump to solution
1,702 Views
mroczeks
Contributor IV

Hi All,

 

So far I have never used loopback mode. Never needed it.

Until today.

I prepare and schedule CAN message for transmission.

Then I wait for CAN0RFLG register's RXF flag with no success. :smileysad:

 

Couldn't find the solution for half a day. Read documentation several times, tried all options which came to my mind, search that forum... No success.

Checked DEMO9S12XEP100 documentation... The CAN jumpers are OK.

I was able to receive CAN message from remote node (usb PCAN interface and PCAN View application).

I was not able to send any message (that's probably for another topic) so switched to loopback mode...

 

Please, have a look at the main routine below. Thanks!

 

 

void main(void) {

  byte canbuffs=0;
  word received_id=0;
 
  DisableInterrupts;

  /* CAN tests: */
 
  /* request CAN initialisation mode */
  _CAN0CTL0.Bits.INITRQ = 1;
 
  /* wait for initialisation acknowledge */
  while(_CAN0CTL1.Bits.INITAK == 0) {
  }
 
  /* enable CAN module */
  _CAN0CTL1.Bits.CANE = 1;
 
  /* CAN clock source is OSC clock: 4MHz */
  _CAN0CTL1.Bits.CLKsrc=0;
 
  /* enable loopback mode */
  _CAN0CTL1.Bits.LOOPB = 1;
 
  /* disable acceptance filters by setting acceptance masks */
  _CAN0IDMR0.Byte = 0xFF;
  _CAN0IDMR1.Byte = 0xFF;
  _CAN0IDMR2.Byte = 0xFF;
  _CAN0IDMR3.Byte = 0xFF;
  _CAN0IDMR4.Byte = 0xFF;
  _CAN0IDMR5.Byte = 0xFF;
  _CAN0IDMR6.Byte = 0xFF;
  _CAN0IDMR7.Byte = 0xFF;
 
  /* configure baud rate: 125kbps for 4MHz OSC clock source */
  _CAN0BTR0.MergedBits.grpBRP = 1;      /* prescaler value 2 */
  _CAN0BTR0.MergedBits.grpSJW = 0;      /* synchronisation jump width 1 Tq */
  _CAN0BTR1.MergedBits.grpTSEG_10 = 7;  /* time segment1 8 Tq */
  _CAN0BTR1.MergedBits.grpTSEG_20 = 6;  /* time segment2 7 Tq */
  _CAN0BTR1.Bits.SAMP = 0;              /* one sample per bit */
 
  /* leave initialisation mode */
  _CAN0CTL0.Bits.INITRQ = 0;
 
  /* wait to enter normal mode */
  while(_CAN0CTL1.Bits.INITAK == 1) {
  }
 
  /* disable receive / transmit interrupts */
  _CAN0RIER.Byte = 0x00;
  _CAN0TIER.Byte = 0x00;
 
  /* wait for CAN module synchronisation */
  while(_CAN0CTL0.Bits.SYNCH == 0) {
  }
 
  /* set module to wake up from sleep if there is CAN activity */
  _CAN0CTL0.Bits.WUPE = 1;                
 
 
 
  /* clear receive buffer full flag to release the buffer */
  /* just in case although no messages has been received yet  */
  /* does not make any difference if the line is removed */
  _CAN0RFLG.Bits.RXF = 0;
 
  /* CAN transmit tests: */
  /* loop-back mode */
 
  /* select empty transmit buffer */
   _CAN0TBSEL.Byte = _CAN0TFLG.Byte;
 
  /* fill selected transmit buffer */         
  /* with some example data */
  /* CAN Node Id: 0x7F0 */
  /* Data: 0x11 0x12 0x13 ... 0x18 */
  /* Length: 8 bytes */
  _CAN0TXIDR0.Byte = (byte)(0x7F0>>3);
  _CAN0TXIDR1.Byte = (byte)(0x7F0<<5);
 
  _CAN0TXDSR0.Byte = 0x11;
  _CAN0TXDSR1.Byte = 0x12;
  _CAN0TXDSR2.Byte = 0x13;
  _CAN0TXDSR3.Byte = 0x14;
  _CAN0TXDSR4.Byte = 0x15;
  _CAN0TXDSR5.Byte = 0x16;
  _CAN0TXDSR6.Byte = 0x17;
  _CAN0TXDSR7.Byte = 0x18;
 
  _CAN0TXDLR.Byte = 8;
 
  /* schedule transmit buffer for transmission */
  _CAN0TFLG.Byte = ~_CAN0TBSEL.Byte; // ??
 
 
  /* wait for new message in receive buffer */
  /* stops on this line, does not go any further */
  while (_CAN0RFLG.Bits.RXF == 0) {
  }
 
  /* ID received */
  received_id = (_CAN0RXIDR0.Byte);
  received_id <<= 3;
  received_id |= (_CAN0RXIDR1.Byte >> 5);

  /* clear receive buffer full flag */
  _CAN0RFLG.Bits.RXF = 0;
 
 
  /* CAN tests completed */


Any ideas what's wrong with it?

Labels (1)
0 Kudos
1 Solution
700 Views
mroczeks
Contributor IV

Hey guys!

 

Sorry for such a long silence.

I have been very busy doing my CAN flash bootloader for s12xep100...

 

I fixed it, initially I did not notice default value for CAN Listen Mode which is 1 (enabled) on reset.

All which is enough to make my CAN working is that single line:

 

_CAN0CTL1.Bits.LISTEN = 0;

 

Stupid... :smileywink:

View solution in original post

0 Kudos
9 Replies
700 Views
Lundin
Senior Contributor IV

/  /* schedule transmit buffer for transmission */
  _CAN0TFLG.Byte = ~_CAN0TBSEL.Byte; // ??

 

You must write 1 to clear, not 0. Remove the ~ and the code should work.

0 Kudos
700 Views
mroczeks
Contributor IV

Changed to _CAN0TFLG.Byte = _CAN0TBSEL.Byte;

 

Still nothing :/

Have no idea where the problem comes from :smileyindifferent:

0 Kudos
700 Views
Lundin
Senior Contributor IV

Shouldn't you set the mask filter to 0xFF rather than the acceptance one? Otherwise I believe you will filter out all messages.

In my experience, those two filters cause much more problems than they solve...

0 Kudos
700 Views
mroczeks
Contributor IV

Mask registers are used to disable/enable respective bits of acceptance filters. If mask is set to 0xFF (ones at every bit) every bit of filter matching is ignored and therefore every CAN message should be received.

 

This is true according to CAN specification, Freescale masks and filters description and my own experience with Microchip devices.

 

Regards

0 Kudos
700 Views
Lundin
Senior Contributor IV

Nevermind, I mixed up the register names.

0 Kudos
700 Views
mroczeks
Contributor IV

Anyone any idea guys?

0 Kudos
701 Views
mroczeks
Contributor IV

Hey guys!

 

Sorry for such a long silence.

I have been very busy doing my CAN flash bootloader for s12xep100...

 

I fixed it, initially I did not notice default value for CAN Listen Mode which is 1 (enabled) on reset.

All which is enough to make my CAN working is that single line:

 

_CAN0CTL1.Bits.LISTEN = 0;

 

Stupid... :smileywink:

0 Kudos
700 Views
Lundin
Senior Contributor IV

Aha, hehe... very sneaky bug. :smileyhappy:

 

Yet another reason why bit fields should never be used! Had you used bit-wise operators instead, this bug would never have appeared.

 

0 Kudos
700 Views
mroczeks
Contributor IV

True, true :smileywink:

0 Kudos