FlexCAN_T4 - FlexCAN for Teensy 4

Oww i see, I read that CAN1 was tested and ok.

while going through the code, the problem is indeed not a problem, just a (certainly) test feature in the
Code:
FCTP_FUNC void FCTP_OPT::writeTxMailbox(uint8_t mb_num, const CAN_message_t &msg)
function.

Changing the size and message data allocation to the actual values fixes everything !

Note : the data registers (FLEXCANb_MBn_WORD0 and FLEXCANb_MBn_WORD1) were masked ( |= ), don't forget to reset them at some point...

Thanks for the fast reply !
 
Yeah they will be redesigned based off the progress on the FD system.
It's been very efficient and easier to work with, progress upon
 
Optimizations/Bug fixes updates for FD:

1) Extended framing in poll mode (non interrupt) does not register bit in iflag (so reception only worked in ISR and not polling), standard ids do. If polling is a requirement, for extended frames, the code field will be checked directly, whereas for standard frames, the iflag will be checked.
Interrupt enabled extended frame mailboxes don't have this issue.

2) several condition statements have been optimised to use a const array for lookups.

3) Corrected distribution to only distribute frames to similar mailboxes, extended vs standard. Previously it was any. This now ensures the callback gets an actual standard or extended frame copy on a filter match, based on the mailbox frame type.
 
Since CANFD is running I started working on CAN2.0 side today.
The function has been corrected with updated code. On a side note, CANFD and CAN2.0 are both running in parallel on my test bench, have 2 t4's and t3.5 and t3.6 all hooked up together, streaming in both read and write. This should help me develope code and corrections on both sides from a single sketch. I will be working on the filtering system this week

If this ends up being a stable build I will later on try to port it to teensy 3.x (CAN2.0), as a successor to IFCT (although would look a bit weird seeing a FlexCAN_T4 constructor on a T3 :p)
 
I want to make sure I start with how much I appreciate all the work that's gone into this so far!

I understand this is very much a work in progress at this point, so trying to understand if my issue is something that just hasn't been implemented yet or an unknown bug that may be easy to configure around.

I'm having issues running both SPI and CAN (1 or 2) on a Teensy 4.0 due to what I'm assuming are overlapping pins of SCK and alternate CRX1.

I have SPI fully up and operational, but when I setup and begin either CAN1 or CAN2 it seems to kill the SCK clock pin. I dug through the FlexCAN_T4 library a bit, but can't seem to find where it may be configuring alternate pin 13 as well as the default pin 23 for CRX1.

CAN1.begin() causes SPI SCK to not output a clock signal, but this line commented out and SPI is fully functional again
 
That was my first thought as well, but it didn't help either.

That being said, I seem to have figured out the issue: user error

I took your example and just changed the CAN port, not even noticing the FD subfix. I ended up with
FlexCAN_T1FD<CAN1, RX_SIZE_256, TX_SIZE_16> CAN_1;

This explains why CAN1 and CAN2 (the ports without FD support) were causing issues when configured to FD. Oddly enough this was knocking down the Serial USB communication as well when configured incorrectly.

Changed it to "FlexCAN_T1<CAN1, RX_SIZE_256, TX_SIZE_16> CAN_1;" and now everything is operational again.
Looking forward to getting the CAN bus all setup and thanks again for the quick response!
 
Thanks, I will upload an updated version tonight to github which has the CAN2.0 transmitting issue ressolved posted above, filtering support works however smart filtering and distribution not added yet.

The demo on github shows FlexCAN_T4FD, but yeah omit the FD part to run in 2.0 mode, and use setBaudRate(1000000) instead of the FD baud generator. They are 2 very different functions.
 
After the last update I worked on adding Teensy 3.x support. Here it is for testing!
Currently This compiles on both Teensy 3.5 and Teensy 4.0 on my test bench, and looks to be working. I've separated it from the main branch in case theres any issues until the merge, enjoy

