├── License.txt ├── README.md ├── canbed_dual.cpp ├── canbed_dual.h ├── examples ├── CAN20_SendRecv │ └── CAN20_SendRecv.ino ├── CANFD_SendRecv │ ├── CANFD_SendRecv.ino │ └── CANFD_SendRecv.ino.bak └── MaskandFilt │ └── MaskandFilt.ino ├── keywords.txt ├── library.json └── library.properties /License.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2018 Longan Labs Inc. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Longan Labs CANBed Dual Arduino Library 2 | 3 | [![Actions Status](https://github.com/arduino/arduino-cli-example/workflows/test/badge.svg)](https://github.com/arduino/arduino-cli-example/actions) 4 | [![Spell Check](https://github.com/arduino/compile-sketches/workflows/Spell%20Check/badge.svg)](https://github.com/arduino/compile-sketches/actions?workflow=Spell+Check) 5 | [![codecov](https://codecov.io/gh/arduino/compile-sketches/branch/main/graph/badge.svg?token=Uv6f1ebMZ4)](https://codecov.io/gh/arduino/compile-sketches) 6 | 7 | Arduino library for CANBed Dual. 8 | 9 | With this library, you can, 10 | 11 | 1. Send a CAN2.0 frame 12 | 2. Receive a CAN2.0 frame 13 | 3. Send a CAN FD frame 14 | 4. Receive a CAN FD frame 15 | 16 | ## Installation 17 | 18 | 1. [Download the library](https://github.com/Longan-Labs/CANBedDual_Arduino_Lib/archive/refs/heads/master.zip) 19 | 2. Extract the zip file 20 | 3. In the Arduino IDE, navigate to Sketch > Include Library > Add .ZIP Library 21 | 22 | ## Respository Contents 23 | 24 | * [**/examples**](./examples) - Example sketches for the library (.ino). Run these from the Arduino IDE. 25 | * [**/src**](./src) - Source files for the library (.cpp, .h). 26 | * [**keywords.txt**](./keywords.txt) - Keywords from this library that will be highlighted in the Arduino IDE. 27 | * [**library.properties**](./library.properties) - General library properties for the Arduino package manager. 28 | 29 | ## Functions 30 | 31 | - init() 32 | - initFD() 33 | - initMaskFilt() 34 | - send() 35 | - read() 36 | 37 | ## Examples 38 | 39 | here are many examples implemented in this library. One of the examples is below. You can find other examples [here](./examples) 40 | 41 | ```Cpp 42 | // CANBED DUAL TEST EXAMPLE 43 | // CAN 0 Send, CAN 1 Recv 44 | // 45 | 46 | #include 47 | #include 48 | #include "canbed_dual.h" 49 | 50 | CANBedDual CAN0(0); 51 | CANBedDual CAN1(1); 52 | 53 | 54 | void setup() 55 | { 56 | Serial.begin(115200); 57 | pinMode(18, OUTPUT); 58 | 59 | Wire1.setSDA(6); 60 | Wire1.setSCL(7); 61 | Wire1.begin(); 62 | 63 | CAN0.initFD(500000, 4000000); // CAN0 baudrate: 500kb/s, FD baudrate, 4Mb/s 64 | CAN1.initFD(500000, 4000000); // CAN1 baudrate: 500kb/s, FD baudrate, 4Mb/s 65 | } 66 | 67 | void loop() 68 | { 69 | sendData(); 70 | 71 | unsigned long id = 0; 72 | int ext = 0; 73 | int rtr = 0; 74 | int fd = 0; 75 | int len = 0; 76 | 77 | unsigned char dtaGet[100]; 78 | 79 | if(CAN1.read(&id, &ext, &rtr, &fd, &len, dtaGet)) 80 | { 81 | Serial.println("CAN1 GET DATA"); 82 | Serial.print("id = "); 83 | Serial.println(id); 84 | Serial.print("ext = "); 85 | Serial.println(ext); 86 | Serial.print("rtr = "); 87 | Serial.println(rtr); 88 | Serial.print("fd = "); 89 | Serial.println(fd); 90 | Serial.print("len = "); 91 | Serial.println(len); 92 | 93 | for(int i=0; i 99)cnt = 0; 107 | unsigned char str[64]; 108 | for(int i=0; i<64; i++)str[i] = cnt; 109 | CAN0.send(0x01, 0, 0, 1, 64, str); 110 | } 111 | 112 | // ENDIF 113 | ``` 114 | 115 | ## Get a Dev Board 116 | 117 | If you need a Dev board, plese try, 118 | 119 | - [CANBed Dual](https://www.longan-labs.cc/1030019.html) 120 | 121 | 122 | ## License 123 | 124 | ``` 125 | MIT License 126 | 127 | Copyright (c) 2018 @ Longan Labs 128 | 129 | Permission is hereby granted, free of charge, to any person obtaining a copy 130 | of this software and associated documentation files (the "Software"), to deal 131 | in the Software without restriction, including without limitation the rights 132 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 133 | copies of the Software, and to permit persons to whom the Software is 134 | furnished to do so, subject to the following conditions: 135 | 136 | The above copyright notice and this permission notice shall be included in all 137 | copies or substantial portions of the Software. 138 | 139 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 140 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 141 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 142 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 143 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 144 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 145 | SOFTWARE. 146 | ``` 147 | 148 | ## Contact us 149 | 150 | If you have any question, please feel free to contact [support@longan-labs.cc](support@longan-labs.cc) 151 | 152 | 153 | [![Analytics](https://ga-beacon.appspot.com/UA-101965714-1/Longan_CANFD)](https://github.com/igrigorik/ga-beacon) 154 | -------------------------------------------------------------------------------- /canbed_dual.cpp: -------------------------------------------------------------------------------- 1 | #include "canbed_dual.h" 2 | 3 | 4 | 5 | unsigned long CANBedDual::char2long(unsigned char *str) 6 | { 7 | unsigned long __t = 0; 8 | 9 | __t = str[0]; 10 | __t <<= 8; 11 | __t |= str[1]; 12 | __t <<= 8; 13 | __t |= str[2]; 14 | __t <<= 8; 15 | __t |= str[3]; 16 | return __t; 17 | } 18 | 19 | void CANBedDual::long2char(unsigned long __t, unsigned char *str) 20 | { 21 | str[0] = (__t>>24)&0xff; 22 | str[1] = (__t>>16)&0xff; 23 | str[2] = (__t>>8)&0xff; 24 | str[3] = (__t>>0)&0xff; 25 | } 26 | 27 | void CANBedDual::makeCanConfig() 28 | { 29 | unsigned long can20_baud = 500000; 30 | unsigned long canfd_baud = 1000000; 31 | 32 | unsigned char mf_set0 = 1; 33 | unsigned char mf_ext0 = 0; 34 | unsigned long mf_mask0 = 0; 35 | unsigned long mf_filt0 = 0; 36 | 37 | unsigned char mf_set1 = 0; 38 | unsigned char mf_ext1 = 0; 39 | unsigned long mf_mask1 = 0; 40 | unsigned long mf_filt1 = 0; 41 | 42 | unsigned char mf_set2 = 0; 43 | unsigned char mf_ext2 = 0; 44 | unsigned long mf_mask2 = 0; 45 | unsigned long mf_filt2 = 0; 46 | 47 | unsigned char mf_set3 = 0; 48 | unsigned char mf_ext3 = 0; 49 | unsigned long mf_mask3 = 0; 50 | unsigned long mf_filt3 = 0; 51 | 52 | long2char(can20_baud, &canconfig[0]); 53 | long2char(canfd_baud, &canconfig[4]); 54 | 55 | // set mask&filt 0 56 | canconfig[8] = mf_set0; 57 | canconfig[9] = mf_ext0; 58 | long2char(mf_mask0, &canconfig[10]); 59 | long2char(mf_filt0, &canconfig[14]); 60 | 61 | // set mask&filt 1 62 | canconfig[18] = mf_set1; 63 | canconfig[19] = mf_ext1; 64 | long2char(mf_mask1, &canconfig[20]); 65 | long2char(mf_filt1, &canconfig[24]); 66 | 67 | // set mask&filt 2 68 | canconfig[28] = mf_set2; 69 | canconfig[29] = mf_ext2; 70 | long2char(mf_mask2, &canconfig[30]); 71 | long2char(mf_filt2, &canconfig[34]); 72 | 73 | // set mask&filt 3 74 | canconfig[38] = mf_set3; 75 | canconfig[39] = mf_ext3; 76 | long2char(mf_mask3, &canconfig[40]); 77 | long2char(mf_filt3, &canconfig[44]); 78 | } 79 | 80 | void CANBedDual::sendConfig() 81 | { 82 | CANI2C.beginTransmission(0x41); 83 | CANI2C.write(canNum ? 0x19 : 0x09); 84 | CANI2C.write(canconfig, 48); 85 | CANI2C.endTransmission(); 86 | 87 | delay(10); 88 | } 89 | 90 | void CANBedDual::init(unsigned long speed) 91 | { 92 | makeCanConfig(); 93 | long2char(speed, &canconfig[0]); 94 | long2char(1000000, &canconfig[4]); 95 | 96 | sendConfig(); 97 | } 98 | 99 | void CANBedDual::initFD(unsigned long speed20, unsigned long speedfd) 100 | { 101 | 102 | makeCanConfig(); 103 | long2char(speed20, &canconfig[0]); 104 | long2char(speedfd, &canconfig[4]); 105 | 106 | sendConfig(); 107 | } 108 | 109 | void CANBedDual::initMaskFilt(unsigned char num, unsigned char ext, unsigned long mask, unsigned long filt) 110 | { 111 | if(num > 3)return; 112 | canconfig[10*num+8] = 1; 113 | canconfig[10*num+9] = ext; 114 | long2char(mask, &canconfig[10+10*num]); 115 | long2char(filt, &canconfig[14+10*num]); 116 | 117 | sendConfig(); 118 | 119 | } 120 | 121 | void CANBedDual::send(unsigned long id, unsigned char ext, unsigned char rtr, unsigned char fd, unsigned char len, unsigned char *dta) 122 | { 123 | /* if(micros()-timerDelay < DELAY_TIME) 124 | { 125 | delayMicroseconds(DELAY_TIME-(micros()-timerDelay)); 126 | } 127 | timerDelay = micros(); 128 | */ 129 | unsigned char dsend[100]; 130 | 131 | dsend[0] = canNum ? 0x15 : 0X05; 132 | dsend[4] = id&0xff; 133 | dsend[3] = (id>>8)&0xff; 134 | dsend[2] = (id>>16)&0xff; 135 | dsend[1] = (id>>24)&0xff; 136 | 137 | dsend[5] = rtr; // data or remote, 1:remote, 0:data 138 | dsend[6] = ext; // standard or ext, 1 ext, 0 standard 139 | 140 | dsend[7] = fd; 141 | dsend[8] = len; 142 | 143 | for(int i=0; i 4 | #include 5 | 6 | #define I2C_ADDR 0X41 7 | #define CANI2C Wire1 8 | #define DELAY_TIME 0 9 | 10 | class CANBedDual 11 | { 12 | private: 13 | 14 | unsigned long timerDelay = 0; 15 | int canNum = 0; 16 | 17 | unsigned char canconfig[48]; 18 | 19 | private: 20 | 21 | unsigned long char2long(unsigned char *str); 22 | void long2char(unsigned long __t, unsigned char *str); 23 | void makeCanConfig(); 24 | void sendConfig(); 25 | public: 26 | 27 | CANBedDual(int num) 28 | { 29 | canNum = num; 30 | } 31 | 32 | void init(unsigned long speed); 33 | void initFD(unsigned long speed20, unsigned long speedfd); 34 | 35 | void initMaskFilt(unsigned char num, unsigned char ext, unsigned long mask, unsigned long filt); 36 | 37 | void send(unsigned long id, unsigned char ext, unsigned char rtr, unsigned char fd, unsigned char len, unsigned char *dta); 38 | byte checkRecv(); 39 | byte read(unsigned long *id, int *ext, int *rtr, int *fd, int *len, unsigned char *str); 40 | }; -------------------------------------------------------------------------------- /examples/CAN20_SendRecv/CAN20_SendRecv.ino: -------------------------------------------------------------------------------- 1 | // CANBED DUAL TEST EXAMPLE 2 | // CAN 0 Send, CAN 1 Recv 3 | 4 | #include 5 | #include 6 | #include "canbed_dual.h" 7 | 8 | CANBedDual CAN0(0); 9 | CANBedDual CAN1(1); 10 | 11 | 12 | void setup() 13 | { 14 | Serial.begin(115200); 15 | pinMode(18, OUTPUT); 16 | 17 | Wire1.setSDA(6); 18 | Wire1.setSCL(7); 19 | Wire1.begin(); 20 | 21 | CAN0.init(500000); // CAN0 baudrate: 500kb/s 22 | CAN1.init(500000); // CAN1 baudrate: 500kb/s 23 | } 24 | 25 | void loop() 26 | { 27 | sendData(); 28 | 29 | unsigned long id = 0; 30 | int ext = 0; 31 | int rtr = 0; 32 | int fd = 0; 33 | int len = 0; 34 | 35 | unsigned char dtaGet[100]; 36 | 37 | if(CAN1.read(&id, &ext, &rtr, &fd, &len, dtaGet)) 38 | { 39 | Serial.println("CAN1 GET DATA"); 40 | Serial.print("id = "); 41 | Serial.println(id); 42 | Serial.print("ext = "); 43 | Serial.println(ext); 44 | Serial.print("rtr = "); 45 | Serial.println(rtr); 46 | Serial.print("fd = "); 47 | Serial.println(fd); 48 | Serial.print("len = "); 49 | Serial.println(len); 50 | 51 | for(int i=0; i 99)cnt = 0; 65 | unsigned char str[8]; 66 | for(int i=0; i<8; i++)str[i] = cnt; 67 | CAN0.send(0x01, 0, 0, 0, 8, str); 68 | } 69 | 70 | // ENDIF -------------------------------------------------------------------------------- /examples/CANFD_SendRecv/CANFD_SendRecv.ino: -------------------------------------------------------------------------------- 1 | // CANBED DUAL TEST EXAMPLE 2 | // CAN 0 Send, CAN 1 Recv 3 | // 4 | 5 | #include 6 | #include 7 | #include "canbed_dual.h" 8 | 9 | CANBedDual CAN0(0); 10 | CANBedDual CAN1(1); 11 | 12 | 13 | void setup() 14 | { 15 | Serial.begin(115200); 16 | pinMode(18, OUTPUT); 17 | 18 | Wire1.setSDA(6); 19 | Wire1.setSCL(7); 20 | Wire1.begin(); 21 | 22 | CAN0.initFD(500000, 4000000); // CAN0 baudrate: 500kb/s, FD baudrate, 4Mb/s 23 | CAN1.initFD(500000, 4000000); // CAN1 baudrate: 500kb/s, FD baudrate, 4Mb/s 24 | } 25 | 26 | void loop() 27 | { 28 | sendData(); 29 | 30 | unsigned long id = 0; 31 | int ext = 0; 32 | int rtr = 0; 33 | int fd = 0; 34 | int len = 0; 35 | 36 | unsigned char dtaGet[100]; 37 | 38 | if(CAN1.read(&id, &ext, &rtr, &fd, &len, dtaGet)) 39 | { 40 | Serial.println("CAN1 GET DATA"); 41 | Serial.print("id = "); 42 | Serial.println(id); 43 | Serial.print("ext = "); 44 | Serial.println(ext); 45 | Serial.print("rtr = "); 46 | Serial.println(rtr); 47 | Serial.print("fd = "); 48 | Serial.println(fd); 49 | Serial.print("len = "); 50 | Serial.println(len); 51 | 52 | for(int i=0; i 99)cnt = 0; 66 | unsigned char str[64]; 67 | for(int i=0; i<64; i++)str[i] = cnt; 68 | CAN0.send(0x01, 0, 0, 1, 64, str); 69 | } 70 | 71 | // ENDIF -------------------------------------------------------------------------------- /examples/CANFD_SendRecv/CANFD_SendRecv.ino.bak: -------------------------------------------------------------------------------- 1 | // CANBED DUAL TEST EXAMPLE 2 | // CAN 0 Send, CAN 1 Recv 3 | 4 | #include 5 | #include 6 | #include "canbed_dual.h" 7 | 8 | CANBedDual CAN0(0); 9 | CANBedDual CAN1(1); 10 | 11 | 12 | void setup() 13 | { 14 | Serial.begin(115200); 15 | pinMode(18, OUTPUT); 16 | 17 | Wire1.setSDA(6); 18 | Wire1.setSCL(7); 19 | Wire1.begin(); 20 | 21 | CAN0.initFD(500000, 4000000); // CAN0 baudrate: 500kb/s, FD baudrate, 4Mb/s 22 | CAN1.initFD(500000, 4000000); // CAN1 baudrate: 500kb/s, FD baudrate, 4Mb/s 23 | } 24 | 25 | void loop() 26 | { 27 | sendData(); 28 | 29 | unsigned long id = 0; 30 | int ext = 0; 31 | int rtr = 0; 32 | int fd = 0; 33 | int len = 0; 34 | 35 | unsigned char dtaGet[100]; 36 | 37 | if(CAN1.read(&id, &ext, &rtr, &fd, &len, dtaGet)) 38 | { 39 | Serial.println("CAN1 GET DATA"); 40 | Serial.print("id = "); 41 | Serial.println(id); 42 | Serial.print("ext = "); 43 | Serial.println(ext); 44 | Serial.print("rtr = "); 45 | Serial.println(rtr); 46 | Serial.print("fd = "); 47 | Serial.println(fd); 48 | Serial.print("len = "); 49 | Serial.println(len); 50 | 51 | for(int i=0; i 99)cnt = 0; 65 | unsigned char str[64]; 66 | for(int i=0; i<64; i++)str[i] = cnt; 67 | CAN0.send(0x01, 0, 0, 1, 64, str); 68 | } 69 | 70 | // ENDIF -------------------------------------------------------------------------------- /examples/MaskandFilt/MaskandFilt.ino: -------------------------------------------------------------------------------- 1 | // CANBED DUAL TEST EXAMPLE, set mask and filter 2 | // CAN 0 Send, CAN 1 Recv 3 | 4 | #include 5 | #include 6 | #include "canbed_dual.h" 7 | 8 | CANBedDual CAN0(0); 9 | CANBedDual CAN1(1); 10 | 11 | 12 | void setup() 13 | { 14 | Serial.begin(115200); 15 | pinMode(18, OUTPUT); 16 | 17 | Wire1.setSDA(6); 18 | Wire1.setSCL(7); 19 | Wire1.begin(); 20 | 21 | CAN0.init(500000); // CAN0 baudrate: 500kb/s 22 | CAN1.init(500000); // CAN1 baudrate: 500kb/s 23 | 24 | CAN1.initMaskFilt(0, 0, 0x7ff, 0x01); 25 | CAN1.initMaskFilt(1, 0, 0x7ff, 0x03); 26 | CAN1.initMaskFilt(2, 0, 0x7ff, 0x05); 27 | CAN1.initMaskFilt(3, 0, 0x7ff, 0x07); 28 | } 29 | 30 | void loop() 31 | { 32 | sendData(); 33 | 34 | unsigned long id = 0; 35 | int ext = 0; 36 | int rtr = 0; 37 | int fd = 0; 38 | int len = 0; 39 | 40 | unsigned char dtaGet[100]; 41 | 42 | if(CAN1.read(&id, &ext, &rtr, &fd, &len, dtaGet)) 43 | { 44 | Serial.println("CAN1 GET DATA"); 45 | Serial.print("id = "); 46 | Serial.println(id); 47 | Serial.print("ext = "); 48 | Serial.println(ext); 49 | Serial.print("rtr = "); 50 | Serial.println(rtr); 51 | Serial.print("fd = "); 52 | Serial.println(fd); 53 | Serial.print("len = "); 54 | Serial.println(len); 55 | 56 | for(int i=0; i 7)id = 0; 70 | 71 | unsigned char str[8] = {1, 2, 3, 4, 5, 6, 7, 8}; 72 | CAN0.send(id, 0, 0, 0, 8, str); 73 | } 74 | 75 | // ENDIF -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | ####################################### 2 | # Syntax Coloring Map For CANBED DUAL 3 | ####################################### 4 | 5 | ####################################### 6 | # Datatypes (KEYWORD1) 7 | ####################################### 8 | CANBedDual KEYWORD1 9 | 10 | ####################################### 11 | # Methods and Functions (KEYWORD2) 12 | ####################################### 13 | init KEYWORD2 14 | initFD KEYWORD2 15 | initMaskFilt KEYWORD2 16 | send KEYWORD2 17 | read KEYWORD2 18 | 19 | ####################################### 20 | # Constants (LITERAL1) 21 | ####################################### 22 | -------------------------------------------------------------------------------- /library.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "CANBed Dual", 3 | "keywords": "can, bus", 4 | "description": "Arduino Library for CANBed Dual", 5 | "repository": 6 | { 7 | "type": "git", 8 | "url": "https://github.com/Longan-Labs/CANBedDual_Arduino_Lib.git" 9 | }, 10 | "version": "1.0.0", 11 | "frameworks": "arduino", 12 | "platforms": "atmelavr" 13 | } 14 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=Longan Labs Arduino CAN Bus Library for MCP2515 2 | version=1.0.0 3 | author=Longan Labs 4 | maintainer=Longan Labs 5 | sentence=A library for MCP2515 6 | paragraph=Longan Labs Arduino CAN Bus Library for MCP2515 7 | category=CAN Bus 8 | url=https://github.com/Longan-Labs/Aruino_CAN_BUS_MCP2515 9 | architectures=* --------------------------------------------------------------------------------