107-systems / 107-Arduino-MCP2515

Arduino library for controlling the MCP2515 in order to receive/transmit CAN frames.
https://107-systems.org
MIT License
88 stars 14 forks source link

added 12mhz and CANopen support #84

Closed munzili closed 1 year ago

munzili commented 1 year ago

This pull requests adds

munzili commented 1 year ago

Baudrates calculation was done with: https://www.kvaser.com/support/calculators/bit-timing-calculator/

aentinger commented 1 year ago

Hi @munzili :coffee: :wave:

Thank you for your PR. However, I'm not entirely happy with it, i.e. You've computed different values for baud-rates that are already known to be working, i.e.

Before:

static CanBitRateConfig constexpr BitRate_250kBPS_16MHz  = {0x41, 0xF1, 0x85};

After:

static CanBitRateConfig constexpr BitRate_250kBPS_16MHz  = {0x01, 0xb5, 0x01};

That gives me little confidence in all the other values. Also I frankly don't have the time to manually check all the baud rates. I'm open to accept part of your changes but I'm not going to throw away baud-rates that I happen to know work - unless you take on the work of verifying all those baud rates on real hardware.

Since 12 MHz are new and you seem to want that, why not focus on that first?

munzili commented 1 year ago

Well, actually i need 12mhz support AND different baudrates.

if you wish, i change the PR to only support the new baudrates and the 12mhz chip and leave the old values. I tested 12mhz with 20kbps.

The new values should be conform with the CANopen standard based on this document https://www.can-cia.org/fileadmin/resources/documents/proceedings/2003_koppe.pdf by setting the baudrates and the sample point to the values defined in the document

aentinger commented 1 year ago

if you wish, i change the PR to only support the new baudrates and the 12mhz chip and leave the old values. I tested 12mhz with 20kbps.

That would be an acceptable proposition.

aentinger commented 1 year ago

Btw. controller type in your online calculator seems to only support MCP2510. Not sure on the difference though. Here seems to be another tool: https://intrepidcs.com/products/free-tools/mb-time-calculator/ . Anything you can do to increase my confidence in this change would be appreciated.

munzili commented 1 year ago

https://ww1.microchip.com/downloads/en/Appnotes/00872a.pdf

the MCP2515 is based on MCP2510 but CNF flags are equal except CNF3 7bit but this bit was not setted before too

aentinger commented 1 year ago

The MCP2515 is based on MCP2510 but CNF flags are equal except CNF3 7bit but this bit was not setted before too

Works for me :+1: . Only the changes for 12 MHz then plus your missing baud rates.

generationmake commented 1 year ago

Hi. setting the sample point is a nice issue.

I think with the previous settings we have a sample point of 0.75. In some settings a different sample point might be useful. So I highly support this topic. But maybe we find a different approach to set the sample point. Unfortunately I havent seen any arduino can driver which supports this issue in a nice way.

aentinger commented 1 year ago

So you'd offer to do some testing with these value @generationmake ?

Relevant for our applications are 250k/500k/1MBit/s at 16 MHz.

github-actions[bot] commented 1 year ago

Memory usage change @ 5c0f1161dc8c97c1660ed4369dc8c11778a87045

