fix: use pSPI->begin() and add skipSpiBegin option for ESP32 compatibility (#159)

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

Co-authored-by: pillar1989 <zuobaozhu@gmail.com>
This commit is contained in:
alias pillar1989
2026-05-26 11:23:15 +08:00
committed by GitHub
co-authored by pillar1989
parent ca8fcc7fe3
commit fc19066027
5 changed files with 8 additions and 7 deletions
+2 -2
View File
@@ -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);
+1 -1
View File
@@ -69,7 +69,7 @@ public:
{
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)
+2 -2
View File
@@ -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);
+2 -1
View File
@@ -112,7 +112,8 @@ public:
* 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
+1 -1
View File
@@ -76,7 +76,7 @@ public:
* 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