diff --git a/examples/OBDII_PIDs/OBDII_PIDs.ino b/examples/OBDII_PIDs/OBDII_PIDs.ino index ff923a6..f3fa1e4 100644 --- a/examples/OBDII_PIDs/OBDII_PIDs.ino +++ b/examples/OBDII_PIDs/OBDII_PIDs.ino @@ -19,28 +19,32 @@ #define CAN_2515 // #define CAN_2518FD -// the cs pin of the version after v1.1 is default to D9 -// v0.9b and v1.0 is default D10 - // Set SPI CS Pin according to your hardware + +#if defined(SEEED_WIO_TERMINAL) && defined(CAN_2518FD) // For Wio Terminal w/ MCP2518FD RPi Hat: // Channel 0 SPI_CS Pin: BCM 8 // Channel 1 SPI_CS Pin: BCM 7 // Interupt Pin: BCM25 -// ***************************************** +const int SPI_CS_PIN = BCM8; +const int CAN_INT_PIN = BCM25; +#else + // For Arduino MCP2515 Hat: -// SPI_CS Pin: D9 +// the cs pin of the version after v1.1 is default to D9 +// v0.9b and v1.0 is default D10 +const int SPI_CS_PIN = 9; +const int CAN_INT_PIN = 2; +#endif #ifdef CAN_2518FD #include "mcp2518fd_can.h" -const int SPI_CS_PIN = 9; mcp2518fd CAN(SPI_CS_PIN); // Set CS pin #endif #ifdef CAN_2515 #include "mcp2515_can.h" -const int SPI_CS_PIN = 9; mcp2515_can CAN(SPI_CS_PIN); // Set CS pin #endif @@ -82,11 +86,8 @@ void sendPid(unsigned char __pid) { void setup() { SERIAL_PORT_MONITOR.begin(115200); while(!Serial){}; -#ifdef CAN_2518FD - while (0 != CAN.begin((byte)CAN_500K_1M)) { // init can bus : baudrate = 500k -#else + while (CAN_OK != CAN.begin(CAN_500KBPS)) { // init can bus : baudrate = 500k -#endif SERIAL_PORT_MONITOR.println("CAN init fail, retry..."); delay(100); } diff --git a/examples/canbus-monitor/canbus-monitor.ino b/examples/canbus-monitor/canbus-monitor.ino index 60af5be..44248eb 100644 --- a/examples/canbus-monitor/canbus-monitor.ino +++ b/examples/canbus-monitor/canbus-monitor.ino @@ -15,13 +15,13 @@ #include "mcp_can.h" #include "can-serial.h" -//#define CAN_2518FD +// #define CAN_2518FD #define CAN_2515 #ifdef CAN_2518FD #include "mcp2518fd_can.h" const int SPI_CS_PIN = BCM8; - const int CAN_INT_PIN = BCM25; + const int CAN_INT_PIN = BCM25;; mcp2518fd CAN(SPI_CS_PIN); // Set CS pin #endif @@ -30,8 +30,7 @@ const int SPI_CS_PIN = 9; const int CAN_INT_PIN = 2; mcp2515_can CAN(SPI_CS_PIN); // Set CS pin -#endif // Set CS pin - +#endif void setup() { diff --git a/examples/gpioRead/gpioRead.ino b/examples/gpioRead/gpioRead.ino index 88895e4..d40cb82 100644 --- a/examples/gpioRead/gpioRead.ino +++ b/examples/gpioRead/gpioRead.ino @@ -7,19 +7,24 @@ #define CAN_2515 // #define CAN_2518FD -// the cs pin of the version after v1.1 is default to D9 -// v0.9b and v1.0 is default D10 - // Set SPI CS Pin according to your hardware + +#if defined(SEEED_WIO_TERMINAL) && defined(CAN_2518FD) // For Wio Terminal w/ MCP2518FD RPi Hat: // Channel 0 SPI_CS Pin: BCM 8 // Channel 1 SPI_CS Pin: BCM 7 // Interupt Pin: BCM25 -// ***************************************** +const int SPI_CS_PIN = BCM8; +const int CAN_INT_PIN = BCM25; +#else + // For Arduino MCP2515 Hat: -// SPI_CS Pin: D9 +// the cs pin of the version after v1.1 is default to D9 +// v0.9b and v1.0 is default D10 +const int SPI_CS_PIN = 9; +const int CAN_INT_PIN = 2; +#endif -#define SPI_CS_PIN 9 #ifdef CAN_2518FD mcp2518fd CAN(SPI_CS_PIN); // Set CS pin @@ -31,13 +36,8 @@ mcp2515_can CAN(SPI_CS_PIN); // Set CS pin void setup() { SERIAL_PORT_MONITOR.begin(115200); -#ifdef CAN_2518FD - while (0 != CAN.begin((byte)CAN_500K_1M)) { // init can bus : baudrate = 500k -#endif -#ifdef CAN_2515 + while (CAN_OK != CAN.begin(CAN_500KBPS)) { -#endif - SERIAL_PORT_MONITOR.println("CAN init failed, retry..."); delay(100); } diff --git a/examples/gpioWrite/gpioWrite.ino b/examples/gpioWrite/gpioWrite.ino index 35dae4b..5751169 100644 --- a/examples/gpioWrite/gpioWrite.ino +++ b/examples/gpioWrite/gpioWrite.ino @@ -7,19 +7,24 @@ #define CAN_2515 // #define CAN_2518FD -// the cs pin of the version after v1.1 is default to D9 -// v0.9b and v1.0 is default D10 - // Set SPI CS Pin according to your hardware + +#if defined(SEEED_WIO_TERMINAL) && defined(CAN_2518FD) // For Wio Terminal w/ MCP2518FD RPi Hat: // Channel 0 SPI_CS Pin: BCM 8 // Channel 1 SPI_CS Pin: BCM 7 // Interupt Pin: BCM25 -// ***************************************** +const int SPI_CS_PIN = BCM8; +const int CAN_INT_PIN = BCM25; +#else + // For Arduino MCP2515 Hat: -// SPI_CS Pin: D9 +// the cs pin of the version after v1.1 is default to D9 +// v0.9b and v1.0 is default D10 +const int SPI_CS_PIN = 9; +const int CAN_INT_PIN = 2; +#endif -#define SPI_CS_PIN 9 #ifdef CAN_2518FD mcp2518fd CAN(SPI_CS_PIN); // Set CS pin @@ -33,12 +38,7 @@ mcp2515_can CAN(SPI_CS_PIN); // Set CS pin void setup() { SERIAL_PORT_MONITOR.begin(115200); -#ifdef CAN_2518FD - while (0 != CAN.begin((byte)CAN_500K_1M)) { // init can bus : baudrate = 500k -#endif -#ifdef CAN_2515 - while (CAN_OK != CAN.begin(CAN_500KBPS)) { -#endif // init can bus : baudrate = 500k + while (CAN_OK != CAN.begin(CAN_500KBPS)) { // init can bus : baudrate = 500k SERIAL_PORT_MONITOR.println("CAN init failed, retry"); delay(100); } diff --git a/examples/receive_Blink/receive_Blink.ino b/examples/receive_Blink/receive_Blink.ino index 8241ecc..66be957 100644 --- a/examples/receive_Blink/receive_Blink.ino +++ b/examples/receive_Blink/receive_Blink.ino @@ -7,31 +7,34 @@ #define CAN_2515 // #define CAN_2518FD -// the cs pin of the version after v1.1 is default to D9 -// v0.9b and v1.0 is default D10 - // Set SPI CS Pin according to your hardware + +#if defined(SEEED_WIO_TERMINAL) && defined(CAN_2518FD) // For Wio Terminal w/ MCP2518FD RPi Hat: // Channel 0 SPI_CS Pin: BCM 8 // Channel 1 SPI_CS Pin: BCM 7 // Interupt Pin: BCM25 -// ***************************************** +const int SPI_CS_PIN = BCM8; +const int CAN_INT_PIN = BCM25; +#else + // For Arduino MCP2515 Hat: -// SPI_CS Pin: D9 +// the cs pin of the version after v1.1 is default to D9 +// v0.9b and v1.0 is default D10 +const int SPI_CS_PIN = 9; +const int CAN_INT_PIN = 2; +#endif + #ifdef CAN_2518FD #include "mcp2518fd_can.h" -const int SPI_CS_PIN = 9; -const int CAN_INT_PIN = 2; mcp2518fd CAN(SPI_CS_PIN); // Set CS pin #endif #ifdef CAN_2515 #include "mcp2515_can.h" -const int SPI_CS_PIN = 9; -const int CAN_INT_PIN = 2; mcp2515_can CAN(SPI_CS_PIN); // Set CS pin -#endif // Set CS pin +#endif const int LED = 8; boolean ledON = 1; @@ -40,11 +43,7 @@ void setup() { SERIAL_PORT_MONITOR.begin(115200); pinMode(LED, OUTPUT); -#ifdef CAN_2518FD - while (0 != CAN.begin((byte)CAN_500K_1M)) { // init can bus : baudrate = 500k -#else while (CAN_OK != CAN.begin(CAN_500KBPS)) { // init can bus : baudrate = 500k -#endif SERIAL_PORT_MONITOR.println("CAN init fail, retry..."); delay(100); } diff --git a/examples/receive_check/receive_check.ino b/examples/receive_check/receive_check.ino index a955b7b..304ff33 100644 --- a/examples/receive_check/receive_check.ino +++ b/examples/receive_check/receive_check.ino @@ -6,40 +6,39 @@ #define CAN_2515 // #define CAN_2518FD -// the cs pin of the version after v1.1 is default to D9 -// v0.9b and v1.0 is default D10 - // Set SPI CS Pin according to your hardware + +#if defined(SEEED_WIO_TERMINAL) && defined(CAN_2518FD) // For Wio Terminal w/ MCP2518FD RPi Hat: // Channel 0 SPI_CS Pin: BCM 8 // Channel 1 SPI_CS Pin: BCM 7 // Interupt Pin: BCM25 -// ***************************************** +const int SPI_CS_PIN = BCM8; +const int CAN_INT_PIN = BCM25; +#else + // For Arduino MCP2515 Hat: -// SPI_CS Pin: D9 +// the cs pin of the version after v1.1 is default to D9 +// v0.9b and v1.0 is default D10 +const int SPI_CS_PIN = 9; +const int CAN_INT_PIN = 2; +#endif + #ifdef CAN_2518FD #include "mcp2518fd_can.h" -const int SPI_CS_PIN = 9; -const int CAN_INT_PIN = 2; mcp2518fd CAN(SPI_CS_PIN); // Set CS pin #endif #ifdef CAN_2515 #include "mcp2515_can.h" -const int SPI_CS_PIN = 9; -const int CAN_INT_PIN = 2; mcp2515_can CAN(SPI_CS_PIN); // Set CS pin #endif void setup() { SERIAL_PORT_MONITOR.begin(115200); -#ifdef CAN_2518FD - while (0 != CAN.begin((byte)CAN_500K_1M)) { // init can bus : baudrate = 500k -#else while (CAN_OK != CAN.begin(CAN_500KBPS)) { // init can bus : baudrate = 500k -#endif SERIAL_PORT_MONITOR.println("CAN init fail, retry..."); delay(100); } diff --git a/examples/receive_interrupt/receive_interrupt.ino b/examples/receive_interrupt/receive_interrupt.ino index fb7da83..c65ad16 100644 --- a/examples/receive_interrupt/receive_interrupt.ino +++ b/examples/receive_interrupt/receive_interrupt.ino @@ -7,31 +7,34 @@ #define CAN_2515 // #define CAN_2518FD -// the cs pin of the version after v1.1 is default to D9 -// v0.9b and v1.0 is default D10 - // Set SPI CS Pin according to your hardware + +#if defined(SEEED_WIO_TERMINAL) && defined(CAN_2518FD) // For Wio Terminal w/ MCP2518FD RPi Hat: // Channel 0 SPI_CS Pin: BCM 8 // Channel 1 SPI_CS Pin: BCM 7 // Interupt Pin: BCM25 -// ***************************************** +const int SPI_CS_PIN = BCM8; +const int CAN_INT_PIN = BCM25; +#else + // For Arduino MCP2515 Hat: -// SPI_CS Pin: D9 +// the cs pin of the version after v1.1 is default to D9 +// v0.9b and v1.0 is default D10 +const int SPI_CS_PIN = 9; +const int CAN_INT_PIN = 2; +#endif + #ifdef CAN_2518FD #include "mcp2518fd_can.h" -const int SPI_CS_PIN = 9; -const int CAN_INT_PIN = 2; mcp2518fd CAN(SPI_CS_PIN); // Set CS pin #endif #ifdef CAN_2515 #include "mcp2515_can.h" -const int SPI_CS_PIN = 9; -const int CAN_INT_PIN = 2; mcp2515_can CAN(SPI_CS_PIN); // Set CS pin -#endif // Set CS pin +#endif unsigned char flagRecv = 0; @@ -45,11 +48,7 @@ void setup() { ; // wait for serial port to connect. Needed for native USB port only } attachInterrupt(digitalPinToInterrupt(CAN_INT_PIN), MCP2515_ISR, FALLING); // start interrupt -#ifdef CAN_2518FD - while (0 != CAN.begin((byte)CAN_500K_1M)) { // init can bus : baudrate = 500k -#else while (CAN_OK != CAN.begin(CAN_500KBPS)) { // init can bus : baudrate = 500k -#endif // init can bus : baudrate = 500k SERIAL_PORT_MONITOR.println("CAN init fail, retry..."); delay(100); } diff --git a/examples/receive_monitor/receive_monitor.ino b/examples/receive_monitor/receive_monitor.ino index 2f199bc..e0f5d68 100644 --- a/examples/receive_monitor/receive_monitor.ino +++ b/examples/receive_monitor/receive_monitor.ino @@ -9,42 +9,42 @@ #define CAN_2515 // #define CAN_2518FD -// the cs pin of the version after v1.1 is default to D9 -// v0.9b and v1.0 is default D10 - // Set SPI CS Pin according to your hardware + +#if defined(SEEED_WIO_TERMINAL) && defined(CAN_2518FD) // For Wio Terminal w/ MCP2518FD RPi Hat: // Channel 0 SPI_CS Pin: BCM 8 // Channel 1 SPI_CS Pin: BCM 7 // Interupt Pin: BCM25 -// ***************************************** +const int SPI_CS_PIN = BCM8; +const int CAN_INT_PIN = BCM25; +#else + // For Arduino MCP2515 Hat: -// SPI_CS Pin: D9 +// the cs pin of the version after v1.1 is default to D9 +// v0.9b and v1.0 is default D10 +const int SPI_CS_PIN = 9; +const int CAN_INT_PIN = 2; +#endif + #ifdef CAN_2518FD #include "mcp2518fd_can.h" -const int SPI_CS_PIN = 9; -const int CAN_INT_PIN = 2; mcp2518fd CAN(SPI_CS_PIN); // Set CS pin #define MAX_DATA_SIZE 64 #endif #ifdef CAN_2515 #include "mcp2515_can.h" -const int SPI_CS_PIN = 9; -const int CAN_INT_PIN = 2; mcp2515_can CAN(SPI_CS_PIN); // Set CS pin #define MAX_DATA_SIZE 8 #endif void setup() { SERIAL_PORT_MONITOR.begin(115200); + while (!SERIAL_PORT_MONITOR) {} -#ifdef CAN_2518FD - while (0 != CAN.begin((byte)CAN_500K_1M)) { // init can bus : baudrate = 500k -#else while (CAN_OK != CAN.begin(CAN_500KBPS)) { // init can bus : baudrate = 500k -#endif SERIAL_PORT_MONITOR.println(F("CAN init fail, retry...")); delay(100); } @@ -62,7 +62,7 @@ void loop() { return; } - char prbuf[0x40]; + char prbuf[32 + MAX_DATA_SIZE * 3]; int i, n; unsigned long t = millis(); diff --git a/examples/recv_sd/recv_sd.ino b/examples/recv_sd/recv_sd.ino index b791cba..811b078 100644 --- a/examples/recv_sd/recv_sd.ino +++ b/examples/recv_sd/recv_sd.ino @@ -11,31 +11,34 @@ File myFile; #define CAN_2515 // #define CAN_2518FD -// the cs pin of the version after v1.1 is default to D9 -// v0.9b and v1.0 is default D10 - // Set SPI CS Pin according to your hardware + +#if defined(SEEED_WIO_TERMINAL) && defined(CAN_2518FD) // For Wio Terminal w/ MCP2518FD RPi Hat: // Channel 0 SPI_CS Pin: BCM 8 // Channel 1 SPI_CS Pin: BCM 7 // Interupt Pin: BCM25 -// ***************************************** +const int SPI_CS_PIN = BCM8; +const int CAN_INT_PIN = BCM25; +#else + // For Arduino MCP2515 Hat: -// SPI_CS Pin: D9 +// the cs pin of the version after v1.1 is default to D9 +// v0.9b and v1.0 is default D10 +const int SPI_CS_PIN = 9; +const int CAN_INT_PIN = 2; +#endif + #ifdef CAN_2518FD #include "mcp2518fd_can.h" -const int SPI_CS_PIN = 9; -const int CAN_INT_PIN = 2; mcp2518fd CAN(SPI_CS_PIN); // Set CS pin #endif #ifdef CAN_2515 #include "mcp2515_can.h" -const int SPI_CS_PIN = 9; -const int CAN_INT_PIN = 2; mcp2515_can CAN(SPI_CS_PIN); // Set CS pin -#endif // Set CS pin +#endif unsigned char flagRecv = 0; unsigned char len = 0; @@ -45,11 +48,7 @@ char str[20]; void setup() { SERIAL_PORT_MONITOR.begin(115200); attachInterrupt(digitalPinToInterrupt(CAN_INT_PIN), MCP2515_ISR, FALLING); // start interrupt -#ifdef CAN_2518FD - while (0 != CAN.begin((byte)CAN_500K_1M)) { // init can bus : baudrate = 500k -#else while (CAN_OK != CAN.begin(CAN_500KBPS)) { // init can bus : baudrate = 500k -#endif SERIAL_PORT_MONITOR.println("CAN init fail, retry..."); delay(100); } diff --git a/examples/send/send.ino b/examples/send/send.ino index 186c129..b44ae14 100644 --- a/examples/send/send.ino +++ b/examples/send/send.ino @@ -7,41 +7,40 @@ #define CAN_2515 // #define CAN_2518FD -// the cs pin of the version after v1.1 is default to D9 -// v0.9b and v1.0 is default D10 - // Set SPI CS Pin according to your hardware + +#if defined(SEEED_WIO_TERMINAL) && defined(CAN_2518FD) // For Wio Terminal w/ MCP2518FD RPi Hat: // Channel 0 SPI_CS Pin: BCM 8 // Channel 1 SPI_CS Pin: BCM 7 // Interupt Pin: BCM25 -// ***************************************** +const int SPI_CS_PIN = BCM8; +const int CAN_INT_PIN = BCM25; +#else + // For Arduino MCP2515 Hat: -// SPI_CS Pin: D9 +// the cs pin of the version after v1.1 is default to D9 +// v0.9b and v1.0 is default D10 +const int SPI_CS_PIN = 9; +const int CAN_INT_PIN = 2; +#endif + #ifdef CAN_2518FD #include "mcp2518fd_can.h" -const int SPI_CS_PIN = 9; -const int CAN_INT_PIN = 2; mcp2518fd CAN(SPI_CS_PIN); // Set CS pin #endif #ifdef CAN_2515 #include "mcp2515_can.h" -const int SPI_CS_PIN = 9; -const int CAN_INT_PIN = 2; mcp2515_can CAN(SPI_CS_PIN); // Set CS pin -#endif // Set CS pin +#endif void setup() { SERIAL_PORT_MONITOR.begin(115200); while(!Serial){}; -#ifdef CAN_2518FD - while (0 != CAN.begin((byte)CAN_500K_1M)) { // init can bus : baudrate = 500k -#else while (CAN_OK != CAN.begin(CAN_500KBPS)) { // init can bus : baudrate = 500k -#endif SERIAL_PORT_MONITOR.println("CAN init fail, retry..."); delay(100); } diff --git a/examples/send_Blink/send_Blink.ino b/examples/send_Blink/send_Blink.ino index 78377ee..40f2902 100644 --- a/examples/send_Blink/send_Blink.ino +++ b/examples/send_Blink/send_Blink.ino @@ -4,31 +4,34 @@ #define CAN_2515 // #define CAN_2518FD -// the cs pin of the version after v1.1 is default to D9 -// v0.9b and v1.0 is default D10 - // Set SPI CS Pin according to your hardware + +#if defined(SEEED_WIO_TERMINAL) && defined(CAN_2518FD) // For Wio Terminal w/ MCP2518FD RPi Hat: // Channel 0 SPI_CS Pin: BCM 8 // Channel 1 SPI_CS Pin: BCM 7 // Interupt Pin: BCM25 -// ***************************************** +const int SPI_CS_PIN = BCM8; +const int CAN_INT_PIN = BCM25; +#else + // For Arduino MCP2515 Hat: -// SPI_CS Pin: D9 +// the cs pin of the version after v1.1 is default to D9 +// v0.9b and v1.0 is default D10 +const int SPI_CS_PIN = 9; +const int CAN_INT_PIN = 2; +#endif + #ifdef CAN_2518FD #include "mcp2518fd_can.h" -const int SPI_CS_PIN = 9; -const int CAN_INT_PIN = 2; mcp2518fd CAN(SPI_CS_PIN); // Set CS pin #endif #ifdef CAN_2515 #include "mcp2515_can.h" -const int SPI_CS_PIN = 9; -const int CAN_INT_PIN = 2; mcp2515_can CAN(SPI_CS_PIN); // Set CS pin -#endif // Set CS pin +#endif const int ledHIGH = 1; const int ledLOW = 0; @@ -37,11 +40,7 @@ const int ledLOW = 0; void setup() { SERIAL_PORT_MONITOR.begin(115200); -#ifdef CAN_2518FD - while (0 != CAN.begin((byte)CAN_500K_1M)) { // init can bus : baudrate = 500k -#else while (CAN_OK != CAN.begin(CAN_500KBPS)) { // init can bus : baudrate = 500k -#endif SERIAL_PORT_MONITOR.println("CAN init fail, retry..."); delay(100); } diff --git a/examples/send_random/send_random.ino b/examples/send_random/send_random.ino index 8878e85..45ed51d 100644 --- a/examples/send_random/send_random.ino +++ b/examples/send_random/send_random.ino @@ -9,43 +9,42 @@ #define CAN_2515 // #define CAN_2518FD -// the cs pin of the version after v1.1 is default to D9 -// v0.9b and v1.0 is default D10 - // Set SPI CS Pin according to your hardware + +#if defined(SEEED_WIO_TERMINAL) && defined(CAN_2518FD) // For Wio Terminal w/ MCP2518FD RPi Hat: // Channel 0 SPI_CS Pin: BCM 8 // Channel 1 SPI_CS Pin: BCM 7 // Interupt Pin: BCM25 -// ***************************************** +const int SPI_CS_PIN = BCM8; +const int CAN_INT_PIN = BCM25; +#else + // For Arduino MCP2515 Hat: -// SPI_CS Pin: D9 +// the cs pin of the version after v1.1 is default to D9 +// v0.9b and v1.0 is default D10 +const int SPI_CS_PIN = 9; +const int CAN_INT_PIN = 2; +#endif + #ifdef CAN_2518FD #include "mcp2518fd_can.h" -const int SPI_CS_PIN = 9; -const int CAN_INT_PIN = 2; mcp2518fd CAN(SPI_CS_PIN); // Set CS pin #define MAX_DATA_SIZE 64 #endif #ifdef CAN_2515 #include "mcp2515_can.h" -const int SPI_CS_PIN = 9; -const int CAN_INT_PIN = 2; mcp2515_can CAN(SPI_CS_PIN); // Set CS pin #define MAX_DATA_SIZE 8 #endif void setup() { SERIAL_PORT_MONITOR.begin(115200); - while(!Serial){}; + while(!SERIAL_PORT_MONITOR){} -#ifdef CAN_2518FD - while (0 != CAN.begin((byte)CAN_500K_1M)) { // init can bus : baudrate = 500k -#else while (CAN_OK != CAN.begin(CAN_500KBPS)) { // init can bus : baudrate = 500k -#endif SERIAL_PORT_MONITOR.println("CAN init fail, retry..."); delay(100); } @@ -86,7 +85,8 @@ void loop() { CAN.sendMsgBuf(id, bool(type & 0x1), bool(type & 0x2), len, cdata); - char prbuf[0x40]; + + char prbuf[32 + MAX_DATA_SIZE * 3]; int n; /* Displayed type: diff --git a/examples/send_sleep/send_sleep.ino b/examples/send_sleep/send_sleep.ino index 04c9b28..ae1f0ef 100644 --- a/examples/send_sleep/send_sleep.ino +++ b/examples/send_sleep/send_sleep.ino @@ -1,4 +1,6 @@ // demo: CAN Sleep Example - send +// *** only works on AVR + MCP2515 platform +// // by Kai, based on the send example by loovee and the additions from Zak Kemble (https://github.com/coryjfowler/MCP_CAN_lib/pull/10/files) // // See receive_sleep example for additional notes. diff --git a/examples/set_mask_filter_recv/set_mask_filter_recv.ino b/examples/set_mask_filter_recv/set_mask_filter_recv.ino index ead31dd..69c5181 100644 --- a/examples/set_mask_filter_recv/set_mask_filter_recv.ino +++ b/examples/set_mask_filter_recv/set_mask_filter_recv.ino @@ -8,31 +8,35 @@ #define CAN_2515 // #define CAN_2518FD -// the cs pin of the version after v1.1 is default to D9 -// v0.9b and v1.0 is default D10 - // Set SPI CS Pin according to your hardware + +#if defined(SEEED_WIO_TERMINAL) && defined(CAN_2518FD) // For Wio Terminal w/ MCP2518FD RPi Hat: // Channel 0 SPI_CS Pin: BCM 8 // Channel 1 SPI_CS Pin: BCM 7 // Interupt Pin: BCM25 -// ***************************************** +const int SPI_CS_PIN = BCM8; +const int CAN_INT_PIN = BCM25; +#else + // For Arduino MCP2515 Hat: -// SPI_CS Pin: D9 +// the cs pin of the version after v1.1 is default to D9 +// v0.9b and v1.0 is default D10 +const int SPI_CS_PIN = 9; +const int CAN_INT_PIN = 2; +#endif + #ifdef CAN_2518FD #include "mcp2518fd_can.h" -const int SPI_CS_PIN = 9; mcp2518fd CAN(SPI_CS_PIN); // Set CS pin #endif #ifdef CAN_2515 #include "mcp2515_can.h" -const int SPI_CS_PIN = 9; mcp2515_can CAN(SPI_CS_PIN); // Set CS pin #endif -const byte interruptPin = 2; unsigned char flagRecv = 0; unsigned char len = 0; unsigned char buf[8]; @@ -41,12 +45,8 @@ char str[20]; void setup() { SERIAL_PORT_MONITOR.begin(115200); while(!Serial){}; - attachInterrupt(digitalPinToInterrupt(interruptPin), MCP2515_ISR, FALLING); // start interrupt -#ifdef CAN_2518FD - while (0 != CAN.begin((byte)CAN_500K_1M)) { // init can bus : baudrate = 500k -#else + attachInterrupt(digitalPinToInterrupt(CAN_INT_PIN), MCP2515_ISR, FALLING); // start interrupt while (CAN_OK != CAN.begin(CAN_500KBPS)) { // init can bus : baudrate = 500k -#endif SERIAL_PORT_MONITOR.println("CAN init fail, retry..."); delay(100); } @@ -69,7 +69,6 @@ void setup() { 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 MCP2515_ISR() { @@ -91,7 +90,6 @@ void loop() { SERIAL_PORT_MONITOR.print("\t"); } SERIAL_PORT_MONITOR.println(); - } } diff --git a/examples/set_mask_filter_send/set_mask_filter_send.ino b/examples/set_mask_filter_send/set_mask_filter_send.ino index 6c98ec1..bb1b294 100644 --- a/examples/set_mask_filter_send/set_mask_filter_send.ino +++ b/examples/set_mask_filter_send/set_mask_filter_send.ino @@ -5,27 +5,32 @@ #define CAN_2515 // #define CAN_2518FD -// the cs pin of the version after v1.1 is default to D9 -// v0.9b and v1.0 is default D10 - // Set SPI CS Pin according to your hardware + +#if defined(SEEED_WIO_TERMINAL) && defined(CAN_2518FD) // For Wio Terminal w/ MCP2518FD RPi Hat: // Channel 0 SPI_CS Pin: BCM 8 // Channel 1 SPI_CS Pin: BCM 7 // Interupt Pin: BCM25 -// ***************************************** +const int SPI_CS_PIN = BCM8; +const int CAN_INT_PIN = BCM25; +#else + // For Arduino MCP2515 Hat: -// SPI_CS Pin: D9 +// the cs pin of the version after v1.1 is default to D9 +// v0.9b and v1.0 is default D10 +const int SPI_CS_PIN = 9; +const int CAN_INT_PIN = 2; +#endif + #ifdef CAN_2518FD #include "mcp2518fd_can.h" -const int SPI_CS_PIN = 9; mcp2518fd CAN(SPI_CS_PIN); // Set CS pin #endif #ifdef CAN_2515 #include "mcp2515_can.h" -const int SPI_CS_PIN = 9; mcp2515_can CAN(SPI_CS_PIN); // Set CS pin #endif @@ -34,11 +39,7 @@ void setup() { SERIAL_PORT_MONITOR.begin(115200); while(!Serial){}; -#ifdef CAN_2518FD - while (0 != CAN.begin((byte)CAN_500K_1M)) { // init can bus : baudrate = 500k -#else while (CAN_OK != CAN.begin(CAN_500KBPS)) { // init can bus : baudrate = 500k -#endif SERIAL_PORT_MONITOR.println("CAN init fail, retry..."); delay(100); } diff --git a/src/mcp2518fd_can_dfs.h b/src/mcp2518fd_can_dfs.h index b249d6a..de37af7 100644 --- a/src/mcp2518fd_can_dfs.h +++ b/src/mcp2518fd_can_dfs.h @@ -1607,6 +1607,32 @@ static const uint32_t canFilterObjectResetValues[] = {0x00000000, 0x00000000}; static const uint32_t mcp25xxfdControlResetValues[] = { 0x00000460, 0x00000003, 0x00000000, 0x00000000, 0x00000000}; #endif + + + +// compatible layer for MCP2515 +typedef enum { + CAN_NOBPS, + CAN_5KBPS, + CAN_10KBPS, + CAN_20KBPS, + CAN_25KBPS, + CAN_31K25BPS, + CAN_33KBPS , + CAN_40KBPS , + CAN_50KBPS , + CAN_80KBPS , + CAN_83K3BPS , + CAN_95KBPS , + CAN_100KBPS , + CAN_125KBPS = CAN_125K_500K, + CAN_200KBPS , + CAN_250KBPS = CAN_250K_500K, + CAN_500KBPS = CAN_500K_1M, + CAN_666KBPS , + CAN_1000KBPS = CAN_1000K_4M, +} MCP2515_BITTIME_SETUP; + #ifdef __cplusplus // Provide C++ Compatibility } #endif