Skip to content

Commit 23ad299

Browse files
authored
Merge pull request #15 from casainho/baudrate_for_8mhz_xtal
Added xtal frequency table for 8MHz
2 parents 0c2c0de + 066e956 commit 23ad299

File tree

1 file changed

+61
-25
lines changed

1 file changed

+61
-25
lines changed

adafruit_mcp2515/__init__.py

Lines changed: 61 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@
3232
from .canio import *
3333
from .timer import Timer
3434

35+
try:
36+
from typing_extensions import Literal
37+
except ImportError:
38+
pass
39+
3540
__version__ = "0.0.0+auto.0"
3641
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MCP2515.git"
3742

@@ -164,28 +169,52 @@
164169
["CTRL_REG", "STD_ID_REG", "INT_FLAG_MASK", "LOAD_CMD", "SEND_CMD"],
165170
)
166171

167-
# This is magic, don't disturb the dragon
168-
# expects a 16Mhz crystal
169172
_BAUD_RATES = {
170-
# CNF1, CNF2, CNF3
171-
1000000: (0x00, 0xD0, 0x82),
172-
500000: (0x00, 0xF0, 0x86),
173-
250000: (0x41, 0xF1, 0x85),
174-
200000: (0x01, 0xFA, 0x87),
175-
125000: (0x03, 0xF0, 0x86),
176-
100000: (0x03, 0xFA, 0x87),
177-
95000: (0x03, 0xAD, 0x07),
178-
83300: (0x03, 0xBE, 0x07),
179-
80000: (0x03, 0xFF, 0x87),
180-
50000: (0x07, 0xFA, 0x87),
181-
40000: (0x07, 0xFF, 0x87),
182-
33000: (0x09, 0xBE, 0x07),
183-
31250: (0x0F, 0xF1, 0x85),
184-
25000: (0x0F, 0xBA, 0x07),
185-
20000: (0x0F, 0xFF, 0x87),
186-
10000: (0x1F, 0xFF, 0x87),
187-
5000: (0x3F, 0xFF, 0x87),
188-
666000: (0x00, 0xA0, 0x04),
173+
# This is magic, don't disturb the dragon
174+
# expects a 16Mhz crystal
175+
16000000: {
176+
# CNF1, CNF2, CNF3
177+
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),
195+
},
196+
# Values based on this calculator, for 8MHz, controller MCP2510:
197+
# https://www.kvaser.com/support/calculators/bit-timing-calculator/
198+
8000000: {
199+
# CNF1, CNF2, CNF3
200+
500000: (0x00, 0x91, 0x01),
201+
250000: (0x40, 0xB5, 0x01),
202+
200000: (0x00, 0xB6, 0x04),
203+
125000: (0x01, 0xAC, 0x03),
204+
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+
},
189218
}
190219

191220

@@ -217,17 +246,20 @@ def __init__(
217246
cs_pin,
218247
*,
219248
baudrate: int = 250000,
249+
crystal_freq: Literal[8000000, 16000000] = 16000000,
220250
loopback: bool = False,
221251
silent: bool = False,
222252
auto_restart: bool = False,
223-
debug: bool = False
253+
debug: bool = False,
224254
):
225255
"""A common shared-bus protocol.
226256
227257
:param ~busio.SPI spi: The SPI bus used to communicate with the MCP2515
228258
:param ~digitalio.DigitalInOut cs_pin: SPI bus enable pin
229259
:param int baudrate: The bit rate of the bus in Hz, using a 16Mhz crystal. All devices on\
230260
the bus must agree on this value. Defaults to 250000.
261+
:param Literal crystal_freq: MCP2515 crystal frequency. Valid values are:\
262+
16000000 and 8000000. Defaults to 16000000 (16MHz).\
231263
:param bool loopback: Receive only packets sent from this device, and send only to this\
232264
device. Requires that `silent` is also set to `True`, but only prevents transmission to\
233265
other devices. Otherwise the send/receive behavior is normal.
@@ -262,9 +294,9 @@ def __init__(
262294
self._mode = None
263295
self._bus_state = BusState.ERROR_ACTIVE
264296
self._baudrate = baudrate
297+
self._crystal_freq = crystal_freq
265298
self._loopback = loopback
266299
self._silent = silent
267-
self._baudrate = baudrate
268300

269301
self._init_buffers()
270302
self.initialize()
@@ -604,9 +636,13 @@ def _get_tx_buffer(self):
604636
return tx_buffer
605637

606638
def _set_baud_rate(self):
639+
# ******* set baud rate ***********
640+
if self._crystal_freq not in (16000000, 8000000):
641+
raise ValueError(
642+
f"Incorrect crystal frequency - must be one of: {tuple(_BAUD_RATES.keys())}"
643+
)
607644

608-
# *******8 set baud rate ***********
609-
cnf1, cnf2, cnf3 = _BAUD_RATES[self.baudrate]
645+
cnf1, cnf2, cnf3 = _BAUD_RATES[self._crystal_freq][self.baudrate]
610646

611647
self._set_register(_CNF1, cnf1)
612648
self._set_register(_CNF2, cnf2)

0 commit comments

Comments
 (0)