Code:
[URL="https://github.com/tonton81/FlexCAN_T4/blob/master/FlexCAN_T4-teensy3x%20support.zip"]https://github.com/tonton81/FlexCAN_T4/blob/master/FlexCAN_T4-teensy3x%20support.zip[/URL]

Here is the test sketch running on a T3.5
Code:
#include <FlexCAN_T4.h>
FlexCAN_T4<[COLOR="#FF0000"]CAN0[/COLOR], RX_SIZE_256, TX_SIZE_16> Can0;

void setup() {
  Serial.begin(115200);
  pinMode(2, OUTPUT); // for the transceiver enable pin
  Can0.begin();
  Can0.setBaudRate(1000000);
  Can0.enableFIFO();
  Can0.enableFIFOInterrupt();
  Can0.onReceive(FIFO, canSniff);
  Can0.mailboxStatus();
}

void loop() {
  Can0.events();

  static uint32_t t = millis();
  if ( millis() - t > 100 ) {
    t = millis();
    CAN_message_t frame;
    static uint32_t id = 0;
    id++;
    if ( id > 100 ) id = 1;
    frame.id = id;
    for ( uint8_t i = 0; i < 8; i++ ) {
      frame.buf[i] = id;
    }
    Can0.write(frame);

  }

  CAN_message_t msg;
  if ( Can0.read(msg) ) canSniff(msg); // polling
}
void canSniff(const CAN_message_t &msg) { // global callback
  Serial.print("MB "); Serial.print(msg.mb);
  Serial.print("  LEN: "); Serial.print(msg.len);
  Serial.print(" EXT: "); Serial.print(msg.flags.extended);
  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();
}
 
Last edited:
1) Library has been updated officially to support T3.x.
2) The FD mailbox offsets have been further optimized.
3) As per request of skpang, EDL and BRS reception flags have been added to the message struct and isr/functions for RX, to identify the incomming frame CAN2.0 vs CANFD and whether baudrate switching for data was activated or not for that frame

EDIT:
Found and corrected a bug in the RFFN filters for FIFO. 31 is the absolute max individual filters for FIFO on the IMXRT1060, while on teensy 3.x, 14 individual masks is the absolute max. Depending on the RFFN value, the limit is also changed. This has been fixed and adjusted accordingly, as for the IMXRT1060, a choosing an RFFN of higher than 0xB will yield a max of 32 individual filters onwards. By default, FIFO has 8 filters consuming mailboxes MB6 and MB7. Should you want 10 filters, setting an RFFN to 1 would give you 10 filters, consuming MB6 -> MB9, leaving you with MB10-15 as transmit or receive mailboxes. There is no point of using a global mask if you are using a set of individual masks, and teensy 3.x fifo supports up to 14 filters max leaving you with 2 mailboxes for transmit or receive. Same goes for the IMXRT1060, absolute max is 32 individual masks, so setting an RFFN of higher than 0xC on it is pointless since the rest depend on the global mask. This would leave you with MB32-63 available for RX/TX.
 
Last edited:
Is there something similar to Can1.end() in the FlexCAN library? I like to turn the network on and off. Thanks.....
 
Setlistenmode effectively disconnects the writes from the network, another option would be putting the controller into freezemode (like a paused state), indefinately, until switched back to normal mode
 
FlexCAN_T4 has been updated!

Up to 3 raw outputs are enabled in background for external libraries (you know where this is going.......... :) )
All reception mailbox has mass enabler for interrupt capability! No more setting each mailbox interrupt manually. --> x.enableMBInterrupts();, CAN2.0 and CANFD equipped.
Knocked off static on the FD buffer implementation. In templates, statics only have effect accross same types. Each bus is a unique type of template, making static quite pointless in usage.

New library in works.... TeensyCAN!

https://forum.pjrc.com/threads/58242-TeensyCAN
 
FlexCAN_T4 readme documentation updated.
CANFD mode setBaudRate changed slightly. Two ways of calling:

Code:
  FD.setBaudRate(config); /* no printout will occur, with the default listing entry of 1st on CAN and 1st on CANFD selected. */
  FD.setBaudRateAdvanced(config,1,1); /* allows of advanced printout and selection of the 1st listings of CAN2.0 and CANFD. */