Board flash % RAM for global variables %
arduino:mbed_edge:edge_control :small_red_triangle: +64 - +64 +0.01 - +0.01 0 - 0 0.0 - 0.0
arduino:mbed_nano:nano33ble :small_red_triangle: +64 - +64 +0.01 - +0.01 0 - 0 0.0 - 0.0
arduino:mbed_nano:nanorp2040connect :small_red_triangle: +66 - +66 0.0 - 0.0 0 - 0 0.0 - 0.0
arduino:samd:mkr1000 :small_red_triangle: +64 - +68 +0.02 - +0.03 0 - 0 0.0 - 0.0
arduino:samd:mkrfox1200 :small_red_triangle: +64 - +68 +0.02 - +0.03 0 - 0 0.0 - 0.0
arduino:samd:mkrgsm1400 :small_red_triangle: +64 - +68 +0.02 - +0.03 0 - 0 0.0 - 0.0
arduino:samd:mkrnb1500 :small_red_triangle: +64 - +68 +0.02 - +0.03 0 - 0 0.0 - 0.0
arduino:samd:mkrvidor4000 :small_red_triangle: +64 - +68 +0.02 - +0.03 0 - 0 0.0 - 0.0
arduino:samd:mkrwan1300 :small_red_triangle: +64 - +68 +0.02 - +0.03 0 - 0 0.0 - 0.0
arduino:samd:mkrwan1310 :small_red_triangle: +64 - +68 +0.02 - +0.03 0 - 0 0.0 - 0.0
arduino:samd:mkrwifi1010 :small_red_triangle: +64 - +68 +0.02 - +0.03 0 - 0 0.0 - 0.0
arduino:samd:mkrzero :small_red_triangle: +64 - +68 +0.02 - +0.03 0 - 0 0.0 - 0.0
esp32:esp32:esp32 :small_red_triangle: +64 - +64 0.0 - 0.0 0 - 0 0.0 - 0.0
rp2040:rp2040:rpipico :small_red_triangle: +64 - +72 0.0 - 0.0 0 - 0 0.0 - 0.0
Click for full report table Board|examples/MCP2515-CAN-Sniffer
flash|%|examples/MCP2515-CAN-Sniffer
RAM for global variables|%|examples/MCP2515-Filter
flash|%|examples/MCP2515-Filter
RAM for global variables|%|examples/MCP2515-Loopback
flash|%|examples/MCP2515-Loopback
RAM for global variables|% -|-|-|-|-|-|-|-|-|-|-|-|- arduino:mbed_edge:edge_control|64|0.01|0|0.0|64|0.01|0|0.0|64|0.01|0|0.0 arduino:mbed_nano:nano33ble|64|0.01|0|0.0|64|0.01|0|0.0|64|0.01|0|0.0 arduino:mbed_nano:nanorp2040connect|66|0.0|0|0.0|66|0.0|0|0.0|66|0.0|0|0.0 arduino:samd:mkr1000|64|0.02|0|0.0|64|0.02|0|0.0|68|0.03|0|0.0 arduino:samd:mkrfox1200|64|0.02|0|0.0|64|0.02|0|0.0|68|0.03|0|0.0 arduino:samd:mkrgsm1400|64|0.02|0|0.0|64|0.02|0|0.0|68|0.03|0|0.0 arduino:samd:mkrnb1500|64|0.02|0|0.0|64|0.02|0|0.0|68|0.03|0|0.0 arduino:samd:mkrvidor4000|64|0.02|0|0.0|64|0.02|0|0.0|68|0.03|0|0.0 arduino:samd:mkrwan1300|64|0.02|0|0.0|64|0.02|0|0.0|68|0.03|0|0.0 arduino:samd:mkrwan1310|64|0.02|0|0.0|64|0.02|0|0.0|68|0.03|0|0.0 arduino:samd:mkrwifi1010|64|0.02|0|0.0|64|0.02|0|0.0|68|0.03|0|0.0 arduino:samd:mkrzero|64|0.02|0|0.0|64|0.02|0|0.0|68|0.03|0|0.0 esp32:esp32:esp32|64|0.0|0|0.0|64|0.0|0|0.0|64|0.0|0|0.0 rp2040:rp2040:rpipico|64|0.0|0|0.0|72|0.0|0|0.0|72|0.0|0|0.0
Click for full report CSV ``` Board,examples/MCP2515-CAN-Sniffer
flash,%,examples/MCP2515-CAN-Sniffer
RAM for global variables,%,examples/MCP2515-Filter
flash,%,examples/MCP2515-Filter
RAM for global variables,%,examples/MCP2515-Loopback
flash,%,examples/MCP2515-Loopback
RAM for global variables,% arduino:mbed_edge:edge_control,64,0.01,0,0.0,64,0.01,0,0.0,64,0.01,0,0.0 arduino:mbed_nano:nano33ble,64,0.01,0,0.0,64,0.01,0,0.0,64,0.01,0,0.0 arduino:mbed_nano:nanorp2040connect,66,0.0,0,0.0,66,0.0,0,0.0,66,0.0,0,0.0 arduino:samd:mkr1000,64,0.02,0,0.0,64,0.02,0,0.0,68,0.03,0,0.0 arduino:samd:mkrfox1200,64,0.02,0,0.0,64,0.02,0,0.0,68,0.03,0,0.0 arduino:samd:mkrgsm1400,64,0.02,0,0.0,64,0.02,0,0.0,68,0.03,0,0.0 arduino:samd:mkrnb1500,64,0.02,0,0.0,64,0.02,0,0.0,68,0.03,0,0.0 arduino:samd:mkrvidor4000,64,0.02,0,0.0,64,0.02,0,0.0,68,0.03,0,0.0 arduino:samd:mkrwan1300,64,0.02,0,0.0,64,0.02,0,0.0,68,0.03,0,0.0 arduino:samd:mkrwan1310,64,0.02,0,0.0,64,0.02,0,0.0,68,0.03,0,0.0 arduino:samd:mkrwifi1010,64,0.02,0,0.0,64,0.02,0,0.0,68,0.03,0,0.0 arduino:samd:mkrzero,64,0.02,0,0.0,64,0.02,0,0.0,68,0.03,0,0.0 esp32:esp32:esp32,64,0.0,0,0.0,64,0.0,0,0.0,64,0.0,0,0.0 rp2040:rp2040:rpipico,64,0.0,0,0.0,72,0.0,0,0.0,72,0.0,0,0.0 ```
munzili commented 1 year ago

