Hi Everybody,
I have DEMO9S12xDT512, I configure CAN0 and everything is OK
I took the same setting and try to configure CAN1 but its does not work.
CAN1 RX/Tx is in PM2/PM3 which can also be CAN0 or SPI.
I don't see in the datasheet if we should configure something to incidate that its CAN1 which should be used?
I saw
The CAN1 function (TXCAN1 and RXCAN1) takes precedence over the routed CAN0, the routed SPI0 and the
general purpose I/O function if the CAN1 module is enabled.
Can someone help me please?
Thank you in advance
My code:
My CAN1 Init Function:
void CAN1Init (void)
{
CAN1CTL0_SLPRQ = 1u; /* sleep mode*/
CAN1CTL0 = 0x01; /* Enter Initialization Mode */
while (! (CAN1CTL1 & 0x01)) {}; /* Wait for Initialization Mode
acknowledge (INITRQ bit = 1) */
CAN1CTL1 = 0x80u;
CAN1BTR0 = 0x9u; /* Prescaller = 10dec*/
CAN1BTR1 = 0x5Cu; // Tseg 1 = 13dec / Tseg2 = 6dec
/* Acceptance Filters */
CAN1IDAC = 0x00u;
/* Set four 16-bit Filters */
CAN1IDAR0 = ACC_CODE_ID100_HIGH; //|\ 16-bit Filter 0
CAN1IDMR0 = 0xFFu;//;MASK_CODE_ST_ID_HIGH; //| \__ Accepts Standard Data Frame Msg
CAN1IDAR1 = ACC_CODE_ID100_LOW; //| / with ID 0x100
CAN1IDMR1 = 0xFFu;//MASK_CODE_ST_ID_LOW; //|/
/* Acceptance Filters */
CAN1IDAC = 0x10; /* Set four 16-bit Filters */
CAN1IDAR2 = 0x00; //|\ 16-bit Filter 1
CAN1IDMR2 = 0xFFu;//MASK_CODE_ST_ID_HIGH; //| \__ Accepts Standard Data Frame Msg
CAN1IDAR3 = 0x00; //| / with ID 0x100
CAN1IDMR3 = 0xFFu;//MASK_CODE_ST_ID_LOW; //|/
CAN1IDAR4 = 0x00; //|\ 16-bit Filter 2
CAN1IDMR4 = 0xFFu;//MASK_CODE_ST_ID_HIGH; //| \__ Accepts Standard Data Frame Msg
CAN1IDAR5 = 0x00; //| / with ID 0x100
CAN1IDMR5 = 0xFFu;//MASK_CODE_ST_ID_LOW; //|/
CAN1IDAR6 = 0x00; //|\ 16-bit Filter 3
CAN1IDMR6 = 0xFFu;//MASK_CODE_ST_ID_HIGH; //| \__ Accepts Standard Data Frame Msg
CAN1IDAR7 = 0x00; //| / with ID 0x100
CAN1IDMR7 = 0xFFu;//MASK_CODE_ST_ID_LOW; //|/
CAN1CTL0 = 0x00; /* Exit Initialization Mode Request */
while ((CAN1CTL1&0x00) != 0) {;} /* Wait for Normal Mode */
CAN1RFLG = 0xC3u;
CAN1RIER = 0x01u;
}
My CAN1 SENT Function:
unsigned char CAN1SendFrame (unsigned char id,unsigned char priority, unsigned char length, unsigned char *txdata )
{
unsigned char txbuffer;
unsigned char index;
/*if (!CAN0TFLG) /* Is Transmit Buffer full?? */
/*return ERR_BUFFER_FULL;*/
CAN1TBSEL = CAN1TFLG; /* Select lowest empty buffer */
txbuffer = CAN1TBSEL; /*CAN0TBSEL; /* Backup selected buffer */
/* Load Id to IDR Register */
*((unsigned long *) ((unsigned long)(&CAN1TXIDR0))) = id;
for (index=0;index<length;index++)
{
/* Load data to Tx buffer * Data Segment Registers */
*(&CAN1TXDSR0 + index) = txdata[index];
}
CAN1TXDLR = length; /* Set Data Length Code */
CAN1TXTBPR = priority; /* Set Priority */
CAN1TFLG = txbuffer; /* Start transmission */
/* Wait for Transmission completion */
while ( (CAN1TFLG & txbuffer) != txbuffer);
}
FOR IT:
void CAN1_Receive_IT_Configuration_XGATE(void)
{
/* ALLOCATE EVENT TO XGATE */
/* Set interrupt control page to CAN1 Receive */
/* Send interrupt to XGate (priotiry level 1) */
/*CAN1 receive Vector Address 0xAA */
INT_CFADDR = 0xA0;
INT_CFDATA5 = 0x81;
}
In Xgate.cxgate
{CAN1RxXSR, (int)&MyData}, // Channel 55 - CAN1 receive
interrupt void CAN1RxXSR (MyDataType* __restrict pData)
{
unsigned char length1;
unsigned char index1;
unsigned char rxdata1[8];
pData->CAN1receive++;
length1 = (CAN1RXDLR & 0x0F);
for (index1=0; index1<length1; index1++)
rxdata1[index1] = *(&CAN1RXDSR0 + index1); /* Get received data */
CAN1RFLG = 0x01; /* Clear RXF */
}
Hello Daniel Thank you Very much
Is the number you asked is in the picture I sent?
If not I can found the package
Thanks again
BR
Mwen
Hi Mwen,
Thank you for the mask number.
I tested it on the same board (the same MCU, mask 0L15Y) with the code attached.
There is no CAN transceiver on the board connected to PM2, PM3 so I just pulled CAN1_RX to VDD and scoped CAN1_TX.
The CAN1 module is clearly routed to the PM2, PM3 pins and it works as expected.
As you sure you connected the transceiver to the correct pins?
Regards,
Daniel
Hi, I try your code and the CAN1 was not working.
I put my code with breakpoitn and I saw the code is blocked in the while loop waiting transmission in sending function
while ( (CAN1TFLG & txbuffer) != txbuffer);
On the off chance do you know why?
Thank you very much,
BR
Mwen
Hello Mwen,
Please specify the maskset number of the MCU.
It is printed on the package.
Thank you,
BR, Daniel
Hello Mwen,
Yes, this should work.
You can create a new project for MC9S12XDT512 and use the code.
Regards,
Daniel
Hello
Thank you for your answer and thank you for confirming that there is nothing to do on the PM2 and PM3 pins because CAN1 is the highest priority.
I don't have the card today but saw your code is for the s12xDP512 and I am using the remo s12xDT512.
Can I only change the MC9S12XDP512. (c & h ) by MC9S12XDT512. (c & h)?
Thank you very much for your help
Best regards
Mwen