Initial commit

This is just the 2018 code configured for PlatformIO
This commit is contained in:
jvblanck
2021-06-09 12:10:12 +02:00
commit 4faddf9248
77 changed files with 10334 additions and 0 deletions

View File

@ -0,0 +1,87 @@
// Arduino Due - CAN Sample 1
// Brief CAN example for Arduino Due
// Test the transmission from CAN0 Mailbox 0 to CAN1 Mailbox 0
// By Thibaut Viard/Wilfredo Molina/Collin Kidder 2013
// Required libraries
#include "variant.h"
#include <due_can.h>
#define TEST1_CAN_COMM_MB_IDX 0
#define TEST1_CAN_TRANSFER_ID 0x07
#define TEST1_CAN0_TX_PRIO 15
#define CAN_MSG_DUMMY_DATA 0x55AAEE22
// CAN frame max data length
#define MAX_CAN_FRAME_DATA_LEN 8
// Message variable to be send
uint32_t CAN_MSG_1 = 0;
//Leave defined if you use native port, comment if using programming port
#define Serial SerialUSB
void setup()
{
CAN_FRAME output;
// start serial port at 9600 bps:
Serial.begin(9600);
Serial.println("Type CAN message to send");
while (Serial.available() == 0);
}
void loop(){
CAN_FRAME output;
while (Serial.available() > 0) {
CAN_MSG_1 = Serial.parseInt();
if (Serial.read() == '\n') {
Serial.print("Sent value= ");
Serial.println(CAN_MSG_1);
}
}
// Initialize CAN0 and CAN1, baudrate is 250kb/s
Can0.begin(CAN_BPS_250K);
Can1.begin(CAN_BPS_250K);
//The default is to allow nothing through if nothing is specified
//only allow this one frame ID through.
Can1.watchFor(TEST1_CAN_TRANSFER_ID);
// Prepare transmit ID, data and data length in CAN0 mailbox 0
output.id = TEST1_CAN_TRANSFER_ID;
output.length = MAX_CAN_FRAME_DATA_LEN;
//Set first four bytes (32 bits) all at once
output.data.low = CAN_MSG_1;
//Set last four bytes (32 bits) all at once
output.data.high = CAN_MSG_DUMMY_DATA;
//Send out the frame on whichever mailbox is free or queue it for
//sending when there is an opening.
CAN.sendFrame(output);
// Wait for second canbus port to receive the frame
while (Can1.available() == 0) {
}
// Read the received data from CAN1 mailbox 0
CAN_FRAME incoming;
Can1.read(incoming);
Serial.print("CAN message received= ");
Serial.print(incoming.data.low, HEX);
Serial.print(incoming.data.high, HEX);
// Disable CAN0 Controller
Can0.disable();
// Disable CAN1 Controller
Can1.disable();
Serial.print("\nEnd of test");
while (1) {
}
}

View File

