Skip to content

Commit

Permalink
Merge pull request #84 from CarlosHdezM/master
Browse files Browse the repository at this point in the history
Allow SPI Peripheral Customization
  • Loading branch information
autowp authored Oct 12, 2022
2 parents 4d0982e + 8c75166 commit d424134
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 d424134

Please sign in to comment.