void CAN1Init(void)
{
CAN_BIT_CONFIG canBitConfig;
CANEnableModule(CAN1,TRUE);
CANSetOperatingMode(CAN1, CAN_CONFIGURATION);
while(CANGetOperatingMode(CAN1) != CAN_CONFIGURATION);
canBitConfig.phaseSeg2Tq = CAN_BIT_3TQ;
canBitConfig.phaseSeg1Tq = CAN_BIT_3TQ;
canBitConfig.propagationSegTq = CAN_BIT_3TQ;
canBitConfig.phaseSeg2TimeSelect = TRUE;
canBitConfig.sample3Time = TRUE;
canBitConfig.syncJumpWidth = CAN_BIT_2TQ;
CANSetSpeed(CAN1,&canBitConfig,SYS_FREQ,500000);
CANAssignMemoryBuffer(CAN1,CAN1MessageFifoArea,(2 * 8 * 16));
CANConfigureChannelForTx(CAN1, CAN_CHANNEL0, 8, CAN_TX_RTR_DISABLED, CAN_LOW_MEDIUM_PRIORITY);
CANConfigureChannelForRx(CAN1, CAN_CHANNEL1, 8, CAN_RX_FULL_RECEIVE);
CANConfigureFilter (CAN1, CAN_FILTER0, 0x201, CAN_SID);
CANConfigureFilterMask (CAN1, CAN_FILTER_MASK0, 0xFFF, CAN_SID, CAN_FILTER_MASK_IDE_TYPE);
CANLinkFilterToChannel (CAN1, CAN_FILTER0, CAN_FILTER_MASK0, CAN_CHANNEL1);
CANEnableFilter (CAN1, CAN_FILTER0, TRUE);
CANEnableChannelEvent(CAN1, CAN_CHANNEL1, CAN_RX_CHANNEL_NOT_EMPTY, TRUE);
CANEnableModuleEvent (CAN1, CAN_RX_EVENT, TRUE);
INTSetVectorPriority(INT_CAN_1_VECTOR, INT_PRIORITY_LEVEL_4);
INTSetVectorSubPriority(INT_CAN_1_VECTOR, INT_SUB_PRIORITY_LEVEL_0);
INTEnable(INT_CAN1, INT_ENABLED);
CANSetOperatingMode(CAN1, CAN_NORMAL_OPERATION);
while(CANGetOperatingMode(CAN1) != CAN_NORMAL_OPERATION);
}
void CAN2Init(void)
{
CAN_BIT_CONFIG canBitConfig;
CANEnableModule(CAN2,TRUE);
CANSetOperatingMode(CAN2, CAN_CONFIGURATION);
while(CANGetOperatingMode(CAN2) != CAN_CONFIGURATION);
canBitConfig.phaseSeg2Tq = CAN_BIT_4TQ;
canBitConfig.phaseSeg1Tq = CAN_BIT_4TQ;
canBitConfig.propagationSegTq = CAN_BIT_4TQ;
canBitConfig.phaseSeg2TimeSelect = TRUE;
canBitConfig.sample3Time = TRUE;
canBitConfig.syncJumpWidth = CAN_BIT_3TQ;
CANSetSpeed(CAN2, &canBitConfig, SYS_FREQ, 500000);
CANAssignMemoryBuffer(CAN2,CAN2MessageFifoArea,2 * 8 * 16);
CANConfigureChannelForTx(CAN2,CAN_CHANNEL2,8,CAN_TX_RTR_ENABLED,CAN_LOW_MEDIUM_PRIORITY);
CANConfigureChannelForRx(CAN2, CAN_CHANNEL3, 8, CAN_RX_FULL_RECEIVE);
CANConfigureFilter (CAN2, CAN_FILTER0, 0x202, CAN_SID);
CANConfigureFilterMask (CAN2, CAN_FILTER_MASK0, 0xFFF, CAN_SID, CAN_FILTER_MASK_IDE_TYPE);
CANLinkFilterToChannel (CAN2, CAN_FILTER0, CAN_FILTER_MASK0, CAN_CHANNEL0);
CANEnableFilter (CAN2, CAN_FILTER0, TRUE);
CANEnableChannelEvent(CAN2, CAN_CHANNEL3, CAN_RX_CHANNEL_NOT_EMPTY, TRUE);
CANEnableModuleEvent (CAN2, CAN_RX_EVENT, TRUE);
INTSetVectorPriority(INT_CAN_2_VECTOR, INT_PRIORITY_LEVEL_4);
INTSetVectorSubPriority(INT_CAN_2_VECTOR, INT_SUB_PRIORITY_LEVEL_0);
INTEnable(INT_CAN2, INT_ENABLED);
CANSetOperatingMode(CAN2, CAN_NORMAL_OPERATION);
while(CANGetOperatingMode(CAN2) != CAN_NORMAL_OPERATION);
}
void CAN1TxSendRTRMsg(void)
{
CANTxMessageBuffer * message;
message = CANGetTxMessageBuffer(CAN1,CAN_CHANNEL0);
//message = CANTxMessageBuffer(CAN1,CAN_CHANNEL0);
m->messageWord[0] = 1;
m->messageWord[1] = 1;
m->messageWord[2] = 1;
m->messageWord[3] = 1;
m->msgSID.SID= 0x202; /* Send message to CAN2. */
m->msgEID.IDE = 0; /* IDE = 0 means Standard ID message. */
m->msgEID.RTR = 1; /* Set the RTR bit. */
m->msgEID.DLC = 1; /* No data payload for RTR messages. */
m->data[0]= 0x03; /* This is the payload. */
m->data[1]= 0x33;
m->data[2]= 0x33;
m->data[3]= 0x33;
m->data[4]= 0x33;
m->data[5]= 0x33;
m->data[6]= 0x33;
m->data[7]= 0x33;
CANUpdateChannel(CAN1,CAN_CHANNEL0);
CANFlushTxChannel(CAN1,CAN_CHANNEL0);
}
void CAN2RxMsgProcess(void)
{
CANRxMessageBuffer * message;
message = (CANRxMessageBuffer *)CANGetRxMessage(CAN2,CAN_CHANNEL3);
if(m->data[0] == '1')
{
//take action
}
else
{
//take action
}
CANUpdateChannel(CAN2, CAN_CHANNEL3);
CANEnableChannelEvent(CAN2, CAN_CHANNEL3, CAN_RX_CHANNEL_NOT_EMPTY, TRUE);
}