Skip to content

Commit

Permalink
MCP2515 constructor now has an optional SPIClass pointer as its
Browse files Browse the repository at this point in the history
last parameter to allow selecting a different SPI peripheral.

Fully backward compatible. All the code that works so far will continue
to work correctly without any changes.

- If no argument is passed during construction (defaults to nullptr),
  the Arduino default "SPI" will be used and initialized with begin().

- If a SPIClass to use is specified, then it is the library user
  responsibility to initialize that SPI ( other_spi.begin()) with the
  proper settings outside of the library (I.E. in void setup()). In this
  way one can use different SPI pins in boards that support it (ESP32,
  some STM32). Thanks to @morcibacsi and @FStefanni for the suggestions.

Tested on ESP32 HSPI and VSPI with default and custom pins. It should
also work with all boards featuring multiple SPI peripherals
(SPI1, SPI2, etc.).
  • Loading branch information
CarlosHdezM committed Oct 8, 2022
1 parent 4d0982e commit 8c75166
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
52 changes: 29 additions & 23 deletions mcp2515.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@ const struct MCP2515::RXBn_REGS MCP2515::RXB[N_RXBUFFERS] = {
{MCP_RXB1CTRL, MCP_RXB1SIDH, MCP_RXB1DATA, CANINTF_RX1IF}
};

MCP2515::MCP2515(const uint8_t _CS, const uint32_t _SPI_CLOCK)
MCP2515::MCP2515(const uint8_t _CS, const uint32_t _SPI_CLOCK, SPIClass * _SPI)
{
SPI.begin();
if (_SPI != nullptr) {
SPIn = _SPI;
}
else {
SPIn = &SPI;
SPIn->begin();
}

SPICS = _CS;
SPI_CLOCK = _SPI_CLOCK;
Expand All @@ -23,19 +29,19 @@ MCP2515::MCP2515(const uint8_t _CS, const uint32_t _SPI_CLOCK)
}

void MCP2515::startSPI() {
SPI.beginTransaction(SPISettings(SPI_CLOCK, MSBFIRST, SPI_MODE0));
SPIn->beginTransaction(SPISettings(SPI_CLOCK, MSBFIRST, SPI_MODE0));
digitalWrite(SPICS, LOW);
}

void MCP2515::endSPI() {
digitalWrite(SPICS, HIGH);
SPI.endTransaction();
SPIn->endTransaction();
}

MCP2515::ERROR MCP2515::reset(void)
{
startSPI();
SPI.transfer(INSTRUCTION_RESET);
SPIn->transfer(INSTRUCTION_RESET);
endSPI();

delay(10);
Expand Down Expand Up @@ -86,9 +92,9 @@ MCP2515::ERROR MCP2515::reset(void)
uint8_t MCP2515::readRegister(const REGISTER reg)
{
startSPI();
SPI.transfer(INSTRUCTION_READ);
SPI.transfer(reg);
uint8_t ret = SPI.transfer(0x00);
SPIn->transfer(INSTRUCTION_READ);
SPIn->transfer(reg);
uint8_t ret = SPIn->transfer(0x00);
endSPI();

return ret;
Expand All @@ -97,50 +103,50 @@ uint8_t MCP2515::readRegister(const REGISTER reg)
void MCP2515::readRegisters(const REGISTER reg, uint8_t values[], const uint8_t n)
{
startSPI();
SPI.transfer(INSTRUCTION_READ);
SPI.transfer(reg);
SPIn->transfer(INSTRUCTION_READ);
SPIn->transfer(reg);
// mcp2515 has auto-increment of address-pointer
for (uint8_t i=0; i<n; i++) {
values[i] = SPI.transfer(0x00);
values[i] = SPIn->transfer(0x00);
}
endSPI();
}

void MCP2515::setRegister(const REGISTER reg, const uint8_t value)
{
startSPI();
SPI.transfer(INSTRUCTION_WRITE);
SPI.transfer(reg);
SPI.transfer(value);
SPIn->transfer(INSTRUCTION_WRITE);
SPIn->transfer(reg);
SPIn->transfer(value);
endSPI();
}

void MCP2515::setRegisters(const REGISTER reg, const uint8_t values[], const uint8_t n)
{
startSPI();
SPI.transfer(INSTRUCTION_WRITE);
SPI.transfer(reg);
SPIn->transfer(INSTRUCTION_WRITE);
SPIn->transfer(reg);
for (uint8_t i=0; i<n; i++) {
SPI.transfer(values[i]);
SPIn->transfer(values[i]);
}
endSPI();
}

void MCP2515::modifyRegister(const REGISTER reg, const uint8_t mask, const uint8_t data)
{
startSPI();
SPI.transfer(INSTRUCTION_BITMOD);
SPI.transfer(reg);
SPI.transfer(mask);
SPI.transfer(data);
SPIn->transfer(INSTRUCTION_BITMOD);
SPIn->transfer(reg);
SPIn->transfer(mask);
SPIn->transfer(data);
endSPI();
}

uint8_t MCP2515::getStatus(void)
{
startSPI();
SPI.transfer(INSTRUCTION_READ_STATUS);
uint8_t i = SPI.transfer(0x00);
SPIn->transfer(INSTRUCTION_READ_STATUS);
uint8_t i = SPIn->transfer(0x00);
endSPI();

return i;
Expand Down
3 changes: 2 additions & 1 deletion mcp2515.h
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,7 @@ class MCP2515

uint8_t SPICS;
uint32_t SPI_CLOCK;
SPIClass * SPIn;

private:

Expand All @@ -461,7 +462,7 @@ class MCP2515
void prepareId(uint8_t *buffer, const bool ext, const uint32_t id);

public:
MCP2515(const uint8_t _CS, const uint32_t _SPI_CLOCK = DEFAULT_SPI_CLOCK);
MCP2515(const uint8_t _CS, const uint32_t _SPI_CLOCK = DEFAULT_SPI_CLOCK, SPIClass * _SPI = nullptr);
ERROR reset(void);
ERROR setConfigMode();
ERROR setListenOnlyMode();
Expand Down

0 comments on commit 8c75166

Please sign in to comment.