Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 2 of 2

Thread: Canbus Mask and filters

  1. #1
    Junior Member
    Join Date
    Mar 2019
    Posts
    2

    Canbus Mask and filters

    I'm very new to the teensy line and I'm trying to convert my Canbus arduino code to work on the teensey 3.6.
    I'm pretty sure I have most of it figured out except the Mask and Filter settings.

    So I'm pretty sure there are 16 Mailboxes to send and recieve Canbus data. I've defiend two of them to be used as transmit. That leaves the first 14 to be used as receive mailboxes.
    I would like to apply the same mask to all of them and then I have four filters to use. This is the code I have for Mask and filter settings:

    Code:
    static CAN_filter_t RxFilter[16];
    static CAN_filter_t RxMask[16];
    long int place[] = {0x01,0x01+0xFF,0x01+0xFFFF,0x01+0xFFFFFF,0x01+0xFFFFFFFF,0x01+0xFFFFFFFFFF,0x01+0xFFFFFFFFFFFF};
    
    //Canbus Module Address
      bitWrite(CanAddress, 0, digitalRead(Canpin[0]));
      bitWrite(CanAddress, 1, digitalRead(Canpin[1]));
      bitWrite(CanAddress, 2, digitalRead(Canpin[2]));
      bitWrite(CanAddress, 3, digitalRead(Canpin[3]));
      bitWrite(CanAddress, 4, digitalRead(Canpin[4]));
      bitWrite(CanAddress, 5, digitalRead(Canpin[5]));
      bitWrite(CanAddress, 6, digitalRead(Canpin[6]));
      bitWrite(CanAddress, 7, digitalRead(Canpin[7]));
      bitWrite(CanAddress, 8, 0);
    
    //Initialize CANbus Filters
      RxFilter[0].id=0x00EAFF00;
      RxFilter[0].ext=1;
      RxFilter[0].rtr=0;
      RxFilter[1].id=0x00EA0000 + (CanAddress * place[1]);
      RxFilter[1].ext=1;
      RxFilter[1].rtr=0;
      RxFilter[2].id=0x00EFFF00;
      RxFilter[2].ext=1;
      RxFilter[2].rtr=0;
      RxFilter[3].id=0x00EF0000 + (CanAddress * place[1]);
      RxFilter[3].ext=1;
      RxFilter[3].rtr=0;
      RxFilter[4].id=0x00EAFF00;
      RxFilter[4].ext=1;
      RxFilter[4].rtr=0;
      RxFilter[5].id=0x00EA0000 + (CanAddress * place[1]);
      RxFilter[5].ext=1;
      RxFilter[5].rtr=0;
      RxFilter[6].id=0x00EFFF00;
      RxFilter[6].ext=1;
      RxFilter[6].rtr=0;
      RxFilter[7].id=0x00EF0000 + (CanAddress * place[1]);
      RxFilter[7].ext=1;
      RxFilter[7].rtr=0;
      RxFilter[8].id=0x00EAFF00;
      RxFilter[8].ext=1;
      RxFilter[8].rtr=0;
      RxFilter[9].id=0x00EA0000 + (CanAddress * place[1]);
      RxFilter[9].ext=1;
      RxFilter[9].rtr=0;
      RxFilter[10].id=0x00EFFF00;
      RxFilter[10].ext=1;
      RxFilter[10].rtr=0;
      RxFilter[11].id=0x00EF0000 + (CanAddress * place[1]);
      RxFilter[11].ext=1;
      RxFilter[11].rtr=0;
      RxFilter[12].id=0x00EAFF00;
      RxFilter[12].ext=1;
      RxFilter[12].rtr=0;
      RxFilter[13].id=0x00EA0000 + (CanAddress * place[1]);
      RxFilter[13].ext=1;
      RxFilter[13].rtr=0;
      RxFilter[14].id=0x00EFFF00;
      RxFilter[14].ext=1;
      RxFilter[14].rtr=0;
    
     Can0.begin(100000,RxMask[16],1,1); //begin(baud, defaultMask, txAlt (29), RxAlt (30))
     Can1.begin(100000,RxMask[16],0,0); //begin(baud, defaultMask, txAlt (33), RxAlt (34))
     Can0.setNumTXBoxes(2); //2 buffers used for transmissions - buffers 14 & 15
     Can1.setNumTXBoxes(2); //2 buffers used for transmissions - buffers 14 & 15
      for (int c = 0; c < 14; c++)
      {
         Can0.setMask(RxMask[16].id,c);
         Can1.setMask(RxMask[16].id,c);
         Can0.setFilter(RxFilter[c], c);
         Can1.setFilter(RxFilter[c], c);
      }
    Please let me know if I'm using the mask and filter correctly. I've applied the same extended mask to the begin statement and the first 14 mailboxes. Since I only have four filters i want to use, I used those same four filters across the 14 mailboxes. I am assuming that each mailbox has its own mask and filter and only the messages that are correctly filtered through that mask and filter, end up in that particular mailbox...am I correct on this assumption?

  2. #2
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,378
    Hi, in order to find out how the masking/filters work in the beginning it is better to do the bits on paper to understand how it’s made up. There are many tutorials on the net that can demonstrate it. There is a library for teensy called IFCT that does all the filter calculations automatically for the user, you just give it the ID(s) and they will put the correct filters in the registers for you, making things alot simpler for the user

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •