T4 Can Bus sending a series of different ID data frames

Status
Not open for further replies.

Ceetee

Member
I have been playing with the FlexCAN T4 library with my Teensy 4 and I have been trying to send a series of different ID data frames out of the T4. This data I aim to use as a data simulation for another project. I can see data on the can bus, using a logic analyser, but it is always the same ID, the lowest number, in my case 'msg.id = 210'. The next ID, 'msg1.id = 300' doesn't appear unless 300 is the lowest number.

I have a short program listing that is probably completely incorrect but I thought I could send one message then send another different message after a short delay and it would appear on the Can Bus.

Code:
#include <FlexCAN_T4.h>

FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_16> CCT;

static CAN_message_t msg, msg1;

void setup(void)
{
  CCT.begin();
  CCT.setBaudRate(500000);   // I like to set the baud rates just to be on the safe side
}

void loop(void)
{
  msg.id = 300;
  msg.len = 8;
  msg.flags.extended = 0;
  msg.flags.remote   = 0;
  msg.flags.overrun  = 0;
  msg.flags.reserved = 0;
  msg.buf[0] = 10;
  msg.buf[1] = 20;
  msg.buf[2] = 255;
  msg.buf[3] = 100;
  msg.buf[4] = 128;
  msg.buf[5] = 64;
  msg.buf[6] = 32;
  msg.buf[7] = 16;
  CCT.write(msg);
  delay(1000);
  
  msg1.id = 210;
  msg1.len = 8;
  msg1.flags.extended = 0;
  msg1.flags.remote   = 0;
  msg1.flags.overrun  = 0;
  msg1.flags.reserved = 0;
  msg1.buf[0] = 240;
  msg1.buf[1] = 2;
  msg1.buf[2] = 25;
  msg1.buf[3] = 10;
  msg1.buf[4] = 18;
  msg1.buf[5] = 54;
  msg1.buf[6] = 22;
  msg1.buf[7] = 36;
  CCT.write(msg1);
  delay(1000);
}

I have had a quick stab at using Mailboxes but I get the same result, I even added a 'toggling' integer so that it would only send one message at each pass through.

Code:
#include <FlexCAN_T4.h>

FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_16> CCT;

CAN_message_t msg, msg1;
int r = 0;

// -------------------------------------------------------------
void setup(void)
{
  Serial.begin(115200);
  CCT.begin();
  CCT.setBaudRate(500000);   // I like to set the baud rates just to be on the safe side
  CCT.reset();
  CCT.setMBFilter(REJECT_ALL);
  CCT.setMB(MB1,TX);
  CCT.setMB(MB2,TX);
  CCT.events();
}

void loop(void)
{
 if (r==0) {
  msg.id = 300;
  msg.len = 8;
  msg.seq = 1;
  msg.buf[0] = 10;
  msg.buf[1] = 20;
  msg.buf[2] = 255;
  msg.buf[4] = 128;
  msg.buf[5] = 64;
  msg.buf[6] = 32;
  msg.buf[7] = 16;
  CCT.write(msg);
  r=1;
 }
  else
  {
  msg1.id = 210;
  msg1.len = 8;
  msg1.seq = 1;
  msg1.buf[0] = 240;
  msg1.buf[1] = 2;
  msg1.buf[2] = 25;
  msg1.buf[3] = 10;
  msg1.buf[4] = 18;
  msg1.buf[5] = 54;
  msg1.buf[6] = 22;
  msg1.buf[7] = 36;
  CCT.write(msg1);
  r=0;
  }
  delay(1000);

}

Could anybody point me in the right direction, please.
 
Dont use reset() at all, it will wipe out begin() configuration. Reset() is not complete yet in the library.
 
Just tested your non-mailbox sketch going from CAN3 to CAN3 in legacy mode and seems to be sending the data without an issue. This is what I am getting on the receiving T4:
Code:
MB 99  LEN: 8 EXT: 0 TS: 45 ID: 300 Buffer: 10 20 255 100 128 64 32 16 
MB 99  LEN: 8 EXT: 0 TS: 34461 ID: 210 Buffer: 240 2 25 10 18 54 22 36 
MB 99  LEN: 8 EXT: 0 TS: 10173 ID: 300 Buffer: 10 20 255 100 128 64 32 16 
MB 99  LEN: 8 EXT: 0 TS: 51421 ID: 210 Buffer: 240 2 25 10 18 54 22 36 
MB 99  LEN: 8 EXT: 0 TS: 27132 ID: 300 Buffer: 10 20 255 100 128 64 32 16
which is what you are sending as packets. I am using the following sketch as a check on the receiving T4.:
Code:
#include <FlexCAN_T4.h>

