Can0.setMBFilter(REJECT_ALL);
Can0.setMBFilter(MB1, id1); // 29 bit id
Can0.setMBFilter(MB2, id2); // 11 bit id
Can0.setMBFilter(MB3, id3); // 11 bit id
Can0.enableMBInterrupt(MB1);
Can0.enableMBInterrupt(MB2);
Can0.enableMBInterrupt(MB3);
Can0.onReceive(MB1, callback1);
Can0.onReceive(MB2, callback2);
Can0.onReceive(MB3, callback3);
Can0.setMB(MB1, RX, EXT);
#include <FlexCAN_T4.h>
#define DRIVER1_ADDRESS 5
/* -------------------------------------------------------------------------- */
/* Burst send many CAN messages to master. Add seq. number to messages */
/* -------------------------------------------------------------------------- */
FlexCAN_T4<CAN3, RX_SIZE_256, TX_SIZE_16> can;
CAN_message_t msg;
const int measuringTimeMillis = 1e3;
elapsedMillis elapsedTime;
unsigned long sentInTotal = 0;
unsigned long sentInBatch = 0;
uint16_t bufContents = 0;
void setup() {
can.begin();
can.setBaudRate(1e6);
Serial.begin(9600);
Serial.println("Started. Waiting for empty Serial input");
}
void loop() {
for ( int i = 0; i < 5000; i++) {
sendMessage();
}
delay(5000);
}
void sendMessage() {
uint16_t index = 0x6040;
uint32_t data = 0x80;
msg.id = DRIVER1_ADDRESS;
msg.len = 8;
msg.seq = 1;
msg.buf[0] = (bufContents >> 8) & 0xFF; // Index bit 1
msg.buf[1] = (bufContents >> 0) & 0xFF; // Index bit 2
// Serial.print((bufContents >> 8) & 0xFF, BIN), Serial.println((bufContents >> 0) & 0xFF, BIN),
msg.buf[2] = 1;
msg.buf[3] = 4;
msg.buf[4] = 1;
msg.buf[5] = 5;
msg.buf[6] = 2;
msg.buf[7] = 6;
can.write(msg);
Serial.println(bufContents);
can.mailboxStatus();
bufContents++;
}
#include <FlexCAN_T4.h>
FlexCAN_T4<CAN1, RX_SIZE_1024, TX_SIZE_1024> can;
CAN_message_t a_msg;
unsigned long counterGood = 0;
unsigned long counterBad = 0;
// Implemented to check integrity;
int currentIndex = 0;
unsigned int count = 0;
void setup(void) {
can.begin();
can.setBaudRate(1e6);
Serial.begin(9600);
can.mailboxStatus();
Serial.println("Starting receiver");
}
elapsedMicros interarrival = 0;
void loop() {
elapsedMicros waiting = 0;
if(can.read(a_msg)) {
Serial.print("interarrival : "); Serial.println(interarrival);
interarrival = 0;
uint16_t num = (a_msg.buf[0] << 8) + (a_msg.buf[1]);
Serial.print(currentIndex); Serial.print(" : receivedNum="); Serial.println(num);
if(num == (currentIndex+1)) {
counterGood++;
} else {
counterBad++;
}
count++;
currentIndex = num;
Serial.print("processingTime : "); Serial.println(waiting);
}
// Print statistics
if(Serial.available()) { // Only print if we send something (just send nothing (empty))
Serial.read(); // Consume character
Serial.print("CounterGood: "); Serial.println(counterGood);
Serial.print("CounterBad: "); Serial.println(counterBad);
Serial.print("Counter: "); Serial.println(count);
}
}
for ( int i = 0; i < 30; i++) {
CAN_message_t msg;
msg.id = 0x555;
msg.seq = 1;
msg.buf[0] = 8;
msg.buf[1] = 8;
msg.buf[2] = 8;
msg.buf[3] = 8;
msg.buf[4] = 8;
msg.buf[5] = 8;
msg.buf[6] = 8;
msg.buf[7] = 8;
if ( Can0.write(msg) ) {
Serial.println("QUEUED!");
}
else {
Serial.println("QUEUE LIMIT REACHED");
int success = 0;
while ( !success ) {
success = Can0.write(msg);
}
Serial.println("QUEUE SUCCEEDED!");
}
}
delay(5000);
}
}
QUEUED!
QUEUED!
QUEUED!
QUEUED!
QUEUED!
QUEUED!
QUEUED!
QUEUED!
QUEUED!
QUEUED!
QUEUED!
QUEUED!
QUEUED!
QUEUED!
QUEUED!
QUEUED!
QUEUED!
QUEUE LIMIT REACHED
QUEUE SUCCEEDED!
QUEUE LIMIT REACHED
QUEUE SUCCEEDED!
QUEUE LIMIT REACHED
QUEUE SUCCEEDED!
QUEUE LIMIT REACHED
QUEUE SUCCEEDED!
QUEUE LIMIT REACHED
QUEUE SUCCEEDED!
QUEUE LIMIT REACHED
QUEUE SUCCEEDED!
QUEUE LIMIT REACHED
QUEUE SUCCEEDED!
QUEUE LIMIT REACHED
QUEUE SUCCEEDED!
QUEUE LIMIT REACHED
QUEUE SUCCEEDED!
QUEUE LIMIT REACHED
QUEUE SUCCEEDED!
QUEUE LIMIT REACHED
QUEUE SUCCEEDED!
QUEUED!
QUEUE LIMIT REACHED
QUEUE SUCCEEDED!
#include <FlexCAN_T4.h>
FlexCAN_T4FD<CAN3, RX_SIZE_256, TX_SIZE_16> can1;
void setup(void) {
can1.begin();
Serial.begin(9600);
}
void loop() {
CANFD_message_t msg;
CANFD_message_t rx_msg;
// Write Data
msg.id = 0x11;
msg.len = 16;
msg.flags.extended = 0;
msg.flags.overrun = 0;
msg.flags.reserved = 0;
msg.buf[0] = 0;
msg.buf[1] = 0;
msg.buf[2] = 0;
msg.buf[3] = 0;
msg.buf[4] = 0;
msg.buf[5] = 0;
msg.buf[6] = 0;
msg.buf[7] = 0;
msg.buf[8] = 0;
msg.buf[9] = 0;
msg.buf[10] = 0;
msg.buf[11] = 0;
msg.buf[12] = 0;
msg.buf[13] = 0;
msg.buf[14] = 0;
msg.buf[15] = 0;
can1.write(msg);
if (can1.read(rx_msg)) {
Serial.print("CAN1 ");
Serial.print("MB: "); Serial.print(rx_msg.mb);
Serial.print(" ID: 0x"); Serial.print(rx_msg.id, HEX );
Serial.print(" EXT: "); Serial.print(rx_msg.flags.extended );
Serial.print(" LEN: "); Serial.print(rx_msg.len);
Serial.print(" DATA: ");
for ( uint8_t i = 0; i < 16; i++ ) {
Serial.print(rx_msg.buf[i]); Serial.print(" ");
}
Serial.print(" TS: "); Serial.println(rx_msg.timestamp);
}
}
#include <FlexCAN_T4.h>
FlexCAN_T4<CAN3, RX_SIZE_256, TX_SIZE_16> can3;
IntervalTimer timer;
int SILENT = 9;
uint8_t a=0;
void setup(void) {
pinMode(SILENT,OUTPUT);
digitalWrite(SILENT,LOW);
can3.begin();
can3.setBaudRate(500000); // Data rate 500kbps
can3.setMaxMB(16);
can3.enableFIFO();
can3.enableFIFOInterrupt();
can3.onReceive(canSniff);
can3.mailboxStatus();
timer.begin(sendframe, 100000); // Send frame every 500ms
}
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 sendframe()
{
CAN_message_t msg;
msg.id = 0x7df;
msg.buf[0] = a++; // Pot 1 value
msg.buf[1] = 0;
msg.buf[2] = 0;
msg.buf[3] = 0;
msg.buf[4] = 0; // SW1 value
msg.buf[5] = 0;
msg.buf[6] = 0;
msg.buf[7] = 0;
msg.len = 8;
msg.seq = 1;
can3.write(MB15, msg);
}
void loop() {
can3.events();
}
#include <FlexCAN_T4.h>
FlexCAN_T4FD<CAN3, RX_SIZE_256, TX_SIZE_16> can3;
IntervalTimer timer;
int SILENT = 9;
uint8_t a=0;
void setup(void) {
pinMode(SILENT,OUTPUT);
digitalWrite(SILENT,LOW);
can3.begin();
CANFD_timings_t config;
config.clock = CLK_24MHz;
config.baudrate = 500000;
config.baudrateFD = 500000;
config.propdelay = 190;
config.bus_length = 1;
config.sample = 70;
can3.setBaudRate(config);
can3.setMaxMB(16);
can3.enableFIFO();
can3.enableFIFOInterrupt();
can3.onReceive(canSniff);
can3.mailboxStatus();
timer.begin(sendframe, 100000); // Send frame every 500ms
}
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 sendframe()
{
CANFD_message_t msg;
msg.id = 0x7df;
msg.buf[0] = a++; // Pot 1 value
msg.buf[1] = 0;
msg.buf[2] = 0;
msg.buf[3] = 0;
msg.buf[4] = 0; // SW1 value
msg.buf[5] = 0;
msg.buf[6] = 0;
msg.buf[7] = 0;
msg.len = 8;
msg.seq = 1;
can3.write(MB15, msg);
}
void loop() {
can3.events();
}
#include <FlexCAN_T4.h>
FlexCAN_T4FD<CAN3, RX_SIZE_256, TX_SIZE_16> FD;
IntervalTimer timer;
int SILENT = 9;
uint8_t a=0;
void setup(void) {
pinMode(SILENT,OUTPUT);
digitalWrite(SILENT,LOW);
FD.begin();
CANFD_timings_t config;
config.clock = CLK_24MHz;
config.baudrate = 500000; // 500kbps Nominal data rate
config.baudrateFD = 2000000; // 2000kpbs Data rate
config.propdelay = 190;
config.bus_length = 1;
config.sample = 75;
FD.setRegions(64);
FD.setBaudRate(config);
FD.setMBFilter(ACCEPT_ALL);
FD.setMBFilter(MB13, 0x1);
FD.setMBFilter(MB12, 0x1, 0x3);
FD.setMBFilterRange(MB8, 0x1, 0x04);
FD.enableMBInterrupt(MB8);
FD.enableMBInterrupt(MB12);
FD.enableMBInterrupt(MB13);
FD.enhanceFilter(MB8);
FD.enhanceFilter(MB10);
FD.distribute();
FD.mailboxStatus();
timer.begin(sendframe, 100000); // Send frame every 500ms
}
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 sendframe()
{
CANFD_message_t msg;
msg.len = 64; // Set frame length to 64 bytes
msg.id = 0x321;
msg.seq = 1;
msg.buf[0] = a++; // Pot 1 value
msg.buf[1] = 0;
msg.buf[2] = 0;
msg.buf[3] = 04;
msg.buf[4] = 0; // SW1 value
msg.buf[5] = 0;
msg.buf[6] = 0;
msg.buf[7] = 0;
FD.write( msg);
}
void loop() {
CANFD_message_t msg;
FD.events();
if (FD.readMB(msg))
{
Serial.print("MB: "); Serial.print(msg.mb);
Serial.print(" OVERRUN: "); Serial.print(msg.flags.overrun);
Serial.print(" ID: 0x"); Serial.print(msg.id, HEX );
Serial.print(" EXT: "); Serial.print(msg.flags.extended );
Serial.print(" LEN: "); Serial.print(msg.len);
Serial.print(" BRS: "); Serial.print(msg.brs);
Serial.print(" DATA: ");
for ( uint8_t i = 0; i <msg.len ; i++ ) {
Serial.print(msg.buf[i]); Serial.print(" ");
}
Serial.print(" TS: "); Serial.println(msg.timestamp);
}
}