lpcware

CAN Problems

Discussion created by lpcware Employee on Jun 15, 2016
Latest reply on Jun 15, 2016 by lpcware
Content originally posted in LPCWare by klaschudi on Wed Mar 06 01:15:06 MST 2013
Hello,
I'm trying to send something over the CAN1-Bus. With the following code:


void can_init(unsigned char bitrate)
{
LPC_SC->PCONP |= (1 << PCCAN1); // enable can1 clock
LPC_SC->PCLKSEL0 |= (0x10 << PCLK_CAN1);// canclk = 60MHz

LPC_PINCON->PINSEL0 &= ~(3 << 0)&(3 << 2); // select can pin function
LPC_PINCON->PINSEL0 |= (1 << 0)|(1 << 2);

LPC_CAN1->MOD = (1 << 0);    // enter reset mode
LPC_CAN1->IER = 0;  // disable receive interrupts
LPC_CAN1->GSR = 0;    // Reset error counter when CANxMOD is in reset

LPC_CAN1->BTR = bitrate;
LPC_CAN1->MOD = 0x0;  // leave reset mode, enter normal operation mode

LPC_CANAF->AFMR |= (1 << 1); // all rx messages are accepted  (acceptance filter)

NVIC_EnableIRQ(CAN_IRQn);

LPC_CAN1->IER = 0x01; // Enable receive interrupts
}



uint8_t CAN1_SendMessage( CAN_MSG *pTxBuf )
{
  uint32_t CANStatus;

  CANStatus = LPC_CAN1->SR;
  if ( CANStatus & (1 << 2) )
  {
LPC_CAN1->TFI1 = pTxBuf->Frame;
LPC_CAN1->TID1 = pTxBuf->MsgID;
LPC_CAN1->TDA1 = pTxBuf->DataA;
LPC_CAN1->TDB1 = pTxBuf->DataB;
LPC_CAN1->CMR = (1 << 0)|(1 << 5);
return ( TRUE );
  }

  else if ( CANStatus & (1 << 10) )
  {
LPC_CAN1->TFI2 = pTxBuf->Frame;
LPC_CAN1->TID2 = pTxBuf->MsgID;
LPC_CAN1->TDA2 = pTxBuf->DataA;
LPC_CAN1->TDB2 = pTxBuf->DataB;
LPC_CAN1->CMR = (1 << 0)|(1 << 6);
return ( TRUE );
  }
  else if ( CANStatus & (1 << 18) )
  {
LPC_CAN1->TFI3 = pTxBuf->Frame;
LPC_CAN1->TID3 = pTxBuf->MsgID;
LPC_CAN1->TDA3 = pTxBuf->DataA;
LPC_CAN1->TDB3 = pTxBuf->DataB;
LPC_CAN1->CMR = (1 << 0)|(1 << 7);
return ( TRUE );
  }

  return ( FALSE );
}


But after setting the CMR-Register for the first time the Controller starts to periodically transmit CAN-Messages with a weird ID (0C889249...). And after sendig once with each Transmit Buffer they are all marked as busy.
I already tried the Local Self test, which seemed to be OK. So I don't think that this is a Hardware Problem.

Did I forget anything in the Initialisation?

Thanks for your help!

Outcomes