FlexCAN noob requiring basics

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

FlexCAN noob requiring basics

3,316 Views
lintonsamueldaw
Contributor I

Hi all,

New subscriber here. My company is using MQX for the first time in their new product. Lucky me gets to write the CAN module that communicates with the periphery.

Trying hard to reconcile the documentation with the example FlexCAN code and how this can be adapted to our problem, but having a hard time.

I wish to understand more about these mailboxes. It seems you cannot escape them.

Each time I want to send a message (FLEXCAN_Tx_message()) I need a mailbox and identifier.

Q1: do I create a separate mailbox for each different CAN message I wish to send or do I re-initialize the same mailbox with every send request?

Q2: is there a limit on the number of mailboxes?

Q3: what *is* the identifier? Is it the CAN message id?

On Q3: Documentation hints towards "message buffer ID to use". Isn't the buffer the same as the mailbox? If so, why would I need to provide both mailbox and buffer ID?

Then on the receiving side:

I seem to recall reading somewhere that a mailbox can only retain one message.

Our solution will be making CAN broadcasts (bonjour style discovery of periphery) and as such may expect {n} replies of the same ID (the "I am here" reply)

Obviously, the mailbox filter criteria would result in all these replies spamming the one Rx mailbox.

Q4: What is the best approach to prevent loss of valuable replies?

We considered the installation of an ISR on one single Rx mailbox and inside this ISR copy received messages out of mailbox and empty out the mailbox in time for subsequent replies.

I am sure I will have other questions, but if I got these answered then that will help me on my way a great deal.

Tags (3)
2 Replies

1,011 Views
Martin_
NXP Employee
NXP Employee

Yes, the mailboxes can't be escaped. Mailboxes provide interface to FlexCAN Message Buffers and these are the key operational interface for a CPU to transmit and receive via CAN bus using the FlexCAN hardware module. If you wish understand more about the MQX FlexCAN driver mailboxes, I'd recommend to study the FlexCAN module in the MCU Reference Manual.

Ad Q1:
For the most simple trasmit function, you can have just one mailbox. Use function FLEXCAN_Tx_message() for possibility to change ID, data length and data for every message.

You can use more mailboxes to prepare other tx messages in advance for transmission (as ERGII says, this can be used when the size of the payload of your protocol is greater than the maximum 8 data bytes per CAN message). Notice the mailbox is a FlexCAN Message Buffer and so it can transmit up to 8 data bytes.

Ad Q2:
There is a hardware limit in the MCU FlexCAN module, it has limited number of Message Buffers, and Message Buffers are used for transmission and/or reception of CAN messages.

Ad Q3:
It's CAN message ID. You pass the ID value to the FLEXCAN driver API as parameter, and the driver then writes the ID to the selected FlexCAN module message buffer.

Ad Q4:
FlexCAN module usually supports Rx queue feature and some more recent designs have also Rx FIFO for incoming messages. The Rx FIFO is the best way to deal with many incoming messages being received back to back, as the FIFO gives the CPU more time to handle them, and the CPU reads them from the FIFO in the same order in which they are received.

The Rx Queue feature also allows to receive messages of the same ID to different Message Buffers, but there you don't know the order and so you would need to read time stamp if the order of reception is important.

I'm afraid none of the two above mentioned FlexCAN module features are supported by the current version of the Freescale MQX FlexCAN I/O driver. So, in case you wish to use Rx FIFO, you may need to either modify the driver code or design your own driver.

1,011 Views
egoodii
Senior Contributor III

You don't say what chip your FlexCAN is part of, so there are minor(?) differences that may apply.  I use Kinetis, and for that I let 8 of the mailboxes be used in an RX FIFO, so I have no worries about RX over-run.  The other 8 are thus available for TX, and as I never expect to enqueue more than a few TX messages at a time (mostly longer answers in Transport Protocol) I just let those 8 be my 'outbound queue'.  For my purposes, I have been using FlexCAN for J1939 so 'filtering' is unavailable to me.  You should find that the RxFIFO filter process will allow you to receive all your 'I am here' answers in order.

--ERGII

0 Kudos