Skip to content

Commit 9d8eb76

Browse files
committed
Remove clockRate arg from begin, add new setClockFrequency API
1 parent 25ee443 commit 9d8eb76

File tree

4 files changed

+27
-9
lines changed

4 files changed

+27
-9
lines changed

API.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@ Initialize the library with the specified bit rate.
1414

1515
```arduino
1616
CAN.begin(bitrate);
17-
CAN.begin(bitrate, clockRate);
1817
```
1918
* `bitrate` - bit rate in bits per seconds (bps) (`1000E3`, `500E3`, `250E3`, `200E3`, `125E3`, `100E3`, `80E3`, `50E3`, `40E3`, `20E3`, `10E3`, `5E3`)
20-
* `clockRate` - (optional) clock rate of clock source (`8E6`, `16E6`, `20E6`) connected to MCP2515, defaults to `16 Mhz`
2119

2220
Returns `1` on success, `0` on failure.
2321

@@ -44,6 +42,17 @@ CAN.setSPIFrequency(frequency);
4442

4543
This call is optional and only needs to be used if you need to change the default SPI frequency used. Some logic level converters cannot support high speeds such as 10 MHz, so a lower SPI frequency can be selected with `CAN.setSPIFrequency(frequency)`.
4644

45+
### Set Clock Frequency
46+
47+
Override the default clock source frequency that is connected to the MCP2515. **Must** be called before `CAN.begin(...)`.
48+
49+
```arduino
50+
CAN.setClockFrequency(clockFrequency);
51+
```
52+
* `clockFrequency` - new clock frequency to use (`8E6`, `16E6`) connected to MCP2515, defaults to `16 Mhz`
53+
54+
This call is optional and only needs to be used if you need to change the clock source frequency connected to the MCP2515. Most shields have a 16 MHz clock source on board, some breakout boards have a 8 MHz source.
55+
4756
### End
4857

4958
Stop the library

keywords.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ wakeup KEYWORD2
3939

4040
setPins KEYWORD2
4141
setSPIFrequency KEYWORD2
42+
setClockFrequency KEYWORD2
4243
dumpRegisters KEYWORD2
4344

4445
#######################################

src/CAN.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ CANClass::CANClass() :
5050
_spiSettings(10E6, MSBFIRST, SPI_MODE0),
5151
_ss(CAN_DEFAULT_SS_PIN),
5252
_irq(CAN_DEFAULT_INT_PIN),
53+
_clockFrequency(CAN_DEFAULT_CLOCK_FREQUENCY),
5354
_onReceive(NULL),
5455

5556
_packetBegun(false),
@@ -70,7 +71,7 @@ CANClass::CANClass() :
7071
setTimeout(0);
7172
}
7273

73-
int CANClass::begin(long baudRate, long clockRate)
74+
int CANClass::begin(long baudRate)
7475
{
7576
_packetBegun = false;
7677
_txId = -1;
@@ -98,7 +99,7 @@ int CANClass::begin(long baudRate, long clockRate)
9899
}
99100

100101
const struct {
101-
long clockRate;
102+
long clockFrequency;
102103
long baudRate;
103104
uint8_t cnf[3];
104105
} CNF_MAPPER[] = {
@@ -132,7 +133,7 @@ int CANClass::begin(long baudRate, long clockRate)
132133
const uint8_t* cnf = NULL;
133134

134135
for (unsigned int i = 0; i < (sizeof(CNF_MAPPER) / sizeof(CNF_MAPPER[0])); i++) {
135-
if (CNF_MAPPER[i].clockRate == clockRate && CNF_MAPPER[i].baudRate == baudRate) {
136+
if (CNF_MAPPER[i].clockFrequency == _clockFrequency && CNF_MAPPER[i].baudRate == baudRate) {
136137
cnf = CNF_MAPPER[i].cnf;
137138
break;
138139
}
@@ -434,6 +435,11 @@ void CANClass::setSPIFrequency(uint32_t frequency)
434435
_spiSettings = SPISettings(frequency, MSBFIRST, SPI_MODE0);
435436
}
436437

438+
void CANClass::setClockFrequency(long clockFrequency)
439+
{
440+
_clockFrequency = clockFrequency;
441+
}
442+
437443
void CANClass::dumpRegisters(Stream& out)
438444
{
439445
for (int i = 0; i < 128; i++) {

src/CAN.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@
77
#include <Arduino.h>
88
#include <SPI.h>
99

10-
#define CAN_DEFAULT_CLOCK_RATE 16e6
11-
#define CAN_DEFAULT_SS_PIN 10
12-
#define CAN_DEFAULT_INT_PIN 2
10+
#define CAN_DEFAULT_CLOCK_FREQUENCY 16e6
11+
#define CAN_DEFAULT_SS_PIN 10
12+
#define CAN_DEFAULT_INT_PIN 2
1313

1414
class CANClass : public Stream {
1515

1616
public:
1717
CANClass();
1818

19-
int begin(long baudRate, long clockRate = CAN_DEFAULT_CLOCK_RATE);
19+
int begin(long baudRate);
2020
void end();
2121

2222
int beginPacket(int id, int dlc = -1, bool rtr = false);
@@ -47,6 +47,7 @@ class CANClass : public Stream {
4747

4848
void setPins(int ss = CAN_DEFAULT_SS_PIN, int irq = CAN_DEFAULT_INT_PIN);
4949
void setSPIFrequency(uint32_t frequency);
50+
void setClockFrequency(long clockFrequency);
5051

5152
void dumpRegisters(Stream& out);
5253

@@ -65,6 +66,7 @@ class CANClass : public Stream {
6566
SPISettings _spiSettings;
6667
int _ss;
6768
int _irq;
69+
long _clockFrequency;
6870
void (*_onReceive)(int);
6971

7072
bool _packetBegun;

0 commit comments

Comments
 (0)