Decode/Revert CAN Frame ID

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

Decode/Revert CAN Frame ID

Jump to solution
761 Views
abdullah_cinar
Contributor III

Hi,

I am receiving hundreds of different CAN frames from a peripheral device to my MKV11Z64 MCU. When I want to send a frame, I use this macro:

FLEXCAN_ID_STD(rxIdentifier)

Then when I receive a CAN packet from the peripheral device, I need to check the frame id. So I use this:

printf("Message Id: 0x%.2X\r\n", rxFrame.id);

But it does not give me the correct value. I get 0x10C40000 as the frame id but it should be less than 0x7FF for standard IDs. Is there any reverse implementation of FLEXCAN_ID_STD macro to get the correct frame id?

I could have write my own macro by looking at the FLEXCAN_ID_STD implementation and do the reverse job but I don't understand why we can not just get the correct frame id directly.

Edit: I think when I shift the result to right for 18 bits, I get the correct result. Why does this happen?

Labels (1)
0 Kudos
1 Solution
660 Views
Sabina_Bruce
NXP Employee
NXP Employee

Hello Abdullah,

The reason you would have to do the shift to get the right id is due to the function that you are using. 

The sdk was designed to make it easier to create the message buffer, therefore different functions were implemented. One of them is FLEXCAN_ID_STD(rxIdentifier) as you can see in its declaration it is 

 (((uint32_t)(((uint32_t)(id)) << CAN_ID_STD_SHIFT)) & CAN_ID_STD_MASK) /*!< Standard Frame ID helper macro. */

With the shift being an 18 bit shift.

So in order to recover the id you will have to shift it to the right or redesign your implementation.

Best Regards,

Sabina

View solution in original post

0 Kudos
1 Reply
661 Views
Sabina_Bruce
NXP Employee
NXP Employee

Hello Abdullah,

The reason you would have to do the shift to get the right id is due to the function that you are using. 

The sdk was designed to make it easier to create the message buffer, therefore different functions were implemented. One of them is FLEXCAN_ID_STD(rxIdentifier) as you can see in its declaration it is 

 (((uint32_t)(((uint32_t)(id)) << CAN_ID_STD_SHIFT)) & CAN_ID_STD_MASK) /*!< Standard Frame ID helper macro. */

With the shift being an 18 bit shift.

So in order to recover the id you will have to shift it to the right or redesign your implementation.

Best Regards,

Sabina

0 Kudos