@ -0,0 +1,108 @@
// Arduino Due - CANbus Library - Extended Frames with Ping/Pong sending
// Ping/Pong torture test with extended frames.
// This example sets up a receive and transmit mailbox on both canbus devices.
// First CAN0 sends to CAN1. When CAN1 receives it sends to CAN0. PING/PONGs forever
// and as quickly as possible - This will saturate the bus so don't have anything important connected.
// By Thibaut Viard/Wilfredo Molina/Collin Kidder 2014
// Required libraries
#include "variant.h"
#include <due_can.h>
#define TEST1_CAN_TRANSFER_ID 0x11AE756A //random 29 bits
#define TEST1_CAN0_TX_PRIO 15
#define CAN_MSG_DUMMY_DATA 0x11BFFA4E
// CAN frame max data length
#define MAX_CAN_FRAME_DATA_LEN 8
uint32_t sentFrames, receivedFrames;
//Leave this defined if you use the native port or comment it out if you use the programming port
#define Serial SerialUSB
CAN_FRAME frame1, frame2, incoming;
void setup() {
// start serial port at 115200 bps:
Serial.begin(115200);
// Verify CAN0 and CAN1 initialization, baudrate is 1Mb/s:
if (Can0.begin(CAN_BPS_1000K) &&
Can1.begin(CAN_BPS_1000K)) {
}
else {
Serial.println("CAN initialization (sync) ERROR");
}
//Initialize the definitions for the frames we'll be sending.
//This can be done here because the frame never changes
frame1.id = TEST1_CAN_TRANSFER_ID;
frame1.length = MAX_CAN_FRAME_DATA_LEN;
//Below we set the 8 data bytes in 32 bit (4 byte) chunks
//Bytes can be set individually with frame1.data.bytes[which] = something
frame1.data.low = 0x20103040;
frame1.data.high = CAN_MSG_DUMMY_DATA;
//We are using extended frames so mark that here. Otherwise it will just use
//the first 11 bits of the ID set
frame1.extended = 1;
frame2.id = TEST1_CAN_TRANSFER_ID + 0x200;
frame2.length = MAX_CAN_FRAME_DATA_LEN;
frame2.data.low = 0xB8C8A8E8;
frame2.data.high = 0x01020304;
frame2.extended = 1;
//Both of these lines create a filter on the corresponding CAN device that allows
//just the one ID we're interested in to get through.
//The syntax is (mailbox #, ID, mask, extended)
//You can also leave off the mailbox number: (ID, mask, extended)
Can1.watchFor(TEST1_CAN_TRANSFER_ID + 0x200);
Can0.watchFor(TEST1_CAN_TRANSFER_ID);
test_1();
}
// Test rapid fire ping/pong of extended frames
static void test_1(void)
{
CAN_FRAME inFrame;
uint32_t counter = 0;
// Send out the first frame
Can0.sendFrame(frame2);
sentFrames++;
while (1==1) {
if (Can0.available() > 0) {
Can0.read(incoming);
Can0.sendFrame(frame2);
delayMicroseconds(100);
sentFrames++;
receivedFrames++;
counter++;
}
if (Can1.available() > 0) {
Can1.read(incoming);
Can1.sendFrame(frame1);
delayMicroseconds(100);
sentFrames++;
receivedFrames++;
counter++;
}
if (counter > 5000) {
counter = 0;
Serial.print("S: ");
Serial.print(sentFrames);
Serial.print(" R: ");
Serial.println(receivedFrames);
}
}
}
// can_example application entry point
void loop()
{
}

View File

@ -0,0 +1,55 @@
//Reads all traffic on CAN0 and forwards it to CAN1 (and in the reverse direction) but modifies some frames first.
// Required libraries
#include "variant.h"
#include <due_can.h>
//Leave defined if you use native port, comment if using programming port
#define Serial SerialUSB
void setup()
{
Serial.begin(115200);
// Initialize CAN0 and CAN1, Set the proper baud rates here
Can0.begin(CAN_BPS_250K);
Can1.begin(CAN_BPS_250K);
Can0.watchFor();
}
void sendData()
{
CAN_FRAME outgoing;
outgoing.id = 0x400;
outgoing.extended = false;
outgoing.priority = 4; //0-15 lower is higher priority
outgoing.data.s0 = 0xFEED;
outgoing.data.byte[2] = 0xDD;
outgoing.data.byte[3] = 0x55;
outgoing.data.high = 0xDEADBEEF;
Can0.sendFrame(outgoing);
}
void loop(){
CAN_FRAME incoming;
static unsigned long lastTime = 0;
if (Can0.available() > 0) {
Can0.read(incoming);
Can1.sendFrame(incoming);
}
if (Can1.available() > 0) {
Can1.read(incoming);
Can0.sendFrame(incoming);
}
if ((millis() - lastTime) > 1000)
{
lastTime = millis();
sendData();
}
}

View File