Appending the overload LISTEN_ONLY to the end of the function will allow the bus to enter listen-only mode.

On a side note, while playing with TeensyCAN, FlexCAN_T4 on the PJRC breakout board is working at 1M nominal and 8M data, 40MHz clock and 70% sampling point.

Code:
  CANFD_timings_t config;
  config.clock = CLK_40MHz;
  config.baudrate = 1000000;
  config.baudrateFD = 8000000;
  config.propdelay = 190;
  config.bus_length = 1;
  config.sample = 70;
  FD.setBaudRate(config);




Console:
Code:
ISR - MB 3  OVERRUN: 0  LEN: 64 EXT: 1  EDL: 1 TS: 14352 ID: 1FFCC014 Buffer: 0 0 0 28 BE C7 5 1 2 3 4 5 6 7 8 9 A B C D E F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20 21 22 23 24 25 26 27 28 AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA 
Node: 20	PacketID: 5	Broadcast: 1	Data: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 
Node: 20	PacketID: 7	Broadcast: 0	Data: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 
ISR - MB 3  OVERRUN: 0  LEN: 64 EXT: 1  EDL: 1 TS: 15862 ID: 1FFCF214 Buffer: 0 0 0 28 BE C7 7 1 2 3 4 5 6 7 8 9 A B C D E F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20 21 22 23 24 25 26 27 28 AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA 
ISR - MB 3  OVERRUN: 0  LEN: 64 EXT: 1  EDL: 1 TS: 35416 ID: 1FFCC014 Buffer: 0 0 0 28 BE C7 5 1 2 3 4 5 6 7 8 9 A B C D E F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20 21 22 23 24 25 26 27 28 AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA 
Node: 20	PacketID: 5	Broadcast: 1	Data: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 
ISR - MB 3  OVERRUN: 0  LEN: 64 EXT: 1  EDL: 1 TS: 36927 ID: 1FFCF214 Buffer: 0 0 0 28 BE C7 7 1 2 3 4 5 6 7 8 9 A B C D E F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20 21 22 23 24 25 26 27 28 AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA 
Node: 20	PacketID: 7	Broadcast: 0	Data: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 
ISR - MB 3  OVERRUN: 0  LEN: 64 EXT: 1  EDL: 1 TS: 56481 ID: 1FFCC014 Buffer: 0 0 0 28 BE C7 5 1 2 3 4 5 6 7 8 9 A B C D E F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20 21 22 23 24 25 26 27 28 AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA 
Node: 20	PacketID: 5	Broadcast: 1	Data: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 
Node: 20	PacketID: 7	Broadcast: 0	Data: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 
ISR - MB 3  OVERRUN: 0  LEN: 64 EXT: 1  EDL: 1 TS: 57991 ID: 1FFCF214 Buffer: 0 0 0 28 BE C7 7 1 2 3 4 5 6 7 8 9 A B C D E F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20 21 22 23 24 25 26 27 28 AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA 
ISR - MB 3  OVERRUN: 0  LEN: 64 EXT: 1  EDL: 1 TS: 12009 ID: 1FFCC014 Buffer: 0 0 0 28 BE C7 5 1 2 3 4 5 6 7 8 9 A B C D E F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20 21 22 23 24 25 26 27 28 AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA 
Node: 20	PacketID: 5	Broadcast: 1	Data: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 
ISR - MB 3  OVERRUN: 0  LEN: 64 EXT: 1  EDL: 1 TS: 13519 ID: 1FFCF214 Buffer: 0 0 0 28 BE C7 7 1 2 3 4 5 6 7 8 9 A B C D E F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20 21 22 23 24 25 26 27 28 AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA 
Node: 20	PacketID: 7	Broadcast: 0	Data: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 
ISR - MB 3  OVERRUN: 0  LEN: 64 EXT: 1  EDL: 1 TS: 33074 ID: 1FFCC014 Buffer: 0 0 0 28 BE C7 5 1 2 3 4 5 6 7 8 9 A B C D E F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20 21 22 23 24 25 26 27 28 AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA 
Node: 20	PacketID: 5	Broadcast: 1	Data: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 
ISR - MB 3  OVERRUN: 0  LEN: 64 EXT: 1  EDL: 1 TS: 34585 ID: 1FFCF214 Buffer: 0 0 0 28 BE C7 7 1 2 3 4 5 6 7 8 9 A B C D E F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20 21 22 23 24 25 26 27 28 AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA 
Node: 20	PacketID: 7	Broadcast: 0	Data: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 
ISR - MB 3  OVERRUN: 0  LEN: 64 EXT: 1  EDL: 1 TS: 54138 ID: 1FFCC014 Buffer: 0 0 0 28 BE C7 5 1 2 3 4 5 6 7 8 9 A B C D E F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20 21 22 23 24 25 26 27 28 AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA 
Node: 20	PacketID: 5	Broadcast: 1	Data: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 
ISR - MB 3  OVERRUN: 0  LEN: 64 EXT: 1  EDL: 1 TS: 55649 ID: 1FFCF214 Buffer: 0 0 0 28 BE C7 7 1 2 3 4 5 6 7 8 9 A B C D E F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20 21 22 23 24 25 26 27 28 AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA 
Node: 20	PacketID: 7	Broadcast: 0	Data: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 
ISR - MB 3  OVERRUN: 0  LEN: 64 EXT: 1  EDL: 1 TS: 9666 ID: 1FFCC014 Buffer: 0 0 0 28 BE C7 5 1 2 3 4 5 6 7 8 9 A B C D E F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20 21 22 23 24 25 26 27 28 AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA 
Node: 20	PacketID: 5	Broadcast: 1	Data: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 
ISR - MB 3  OVERRUN: 0  LEN: 64 EXT: 1  EDL: 1 TS: 11177 ID: 1FFCF214 Buffer: 0 0 0 28 BE C7 7 1 2 3 4 5 6 7 8 9 A B C D E F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20 21 22 23 24 25 26 27 28 AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA 
Node: 20	PacketID: 7	Broadcast: 0	Data: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 
ISR - MB 3  OVERRUN: 0  LEN: 64 EXT: 1  EDL: 1 TS: 30731 ID: 1FFCC014 Buffer: 0 0 0 28 BE C7 5 1 2 3 4 5 6 7 8 9 A B C D E F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20 21 22 23 24 25 26 27 28 AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA 
Node: 20	PacketID: 5	Broadcast: 1	Data: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 
ISR - MB 3  OVERRUN: 0  LEN: 64 EXT: 1  EDL: 1 TS: 32241 ID: 1FFCF214 Buffer: 0 0 0 28 BE C7 7 1 2 3 4 5 6 7 8 9 A B C D E F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20 21 22 23 24 25 26 27 28 AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA 
Node: 20	PacketID: 7	Broadcast: 0	Data: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
 
