Skip to content

Commit d424134

Browse files
authored
Merge pull request #84 from CarlosHdezM/master
Allow SPI Peripheral Customization
2 parents 4d0982e + 8c75166 commit d424134

File tree

2 files changed

+31
-24
lines changed

2 files changed

+31
-24
lines changed

mcp2515.cpp

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,15 @@ const struct MCP2515::RXBn_REGS MCP2515::RXB[N_RXBUFFERS] = {
1212
{MCP_RXB1CTRL, MCP_RXB1SIDH, MCP_RXB1DATA, CANINTF_RX1IF}
1313
};
1414

15-
MCP2515::MCP2515(const uint8_t _CS, const uint32_t _SPI_CLOCK)
15+
MCP2515::MCP2515(const uint8_t _CS, const uint32_t _SPI_CLOCK, SPIClass * _SPI)
1616
{
17-
SPI.begin();
17+
if (_SPI != nullptr) {
18+
SPIn = _SPI;
19+
}
20+
else {
21+
SPIn = &SPI;
22+
SPIn->begin();
23+
}
1824

1925
SPICS = _CS;
2026
SPI_CLOCK = _SPI_CLOCK;
@@ -23,19 +29,19 @@ MCP2515::MCP2515(const uint8_t _CS, const uint32_t _SPI_CLOCK)
2329
}
2430

2531
void MCP2515::startSPI() {
26-
SPI.beginTransaction(SPISettings(SPI_CLOCK, MSBFIRST, SPI_MODE0));
32+
SPIn->beginTransaction(SPISettings(SPI_CLOCK, MSBFIRST, SPI_MODE0));
2733
digitalWrite(SPICS, LOW);
2834
}
2935

3036
void MCP2515::endSPI() {
3137
digitalWrite(SPICS, HIGH);
32-
SPI.endTransaction();
38+
SPIn->endTransaction();
3339
}
3440

3541
MCP2515::ERROR MCP2515::reset(void)
3642
{
3743
startSPI();
38-
SPI.transfer(INSTRUCTION_RESET);
44+
SPIn->transfer(INSTRUCTION_RESET);
3945
endSPI();
4046

4147
delay(10);
@@ -86,9 +92,9 @@ MCP2515::ERROR MCP2515::reset(void)
8692
uint8_t MCP2515::readRegister(const REGISTER reg)
8793
{
8894
startSPI();
89-
SPI.transfer(INSTRUCTION_READ);
90-
SPI.transfer(reg);
91-
uint8_t ret = SPI.transfer(0x00);
95+
SPIn->transfer(INSTRUCTION_READ);
96+
SPIn->transfer(reg);
97+
uint8_t ret = SPIn->transfer(0x00);
9298
endSPI();
9399

94100
return ret;
@@ -97,50 +103,50 @@ uint8_t MCP2515::readRegister(const REGISTER reg)
97103
void MCP2515::readRegisters(const REGISTER reg, uint8_t values[], const uint8_t n)
98104
{
99105
startSPI();
100-
SPI.transfer(INSTRUCTION_READ);
101-
SPI.transfer(reg);
106+
SPIn->transfer(INSTRUCTION_READ);
107+
SPIn->transfer(reg);
102108
// mcp2515 has auto-increment of address-pointer
103109
for (uint8_t i=0; i<n; i++) {
104-
values[i] = SPI.transfer(0x00);
110+
values[i] = SPIn->transfer(0x00);
105111
}
106112
endSPI();
107113
}
108114

109115
void MCP2515::setRegister(const REGISTER reg, const uint8_t value)
110116
{
111117
startSPI();
112-
SPI.transfer(INSTRUCTION_WRITE);
113-
SPI.transfer(reg);
114-
SPI.transfer(value);
118+
SPIn->transfer(INSTRUCTION_WRITE);
119+
SPIn->transfer(reg);
120+
SPIn->transfer(value);
115121
endSPI();
116122
}
117123

118124
void MCP2515::setRegisters(const REGISTER reg, const uint8_t values[], const uint8_t n)
119125
{
120126
startSPI();
121-
SPI.transfer(INSTRUCTION_WRITE);
122-
SPI.transfer(reg);
127+
SPIn->transfer(INSTRUCTION_WRITE);
128+
SPIn->transfer(reg);
123129
for (uint8_t i=0; i<n; i++) {
124-
SPI.transfer(values[i]);
130+
SPIn->transfer(values[i]);
125131
}
126132
endSPI();
127133
}
128134

129135
void MCP2515::modifyRegister(const REGISTER reg, const uint8_t mask, const uint8_t data)
130136
{
131137
startSPI();
132-
SPI.transfer(INSTRUCTION_BITMOD);
133-
SPI.transfer(reg);
134-
SPI.transfer(mask);
135-
SPI.transfer(data);
138+
SPIn->transfer(INSTRUCTION_BITMOD);
139+
SPIn->transfer(reg);
140+
SPIn->transfer(mask);
141+
SPIn->transfer(data);
136142
endSPI();
137143
}
138144

139145
uint8_t MCP2515::getStatus(void)
140146
{
141147
startSPI();
142-
SPI.transfer(INSTRUCTION_READ_STATUS);
143-
uint8_t i = SPI.transfer(0x00);
148+
SPIn->transfer(INSTRUCTION_READ_STATUS);
149+
uint8_t i = SPIn->transfer(0x00);
144150
endSPI();
145151

146152
return i;

mcp2515.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,7 @@ class MCP2515
444444

445445
uint8_t SPICS;
446446
uint32_t SPI_CLOCK;
447+
SPIClass * SPIn;
447448

448449
private:
449450

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

463464
public:
464-
MCP2515(const uint8_t _CS, const uint32_t _SPI_CLOCK = DEFAULT_SPI_CLOCK);
465+
MCP2515(const uint8_t _CS, const uint32_t _SPI_CLOCK = DEFAULT_SPI_CLOCK, SPIClass * _SPI = nullptr);
465466
ERROR reset(void);
466467
ERROR setConfigMode();
467468
ERROR setListenOnlyMode();

0 commit comments

Comments
 (0)