@ -0,0 +1,93 @@
// Arduino Due - Displays all traffic found on either canbus port
//Modified version of the SnooperCallback sketch. This one illustrates
//how to use a class object to receive callbacks. It's a little different
//from the non-OO approach.
// By Thibaut Viard/Wilfredo Molina/Collin Kidder 2013-2015
// Required libraries
#include "variant.h"
#include <due_can.h>
//Leave defined if you use native port, comment if using programming port
//This sketch could provide a lot of traffic so it might be best to use the
//native port
#define Serial SerialUSB
class ExampleClass : public CANListener //CANListener provides an interface to get callbacks on this class
{
public:
void printFrame(CAN_FRAME *frame, int mailbox);
void gotFrame(CAN_FRAME *frame, int mailbox); //overrides the parent version so we can actually do something
};
//Prints out the most useful information about the incoming frame.
void ExampleClass::printFrame(CAN_FRAME *frame, int mailbox)
{
Serial.print("MB: ");
if (mailbox > -1) Serial.print(mailbox);
else Serial.print("???");
Serial.print(" ID: 0x");
Serial.print(frame->id, HEX);
Serial.print(" Len: ");
Serial.print(frame->length);
Serial.print(" Data: 0x");
for (int count = 0; count < frame->length; count++) {
Serial.print(frame->data.bytes[count], HEX);
Serial.print(" ");
}
Serial.print("\r\n");
}
//Classes register just one method that receives all callbacks. If a frame didn't match any specific mailbox
//callback but the general callback was registered then the mailbox will be set as -1. Otherwise it is the mailbox
//with the matching filter for this frame.
void ExampleClass::gotFrame(CAN_FRAME* frame, int mailbox)
{
this->printFrame(frame, mailbox);
}
ExampleClass myClass; //initialize the class global so the reference to it can be picked up anywhere
void setup()
{
Serial.begin(115200);
// Initialize CAN0, Set the proper baud rates here
// Pass the proper pin for enabling your transceiver or 255 if you don't need an enable pin to be toggled.
Can0.begin(CAN_BPS_500K, 50);
//By default there are 7 RX mailboxes for each device
//extended
//syntax is mailbox, ID, mask, extended
Can0.setRXFilter(0, 0x2FF00, 0x1FF2FF00, true);
Can0.setRXFilter(1, 0x1F0000, 0x1F1F0000, true);
Can0.setRXFilter(2, 0, 0, true); //catch all mailbox
//standard
Can0.setRXFilter(3, 0x40F, 0x7FF, false);
Can0.setRXFilter(4, 0x310, 0x7F0, false);
Can0.setRXFilter(5, 0x200, 0x700, false);
Can0.setRXFilter(6, 0, 0, false); //catch all mailbox
Can0.attachObj(&myClass);
//let library know we want to receive callbacks for the following mailboxes
//once we attach above the canbus object knows about us. The actual functions
//to attach are members of CANListener so use your class name
myClass.attachMBHandler(0);
myClass.attachMBHandler(1);
myClass.attachMBHandler(3);
myClass.attachMBHandler(4);
myClass.attachMBHandler(5);
//set to get a callback for any other mailboxes not already covered above
myClass.attachGeneralHandler();
}
void loop(){ //All work is done via callback as frames come in - no need to poll for them. SO, just print periodic message to show we're alive
delay(5000);
Serial.println("Still listening");
}

View File

