Skip to content

Commit 02b71aa

Browse files
authored
Merge pull request #17 from HeCoding180/10MHz-Crystal-Add
10MHz Crystal Lookup Table added, improved crystal validity check
2 parents f805d77 + e07c609 commit 02b71aa

File tree

1 file changed

+35
-7
lines changed

1 file changed

+35
-7
lines changed

adafruit_mcp2515/__init__.py

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,20 @@
169169
["CTRL_REG", "STD_ID_REG", "INT_FLAG_MASK", "LOAD_CMD", "SEND_CMD"],
170170
)
171171

172+
# --- Baud Rates Table ---
173+
# Values for the 8MHz and 10MHz Crystal Oscillator are based on this calculator:
174+
# https://www.kvaser.com/support/calculators/bit-timing-calculator/
175+
# - MCP2510 can be used to calculate the timing values since the registers are allmost the same
176+
# Only difference is the CNF3, which has an extra bit (CNF3[7] = SOF, Start of Frame signal bit)
177+
# The SOF bit can be left on zero
178+
# - A bit sample point (SP%) of 70% is used if nothing else is defined
179+
# - A Synchronization Jump Width (SJW) of 1 Time Quanta (TQ) is used
180+
172181
_BAUD_RATES = {
173182
# This is magic, don't disturb the dragon
174183
# expects a 16Mhz crystal
175184
16000000: {
176-
# CNF1, CNF2, CNF3
185+
# CNF1, CNF2, CNF3
177186
1000000: (0x00, 0xD0, 0x82),
178187
500000: (0x00, 0xF0, 0x86),
179188
250000: (0x41, 0xF1, 0x85),
@@ -193,10 +202,29 @@
193202
5000: (0x3F, 0xFF, 0x87),
194203
666000: (0x00, 0xA0, 0x04),
195204
},
196-
# Values based on this calculator, for 8MHz, controller MCP2510:
197-
# https://www.kvaser.com/support/calculators/bit-timing-calculator/
205+
# 10MHz Crystal oscillator (used on the MIKROE "CAN SPI click"-board)
206+
10000000: {
207+
# CNF1, CNF2, CNF3
208+
500000: (0x00, 0x92, 0x02),
209+
250000: (0x00, 0xB5, 0x05),
210+
200000: (0x00, 0xBF, 0x07), # SP is 68%!
211+
125000: (0x01, 0xA4, 0x04), # SP is 68.75%!
212+
100000: (0x01, 0xB5, 0x05),
213+
95000: (0x05, 0x89, 0x01), # SP is 71.43%, Baud rate is 95.238kbps!
214+
83300: (0x02, 0xA4, 0x04), # SP is 68.75%, Baud rate is 83.333kbps!
215+
80000: (0x04, 0x92, 0x02),
216+
50000: (0x03, 0xB5, 0x05),
217+
40000: (0x04, 0xB5, 0x05),
218+
33000: (0x05, 0xB5, 0x05), # Baud rate is 33.333kbps!
219+
31250: (0x07, 0xA4, 0x04), # SP is 68.75%!
220+
25000: (0x07, 0xB5, 0x05),
221+
20000: (0x09, 0xB5, 0x05),
222+
10000: (0x13, 0xB5, 0x05),
223+
5000: (0x27, 0xB5, 0x05),
224+
},
225+
# 8MHz Crystal oscillator
198226
8000000: {
199-
# CNF1, CNF2, CNF3
227+
# CNF1, CNF2, CNF3
200228
500000: (0x00, 0x91, 0x01),
201229
250000: (0x40, 0xB5, 0x01),
202230
200000: (0x00, 0xB6, 0x04),
@@ -246,7 +274,7 @@ def __init__(
246274
cs_pin,
247275
*,
248276
baudrate: int = 250000,
249-
crystal_freq: Literal[8000000, 16000000] = 16000000,
277+
crystal_freq: Literal[8000000, 10000000, 16000000] = 16000000,
250278
loopback: bool = False,
251279
silent: bool = False,
252280
auto_restart: bool = False,
@@ -259,7 +287,7 @@ def __init__(
259287
:param int baudrate: The bit rate of the bus in Hz, using a 16Mhz crystal. All devices on\
260288
the bus must agree on this value. Defaults to 250000.
261289
:param Literal crystal_freq: MCP2515 crystal frequency. Valid values are:\
262-
16000000 and 8000000. Defaults to 16000000 (16MHz).\
290+
16000000, 10000000 and 8000000. Defaults to 16000000 (16MHz).\
263291
:param bool loopback: Receive only packets sent from this device, and send only to this\
264292
device. Requires that `silent` is also set to `True`, but only prevents transmission to\
265293
other devices. Otherwise the send/receive behavior is normal.
@@ -637,7 +665,7 @@ def _get_tx_buffer(self):
637665

638666
def _set_baud_rate(self):
639667
# ******* set baud rate ***********
640-
if self._crystal_freq not in (16000000, 8000000):
668+
if self._crystal_freq not in _BAUD_RATES:
641669
raise ValueError(
642670
f"Incorrect crystal frequency - must be one of: {tuple(_BAUD_RATES.keys())}"
643671
)

0 commit comments

Comments
 (0)