|
32 | 32 | from .canio import *
|
33 | 33 | from .timer import Timer
|
34 | 34 |
|
| 35 | +try: |
| 36 | + from typing_extensions import Literal |
| 37 | +except ImportError: |
| 38 | + pass |
| 39 | + |
35 | 40 | __version__ = "0.0.0+auto.0"
|
36 | 41 | __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MCP2515.git"
|
37 | 42 |
|
|
164 | 169 | ["CTRL_REG", "STD_ID_REG", "INT_FLAG_MASK", "LOAD_CMD", "SEND_CMD"],
|
165 | 170 | )
|
166 | 171 |
|
167 |
| -# This is magic, don't disturb the dragon |
168 |
| -# expects a 16Mhz crystal |
169 | 172 | _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 | + }, |
189 | 218 | }
|
190 | 219 |
|
191 | 220 |
|
@@ -217,17 +246,20 @@ def __init__(
|
217 | 246 | cs_pin,
|
218 | 247 | *,
|
219 | 248 | baudrate: int = 250000,
|
| 249 | + crystal_freq: Literal[8000000, 16000000] = 16000000, |
220 | 250 | loopback: bool = False,
|
221 | 251 | silent: bool = False,
|
222 | 252 | auto_restart: bool = False,
|
223 |
| - debug: bool = False |
| 253 | + debug: bool = False, |
224 | 254 | ):
|
225 | 255 | """A common shared-bus protocol.
|
226 | 256 |
|
227 | 257 | :param ~busio.SPI spi: The SPI bus used to communicate with the MCP2515
|
228 | 258 | :param ~digitalio.DigitalInOut cs_pin: SPI bus enable pin
|
229 | 259 | :param int baudrate: The bit rate of the bus in Hz, using a 16Mhz crystal. All devices on\
|
230 | 260 | 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).\ |
231 | 263 | :param bool loopback: Receive only packets sent from this device, and send only to this\
|
232 | 264 | device. Requires that `silent` is also set to `True`, but only prevents transmission to\
|
233 | 265 | other devices. Otherwise the send/receive behavior is normal.
|
@@ -262,9 +294,9 @@ def __init__(
|
262 | 294 | self._mode = None
|
263 | 295 | self._bus_state = BusState.ERROR_ACTIVE
|
264 | 296 | self._baudrate = baudrate
|
| 297 | + self._crystal_freq = crystal_freq |
265 | 298 | self._loopback = loopback
|
266 | 299 | self._silent = silent
|
267 |
| - self._baudrate = baudrate |
268 | 300 |
|
269 | 301 | self._init_buffers()
|
270 | 302 | self.initialize()
|
@@ -604,9 +636,13 @@ def _get_tx_buffer(self):
|
604 | 636 | return tx_buffer
|
605 | 637 |
|
606 | 638 | 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 | + ) |
607 | 644 |
|
608 |
| - # *******8 set baud rate *********** |
609 |
| - cnf1, cnf2, cnf3 = _BAUD_RATES[self.baudrate] |
| 645 | + cnf1, cnf2, cnf3 = _BAUD_RATES[self._crystal_freq][self.baudrate] |
610 | 646 |
|
611 | 647 | self._set_register(_CNF1, cnf1)
|
612 | 648 | self._set_register(_CNF2, cnf2)
|
|
0 commit comments