No communication via CAN3 Pins (13[rx] and 22[tx])

Hi tonton81 and all others in this Thread.
I am playing around with two teensy 4 boards and your FlexCAN_T4 library. I am using the MCP2562FD Transreceiver and sending Can Frames from one device to the other via CAN2.0 works fine on CAN1 and CAN2 :)
But somehow it does not work on CAN3 (not with the CAN20 nor with the CANFD class).
May I have to disconnect the onboard LED which is hardwired to Pin 13?
Im struggeing with that issue since a couple of days and i can't just see what im doing wrong, maybe you can give me a hint...

Cheers, Daniel

Code:
#include <FlexCAN_T4.h>

#define LEDPIN 16

FlexCAN_T4FD<CAN3, RX_SIZE_256, TX_SIZE_16> flexCan;

void printInfo(const CANFD_message_t& message, bool received = true, bool result = true)
{
  Serial.println("*******************************");
  Serial.println(received ? "* Frame Received" : "* Frame Send");  
  Serial.println(result ? "* Success" : "* Fail");  
  Serial.println("*");
  Serial.print("* Id: "); Serial.print(message.id, HEX); Serial.println();
  Serial.print("* Data: "); for(int i=0; i<message.len; i++){ Serial.print(message.buf[i], HEX); Serial.print(" "); } Serial.println();
  Serial.println("*");
  Serial.println("*******************************");
}

