diff --git a/Easylink_CAN_Waker/lib/Arduino_CAN_BUS_MCP2515-master/LICENSE b/Easylink_CAN_Waker/lib/Arduino_CAN_BUS_MCP2515-master/LICENSE new file mode 100644 index 0000000..d1cb698 --- /dev/null +++ b/Easylink_CAN_Waker/lib/Arduino_CAN_BUS_MCP2515-master/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2018 @ Longan Labs + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/Easylink_CAN_Waker/lib/Arduino_CAN_BUS_MCP2515-master/License.txt b/Easylink_CAN_Waker/lib/Arduino_CAN_BUS_MCP2515-master/License.txt new file mode 100644 index 0000000..5fd8f65 --- /dev/null +++ b/Easylink_CAN_Waker/lib/Arduino_CAN_BUS_MCP2515-master/License.txt @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2018 Longan Labs Inc. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. \ No newline at end of file diff --git a/Easylink_CAN_Waker/lib/Arduino_CAN_BUS_MCP2515-master/README.md b/Easylink_CAN_Waker/lib/Arduino_CAN_BUS_MCP2515-master/README.md new file mode 100644 index 0000000..dc57112 --- /dev/null +++ b/Easylink_CAN_Waker/lib/Arduino_CAN_BUS_MCP2515-master/README.md @@ -0,0 +1,143 @@ +# CAN Bus Library +[![GitHub](https://img.shields.io/github/license/Longan-Labs/Arduino_CAN_BUS_MCP2515)](https://github.com/Longan-Labs/Arduino_CAN_BUS_MCP2515/blob/master/LICENSE) +[![Installation instructions](https://www.ardu-badge.com/badge/mcp_canbus.svg?)](https://www.ardu-badge.com/mcp_canbus) +[![GitHub version](https://img.shields.io/github/release/Longan-Labs/Arduino_CAN_BUS_MCP2515.svg?logo=github&logoColor=ffffff)](https://github.com/Longan-Labs/Arduino_CAN_BUS_MCP2515/releases/latest) +[![GitHub Release Date](https://img.shields.io/github/release-date/Longan-Labs/Arduino_CAN_BUS_MCP2515.svg?logo=github&logoColor=ffffff)](https://github.com/Longan-Labs/Arduino_CAN_BUS_MCP2515/releases/latest) +[![GitHub Repo stars](https://img.shields.io/github/stars/Longan-Labs/Arduino_CAN_BUS_MCP2515?logo=github&logoColor=ffffff)](https://github.com/Longan-Labs/Arduino_CAN_BUS_MCP2515/stargazers) +[![GitHub issues](https://img.shields.io/github/issues/Longan-Labs/Arduino_CAN_BUS_MCP2515.svg?logo=github&logoColor=ffffff)](https://github.com/Longan-Labs/Arduino_CAN_BUS_MCP2515/issues) + +Arduino library for MCP2515, it's available for most of theArduino boards, we test it with Arduino UNO, Leonardo, Mega as well as Zero. + +With this library, you can, + +1. Send a CAN2.0 frame +2. Receive a CAN2.0 frame +3. Get data from OBD-II +4. Set the masks and filters, there're 32 masks and filters. + +## Installation + +You can install it similiar to any other Arduino libraries, using the Library Manager or from a Zip file in the Arduino IDE. + +- Install using the Library Manager +1. In the Arduino IDE, navigate to Sketch > Include Library > Manage Libraries... +2. Search for "mcp_canbus" to find this library +3. Click "INSTALL" + +- Install from a Zip file +1. [Download this library](https://github.com/Longan-Labs/Aruino_CAN_BUS_MCP2515/archive/refs/heads/master.zip) +2. Extract the zip file +3. In the Arduino IDE, navigate to Sketch > Include Library > Add .ZIP Library + +You can also read [how to install mcp_canbus library](https://www.ardu-badge.com/mcp_canbus) for a reference. + +## Respository Contents + +* [**/examples**](./examples) - Example sketches for the library (.ino). Run these from the Arduino IDE. +* [**/src**](./src) - Source files for the library (.cpp, .h). +* [**keywords.txt**](./keywords.txt) - Keywords from this library that will be highlighted in the Arduino IDE. +* [**library.properties**](./library.properties) - General library properties for the Arduino package manager. + +## How to use +### Header +``` +#include +``` + +### Functions + +- begin() +- init_Mask() +- init_filt() +- checkReceive() +- readMsgBufID() +- readMsgBuf() +- getCanId() +- sendMsgBuf() +- isRemoteRequest() +- isExtendedFrame() + +### Examples + +here are many examples implemented in this library. One of the examples is below. You can find other examples [here](./examples) + +```Cpp +/* send a frame from can bus + support@longan-labs.cc + + CAN Baudrate, + + #define CAN_5KBPS 1 + #define CAN_10KBPS 2 + #define CAN_20KBPS 3 + #define CAN_25KBPS 4 + #define CAN_31K25BPS 5 + #define CAN_33KBPS 6 + #define CAN_40KBPS 7 + #define CAN_50KBPS 8 + #define CAN_80KBPS 9 + #define CAN_83K3BPS 10 + #define CAN_95KBPS 11 + #define CAN_100KBPS 12 + #define CAN_125KBPS 13 + #define CAN_200KBPS 14 + #define CAN_250KBPS 15 + #define CAN_500KBPS 16 + #define CAN_666KBPS 17 + #define CAN_1000KBPS 18 + + CANBed V1: https://www.longan-labs.cc/1030008.html + CANBed M0: https://www.longan-labs.cc/1030014.html + CAN Bus Shield: https://www.longan-labs.cc/1030016.html + OBD-II CAN Bus GPS Dev Kit: https://www.longan-labs.cc/1030003.html +*/ + +#include +#include + +const int SPI_CS_PIN = 17; // CANBed V1 +// const int SPI_CS_PIN = 3; // CANBed M0 +// const int SPI_CS_PIN = 9; // CAN Bus Shield + +MCP_CAN CAN(SPI_CS_PIN); // Set CS pin + +void setup() +{ + Serial.begin(115200); + while(!Serial); + + // below code need for OBD-II GPS Dev Kit + // pinMode(A3, OUTPUT); + // digitalWrite(A3, HIGH); + + while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k + { + Serial.println("CAN BUS FAIL!"); + delay(100); + } + Serial.println("CAN BUS OK!"); +} + +unsigned char stmp[8] = {0, 1, 2, 3, 4, 5, 6, 7}; +void loop() +{ + CAN.sendMsgBuf(0x00, 0, 8, stmp); + delay(100); // send data per 100ms +} + +// END FILE +``` + +## Get a Dev Board + +If you need a Dev board, plese try, + +- [CAN Bus Shield for Arduino](https://www.longan-labs.cc/1030016.html) +- [CANBed V1](https://www.longan-labs.cc/1030008.html) +- [CANBed M0](https://www.longan-labs.cc/1030014.html) +- [OBD-II CAN Bus GPS Dev Kit](https://www.longan-labs.cc/1030003.html) + +## Contact us + +If you have any question, please feel free to contact [support@longan-labs.cc](support@longan-labs.cc) + diff --git a/Easylink_CAN_Waker/lib/Arduino_CAN_BUS_MCP2515-master/examples/OBDII_PIDs/getRpm/getRpm.ino b/Easylink_CAN_Waker/lib/Arduino_CAN_BUS_MCP2515-master/examples/OBDII_PIDs/getRpm/getRpm.ino new file mode 100644 index 0000000..6519150 --- /dev/null +++ b/Easylink_CAN_Waker/lib/Arduino_CAN_BUS_MCP2515-master/examples/OBDII_PIDs/getRpm/getRpm.ino @@ -0,0 +1,126 @@ +/************************************************************************************************* + OBD-II_PIDs TEST CODE + + Query + send id: 0x7df + dta: 0x02, 0x01, PID_CODE, 0, 0, 0, 0, 0 + + Response + From id: 0x7E9 or 0x7EA or 0x7EB + dta: len, 0x41, PID_CODE, byte0, byte1(option), byte2(option), byte3(option), byte4(option) + + https://en.wikipedia.org/wiki/OBD-II_PIDs +***************************************************************************************************/ + +#include +#include + +/* Please modify SPI_CS_PIN to adapt to your board. + + CANBed V1 - 17 + CANBed M0 - 3 + CAN Bus Shield - 9 + CANBed 2040 - 9 + CANBed Dual - 9 + OBD-2G Dev Kit - 9 + OBD-II GPS Kit - 9 + Hud Dev Kit - 9 + + Seeed Studio CAN-Bus Breakout Board for XIAO and QT Py - D7 +*/ + +#define SPI_CS_PIN 9 + +MCP_CAN CAN(SPI_CS_PIN); // Set CS pin + +#define PID_ENGIN_PRM 0x0C +#define PID_VEHICLE_SPEED 0x0D +#define PID_COOLANT_TEMP 0x05 + +#define CAN_ID_PID 0x7DF + +void set_mask_filt() +{ + // set mask, set both the mask to 0x3ff + + CAN.init_Mask(0, 0, 0x7FC); + CAN.init_Mask(1, 0, 0x7FC); + + // set filter, we can receive id from 0x04 ~ 0x09 + + CAN.init_Filt(0, 0, 0x7E8); + CAN.init_Filt(1, 0, 0x7E8); + + CAN.init_Filt(2, 0, 0x7E8); + CAN.init_Filt(3, 0, 0x7E8); + CAN.init_Filt(4, 0, 0x7E8); + CAN.init_Filt(5, 0, 0x7E8); +} + +void sendPid(unsigned char __pid) { + unsigned char tmp[8] = {0x02, 0x01, __pid, 0, 0, 0, 0, 0}; + CAN.sendMsgBuf(CAN_ID_PID, 0, 8, tmp); +} + +bool getRPM(int *r) +{ + sendPid(PID_ENGIN_PRM); + unsigned long __timeout = millis(); + + while(millis()-__timeout < 1000) // 1s time out + { + unsigned char len = 0; + unsigned char buf[8]; + + if (CAN_MSGAVAIL == CAN.checkReceive()) { // check if get data + CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf + + if(buf[1] == 0x41) + { + *r = (256*buf[3]+buf[4])/4; + return 1; + } + } + } + + return 0; +} + +void setup() +{ + Serial.begin(115200); + while(!Serial); + + // below code need for OBD-II GPS Dev Kit Atemga32U4 version + // pinMode(A3, OUTPUT); + // digitalWrite(A3, HIGH); + + // below code need for OBD-II GPS Dev Kit RP2040 version + // pinMode(12, OUTPUT); + // digitalWrite(12, HIGH); + + while (CAN_OK != CAN.begin(CAN_500KBPS)) { // init can bus : baudrate = 500k + Serial.println("CAN init fail, retry..."); + delay(100); + } + Serial.println("CAN init ok!"); + set_mask_filt(); +} + +void loop() { + + int __rpm = 0; + + int ret = getRPM(&__rpm); + + if(ret) + { + Serial.print("Engin Speed: "); + Serial.print(__rpm); + Serial.println(" rpm"); + }else Serial.println("get Engin Speed Fail..."); + + delay(500); +} + +// END FILE diff --git a/Easylink_CAN_Waker/lib/Arduino_CAN_BUS_MCP2515-master/examples/OBDII_PIDs/getSpeed/getSpeed.ino b/Easylink_CAN_Waker/lib/Arduino_CAN_BUS_MCP2515-master/examples/OBDII_PIDs/getSpeed/getSpeed.ino new file mode 100644 index 0000000..db00d72 --- /dev/null +++ b/Easylink_CAN_Waker/lib/Arduino_CAN_BUS_MCP2515-master/examples/OBDII_PIDs/getSpeed/getSpeed.ino @@ -0,0 +1,122 @@ +/************************************************************************************************* + OBD-II_PIDs TEST CODE + + Query + send id: 0x7df + dta: 0x02, 0x01, PID_CODE, 0, 0, 0, 0, 0 + + Response + From id: 0x7E9 or 0x7EA or 0x7EB + dta: len, 0x41, PID_CODE, byte0, byte1(option), byte2(option), byte3(option), byte4(option) + + https://en.wikipedia.org/wiki/OBD-II_PIDs +***************************************************************************************************/ + +#include +#include + +/* Please modify SPI_CS_PIN to adapt to your board. + + CANBed V1 - 17 + CANBed M0 - 3 + CAN Bus Shield - 9 + CANBed 2040 - 9 + CANBed Dual - 9 + OBD-2G Dev Kit - 9 + OBD-II GPS Kit - 9 + Hud Dev Kit - 9 + + Seeed Studio CAN-Bus Breakout Board for XIAO and QT Py - D7 +*/ + +#define SPI_CS_PIN 9 + +MCP_CAN CAN(SPI_CS_PIN); // Set CS pin + +#define PID_ENGIN_PRM 0x0C +#define PID_VEHICLE_SPEED 0x0D +#define PID_COOLANT_TEMP 0x05 + +#define CAN_ID_PID 0x7DF + +void set_mask_filt() +{ + // set mask, set both the mask to 0x3ff + + CAN.init_Mask(0, 0, 0x7FC); + CAN.init_Mask(1, 0, 0x7FC); + + // set filter, we can receive id from 0x04 ~ 0x09 + + CAN.init_Filt(0, 0, 0x7E8); + CAN.init_Filt(1, 0, 0x7E8); + + CAN.init_Filt(2, 0, 0x7E8); + CAN.init_Filt(3, 0, 0x7E8); + CAN.init_Filt(4, 0, 0x7E8); + CAN.init_Filt(5, 0, 0x7E8); +} + +void sendPid(unsigned char __pid) { + unsigned char tmp[8] = {0x02, 0x01, __pid, 0, 0, 0, 0, 0}; + CAN.sendMsgBuf(CAN_ID_PID, 0, 8, tmp); +} + +bool getSpeed(int *s) +{ + sendPid(PID_VEHICLE_SPEED); + unsigned long __timeout = millis(); + + while(millis()-__timeout < 1000) // 1s time out + { + unsigned char len = 0; + unsigned char buf[8]; + + if (CAN_MSGAVAIL == CAN.checkReceive()) { // check if get data + CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf + + if(buf[1] == 0x41) + { + *s = buf[3]; + return 1; + } + } + } + + return 0; +} + +void setup() { + Serial.begin(115200); + while(!Serial); + + // below code need for OBD-II GPS Dev Kit Atemga32U4 version + // pinMode(A3, OUTPUT); + // digitalWrite(A3, HIGH); + + // below code need for OBD-II GPS Dev Kit RP2040 version + // pinMode(12, OUTPUT); + // digitalWrite(12, HIGH); + + while (CAN_OK != CAN.begin(CAN_500KBPS)) { // init can bus : baudrate = 500k + Serial.println("CAN init fail, retry..."); + delay(100); + } + Serial.println("CAN init ok!"); + set_mask_filt(); +} + +void loop() { + + int __speed = 0; + int ret = getSpeed(&__speed); + if(ret) + { + Serial.print("Vehicle Speed: "); + Serial.print(__speed); + Serial.println(" kmh"); + } + delay(500); +} + +// END FILE diff --git a/Easylink_CAN_Waker/lib/Arduino_CAN_BUS_MCP2515-master/examples/recv/recv.ino b/Easylink_CAN_Waker/lib/Arduino_CAN_BUS_MCP2515-master/examples/recv/recv.ino new file mode 100644 index 0000000..ec322b5 --- /dev/null +++ b/Easylink_CAN_Waker/lib/Arduino_CAN_BUS_MCP2515-master/examples/recv/recv.ino @@ -0,0 +1,99 @@ +/* receive a frame from can bus + + CAN Baudrate, + + #define CAN_5KBPS 1 + #define CAN_10KBPS 2 + #define CAN_20KBPS 3 + #define CAN_25KBPS 4 + #define CAN_31K25BPS 5 + #define CAN_33KBPS 6 + #define CAN_40KBPS 7 + #define CAN_50KBPS 8 + #define CAN_80KBPS 9 + #define CAN_83K3BPS 10 + #define CAN_95KBPS 11 + #define CAN_100KBPS 12 + #define CAN_125KBPS 13 + #define CAN_200KBPS 14 + #define CAN_250KBPS 15 + #define CAN_500KBPS 16 + #define CAN_666KBPS 17 + #define CAN_1000KBPS 18 + + CANBed V1: https://www.longan-labs.cc/1030008.html + CANBed M0: https://www.longan-labs.cc/1030014.html + CAN Bus Shield: https://www.longan-labs.cc/1030016.html + OBD-II CAN Bus GPS Dev Kit: https://www.longan-labs.cc/1030003.html +*/ + +#include +#include + +/* Please modify SPI_CS_PIN to adapt to your board. + + CANBed V1 - 17 + CANBed M0 - 3 + CAN Bus Shield - 9 + CANBed 2040 - 9 + CANBed Dual - 9 + OBD-2G Dev Kit - 9 + OBD-II GPS Kit - 9 + Hud Dev Kit - 9 + + Seeed Studio CAN-Bus Breakout Board for XIAO and QT Py - D7 +*/ + + +#define SPI_CS_PIN 9 + +MCP_CAN CAN(SPI_CS_PIN); // Set CS pin + + +void setup() +{ + Serial.begin(115200); + while(!Serial); + + // below code need for OBD-II GPS Dev Kit Atemga32U4 version + // pinMode(A3, OUTPUT); + // digitalWrite(A3, HIGH); + + // below code need for OBD-II GPS Dev Kit RP2040 version + // pinMode(12, OUTPUT); + // digitalWrite(12, HIGH); + + while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k + { + Serial.println("CAN BUS FAIL!"); + delay(100); + } + Serial.println("CAN BUS OK!"); +} + + +void loop() +{ + unsigned char len = 0; + unsigned char buf[8]; + + if(CAN_MSGAVAIL == CAN.checkReceive()) // check if data coming + { + CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf + + unsigned long canId = CAN.getCanId(); + + Serial.println("-----------------------------"); + Serial.print("Get data from ID: "); + Serial.println(canId, HEX); + + for(int i = 0; i +#include + +/* Please modify SPI_CS_PIN to adapt to your board. + + CANBed V1 - 17 + CANBed M0 - 3 + CAN Bus Shield - 9 + CANBed 2040 - 9 + CANBed Dual - 9 + OBD-2G Dev Kit - 9 + OBD-II GPS Kit - 9 + Hud Dev Kit - 9 + + Seeed Studio CAN-Bus Breakout Board for XIAO and QT Py - D7 +*/ + +#define SPI_CS_PIN 9 + +MCP_CAN CAN(SPI_CS_PIN); // Set CS pin + +void setup() +{ + Serial.begin(115200); + while(!Serial); + + // below code need for OBD-II GPS Dev Kit Atemga32U4 version + // pinMode(A3, OUTPUT); + // digitalWrite(A3, HIGH); + + // below code need for OBD-II GPS Dev Kit RP2040 version + // pinMode(12, OUTPUT); + // digitalWrite(12, HIGH); + + while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k + { + Serial.println("CAN BUS FAIL!"); + delay(100); + } + Serial.println("CAN BUS OK!"); +} + +unsigned char stmp[8] = {0, 1, 2, 3, 4, 5, 6, 7}; +void loop() +{ + CAN.sendMsgBuf(0x00, 0, 8, stmp); + delay(100); // send data per 100ms +} + +// END FILE + diff --git a/Easylink_CAN_Waker/lib/Arduino_CAN_BUS_MCP2515-master/examples/set_mask_filter_recv/set_mask_filter_recv.ino b/Easylink_CAN_Waker/lib/Arduino_CAN_BUS_MCP2515-master/examples/set_mask_filter_recv/set_mask_filter_recv.ino new file mode 100644 index 0000000..902dbaf --- /dev/null +++ b/Easylink_CAN_Waker/lib/Arduino_CAN_BUS_MCP2515-master/examples/set_mask_filter_recv/set_mask_filter_recv.ino @@ -0,0 +1,116 @@ +/* receive a frame from can bus with mask and filter setting + + CAN Baudrate, + + #define CAN_5KBPS 1 + #define CAN_10KBPS 2 + #define CAN_20KBPS 3 + #define CAN_25KBPS 4 + #define CAN_31K25BPS 5 + #define CAN_33KBPS 6 + #define CAN_40KBPS 7 + #define CAN_50KBPS 8 + #define CAN_80KBPS 9 + #define CAN_83K3BPS 10 + #define CAN_95KBPS 11 + #define CAN_100KBPS 12 + #define CAN_125KBPS 13 + #define CAN_200KBPS 14 + #define CAN_250KBPS 15 + #define CAN_500KBPS 16 + #define CAN_666KBPS 17 + #define CAN_1000KBPS 18 + + CANBed V1: https://www.longan-labs.cc/1030008.html + CANBed M0: https://www.longan-labs.cc/1030014.html + CAN Bus Shield: https://www.longan-labs.cc/1030016.html + OBD-II CAN Bus GPS Dev Kit: https://www.longan-labs.cc/1030003.html + +*/ +#include +#include + +/* Please modify SPI_CS_PIN to adapt to your board. + + CANBed V1 - 17 + CANBed M0 - 3 + CAN Bus Shield - 9 + CANBed 2040 - 9 + CANBed Dual - 9 + OBD-2G Dev Kit - 9 + OBD-II GPS Kit - 9 + Hud Dev Kit - 9 + + Seeed Studio CAN-Bus Breakout Board for XIAO and QT Py - D7 +*/ + +#define SPI_CS_PIN 9 + +MCP_CAN CAN(SPI_CS_PIN); // Set CS pin + +unsigned char flagRecv = 0; +unsigned char len = 0; +unsigned char buf[8]; +char str[20]; + +void setup() +{ + Serial.begin(115200); + while(!Serial); + + // below code need for OBD-II GPS Dev Kit Atemga32U4 version + // pinMode(A3, OUTPUT); + // digitalWrite(A3, HIGH); + + // below code need for OBD-II GPS Dev Kit RP2040 version + // pinMode(12, OUTPUT); + // digitalWrite(12, HIGH); + + while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k + { + Serial.println("CAN BUS FAIL!"); + delay(100); + } + Serial.println("CAN BUS OK!"); + + /* + * set mask, set both the mask to 0x3ff + */ + CAN.init_Mask(0, 0, 0x3ff); // there are 2 mask in mcp2515, you need to set both of them + CAN.init_Mask(1, 0, 0x3ff); + + + /* + * set filter, we can receive id from 0x04 ~ 0x09 + */ + CAN.init_Filt(0, 0, 0x04); // there are 6 filter in mcp2515 + CAN.init_Filt(1, 0, 0x05); // there are 6 filter in mcp2515 + + CAN.init_Filt(2, 0, 0x06); // there are 6 filter in mcp2515 + CAN.init_Filt(3, 0, 0x07); // there are 6 filter in mcp2515 + CAN.init_Filt(4, 0, 0x08); // there are 6 filter in mcp2515 + CAN.init_Filt(5, 0, 0x09); // there are 6 filter in mcp2515 + +} + +void loop() +{ + if(CAN_MSGAVAIL == CAN.checkReceive()) // check if get data + { + CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf + + Serial.println("\r\n------------------------------------------------------------------"); + Serial.print("Get Data From id: "); + Serial.println(CAN.getCanId()); + for(int i = 0; i +#include + +/* Please modify SPI_CS_PIN to adapt to your board. + + CANBed V1 - 17 + CANBed M0 - 3 + CAN Bus Shield - 9 + CANBed 2040 - 9 + CANBed Dual - 9 + OBD-2G Dev Kit - 9 + OBD-II GPS Kit - 9 + Hud Dev Kit - 9 + + Seeed Studio CAN-Bus Breakout Board for XIAO and QT Py - D7 +*/ + +#define SPI_CS_PIN 9 + +MCP_CAN CAN(SPI_CS_PIN); // Set CS pin + +void setup() +{ + Serial.begin(115200); + while(!Serial); + + // below code need for OBD-II GPS Dev Kit Atemga32U4 version + // pinMode(A3, OUTPUT); + // digitalWrite(A3, HIGH); + + // below code need for OBD-II GPS Dev Kit RP2040 version + // pinMode(12, OUTPUT); + // digitalWrite(12, HIGH); + + while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k + { + Serial.println("CAN BUS FAIL!"); + delay(100); + } + Serial.println("CAN BUS OK!"); +} + +unsigned char stmp[8] = {0, 1, 2, 3, 4, 5, 6, 7}; + +void loop() +{ + for(int id=0; id<10; id++) + { + memset(stmp, id, sizeof(stmp)); // set id to send data buff + CAN.sendMsgBuf(id, 0, sizeof(stmp), stmp); + delay(100); + } +} + +// END FILE diff --git a/Easylink_CAN_Waker/lib/Arduino_CAN_BUS_MCP2515-master/keywords.txt b/Easylink_CAN_Waker/lib/Arduino_CAN_BUS_MCP2515-master/keywords.txt new file mode 100644 index 0000000..30b0f02 --- /dev/null +++ b/Easylink_CAN_Waker/lib/Arduino_CAN_BUS_MCP2515-master/keywords.txt @@ -0,0 +1,55 @@ +####################################### +# Syntax Coloring Map For debug_lvc +####################################### + +####################################### +# Datatypes (KEYWORD1) +####################################### +MCP_CAN KEYWORD1 +mcp_can_dfs KEYWORD1 +mcp_can KEYWORD1 +CAN KEYWORD1 + +####################################### +# Methods and Functions (KEYWORD2) +####################################### +begin KEYWORD2 +init_Mask KEYWORD2 +init_Filt KEYWORD2 +sendMsgBuf KEYWORD2 +readMsgBuf KEYWORD2 +checkReceive KEYWORD2 +checkError KEYWORD2 +getCanId KEYWORD2 + +####################################### +# Constants (LITERAL1) +####################################### +CAN_5KBPS LITERAL1 +CAN_10KBPS LITERAL1 +CAN_20KBPS LITERAL1 +CAN_25KBPS LITERAL1 +CAN_31KBPS LITERAL1 +CAN_33KBPS LITERAL1 +CAN_40KBPS LITERAL1 +CAN_50KBPS LITERAL1 +CAN_80KBPS LITERAL1 +CAN_83KBPS LITERAL1 +CAN_95KBPS LITERAL1 +CAN_100KBPS LITERAL1 +CAN_125KBPS LITERAL1 +CAN_200KBPS LITERAL1 +CAN_250KBPS LITERAL1 +CAN_500KBPS LITERAL1 +CAN_666KBPS LITERAL1 +CAN_1000KBPS LITERAL1 +CAN_OK LITERAL1 +CAN_FAILINIT LITERAL1 +CAN_FAILTX LITERAL1 +CAN_MSGAVAIL LITERAL1 +CAN_NOMSG LITERAL1 +CAN_CTRLERROR LITERAL1 +CAN_GETTXBFTIMEOUT LITERAL1 +CAN_SENDMSGTIMEOUT LITERAL1 +CAN_FAIL LITERAL1 +SPI_CS_PIN LITERAL1 \ No newline at end of file diff --git a/Easylink_CAN_Waker/lib/Arduino_CAN_BUS_MCP2515-master/library.json b/Easylink_CAN_Waker/lib/Arduino_CAN_BUS_MCP2515-master/library.json new file mode 100644 index 0000000..ec4c86f --- /dev/null +++ b/Easylink_CAN_Waker/lib/Arduino_CAN_BUS_MCP2515-master/library.json @@ -0,0 +1,13 @@ +{ + "name": "CAN_BUS_Shield", + "keywords": "can, bus, mcp2515, MCP-2515", + "description": "Arduino Library for MCP2515", + "repository": + { + "type": "git", + "url": "https://github.com/Longan-Labs/Aruino_CAN_BUS_MCP2515.git" + }, + "version": "1.0.0", + "frameworks": "arduino", + "platforms": "atmelavr" +} diff --git a/Easylink_CAN_Waker/lib/Arduino_CAN_BUS_MCP2515-master/library.properties b/Easylink_CAN_Waker/lib/Arduino_CAN_BUS_MCP2515-master/library.properties new file mode 100644 index 0000000..2e27dd8 --- /dev/null +++ b/Easylink_CAN_Waker/lib/Arduino_CAN_BUS_MCP2515-master/library.properties @@ -0,0 +1,9 @@ +name=mcp_canbus +version=1.0.0 +author=Longan Labs +maintainer=Longan Labs +sentence=A library for MCP2515 +paragraph=Arduino CAN Bus Library for MCP2515 +category=Communication +url=https://github.com/Longan-Labs/Aruino_CAN_BUS_MCP2515 +architectures=* diff --git a/Easylink_CAN_Waker/lib/Arduino_CAN_BUS_MCP2515-master/mcp_can_dfs.h b/Easylink_CAN_Waker/lib/Arduino_CAN_BUS_MCP2515-master/mcp_can_dfs.h new file mode 100644 index 0000000..659daa2 --- /dev/null +++ b/Easylink_CAN_Waker/lib/Arduino_CAN_BUS_MCP2515-master/mcp_can_dfs.h @@ -0,0 +1,356 @@ +/* + mcp_can_dfs.h + + The MIT License (MIT) + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ +#ifndef _MCP2515DFS_H_ +#define _MCP2515DFS_H_ + +#include +#include +//#include + + +// if print debug information +#define DEBUG_EN 0 + +// Begin mt + +#define TIMEOUTVALUE 50 +#define MCP_SIDH 0 +#define MCP_SIDL 1 +#define MCP_EID8 2 +#define MCP_EID0 3 + +#define MCP_TXB_EXIDE_M 0x08 // In TXBnSIDL +#define MCP_DLC_MASK 0x0F // 4 LSBits +#define MCP_RTR_MASK 0x40 // (1<<6) Bit 6 + +#define MCP_RXB_RX_ANY 0x60 +#define MCP_RXB_RX_EXT 0x40 +#define MCP_RXB_RX_STD 0x20 +#define MCP_RXB_RX_STDEXT 0x00 +#define MCP_RXB_RX_MASK 0x60 +#define MCP_RXB_BUKT_MASK (1<<2) + + +// Bits in the TXBnCTRL registers. + +#define MCP_TXB_TXBUFE_M 0x80 +#define MCP_TXB_ABTF_M 0x40 +#define MCP_TXB_MLOA_M 0x20 +#define MCP_TXB_TXERR_M 0x10 +#define MCP_TXB_TXREQ_M 0x08 +#define MCP_TXB_TXIE_M 0x04 +#define MCP_TXB_TXP10_M 0x03 + +#define MCP_TXB_RTR_M 0x40 // In TXBnDLC +#define MCP_RXB_IDE_M 0x08 // In RXBnSIDL +#define MCP_RXB_RTR_M 0x40 // In RXBnDLC + +#define MCP_STAT_RXIF_MASK (0x03) +#define MCP_STAT_RX0IF (1<<0) +#define MCP_STAT_RX1IF (1<<1) + +#define MCP_EFLG_RX1OVR (1<<7) +#define MCP_EFLG_RX0OVR (1<<6) +#define MCP_EFLG_TXBO (1<<5) +#define MCP_EFLG_TXEP (1<<4) +#define MCP_EFLG_RXEP (1<<3) +#define MCP_EFLG_TXWAR (1<<2) +#define MCP_EFLG_RXWAR (1<<1) +#define MCP_EFLG_EWARN (1<<0) +#define MCP_EFLG_ERRORMASK (0xF8) // 5 MS-Bits + +// Define MCP2515 register addresses + +#define MCP_RXF0SIDH 0x00 +#define MCP_RXF0SIDL 0x01 +#define MCP_RXF0EID8 0x02 +#define MCP_RXF0EID0 0x03 +#define MCP_RXF1SIDH 0x04 +#define MCP_RXF1SIDL 0x05 +#define MCP_RXF1EID8 0x06 +#define MCP_RXF1EID0 0x07 +#define MCP_RXF2SIDH 0x08 +#define MCP_RXF2SIDL 0x09 +#define MCP_RXF2EID8 0x0A +#define MCP_RXF2EID0 0x0B +#define MCP_CANSTAT 0x0E +#define MCP_CANCTRL 0x0F +#define MCP_RXF3SIDH 0x10 +#define MCP_RXF3SIDL 0x11 +#define MCP_RXF3EID8 0x12 +#define MCP_RXF3EID0 0x13 +#define MCP_RXF4SIDH 0x14 +#define MCP_RXF4SIDL 0x15 +#define MCP_RXF4EID8 0x16 +#define MCP_RXF4EID0 0x17 +#define MCP_RXF5SIDH 0x18 +#define MCP_RXF5SIDL 0x19 +#define MCP_RXF5EID8 0x1A +#define MCP_RXF5EID0 0x1B +#define MCP_TEC 0x1C +#define MCP_REC 0x1D +#define MCP_RXM0SIDH 0x20 +#define MCP_RXM0SIDL 0x21 +#define MCP_RXM0EID8 0x22 +#define MCP_RXM0EID0 0x23 +#define MCP_RXM1SIDH 0x24 +#define MCP_RXM1SIDL 0x25 +#define MCP_RXM1EID8 0x26 +#define MCP_RXM1EID0 0x27 +#define MCP_CNF3 0x28 +#define MCP_CNF2 0x29 +#define MCP_CNF1 0x2A +#define MCP_CANINTE 0x2B +#define MCP_CANINTF 0x2C +#define MCP_EFLG 0x2D +#define MCP_TXB0CTRL 0x30 +#define MCP_TXB1CTRL 0x40 +#define MCP_TXB2CTRL 0x50 +#define MCP_RXB0CTRL 0x60 +#define MCP_RXB0SIDH 0x61 +#define MCP_RXB1CTRL 0x70 +#define MCP_RXB1SIDH 0x71 +#define MCP_TX_INT 0x1C // Enable all transmit interrup ts +#define MCP_TX01_INT 0x0C // Enable TXB0 and TXB1 interru pts +#define MCP_RX_INT 0x03 // Enable receive interrupts +#define MCP_NO_INT 0x00 // Disable all interrupts + +#define MCP_TX01_MASK 0x14 +#define MCP_TX_MASK 0x54 + + +// Define SPI Instruction Set + +#define MCP_WRITE 0x02 +#define MCP_READ 0x03 +#define MCP_BITMOD 0x05 +#define MCP_LOAD_TX0 0x40 +#define MCP_LOAD_TX1 0x42 +#define MCP_LOAD_TX2 0x44 + +#define MCP_RTS_TX0 0x81 +#define MCP_RTS_TX1 0x82 +#define MCP_RTS_TX2 0x84 +#define MCP_RTS_ALL 0x87 +#define MCP_READ_RX0 0x90 +#define MCP_READ_RX1 0x94 +#define MCP_READ_STATUS 0xA0 +#define MCP_RX_STATUS 0xB0 +#define MCP_RESET 0xC0 + + +// CANCTRL Register Values + +#define MODE_NORMAL 0x00 +#define MODE_SLEEP 0x20 +#define MODE_LOOPBACK 0x40 +#define MODE_LISTENONLY 0x60 +#define MODE_CONFIG 0x80 +#define MODE_POWERUP 0xE0 +#define MODE_MASK 0xE0 +#define ABORT_TX 0x10 +#define MODE_ONESHOT 0x08 +#define CLKOUT_ENABLE 0x04 +#define CLKOUT_DISABLE 0x00 +#define CLKOUT_PS1 0x00 +#define CLKOUT_PS2 0x01 +#define CLKOUT_PS4 0x02 +#define CLKOUT_PS8 0x03 + + +// CNF1 Register Values + +#define SJW1 0x00 +#define SJW2 0x40 +#define SJW3 0x80 +#define SJW4 0xC0 + + +// CNF2 Register Values + +#define BTLMODE 0x80 +#define SAMPLE_1X 0x00 +#define SAMPLE_3X 0x40 + + +// CNF3 Register Values + +#define SOF_ENABLE 0x80 +#define SOF_DISABLE 0x00 +#define WAKFIL_ENABLE 0x40 +#define WAKFIL_DISABLE 0x00 + + +// CANINTF Register Bits + +#define MCP_RX0IF 0x01 +#define MCP_RX1IF 0x02 +#define MCP_TX0IF 0x04 +#define MCP_TX1IF 0x08 +#define MCP_TX2IF 0x10 +#define MCP_ERRIF 0x20 +#define MCP_WAKIF 0x40 +#define MCP_MERRF 0x80 + +// speed 16M + +#define MCP_16MHz_1000kBPS_CFG1 (0x00) +#define MCP_16MHz_1000kBPS_CFG2 (0xD0) +#define MCP_16MHz_1000kBPS_CFG3 (0x82) + +#define MCP_16MHz_500kBPS_CFG1 (0x00) +#define MCP_16MHz_500kBPS_CFG2 (0xF0) +#define MCP_16MHz_500kBPS_CFG3 (0x86) + +#define MCP_16MHz_250kBPS_CFG1 (0x41) +#define MCP_16MHz_250kBPS_CFG2 (0xF1) +#define MCP_16MHz_250kBPS_CFG3 (0x85) + +#define MCP_16MHz_200kBPS_CFG1 (0x01) +#define MCP_16MHz_200kBPS_CFG2 (0xFA) +#define MCP_16MHz_200kBPS_CFG3 (0x87) + +#define MCP_16MHz_125kBPS_CFG1 (0x03) +#define MCP_16MHz_125kBPS_CFG2 (0xF0) +#define MCP_16MHz_125kBPS_CFG3 (0x86) + +#define MCP_16MHz_100kBPS_CFG1 (0x03) +#define MCP_16MHz_100kBPS_CFG2 (0xFA) +#define MCP_16MHz_100kBPS_CFG3 (0x87) + +#define MCP_16MHz_95kBPS_CFG1 (0x03) +#define MCP_16MHz_95kBPS_CFG2 (0xAD) +#define MCP_16MHz_95kBPS_CFG3 (0x07) + +#define MCP_16MHz_83k3BPS_CFG1 (0x03) +#define MCP_16MHz_83k3BPS_CFG2 (0xBE) +#define MCP_16MHz_83k3BPS_CFG3 (0x07) + +#define MCP_16MHz_80kBPS_CFG1 (0x03) +#define MCP_16MHz_80kBPS_CFG2 (0xFF) +#define MCP_16MHz_80kBPS_CFG3 (0x87) + +#define MCP_16MHz_50kBPS_CFG1 (0x07) +#define MCP_16MHz_50kBPS_CFG2 (0xFA) +#define MCP_16MHz_50kBPS_CFG3 (0x87) + +#define MCP_16MHz_40kBPS_CFG1 (0x07) +#define MCP_16MHz_40kBPS_CFG2 (0xFF) +#define MCP_16MHz_40kBPS_CFG3 (0x87) + +#define MCP_16MHz_33kBPS_CFG1 (0x09) +#define MCP_16MHz_33kBPS_CFG2 (0xBE) +#define MCP_16MHz_33kBPS_CFG3 (0x07) + +#define MCP_16MHz_31k25BPS_CFG1 (0x0F) +#define MCP_16MHz_31k25BPS_CFG2 (0xF1) +#define MCP_16MHz_31k25BPS_CFG3 (0x85) + +#define MCP_16MHz_25kBPS_CFG1 (0X0F) +#define MCP_16MHz_25kBPS_CFG2 (0XBA) +#define MCP_16MHz_25kBPS_CFG3 (0X07) + +#define MCP_16MHz_20kBPS_CFG1 (0x0F) +#define MCP_16MHz_20kBPS_CFG2 (0xFF) +#define MCP_16MHz_20kBPS_CFG3 (0x87) + +#define MCP_16MHz_10kBPS_CFG1 (0x1F) +#define MCP_16MHz_10kBPS_CFG2 (0xFF) +#define MCP_16MHz_10kBPS_CFG3 (0x87) + +#define MCP_16MHz_5kBPS_CFG1 (0x3F) +#define MCP_16MHz_5kBPS_CFG2 (0xFF) +#define MCP_16MHz_5kBPS_CFG3 (0x87) + +#define MCP_16MHz_666kBPS_CFG1 (0x00) +#define MCP_16MHz_666kBPS_CFG2 (0xA0) +#define MCP_16MHz_666kBPS_CFG3 (0x04) + + +#define MCPDEBUG (0) +#define MCPDEBUG_TXBUF (0) +#define MCP_N_TXBUFFERS (3) + +#define MCP_RXBUF_0 (MCP_RXB0SIDH) +#define MCP_RXBUF_1 (MCP_RXB1SIDH) + +#define MCP2515_SELECT() digitalWrite(SPICS, LOW) +#define MCP2515_UNSELECT() digitalWrite(SPICS, HIGH) + +#define MCP2515_OK (0) +#define MCP2515_FAIL (1) +#define MCP_ALLTXBUSY (2) + +#define CANDEBUG 1 + +#define CANUSELOOP 0 + +#define CANSENDTIMEOUT (200) // milliseconds + + +// initial value of gCANAutoProcess + +#define CANAUTOPROCESS (1) +#define CANAUTOON (1) +#define CANAUTOOFF (0) +#define CAN_STDID (0) +#define CAN_EXTID (1) +#define CANDEFAULTIDENT (0x55CC) +#define CANDEFAULTIDENTEXT (CAN_EXTID) + +#define CAN_5KBPS 1 +#define CAN_10KBPS 2 +#define CAN_20KBPS 3 +#define CAN_25KBPS 4 +#define CAN_31K25BPS 5 +#define CAN_33KBPS 6 +#define CAN_40KBPS 7 +#define CAN_50KBPS 8 +#define CAN_80KBPS 9 +#define CAN_83K3BPS 10 +#define CAN_95KBPS 11 +#define CAN_100KBPS 12 +#define CAN_125KBPS 13 +#define CAN_200KBPS 14 +#define CAN_250KBPS 15 +#define CAN_500KBPS 16 +#define CAN_666KBPS 17 +#define CAN_1000KBPS 18 + +#define CAN_OK (0) +#define CAN_FAILINIT (1) +#define CAN_FAILTX (2) +#define CAN_MSGAVAIL (3) +#define CAN_NOMSG (4) +#define CAN_CTRLERROR (5) +#define CAN_GETTXBFTIMEOUT (6) +#define CAN_SENDMSGTIMEOUT (7) +#define CAN_FAIL (0xff) + +#define CAN_MAX_CHAR_IN_MESSAGE (8) + +#endif +/********************************************************************************************************* + END FILE +*********************************************************************************************************/ diff --git a/Easylink_CAN_Waker/lib/Arduino_CAN_BUS_MCP2515-master/mcp_canbus.cpp b/Easylink_CAN_Waker/lib/Arduino_CAN_BUS_MCP2515-master/mcp_canbus.cpp new file mode 100644 index 0000000..929fe29 --- /dev/null +++ b/Easylink_CAN_Waker/lib/Arduino_CAN_BUS_MCP2515-master/mcp_canbus.cpp @@ -0,0 +1,971 @@ +/* + mcp_can.cpp + + Author:Stephen Lo + 2014-1-16 + + Contributor: + + Cory J. Fowler + Latonita + Woodward1 + Mehtajaghvi + BykeBlast + TheRo0T + Tsipizic + ralfEdmund + Nathancheek + BlueAndi + Adlerweb + Btetz + Hurvajs + xboxpro1 + + The MIT License (MIT) + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ +#include "mcp_canbus.h" + +#define spi_readwrite SPI.transfer +#define spi_read() spi_readwrite(0x00) +#define SPI_BEGIN() SPI.beginTransaction(SPISettings(10000000, MSBFIRST, SPI_MODE0)) +#define SPI_END() SPI.endTransaction() + +/********************************************************************************************************* +** Function name: mcp2515_reset +** Descriptions: reset the device +*********************************************************************************************************/ +void MCP_CAN::mcp2515_reset(void) +{ +#ifdef SPI_HAS_TRANSACTION + SPI_BEGIN(); +#endif + MCP2515_SELECT(); + spi_readwrite(MCP_RESET); + MCP2515_UNSELECT(); +#ifdef SPI_HAS_TRANSACTION + SPI_END(); +#endif + delay(10); +} + +/********************************************************************************************************* +** Function name: mcp2515_readRegister +** Descriptions: read register +*********************************************************************************************************/ +byte MCP_CAN::mcp2515_readRegister(const byte address) +{ + byte ret; + +#ifdef SPI_HAS_TRANSACTION + SPI_BEGIN(); +#endif + MCP2515_SELECT(); + spi_readwrite(MCP_READ); + spi_readwrite(address); + ret = spi_read(); + MCP2515_UNSELECT(); +#ifdef SPI_HAS_TRANSACTION + SPI_END(); +#endif + + return ret; +} + +/********************************************************************************************************* +** Function name: mcp2515_readRegisterS +** Descriptions: read registerS +*********************************************************************************************************/ +void MCP_CAN::mcp2515_readRegisterS(const byte address, byte values[], const byte n) +{ + byte i; +#ifdef SPI_HAS_TRANSACTION + SPI_BEGIN(); +#endif + MCP2515_SELECT(); + spi_readwrite(MCP_READ); + spi_readwrite(address); + // mcp2515 has auto-increment of address-pointer + for(i=0; i 0) + { +#if DEBUG_EN + Serial.print("Enter setting mode fall\r\n"); +#else + delay(10); +#endif + return res; + } +#if DEBUG_EN + Serial.print("Enter setting mode success \r\n"); +#else + delay(10); +#endif + + // set boadrate + if(mcp2515_configRate(canSpeed)) + { +#if DEBUG_EN + Serial.print("set rate fall!!\r\n"); +#else + delay(10); +#endif + return res; + } +#if DEBUG_EN + Serial.print("set rate success!!\r\n"); +#else + delay(10); +#endif + + if(res == MCP2515_OK) { + + // init canbuffers + mcp2515_initCANBuffers(); + + // interrupt mode + mcp2515_setRegister(MCP_CANINTE, MCP_RX0IF | MCP_RX1IF); + +#if (DEBUG_RXANY==1) + // enable both receive-buffers to receive any message and enable rollover + mcp2515_modifyRegister(MCP_RXB0CTRL, + MCP_RXB_RX_MASK | MCP_RXB_BUKT_MASK, + MCP_RXB_RX_ANY | MCP_RXB_BUKT_MASK); + mcp2515_modifyRegister(MCP_RXB1CTRL, MCP_RXB_RX_MASK, + MCP_RXB_RX_ANY); +#else + // enable both receive-buffers to receive messages with std. and ext. identifiers and enable rollover + mcp2515_modifyRegister(MCP_RXB0CTRL, + MCP_RXB_RX_MASK | MCP_RXB_BUKT_MASK, + MCP_RXB_RX_STDEXT | MCP_RXB_BUKT_MASK); + mcp2515_modifyRegister(MCP_RXB1CTRL, MCP_RXB_RX_MASK, + MCP_RXB_RX_STDEXT); +#endif + // enter normal mode + res = mcp2515_setCANCTRL_Mode(MODE_NORMAL); + if(res) + { +#if DEBUG_EN + Serial.print("Enter Normal Mode Fall!!\r\n"); +#else + delay(10); +#endif + return res; + } + + +#if DEBUG_EN + Serial.print("Enter Normal Mode Success!!\r\n"); +#else + delay(10); +#endif + + } + return res; + +} + +/********************************************************************************************************* +** Function name: mcp2515_write_id +** Descriptions: write can id +*********************************************************************************************************/ +void MCP_CAN::mcp2515_write_id(const byte mcp_addr, const byte ext, const unsigned long id) +{ + uint16_t canid; + byte tbufdata[4]; + + canid = (uint16_t)(id & 0x0FFFF); + + if(ext == 1) + { + tbufdata[MCP_EID0] = (byte) (canid & 0xFF); + tbufdata[MCP_EID8] = (byte) (canid >> 8); + canid = (uint16_t)(id >> 16); + tbufdata[MCP_SIDL] = (byte) (canid & 0x03); + tbufdata[MCP_SIDL] += (byte) ((canid & 0x1C) << 3); + tbufdata[MCP_SIDL] |= MCP_TXB_EXIDE_M; + tbufdata[MCP_SIDH] = (byte) (canid >> 5); + } + else + { + tbufdata[MCP_SIDH] = (byte) (canid >> 3); + tbufdata[MCP_SIDL] = (byte) ((canid & 0x07) << 5); + tbufdata[MCP_EID0] = 0; + tbufdata[MCP_EID8] = 0; + } + mcp2515_setRegisterS(mcp_addr, tbufdata, 4); +} + +/********************************************************************************************************* +** Function name: mcp2515_read_id +** Descriptions: read can id +*********************************************************************************************************/ +void MCP_CAN::mcp2515_read_id(const byte mcp_addr, byte* ext, unsigned long* id) +{ + byte tbufdata[4]; + + *ext = 0; + *id = 0; + + mcp2515_readRegisterS(mcp_addr, tbufdata, 4); + + *id = (tbufdata[MCP_SIDH]<<3) + (tbufdata[MCP_SIDL]>>5); + + if((tbufdata[MCP_SIDL] & MCP_TXB_EXIDE_M) == MCP_TXB_EXIDE_M) + { + // extended id + *id = (*id<<2) + (tbufdata[MCP_SIDL] & 0x03); + *id = (*id<<8) + tbufdata[MCP_EID8]; + *id = (*id<<8) + tbufdata[MCP_EID0]; + *ext = 1; + } +} + +/********************************************************************************************************* +** Function name: mcp2515_write_canMsg +** Descriptions: write msg +*********************************************************************************************************/ +void MCP_CAN::mcp2515_write_canMsg(const byte buffer_sidh_addr, int rtrBit) +{ + byte mcp_addr; + mcp_addr = buffer_sidh_addr; + mcp2515_setRegisterS(mcp_addr+5, dta, dta_len); // write data bytes + // Serial.print("RTR: "); + // Serial.println(rtrBit); + if(rtrBit == 1) // if RTR set bit in byte + { + dta_len |= MCP_RTR_MASK; + } + mcp2515_setRegister((mcp_addr+4), dta_len); // write the RTR and DLC + mcp2515_write_id(mcp_addr, ext_flg, can_id); // write CAN id + +} + +/********************************************************************************************************* +** Function name: mcp2515_read_canMsg +** Descriptions: read message +*********************************************************************************************************/ +void MCP_CAN::mcp2515_read_canMsg(const byte buffer_sidh_addr) // read can msg +{ + byte mcp_addr, ctrl; + + mcp_addr = buffer_sidh_addr; + mcp2515_read_id(mcp_addr, &ext_flg,&can_id); + ctrl = mcp2515_readRegister(mcp_addr-1); + dta_len = mcp2515_readRegister(mcp_addr+4); + + rtr = (ctrl & 0x08) ? 1 : 0; + + dta_len &= MCP_DLC_MASK; + mcp2515_readRegisterS(mcp_addr+5, &(dta[0]), dta_len); +} + +/********************************************************************************************************* +** Function name: mcp2515_start_transmit +** Descriptions: start transmit +*********************************************************************************************************/ +void MCP_CAN::mcp2515_start_transmit(const byte mcp_addr) // start transmit +{ + mcp2515_modifyRegister(mcp_addr-1 , MCP_TXB_TXREQ_M, MCP_TXB_TXREQ_M); +} + +/********************************************************************************************************* +** Function name: mcp2515_getNextFreeTXBuf +** Descriptions: get Next free txbuf +*********************************************************************************************************/ +byte MCP_CAN::mcp2515_getNextFreeTXBuf(byte *txbuf_n) // get Next free txbuf +{ + byte res, i, ctrlval; + byte ctrlregs[MCP_N_TXBUFFERS] = { MCP_TXB0CTRL, MCP_TXB1CTRL, MCP_TXB2CTRL }; + + res = MCP_ALLTXBUSY; + *txbuf_n = 0x00; + + // check all 3 TX-Buffers + for(i=0; i 0){ +#if DEBUG_EN + Serial.print("Enter setting mode fall\r\n"); +#else + delay(10); +#endif + return res; + } + + if(num == 0){ + mcp2515_write_id(MCP_RXM0SIDH, ext, ulData); + + } + else if(num == 1){ + mcp2515_write_id(MCP_RXM1SIDH, ext, ulData); + } + else res = MCP2515_FAIL; + + res = mcp2515_setCANCTRL_Mode(MODE_NORMAL); + if(res > 0){ +#if DEBUG_EN + Serial.print("Enter normal mode fall\r\n"); +#else + delay(10); +#endif + return res; + } +#if DEBUG_EN + Serial.print("set Mask success!!\r\n"); +#else + delay(10); +#endif + return res; +} + +/********************************************************************************************************* +** Function name: init_Filt +** Descriptions: init canid filters +*********************************************************************************************************/ +byte MCP_CAN::init_Filt(byte num, byte ext, unsigned long ulData) +{ + byte res = MCP2515_OK; +#if DEBUG_EN + Serial.print("Begin to set Filter!!\r\n"); +#else + delay(10); +#endif + res = mcp2515_setCANCTRL_Mode(MODE_CONFIG); + if(res > 0) + { +#if DEBUG_EN + Serial.print("Enter setting mode fall\r\n"); +#else + delay(10); +#endif + return res; + } + + switch(num) + { + case 0: + mcp2515_write_id(MCP_RXF0SIDH, ext, ulData); + break; + + case 1: + mcp2515_write_id(MCP_RXF1SIDH, ext, ulData); + break; + + case 2: + mcp2515_write_id(MCP_RXF2SIDH, ext, ulData); + break; + + case 3: + mcp2515_write_id(MCP_RXF3SIDH, ext, ulData); + break; + + case 4: + mcp2515_write_id(MCP_RXF4SIDH, ext, ulData); + break; + + case 5: + mcp2515_write_id(MCP_RXF5SIDH, ext, ulData); + break; + + default: + res = MCP2515_FAIL; + } + + res = mcp2515_setCANCTRL_Mode(MODE_NORMAL); + if(res > 0) + { +#if DEBUG_EN + Serial.print("Enter normal mode fall\r\nSet filter fail!!\r\n"); +#else + delay(10); +#endif + return res; + } +#if DEBUG_EN + Serial.print("set Filter success!!\r\n"); +#else + delay(10); +#endif + + return res; +} + +/********************************************************************************************************* +** Function name: setMsg +** Descriptions: set can message, such as dlc, id, dta[] and so on +*********************************************************************************************************/ +byte MCP_CAN::setMsg(unsigned long id, byte ext, byte len, byte rtr, byte *pData) +{ + ext_flg = ext; + can_id = id; + //dta_len = min(len, MAX_CHAR_IN_MESSAGE); + dta_len = len > MAX_CHAR_IN_MESSAGE ? MAX_CHAR_IN_MESSAGE: len; + rtr = rtr; + for(int i = 0; i -#include +#include #include MCP_CAN CAN0(53); // Set CS pin for MCP2515 -#define TX_DELAY 50 // Delay between messages +#define TX_DELAY 3 // Delay between messages byte sndStat; // Status of the sent message (global variable) @@ -1441,15 +1441,17 @@ void sendCanMessage(unsigned long canId, const char* hexString) { void setup() { Serial.begin(115200); - + Serial.println("CAN_Waker starting up..."); // Initialize MCP2515 - if (CAN0.begin(MCP_STD, CAN_500KBPS, MCP_8MHZ) == CAN_OK) { + if (CAN0.begin(CAN_500KBPS) == CAN_OK) { Serial.println("MCP2515 Initialized Successfully!"); } else { Serial.println("Error Initializing MCP2515..."); } - CAN0.setMode(MCP_NORMAL); // Change to normal mode + //CAN0.setMode(MCP_NORMAL); // Change to normal mode + //CAN0.setMode(MODE_ONESHOT); // Change to normal mode + delay(2000); } void loop() {