Skip to content

Add support for 20MHz clock source #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Returns `1` on success, `0` on failure.

### Set pins

#### MCP2515
#### MCP2515 and MCP25625

Override the default `CS` and `INT` pins used by the library. **Must** be called before `CAN.begin(...)`.

Expand All @@ -47,7 +47,7 @@ This call is optional and only needs to be used if you need to change the defaul

### Set SPI Frequency

**MCP2515 only**
**MCP2515 and MCP25625 only**

Override the default SPI frequency of 10 MHz used by the library. **Must** be called before `CAN.begin(...)`.

Expand All @@ -60,16 +60,21 @@ This call is optional and only needs to be used if you need to change the defaul

### Set Clock Frequency

**MCP2515 only**
**MCP2515 and MCP25625 only**

Override the default clock source frequency that is connected to the MCP2515. **Must** be called before `CAN.begin(...)`.

```arduino
CAN.setClockFrequency(clockFrequency);
```
* `clockFrequency` - new clock frequency to use (`8E6`, `16E6`) connected to MCP2515, defaults to `16 Mhz`

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.
* `clockFrequency` - new clock frequency to use (`8E6`, `16E6`, `20E6`)
connected to MCP2515/MCP25625 OSC pins, defaults to `16 Mhz`

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, and some MCP25625 boards use 20 MHz.
If using 20 MHz, the 5E3 bps CAN bitrate is not avilable.

### End

Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ An Arduino library for sending and receiving data using CAN bus.

* [Microchip MCP2515](http://www.microchip.com/wwwproducts/en/en010406) based boards/shields
* [Arduino MKR CAN shield](https://store.arduino.cc/arduino-mkr-can-shield)
* Microchip MCP25625 based boards/shields
* [Espressif ESP32](http://espressif.com/en/products/hardware/esp32/overview)'s built-in [SJA1000](https://www.nxp.com/products/analog/interfaces/in-vehicle-network/can-transceiver-and-controllers/stand-alone-can-controller:SJA1000T) compatible CAN controller with an external 3.3V CAN transceiver

### Microchip MCP2515 wiring
### Microchip MCP2515/MCP25625 wiring

| Microchip MCP2515 | Arduino |
| :---------------: | :-----: |
Expand All @@ -23,7 +24,9 @@ An Arduino library for sending and receiving data using CAN bus.

`CS` and `INT` pins can be changed by using `CAN.setPins(cs, irq)`. `INT` pin is optional, it is only needed for receive callback mode. If `INT` pin is used, it **must** be interrupt capable via [`attachInterrupt(...)`](https://www.arduino.cc/en/Reference/AttachInterrupt).

**NOTE**: Logic level converters must be used for boards which operate at 3.3V.
**NOTE**: Logic level converters must be used for boards MCP2515
which operate at 3.3V. The MCP25625 can run on 3.3V or 5V power,
resolving this issue.

### Espressif ESP32 wiring

Expand Down
18 changes: 18 additions & 0 deletions src/MCP2515.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ int MCP2515Class::begin(long baudRate, bool stayInConfigurationMode)
return 0;
}

if( (baudRate == 5E3) && (_clockFrequency == 20E6) )
// with a 20MHz clock, the minimum baud rate is 6.25kHz
return 0;

const struct {
long clockFrequency;
long baudRate;
Expand Down Expand Up @@ -118,7 +122,21 @@ int MCP2515Class::begin(long baudRate, bool stayInConfigurationMode)
{ (long)16E6, (long)20E3, { 0x0f, 0xff, 0x87 } },
{ (long)16E6, (long)10E3, { 0x1f, 0xff, 0x87 } },
{ (long)16E6, (long)5E3, { 0x3f, 0xff, 0x87 } },

{ (long)20E6, (long)1000E3, { 0x40, 0xd2, 0x82 } }, // 10 Tq, sp 70%, sjw=2, 3 samples (tested)
{ (long)20E6, (long)500E3, { 0x40, 0xf5, 0x85 } }, // 20 Tq, sp 70%, sjw=2, 3 samples (tested)
{ (long)20E6, (long)250E3, { 0x41, 0xf5, 0x85 } }, // 20 Tq, sp 70%, sjw=2, 3 samples (tested)
{ (long)20E6, (long)200E3, { 0x41, 0xff, 0x87 } }, // 25 Tq, sp 68%, sjw=2, 3 samples
{ (long)20E6, (long)125E3, { 0x43, 0xf5, 0x85 } }, // 20 Tq, sp 70%, sjw=2, 3 samples (tested)
{ (long)20E6, (long)100E3, { 0x44, 0xf5, 0x85 } }, // 20 Tq, sp 70%, sjw=2, 3 samples
{ (long)20E6, (long)80E3, { 0x44, 0xff, 0x87 } }, // 25 Tq, sp 70%, sjw=2, 3 samples
{ (long)20E6, (long)50E3, { 0x49, 0xf5, 0x85 } }, // 20 Tq, sp 70%, sjw=2, 3 samples
{ (long)20E6, (long)40E3, { 0x49, 0xff, 0x87 } }, // 25 Tq, sp 68%, sjw=2, 3 samples
{ (long)20E6, (long)20E3, { 0x58, 0xf5, 0x85 } }, // 20 Tq, sp 70%, sjw=2, 3 samples
{ (long)20E6, (long)10E3, { 0x71, 0xf5, 0x85 } }, // 20 Tq, sp 70%, sjw=2, 3 samples
{ (long)20E6, (long)5E3, { 0x7f, 0xff, 0x87 } }, // 6.25kHz is as slow as it can go
};
//https://www.kvaser.com/support/calculators/bit-timing-calculator/ (use MCP2510 option)

const uint8_t* cnf = NULL;

Expand Down