FlexCAN_T4<CAN3, RX_SIZE_256, TX_SIZE_512> Can0;
#define PRINTF Serial.printf

void setup(void) {
  Serial2.begin(115200);
  Serial.begin(2000000);
  Can0.begin();

  Can0.setBaudRate(1000000);
  Can0.setMaxMB(16);
  Can0.enableFIFO(1); 
  Can0.enableFIFOInterrupt();
  Can0.mailboxStatus();
  Can0.setMBFilter(ACCEPT_ALL);
  Can0.onReceive(MB0, canSniff);
  Can0.setMB(MB0,RX); 

delay(200);

}

void loop() {
/*
  CAN_message_t rx_msg;
  if ( Can0.read(rx_msg) ) {
    Serial.print("ID: 0x");
    Serial.print(rx_msg.id, HEX);
    Serial.print("\tData: ");
    for ( uint8_t i = 0; i < 8; i++ ) {

      Serial.print("0x");
      Serial.print(rx_msg.buf[i], HEX);
     Serial.print(" ");
    }
    Serial.println();
  }
*/
  static uint32_t sending = millis();
  if ( millis() - sending > 500 ) {
    sending = millis();
    CAN_message_t msg;
    msg.id = 0x55;
    msg.flags.extended = 1;
    msg.len = 8;
    for ( uint8_t i = 0; i < 8; i++ ) msg.buf[i] = 0xFF;
    for ( uint8_t i = 14; i < 32; i++ )     Can0.write( msg);

  }
}



void canSniff(const CAN_message_t &msg) { // global callback
  Serial.print("MB "); Serial.print(msg.mb);
  Serial.print(" BUS "); Serial.print(msg.bus);
  Serial.print(" LEN: "); Serial.print(msg.len);
  Serial.print(" EXT: "); Serial.print(msg.flags.extended);
  Serial.print(" REMOTE: "); Serial.print(msg.flags.remote);
  Serial.print(" TS: "); Serial.print(msg.timestamp);
  Serial.print(" ID: "); Serial.print(msg.id, HEX);
  Serial.print(" Buffer: ");
  for ( uint8_t i = 0; i < msg.len; i++ ) {
    Serial.print(msg.buf[i], HEX); Serial.print(" ");
  } Serial.println();
}

One thing to check any filters you set up on the receiving T4. In the sketch above I have it set as Can0.setMBFilter(ACCEPT_ALL); to accept all IDs.
 
Yeah just don't use reset, as per the datasheet:

Code:
The following registers are reset: MCR (except the MDIS bit), TIMER , ECR, ESR1, ESR2, IMASK1, IMASK2, IFLAG1, IFLAG2 and CRCR. Configuration registers that control the interface to the CAN bus are not affected by soft reset.

This may be an issue, then, the other issue, which most likely is more of the cause, you enabled sequential frame transmission (msg.seq = 1), however you didn't add CCT.events() to your loop. See below:

Interrupt mailbox and sequential transmissions are REQUIRED to run events() in loop() for processing. Not running events() will make you not receive frames in assigned callbacks nor will sequential frames continue. Now that makes sense. The first frame(lowest) is sent while the second one is QUEUEd for next events() transmission, and further sends stop because the remaining transmit roll into a queue which no events() can deque.

Please remove reset() and put CCT.events() in your loop
 
Hello tonton81 and mjs513

Thank you for your very quick replies I will try both answers, when I finish work, and let you know how it goes.

Chris
 
Right I got to the bottom of the initial problem and that was a faulty (not sure why yet) Logic analyser, when I attached my Saleae Logic 4 all the messages were there as you reported mjs513. So thank you for confirming my first sketch was working fine. The down side was, if its a downside, I used your 'sniffing' sketch and received nothing in the Serial Monitor but I was actually using a T3.6 as the 'receiver' and that may have been the issue. Sadly only have the one T4 at the moment, appear to have broken my other one. However, I did try the IFCTsniffer sketch (which uses FIFO) and all was as it should be both Id's data frames are visible.

I will try with the small mailbox sketch now, initially though do you think it will work or am I miles off.