@ -0,0 +1,101 @@
/*
Arduino Due - Displays frames that fall between a range of addresses.
Modified from some of the other examples - notably CAN_SnooperCallBack
The difference here is that we only allow a range of ids. This demonstrates
the new watchForRange function as well as per-mailbox and general callback
functionality
By Thibaut Viard/Wilfredo Molina/Collin Kidder 2013-2014
*/
// Required libraries
#include "variant.h"
#include <due_can.h>
//Leave defined if you use native port, comment if using programming port
//This sketch could provide a lot of traffic so it might be best to use the
//native port
#define Serial SerialUSB
void printFrame(CAN_FRAME *frame, int filter) {
Serial.print("Fltr: ");
if (filter > -1) Serial.print(filter);
else Serial.print("???");
Serial.print(" ID: 0x");
Serial.print(frame->id, HEX);
Serial.print(" Len: ");
Serial.print(frame->length);
Serial.print(" Data: 0x");
for (int count = 0; count < frame->length; count++) {
Serial.print(frame->data.bytes[count], HEX);
Serial.print(" ");
}
Serial.print("\r\n");
}
void gotFrameMB0(CAN_FRAME *frame)
{
printFrame(frame, 0);
}
void gotFrameMB1(CAN_FRAME *frame)
{
printFrame(frame, 1);
}
void gotFrameMB3(CAN_FRAME *frame)
{
printFrame(frame, 3);
}
void gotFrameMB4(CAN_FRAME *frame)
{
printFrame(frame, 4);
}
void gotFrameMB5(CAN_FRAME *frame)
{
printFrame(frame, 5);
}
void gotFrame(CAN_FRAME *frame)
{
printFrame(frame, -1);
}
void setup()
{
Serial.begin(115200);
// Initialize CAN0, Set the proper baud rates here
Can0.init(CAN_BPS_250K);
//By default there are 7 RX mailboxes for each device
//extended
//syntax is mailbox, ID, mask, extended
Can0.watchForRange(0x10000000, 0x10004000);
Can0.watchForRange(0x10050000, 0x100500FF);
Can0.setRXFilter(0, 0, true); //catch all mailbox
//standard
Can0.watchForRange(0x400, 0x470);
Can0.watchForRange(0x500, 0x5FF);
Can0.watchForRange(0x200, 0x220);
Can0.setRXFilter(0, 0, false); //catch all mailbox
//now register all of the callback functions.
Can0.attachCANInterrupt(0, gotFrameMB0);
Can0.attachCANInterrupt(1, gotFrameMB1);
Can0.attachCANInterrupt(3, gotFrameMB3);
Can0.attachCANInterrupt(4, gotFrameMB4);
Can0.attachCANInterrupt(5, gotFrameMB5);
//this function will get a callback for any mailbox that doesn't have a registered callback from above -> 2 and 6
Can0.attachCANInterrupt(gotFrame);
}
void loop(){ //note the empty loop here. All work is done via callback as frames come in - no need to poll for them
}

View File

@ -0,0 +1,97 @@
// Arduino Due - Displays all traffic found on either canbus port
//Modified from the more generic TrafficSniffer sketch to instead use
//callback functions to receive the frames. Illustrates how to use
//the per-mailbox and general callback functionality
// By Thibaut Viard/Wilfredo Molina/Collin Kidder 2013-2014
// Required libraries
#include "variant.h"
#include <due_can.h>
//Leave defined if you use native port, comment if using programming port
//This sketch could provide a lot of traffic so it might be best to use the
//native port
#define Serial SerialUSB
void printFrame(CAN_FRAME *frame, int filter) {
Serial.print("Fltr: ");
if (filter > -1) Serial.print(filter);
else Serial.print("???");
Serial.print(" ID: 0x");
Serial.print(frame->id, HEX);
Serial.print(" Len: ");
Serial.print(frame->length);
Serial.print(" Data: 0x");
for (int count = 0; count < frame->length; count++) {
Serial.print(frame->data.bytes[count], HEX);
Serial.print(" ");
}
Serial.print("\r\n");
}
void gotFrameMB0(CAN_FRAME *frame)
{
printFrame(frame, 0);
}
void gotFrameMB1(CAN_FRAME *frame)
{
printFrame(frame, 1);
}
void gotFrameMB3(CAN_FRAME *frame)
{
printFrame(frame, 3);
}
void gotFrameMB4(CAN_FRAME *frame)
{
printFrame(frame, 4);
}
void gotFrameMB5(CAN_FRAME *frame)
{
printFrame(frame, 5);
}
void gotFrame(CAN_FRAME *frame)
{
printFrame(frame, -1);
}
void setup()
{
Serial.begin(115200);
// Initialize CAN0, Set the proper baud rates here
Can0.begin(CAN_BPS_250K);
//By default there are 7 RX mailboxes for each device
//extended
//syntax is mailbox, ID, mask, extended
Can0.setRXFilter(0, 0x2FF00, 0x1FF2FF00, true);
Can0.setRXFilter(1, 0x1F0000, 0x1F1F0000, true);
Can0.setRXFilter(2, 0, 0, true); //catch all mailbox
//standard
Can0.setRXFilter(3, 0x40F, 0x7FF, false);
Can0.setRXFilter(4, 0x310, 0x7F0, false);
Can0.watchFor(0x200, 0x700); //used in place of above syntax
Can0.setRXFilter(0, 0, false); //catch all mailbox - no mailbox ID specified
//now register all of the callback functions.
Can0.setCallback(0, gotFrameMB0);
Can0.setCallback(1, gotFrameMB1);
Can0.setCallback(3, gotFrameMB3);
Can0.setCallback(4, gotFrameMB4);
Can0.setCallback(5, gotFrameMB5);
//this function will get a callback for any mailbox that doesn't have a registered callback from above -> 2 and 6
Can0.setGeneralCallback(gotFrame);
}
void loop(){ //note the empty loop here. All work is done via callback as frames come in - no need to poll for them
}

