Building a CAN API for Arduino DUE

robertspark:
Hello all, firstly may I please apologise if this appears to be a daft question, I am new to Arduino Can Bus on the Due.

I've had a look at the examples given and the library and the SAM manual section 41.7.2.1, but I'm at a loss to understand how to setup the Due to receive ALL messages regardless of id.

If you want to receive all messages then one easy way to do that is to just set your mask to all zeros. In example 4 you can see lines like this:
  CAN.mailbox_set_accept_mask(0, 0x1FFFFFFF, true);.
Well, setting the accept mask to zero makes it totally accepting.

Or if it was setup to receive SAE J1939 messages (29 bit messages) that uses a 3 bit priority, then a 18 bit Parameter Group Number (PGN), and then an 8 bit source address for the message. The PGN allows for destination specific communication and broadcast communication. How to set it up to receive a range or all PGN messages or a bank of PGN messages.

Once again, you would set the acceptance mask. However, for these examples you would also need to set an ID to match against. I know it will probably be easier if I give some examples. I don't remember J1939 off hand but lets say the top three bits are the priority, the next 18 PGN and the lowest 8 the source. So, with that in mind lets say you want to accept only messages with PGN of 0x3946B. You don't care about the priority and you don't care about the source. So, anything you don't care about should be a 0 in the bit mask and everything you do care about should be a 1. You want to match only one PGN so you want to match every bit in the PGN. This makes your acceptance mask: 0b00000011111111111111111100000000 = 0x3FFFF000. You then set the ID for the mailbox to be the ID you want to accept. You have to keep in mind that the PGN is shifted up 8 bits in the canbus ID so you take your desired ID of 0x3946B and shift it up 8 bits to yield 0x3946B00. This value goes in the ID field. The result looks like this:

CAN.mailbox_set_accept_mask(0, 0x3FFFF000, true);
CAN.mailbox_set_id(0, 0x3946B00, true);

In that code the true's at the end mean to use extended addresses. False would have meant standard (11 bit) IDs.

Now, what if you know that there a group of PGNs you want to receive. Let's say you need to accept PGN 0x39460 - 0x3946F (16 PGN codes) and further that you only want to accept from a source address that ranges from 0x50 - 0x57. So, you want to match everything but the last 4 bits of the PGN which you'll allow to be whatever (this gives you the 16 PGN codes listed above.) You also want to match everything but the last 3 bits of the source. So, this makes your acceptance mask 0b00000011111111111111000011111000 which is 0x3FFF0F8. You make your ID that you set be the lower bounds of the two values (PGN = 0x49460 and source = 0x50). This makes your ID 0x4946050. So, the relevant code would be:

CAN.mailbox_set_accept_mask(0, 0x3FFF0F8, true);
CAN.mailbox_set_id(0, 0x3946050, true);

If you need to accept ranges that aren't possible with a single bit pattern then you need to set up multiple mailboxes, each with the separate settings to accept each discrete pattern.