From fd2f83192ce3d645cada0da1eec2f1b62b513404 Mon Sep 17 00:00:00 2001 From: Italo Soares Date: Fri, 1 Oct 2021 18:22:49 -0300 Subject: [PATCH 1/2] Added new example --- examples/CANFilter-Interrupt/main.cpp | 59 ++++++++++++++++++++++++++ examples/CANFilter-Interrupt/readme.md | 29 +++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 examples/CANFilter-Interrupt/main.cpp create mode 100644 examples/CANFilter-Interrupt/readme.md diff --git a/examples/CANFilter-Interrupt/main.cpp b/examples/CANFilter-Interrupt/main.cpp new file mode 100644 index 0000000..3876cac --- /dev/null +++ b/examples/CANFilter-Interrupt/main.cpp @@ -0,0 +1,59 @@ +/* How the code works and tips: + +-When importing CAN.h it will automatically select which controller to be used depending on the board, (Internal SJA1000 for ESP, External MPU2515 for arduinos) + +-When a packet is received + The library will setup a interrupt based function called onReceive, everytime a packet is received it will be processed + by the controller (MPU2515 or SJA1000) outside the main program, where the package will be filtered, then, if the ID is + inside of what the filter allows (or there is no filter) it will call this function, where you can process your messages. + +-Filtering and masks are confusing. + this might be helpful to understand it https://www.microchip.com/forums/m456043.aspx + +Tips for better stability: + 1 - You should prefer using an interrupt based processing over loop-based to avoid buffer issues, Heavy or light bus errors. + (When buffer issues happens you will receive the same message, or anything at all until reboot) + + 2 - Filtering only the used ID's will massively reduce the amound of data to be processed. + + 3 - On ESP32 its possible to run the CAN in the second processor, allowing the first to be used to heavy tasks (check FREERTOS guides) +*/ + +#include //only needed for VSCODE/Platformio +#include + +void onReceive(int packetSize) //This will be called every time a new and filtered packet is received +{ //Only packages with the ID 0x12 will get here (defined by the filter) + + Serial.print("packet id 0x"); + Serial.print(CAN.packetId(), HEX);//For platformio you might need to add "monitor_flags = --raw" to platformio.ini (otherwise you will see garbage) + Serial.print(" data: "); + + while (CAN.available()) //read and print all the bytes available in the package + { + Serial.print((char)CAN.read(), HEX); + Serial.print(" "); + } + Serial.println(""); +} + +void setup() +{ + Serial.begin(115200); + while (!Serial) + ; //this will make sure the code wont start unless there is someone listening in the serial. Use only for debugging + if (!CAN.begin(500E3)) // try to start the CAN bus at 500 kbps + { + Serial.println("Starting CAN failed!"); + while (1) //if failed to start the program will hang + ; + } + Serial.println("Can initialized"); + CAN.filter(0x12, 0x1FFFFFFF); //initializa a CAN.filter(id, mask); + CAN.onReceive(onReceive); // register the receive callback +} + +void loop() +{ + // No CAN related code needs to run inside the loop: +} \ No newline at end of file diff --git a/examples/CANFilter-Interrupt/readme.md b/examples/CANFilter-Interrupt/readme.md new file mode 100644 index 0000000..3caa605 --- /dev/null +++ b/examples/CANFilter-Interrupt/readme.md @@ -0,0 +1,29 @@ +# Can filtering and interrupt based processing + +## Filter and mask setup. +this might be helpful to understand how filters and masks works https://www.microchip.com/forums/m456043.aspx +```arduino + CAN.filter(0x12, 0x1FFFFFFF); //initializa a CAN.filter(id, mask); +``` + + +## Interrupt setup +```arduino + CAN.onReceive(onReceive); // register the receive callback +``` + + +## How the code works and tips: + +- When importing CAN.h it will automatically select which controller to be used depending on the board, (Internal SJA1000 for ESP, External MPU2515 for arduino's) + +- When a packet is received The library will setup a interrupt based function called onReceive, everytime a packet is received it will be processed by the controller (MPU2515 or SJA1000) outside the main program, where the package will be filtered, then, if the ID is +inside of what the filter allows (or there is no filter) it will call this function, where you can process your messages. + +### Tips for better stability: +- You should prefer using an interrupt based processing over loop-based to avoid buffer issues, Heavy or light bus errors. +(When buffer issues happens you will receive the same message, or anything at all until reboot) + +- Filtering only the used ID's will massively reduce the amound of data to be processed. + +- On ESP32 its possible to run the CAN in the second processor, allowing the first to be used to heavy tasks (check FREERTOS guides) \ No newline at end of file From ab10b72ebcaf9665ab4cdfa6f29d54c6746b17ab Mon Sep 17 00:00:00 2001 From: Italo Soares Date: Fri, 1 Oct 2021 18:32:52 -0300 Subject: [PATCH 2/2] readme updated --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 5723c97..8b11d89 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ -# Arduino CAN +# Arduino and ESP32 CAN library [![Build Status](https://travis-ci.org/sandeepmistry/arduino-CAN.svg?branch=master)](https://travis-ci.org/sandeepmistry/arduino-CAN) -An Arduino library for sending and receiving data using CAN bus. +An library for sending and receiving data using CAN bus, forked from sandeepmistry/arduino-CAN with some improvements on documentation. ## Compatible Hardware