View File

@ -0,0 +1,63 @@
//Reads all traffic on CAN0 and forwards it to CAN1 (and in the reverse direction) but modifies some frames first.
// Required libraries
#include "variant.h"
#include <due_can.h>
//Leave defined if you use native port, comment if using programming port
#define Serial SerialUSB
void setup()
{
Serial.begin(115200);
// Initialize CAN0 and CAN1, Set the proper baud rates here
Can0.begin(CAN_BPS_500K);
Can1.begin(CAN_BPS_250K);
//By default there are 7 mailboxes for each device that are RX boxes
//This sets each mailbox to have an open filter that will accept extended
//or standard frames
int filter;
//extended
for (filter = 0; filter < 3; filter++) {
Can0.setRXFilter(filter, 0, 0, true);
Can1.setRXFilter(filter, 0, 0, true);
}
//standard
//for (int filter = 3; filter < 7; filter++) {
//Can0.setRXFilter(filter, 0, 0, false);
//Can1.setRXFilter(filter, 0, 0, false);
//}
}
void printFrame(CAN_FRAME &frame) {
Serial.print("ID: 0x");
Serial.print(frame.id, HEX);
Serial.print(" Len: ");
Serial.print(frame.length);
Serial.print(" Data: 0x");
for (int count = 0; count < frame.length; count++) {
Serial.print(frame.data.bytes[count], HEX);
Serial.print(" ");
}
Serial.print("\r\n");
}
void loop(){
CAN_FRAME incoming;
if (Can0.available() > 0) {
Can0.read(incoming);
Can1.sendFrame(incoming);
//printFrame(incoming); //uncomment line to print frames that are going out
}
if (Can1.available() > 0) {
Can1.read(incoming);
Can0.sendFrame(incoming);
//printFrame(incoming);
}
}

View File

@ -0,0 +1,65 @@
// Arduino Due - Displays all traffic found on either canbus port
// By Thibaut Viard/Wilfredo Molina/Collin Kidder 2013-2014
// Required libraries
#include "variant.h"
#include <due_can.h>
//Leave defined if you use native port, comment if using programming port
//This sketch could provide a lot of traffic so it might be best to use the
//native port
#define Serial SerialUSB
void setup()
{
Serial.begin(115200);
// Initialize CAN0 and CAN1, Set the proper baud rates here
Can0.begin(CAN_BPS_250K);
Can1.begin(CAN_BPS_250K);
//By default there are 7 mailboxes for each device that are RX boxes
//This sets each mailbox to have an open filter that will accept extended
//or standard frames
int filter;
//extended
for (filter = 0; filter < 3; filter++) {
Can0.setRXFilter(filter, 0, 0, true);
Can1.setRXFilter(filter, 0, 0, true);
}
//standard
for (int filter = 3; filter < 7; filter++) {
Can0.setRXFilter(filter, 0, 0, false);
Can1.setRXFilter(filter, 0, 0, false);
}
}
void printFrame(CAN_FRAME &frame) {
Serial.print("ID: 0x");
Serial.print(frame.id, HEX);
Serial.print(" Len: ");
Serial.print(frame.length);
Serial.print(" Data: 0x");
for (int count = 0; count < frame.length; count++) {
Serial.print(frame.data.bytes[count], HEX);
Serial.print(" ");
}
Serial.print("\r\n");
}
void loop(){
CAN_FRAME incoming;
if (Can0.available() > 0) {
Can0.read(incoming);
printFrame(incoming);
}
if (Can1.available() > 0) {
Can1.read(incoming);
printFrame(incoming);
}
}