From 34c1d6ff911bb2766e8d6d1573ef26ba985b3d5e Mon Sep 17 00:00:00 2001 From: pillar1989 Date: Tue, 26 May 2026 11:20:23 +0800 Subject: [PATCH] fix: use pSPI->begin() and add skipSpiBegin option for ESP32 compatibility MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ESP32 users need to call SPI.begin() with custom pin assignments before using the library (GPIO matrix). The hardcoded SPI.begin() in mcp2518fd::begin() overwrites this configuration. Changes: - Replace SPI.begin() with pSPI->begin() (consistent with MCP2515) - Add optional _initSPI parameter (default true) to begin() across MCP_CAN, mcp2515, and mcp2518fd so ESP32 users can skip SPI init Existing code is unaffected — default behavior unchanged. Closes #158 --- src/mcp2515_can.cpp | 4 ++-- src/mcp2515_can.h | 2 +- src/mcp2518fd_can.cpp | 4 ++-- src/mcp2518fd_can.h | 3 ++- src/mcp_can.h | 2 +- 5 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/mcp2515_can.cpp b/src/mcp2515_can.cpp index 9bfbc46..32522ff 100644 --- a/src/mcp2515_can.cpp +++ b/src/mcp2515_can.cpp @@ -1058,8 +1058,8 @@ byte mcp2515_can::mcp2515_getNextFreeTXBuf(byte* txbuf_n) { // get ** Function name: begin ** Descriptions: init can and set speed *********************************************************************************************************/ -byte mcp2515_can::begin(uint32_t speedset, const byte clockset) { - pSPI->begin(); +byte mcp2515_can::begin(uint32_t speedset, const byte clockset, const bool _initSPI) { + if (_initSPI) pSPI->begin(); byte res = mcp2515_init((byte)speedset, clockset); return ((res == MCP2515_OK) ? CAN_OK : CAN_FAILINIT); diff --git a/src/mcp2515_can.h b/src/mcp2515_can.h index a95dde0..94690d6 100644 --- a/src/mcp2515_can.h +++ b/src/mcp2515_can.h @@ -69,7 +69,7 @@ class mcp2515_can : public MCP_CAN { return MCP_N_TXBUFFERS - 1; // read index of last tx buffer } - virtual byte begin(uint32_t speedset, const byte clockset = MCP_16MHz); // init can + virtual byte begin(uint32_t speedset, const byte clockset = MCP_16MHz, const bool _initSPI = true); // init can virtual byte init_Mask(byte num, byte ext, unsigned long ulData); // init Masks virtual byte init_Filt(byte num, byte ext, unsigned long ulData); // init filters virtual void setSleepWakeup(byte enable); // Enable or disable the wake up interrupt (If disabled the MCP2515 will not be woken up by CAN bus activity, making it send only) diff --git a/src/mcp2518fd_can.cpp b/src/mcp2518fd_can.cpp index f2968c5..29f4159 100644 --- a/src/mcp2518fd_can.cpp +++ b/src/mcp2518fd_can.cpp @@ -55,8 +55,8 @@ uint16_t DRV_CANFDSPI_CalculateCRC16(uint8_t *data, uint16_t size) { ** Function name: begin ** Descriptions: init can and set speed *********************************************************************************************************/ -byte mcp2518fd::begin(uint32_t speedset, const byte clockset) { - SPI.begin(); +byte mcp2518fd::begin(uint32_t speedset, const byte clockset, const bool _initSPI) { + if (_initSPI) pSPI->begin(); /* compatible layer translation */ speedset = bittime_compat_to_mcp2518fd(speedset); diff --git a/src/mcp2518fd_can.h b/src/mcp2518fd_can.h index 181da31..1d1e14e 100644 --- a/src/mcp2518fd_can.h +++ b/src/mcp2518fd_can.h @@ -112,7 +112,8 @@ class mcp2518fd : public MCP_CAN { * or fill by CANFD::BITRATE() */ virtual byte begin(uint32_t speedset, - const byte clockset = MCP2518FD_40MHz); // init can + const byte clockset = MCP2518FD_40MHz, + const bool _initSPI = true); // init can virtual byte init_Mask(byte num, byte ext, unsigned long ulData); virtual byte init_Filt(byte num, byte ext, unsigned long ulData); // init filters diff --git a/src/mcp_can.h b/src/mcp_can.h index c46e8a8..c4d502c 100644 --- a/src/mcp_can.h +++ b/src/mcp_can.h @@ -76,7 +76,7 @@ class MCP_CAN * speedset be in MCP_BITTIME_SETUP * clockset be in MCP_CLOCK_T */ - virtual byte begin(uint32_t speedset, const byte clockset) = 0; // init can + virtual byte begin(uint32_t speedset, const byte clockset, const bool _initSPI = true) = 0; // init can virtual byte init_Mask(byte num, byte ext, unsigned long ulData) = 0; // init Masks virtual byte init_Filt(byte num, byte ext, unsigned long ulData) = 0; // init filters virtual void setSleepWakeup(byte enable) = 0; // Enable or disable the wake up interrupt