void frameReceivedHandler(const CANFD_message_t& message)
{
  printInfo(message);
}


#define OPTION 1

#if OPTION == 0
int txId = 0x123, rxId = 0x321;
#endif

#if OPTION == 1
int txId = 0x321, rxId = 0x123;
#endif


void setup(void) {  
  Serial.begin(9600);
  
  /************** Config LED **************/
  pinMode(LEDPIN, OUTPUT);    

  delay(500);
  
  /************ Initialize CAN ************/  
  flexCan.begin();
  CANFD_timings_t config;
  config.clock = CLK_24MHz;
  config.baudrate = 1000000;
  config.baudrateFD = 2000000;
  config.propdelay = 190;
  config.bus_length = 1;
  config.sample = 87.5;
  flexCan.setRegions(64);
  flexCan.setBaudRate(config);
  //flexCan.setBaudRate(500000);
}

void loop() {  

  static unsigned long timeout = millis();
  
  CANFD_message_t message;
  
  int result;
  static int ctr = 0;

    
  message.id = txId;
  message.len = 8;
  message.buf[0] = ctr;
  message.buf[1] = ctr;
  message.buf[2] = ctr;
  message.buf[3] = ctr;
  message.buf[4] = ctr;
  message.buf[5] = ctr;
  message.buf[6] = ctr;
  message.buf[7] = ctr;

  if(millis() - timeout >= 500)
  {
    timeout = millis();
      
    result = flexCan.write(message);
    printInfo(message, false, result);
      
    flexCan.mailboxStatus();
    
    /************ Blink LED ************/
    if(result)
    {
      digitalWrite(LEDPIN, (ctr++)%2);
    }
      
  }

  /************ Call event handlers ************/
  if(flexCan.read(message))frameReceivedHandler(message);
}
 
CAN FD is on CRX3 (pin 30) and CTX3 (pin 31) only.

You can't use pin 13 and 22 for CAN FD.
 
Skpang is correct. CAN3 pins (same ones for 2.0 and FD) are pads underneath the T4. The pads are very small so you need to be good at soldering tiny wires there or opt in for a breakout board, which couple were posted already in the forums.
 
@tonton81
Can you add to library some working example for Teensy 4.0, CAN 2.0B and SN65HVD230 receiver?
 
Code:
#include <FlexCAN_T4.h>
FlexCAN_T4<CAN2, RX_SIZE_256, TX_SIZE_16> Can0;

void setup(void) {
  Serial.begin(115200); delay(400);
  pinMode(6, OUTPUT); digitalWrite(6, LOW); // enable tranceiver
  Can0.begin();
  Can0.setBaudRate(1000000);
  Can0.setMaxMB(16); // up to 64 max for T4, not important in FIFO mode, unless you want to use additional mailboxes with FIFO
  Can0.enableFIFO();
  Can0.enableFIFOInterrupt();
  Can0.onReceive(canSniff);
  Can0.mailboxStatus();
}

void canSniff(const CAN_message_t &msg) {
  Serial.print("MB "); Serial.print(msg.mb);
  Serial.print("  OVERRUN: "); Serial.print(msg.flags.overrun);
  Serial.print("  LEN: "); Serial.print(msg.len);
  Serial.print(" EXT: "); Serial.print(msg.flags.extended);
  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();
}

void loop() {
  Can0.events();

  static uint32_t timeout = millis();
  if ( millis() - timeout > 20 ) { // send random frame every 20ms
    CAN_message_t msg;
    msg.id = random(0x1,0x7FE);
    for ( uint8_t i = 0; i < 8; i++ ) msg.buf[i] = i + 1;
    Can0.write(msg);
    timeout = millis();
  }

}
 
Back
Top