How do canbus Callbacks work?

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

How do canbus Callbacks work?

2,009 Views
pb632146
Contributor V

Hello I am trying to configure my canbus to receive a message, send data based on the message, and look to receive again. However I have only worked with sendblocking so far and do not know how to trigger callbacks and my receive isnt working anymore. 

I have this sample callback from other code but do not really understand what it is doing.

void flexcan0_Callback(uint8 instance, Flexcan_Ip_EventType eventType,
uint32 buffIdx, const Flexcan_Ip_StateType *flexcanState) {
(void) flexcanState;
(void) instance;
 
switch (eventType) {
case FLEXCAN_EVENT_RX_COMPLETE:
count += 1;
if (buffIdx == 1) // MB1 received
{
FlexCAN_Ip_Receive(INST_BOARD_INITPERIPHERALS_FLEXCAN_1, RX_MB_IDX, &rxData, FALSE);
}
 
break;
case FLEXCAN_EVENT_RXFIFO_COMPLETE:
break;
case FLEXCAN_EVENT_TX_COMPLETE:
 
/* if (buffIdx == 0) // MB0 transmitted
{
//dummyData[0]++;
FlexCAN_Ip_Send(INST_BOARD_INITPERIPHERALS_FLEXCAN_1, TX_MB_IDX, &rx_info, MSG_ID,
(uint8*) &txData);
}
*/
break;
default:
break;
}
 
Will it automatically be called when completing a non blocking send? Does this trigger when a message/rxfifo is received?
0 Kudos
Reply
8 Replies

1,987 Views
pb632146
Contributor V

I have figured out how the send callback works, it was that I did not have the interrupt configured correctly so the first send never marked as finished.

 

However I am still confused on how to receive messages properly. I have the interrupt corrected but it does not trigger while I have a transmitter constantly sending messages at it. Do I need is_polling to be TRUE for it to see messages? Does it only check when "FlexCAN_Ip_MainFunctionRead" is called? I had this working before but now it does not and I have no idea what has broken.

0 Kudos
Reply

1,976 Views
pb632146
Contributor V
When I use ReceiveBlocking it just times out, when I use Receive it is permanentl Flexcan_status_busy, any suggestions on what could be happening? The board transmits messages perfectly both blocking and non blocking but receive does not work at all.
0 Kudos
Reply

1,972 Views
PetrS
NXP TechSupport
NXP TechSupport

Hi,

check below example for interrupt driven code with callbacks
https://community.nxp.com/t5/S32K-Knowledge-Base/Example-S32K344-FlexCAN-Ip-TX-RX-EnhanceRXFIFO-test...
It would be helpful.

BR, Petr

0 Kudos
Reply

1,955 Views
pb632146
Contributor V
I got the callbacks working it was related to other errors such as the transmitter not being clear whether it was sending std or ext frames.

FlexCAN_Ip_ConfigRxMb asks for a message id argument. Does this assign a single id for the message buffer to accept? I see in your example that MB1 and MB2 are written as configured to receive 1 ID each.

If message buffers can only take 1 ID am I then required to use FIFO for taking multiple IDs? The FIfo is stated to only have 6 spaces so I was hoping to use message buffers as a larger space.

In the RxFifo section they have both a mask and a filter. If I understand the mask mechanics correctly then if I had a std frame with 0xFF filter and a 0xFF Mask would accept 0x0FF,1FF,2FF, etc? Is applying a filter possible to message buffers as well?

I am looking to work with J1939 standards so I don't know if I am able to assign exact IDs to message buffers as I need to use the ID as a source of incoming data.
0 Kudos
Reply

1,921 Views
PetrS
NXP TechSupport
NXP TechSupport

Hi,

yes, FlexCAN_Ip_ConfigRxMb configures given MB to accept message with defined ID. You can configure mask acceptance registers (either global or individual mask registers) to allow more IDs to be accepted into single MB. A mask work exactly as you mentioned. There is bit to bit correspondence between mask, MB ID and ID received. A zero in mask means a bit is don't care.
Individual masking scheme is used in the example I pointed to, using FlexCAN_Ip_SetRxMaskType_Privileged and FlexCAN_Ip_SetRxIndividualMask_Privileged functions.

BR, Petr

1,886 Views
pb632146
Contributor V
Thank you so much for your help so far, masking has been working successfully for me.

A few more questions
1. It is fine practice to set the mask for MBs I would like to accept any IDs to 0x0 right? So that the assigned ID does not matter, as there is seemingly no way to avoid assigning an ID to a MB

2. I see in your sample code that you have an instance of Flexcan_Ip_MsgBuffType for both RxData and RxFIFO, is there any particular reason for that? As the number of MB increases is there any need for using multiple instances of RxData to avoid data overwriting?

3. Do send/receive blocking still trigger callbacks upon completion?
0 Kudos
Reply

1,846 Views
PetrS
NXP TechSupport
NXP TechSupport

Hi,

1) yes, if mask is cleared then the MB accepts all IDs (std or ext as defined)
2) this is up to user how many buffers will use. If each callback call a buffer is processed in an RX event, then there can be single buffer, I think. 
3) yes, if installed callback is called in blocked functions as well.

BR, Petr

1,883 Views
pb632146
Contributor V
Actually sorry just one more

I want to have the MBs active and receiving but only get the data out once a loop every 100ms. I assume I could do one of two things but I do not know the caveats or preferred practice.

1. Not use callback and use FlexCAN_Ip_GetTransferStatus on each MB Calling MainFunctionRead on the ones that return successful would get the ones that have new data correct?

or

2. Use the callback to extract the data and raise a flag, but do not run FlexCAN_Ip_Receive() in the callback to keep the MB closed. Then when the loop reaches the point I check for new messages reopen any MBs that have been closed.

The first seems to be a little easier, are there any considerations I might be missing?
0 Kudos
Reply