Thanks.
 
Greetings, I have got the non-mailbox CAN comm's working absolutely fine so happy there, but I have just bumped into an error when playing with mailbox comm's. The error message is "request for member 'msg' in 'MB1', which is of non-class type 'FLEXCAN_MAILBOX'".

I am using a T4 for sending.

I have this in 'setup'
Code:
  CCT.begin();
  CCT.setBaudRate(500000);   // I like to set the baud rates just to be on the safe side
  CCT.setMBFilter(REJECT_ALL);
  CCT.setMB(MB1,TX);
  CCT.setMB(MB2,TX);
  CCT.setMB(MB3,TX);
  CCT.setMB(MB4,TX);
  CCT.setMB(MB5,TX);

and I send in 'loop'
Code:
  CCT.events();
  
  msg.id = 101;
  msg.len = 8;
  msg.flags.extended = 0;
  msg.flags.remote   = 0;
  msg.flags.overrun  = 0;
  msg.flags.reserved = 0;
  msg.buf[0] = 10;
  msg.buf[1] = 20;
  msg.buf[2] = 255;
  msg.buf[3] = 100;
  msg.buf[4] = 128;
  msg.buf[5] = 64;
  msg.buf[6] = 32;
  msg.buf[7] = 16;
  CCT.write(MB1.msg);
  //delay(10);
  

  msg1.id = 201;
  msg1.len = 8;
  msg1.flags.extended = 0;
  msg1.flags.remote   = 0;
  msg1.flags.overrun  = 0;
  msg1.flags.reserved = 0;
  msg1.buf[0] = 240;
  msg1.buf[1] = 2;
  msg1.buf[2] = 25;
  msg1.buf[3] = 10;
  msg1.buf[4] = 18;
  msg1.buf[5] = 54;
  msg1.buf[6] = 22;
  msg1.buf[7] = 36;
  CCT.write(MB2.msg1);

  msg2.id = 301;
  msg2.len = 8;
  msg2.seq = 1;
  msg2.flags.extended = 0;
  msg2.flags.remote   = 0;
  msg2.flags.overrun  = 0;
  msg2.flags.reserved = 0;
  msg2.buf[0] = 0;
  msg2.buf[1] = 0;
  msg2.buf[2] = 0;
  msg2.buf[3] = 0;
  msg2.buf[4] = 0;
  msg2.buf[5] = 0;
  msg2.buf[6] = 0;
  msg2.buf[7] = 0;
  CCT.write(MB3.msg2);

  msg3.id = 401;
  msg3.len = 8;
  msg3.flags.extended = 0;
  msg3.flags.remote   = 0;
  msg3.flags.overrun  = 0;
  msg3.flags.reserved = 0;
  msg3.buf[0] = 0;
  msg3.buf[1] = 1;
  msg3.buf[2] = 0;
  msg3.buf[3] = 1;
  msg3.buf[4] = 0;
  msg3.buf[5] = 1;
  msg3.buf[6] = 0;
  msg3.buf[7] = 1;
  CCT.write(MB4.msg3);   

  msg4.id = 402;
  msg4.len = 8;
  msg4.flags.extended = 0;
  msg4.flags.remote   = 0;
  msg4.flags.overrun  = 0;
  msg4.flags.reserved = 0;
  msg4.buf[0] = 1;
  msg4.buf[1] = 0;
  msg4.buf[2] = 1;
  msg4.buf[3] = 0;
  msg4.buf[4] = 0;
  msg4.buf[5] = 0;
  msg4.buf[6] = 0;
  msg4.buf[7] = 0;
  CCT.write(MB5.msg4);
  delay(2);

I am using IFCT simpleMBpolling for reading on a T3.6 and suspect I am missing something really basic here, just can't see it.
 
Why you using a period instead of a comma for writes? Should be "MB1,msg"

Filters should be set after mailbox configuration (reject all)

If using FIFO, MB0-7 dont exist, and using setMB does nothing with them

By default t3 has 16 mailboxes and t4 has 64 mailboxes, half of each, by default, are TX, so you can save on setting them. So theres 8tx on t3 and 32 on t4, for standard mailboxes and extended, 4,4 on t3, 16,16 on t4 :) this is based on no FIFO.

You dont need to specify the mailbox number in write() function, it will drop it in the next available mailbox
 
Status
Not open for further replies.
Back
Top