Skip to content

Commit 887e4cb

Browse files
committed
10MHz Crystal Lookup Table added, improved crystal validity check
1 parent f805d77 commit 887e4cb

File tree

1 file changed

+65
-37
lines changed

1 file changed

+65
-37
lines changed

adafruit_mcp2515/__init__.py

Lines changed: 65 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -169,52 +169,80 @@
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),
178-
500000: (0x00, 0xF0, 0x86),
179-
250000: (0x41, 0xF1, 0x85),
180-
200000: (0x01, 0xFA, 0x87),
181-
125000: (0x03, 0xF0, 0x86),
182-
100000: (0x03, 0xFA, 0x87),
183-
95000: (0x03, 0xAD, 0x07),
184-
83300: (0x03, 0xBE, 0x07),
185-
80000: (0x03, 0xFF, 0x87),
186-
50000: (0x07, 0xFA, 0x87),
187-
40000: (0x07, 0xFF, 0x87),
188-
33000: (0x09, 0xBE, 0x07),
189-
31250: (0x0F, 0xF1, 0x85),
190-
25000: (0x0F, 0xBA, 0x07),
191-
20000: (0x0F, 0xFF, 0x87),
192-
10000: (0x1F, 0xFF, 0x87),
193-
5000: (0x3F, 0xFF, 0x87),
194-
666000: (0x00, 0xA0, 0x04),
187+
500000: (0x00, 0xF0, 0x86),
188+
250000: (0x41, 0xF1, 0x85),
189+
200000: (0x01, 0xFA, 0x87),
190+
125000: (0x03, 0xF0, 0x86),
191+
100000: (0x03, 0xFA, 0x87),
192+
95000: (0x03, 0xAD, 0x07),
193+
83300: (0x03, 0xBE, 0x07),
194+
80000: (0x03, 0xFF, 0x87),
195+
50000: (0x07, 0xFA, 0x87),
196+
40000: (0x07, 0xFF, 0x87),
197+
33000: (0x09, 0xBE, 0x07),
198+
31250: (0x0F, 0xF1, 0x85),
199+
25000: (0x0F, 0xBA, 0x07),
200+
20000: (0x0F, 0xFF, 0x87),
201+
10000: (0x1F, 0xFF, 0x87),
202+
5000: (0x3F, 0xFF, 0x87),
203+
666000: (0x00, 0xA0, 0x04)
204+
},
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)
195224
},
196-
# Values based on this calculator, for 8MHz, controller MCP2510:
197-
# https://www.kvaser.com/support/calculators/bit-timing-calculator/
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),
203231
125000: (0x01, 0xAC, 0x03),
204232
100000: (0x01, 0xB6, 0x04),
205-
95000: (0x41, 0xBE, 0x04),
206-
83300: (0x02, 0xAC, 0x03),
207-
80000: (0x04, 0x9A, 0x01),
208-
50000: (0x03, 0xB6, 0x04),
209-
40000: (0x04, 0xB6, 0x04),
210-
33000: (0x0A, 0x9A, 0x02),
211-
31250: (0x07, 0xAC, 0x03),
212-
25000: (0x07, 0xB6, 0x04),
213-
20000: (0x09, 0xB6, 0x04),
214-
10000: (0x13, 0xB6, 0x04),
215-
5000: (0x27, 0xB6, 0x04),
216-
666000: (0x00, 0x88, 0x01),
217-
},
233+
95000: (0x41, 0xBE, 0x04),
234+
83300: (0x02, 0xAC, 0x03),
235+
80000: (0x04, 0x9A, 0x01),
236+
50000: (0x03, 0xB6, 0x04),
237+
40000: (0x04, 0xB6, 0x04),
238+
33000: (0x0A, 0x9A, 0x02),
239+
31250: (0x07, 0xAC, 0x03),
240+
25000: (0x07, 0xB6, 0x04),
241+
20000: (0x09, 0xB6, 0x04),
242+
10000: (0x13, 0xB6, 0x04),
243+
5000: (0x27, 0xB6, 0x04),
244+
666000: (0x00, 0x88, 0x01)
245+
}
218246
}
219247

220248

@@ -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.keys():
641669
raise ValueError(
642670
f"Incorrect crystal frequency - must be one of: {tuple(_BAUD_RATES.keys())}"
643671
)

0 commit comments

Comments
 (0)