I have restored the original flag definitions

github-actions[bot] commented 1 year ago

Memory usage change @ e05959771e922262279e6d4fd58d9a7f174990ad

Board flash % RAM for global variables %
arduino:mbed_edge:edge_control :small_red_triangle: +64 - +64 +0.01 - +0.01 0 - 0 0.0 - 0.0
arduino:mbed_nano:nano33ble :small_red_triangle: +64 - +64 +0.01 - +0.01 0 - 0 0.0 - 0.0
arduino:mbed_nano:nanorp2040connect :small_red_triangle: +66 - +66 0.0 - 0.0 0 - 0 0.0 - 0.0
arduino:mbed_portenta:envie_m7:target_core=cm4 N/A N/A N/A N/A
arduino:mbed_portenta:envie_m7 N/A N/A N/A N/A
arduino:samd:mkr1000 :small_red_triangle: +64 - +68 +0.02 - +0.03 0 - 0 0.0 - 0.0
arduino:samd:mkrfox1200 :small_red_triangle: +64 - +68 +0.02 - +0.03 0 - 0 0.0 - 0.0
arduino:samd:mkrgsm1400 :small_red_triangle: +64 - +68 +0.02 - +0.03 0 - 0 0.0 - 0.0
arduino:samd:mkrnb1500 :small_red_triangle: +64 - +68 +0.02 - +0.03 0 - 0 0.0 - 0.0
arduino:samd:mkrvidor4000 :small_red_triangle: +64 - +68 +0.02 - +0.03 0 - 0 0.0 - 0.0
arduino:samd:mkrwan1300 :small_red_triangle: +64 - +68 +0.02 - +0.03 0 - 0 0.0 - 0.0
arduino:samd:mkrwan1310 :small_red_triangle: +64 - +68 +0.02 - +0.03 0 - 0 0.0 - 0.0
arduino:samd:mkrwifi1010 :small_red_triangle: +64 - +68 +0.02 - +0.03 0 - 0 0.0 - 0.0
arduino:samd:mkrzero :small_red_triangle: +64 - +68 +0.02 - +0.03 0 - 0 0.0 - 0.0
esp32:esp32:esp32 :small_red_triangle: +64 - +64 0.0 - 0.0 0 - 0 0.0 - 0.0
rp2040:rp2040:rpipico :small_red_triangle: +64 - +72 0.0 - 0.0 0 - 0 0.0 - 0.0
Click for full report table Board|`examples/MCP2515-CAN-Sniffer`
flash|%|`examples/MCP2515-CAN-Sniffer`
RAM for global variables|%|`examples/MCP2515-Filter`
flash|%|`examples/MCP2515-Filter`
RAM for global variables|%|`examples/MCP2515-Loopback`
flash|%|`examples/MCP2515-Loopback`
RAM for global variables|% -|-|-|-|-|-|-|-|-|-|-|-|- `arduino:mbed_edge:edge_control`|64|0.01|0|0.0|64|0.01|0|0.0|64|0.01|0|0.0 `arduino:mbed_nano:nano33ble`|64|0.01|0|0.0|64|0.01|0|0.0|64|0.01|0|0.0 `arduino:mbed_nano:nanorp2040connect`|66|0.0|0|0.0|66|0.0|0|0.0|66|0.0|0|0.0 `arduino:mbed_portenta:envie_m7:target_core=cm4`|N/A|N/A|N/A|N/A|N/A|N/A|N/A|N/A|N/A|N/A|N/A|N/A `arduino:mbed_portenta:envie_m7`|N/A|N/A|N/A|N/A|N/A|N/A|N/A|N/A|N/A|N/A|N/A|N/A `arduino:samd:mkr1000`|64|0.02|0|0.0|64|0.02|0|0.0|68|0.03|0|0.0 `arduino:samd:mkrfox1200`|64|0.02|0|0.0|64|0.02|0|0.0|68|0.03|0|0.0 `arduino:samd:mkrgsm1400`|64|0.02|0|0.0|64|0.02|0|0.0|68|0.03|0|0.0 `arduino:samd:mkrnb1500`|64|0.02|0|0.0|64|0.02|0|0.0|68|0.03|0|0.0 `arduino:samd:mkrvidor4000`|64|0.02|0|0.0|64|0.02|0|0.0|68|0.03|0|0.0 `arduino:samd:mkrwan1300`|64|0.02|0|0.0|64|0.02|0|0.0|68|0.03|0|0.0 `arduino:samd:mkrwan1310`|64|0.02|0|0.0|64|0.02|0|0.0|68|0.03|0|0.0 `arduino:samd:mkrwifi1010`|64|0.02|0|0.0|64|0.02|0|0.0|68|0.03|0|0.0 `arduino:samd:mkrzero`|64|0.02|0|0.0|64|0.02|0|0.0|68|0.03|0|0.0 `esp32:esp32:esp32`|64|0.0|0|0.0|64|0.0|0|0.0|64|0.0|0|0.0 `rp2040:rp2040:rpipico`|64|0.0|0|0.0|72|0.0|0|0.0|72|0.0|0|0.0
Click for full report CSV ``` Board,examples/MCP2515-CAN-Sniffer
flash,%,examples/MCP2515-CAN-Sniffer
RAM for global variables,%,examples/MCP2515-Filter
flash,%,examples/MCP2515-Filter
RAM for global variables,%,examples/MCP2515-Loopback
flash,%,examples/MCP2515-Loopback
RAM for global variables,% arduino:mbed_edge:edge_control,64,0.01,0,0.0,64,0.01,0,0.0,64,0.01,0,0.0 arduino:mbed_nano:nano33ble,64,0.01,0,0.0,64,0.01,0,0.0,64,0.01,0,0.0 arduino:mbed_nano:nanorp2040connect,66,0.0,0,0.0,66,0.0,0,0.0,66,0.0,0,0.0 arduino:mbed_portenta:envie_m7:target_core=cm4,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A arduino:mbed_portenta:envie_m7,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A,N/A arduino:samd:mkr1000,64,0.02,0,0.0,64,0.02,0,0.0,68,0.03,0,0.0 arduino:samd:mkrfox1200,64,0.02,0,0.0,64,0.02,0,0.0,68,0.03,0,0.0 arduino:samd:mkrgsm1400,64,0.02,0,0.0,64,0.02,0,0.0,68,0.03,0,0.0 arduino:samd:mkrnb1500,64,0.02,0,0.0,64,0.02,0,0.0,68,0.03,0,0.0 arduino:samd:mkrvidor4000,64,0.02,0,0.0,64,0.02,0,0.0,68,0.03,0,0.0 arduino:samd:mkrwan1300,64,0.02,0,0.0,64,0.02,0,0.0,68,0.03,0,0.0 arduino:samd:mkrwan1310,64,0.02,0,0.0,64,0.02,0,0.0,68,0.03,0,0.0 arduino:samd:mkrwifi1010,64,0.02,0,0.0,64,0.02,0,0.0,68,0.03,0,0.0 arduino:samd:mkrzero,64,0.02,0,0.0,64,0.02,0,0.0,68,0.03,0,0.0 esp32:esp32:esp32,64,0.0,0,0.0,64,0.0,0,0.0,64,0.0,0,0.0 rp2040:rp2040:rpipico,64,0.0,0,0.0,72,0.0,0,0.0,72,0.0,0,0.0 ```