164
164
["CTRL_REG" , "STD_ID_REG" , "INT_FLAG_MASK" , "LOAD_CMD" , "SEND_CMD" ],
165
165
)
166
166
167
- # This is magic, don't disturb the dragon
168
- # expects a 16Mhz crystal
169
167
_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 ),
168
+ # This is magic, don't disturb the dragon
169
+ # expects a 16Mhz crystal
170
+ 16000000 : {
171
+ # CNF1, CNF2, CNF3
172
+ 1000000 : (0x00 , 0xD0 , 0x82 ),
173
+ 500000 : (0x00 , 0xF0 , 0x86 ),
174
+ 250000 : (0x41 , 0xF1 , 0x85 ),
175
+ 200000 : (0x01 , 0xFA , 0x87 ),
176
+ 125000 : (0x03 , 0xF0 , 0x86 ),
177
+ 100000 : (0x03 , 0xFA , 0x87 ),
178
+ 95000 : (0x03 , 0xAD , 0x07 ),
179
+ 83300 : (0x03 , 0xBE , 0x07 ),
180
+ 80000 : (0x03 , 0xFF , 0x87 ),
181
+ 50000 : (0x07 , 0xFA , 0x87 ),
182
+ 40000 : (0x07 , 0xFF , 0x87 ),
183
+ 33000 : (0x09 , 0xBE , 0x07 ),
184
+ 31250 : (0x0F , 0xF1 , 0x85 ),
185
+ 25000 : (0x0F , 0xBA , 0x07 ),
186
+ 20000 : (0x0F , 0xFF , 0x87 ),
187
+ 10000 : (0x1F , 0xFF , 0x87 ),
188
+ 5000 : (0x3F , 0xFF , 0x87 ),
189
+ 666000 : (0x00 , 0xA0 , 0x04 ),
190
+ },
191
+
192
+ # Values based on this calculator, for 8MHz, controller MCP2510: https://www.kvaser.com/support/calculators/bit-timing-calculator/
193
+ 8000000 : {
194
+ # CNF1, CNF2, CNF3
195
+ 1000000 : (0x00 , 0x91 , 0x01 ), # seems it is not possible, so use the previous value of 500000
196
+ 500000 : (0x00 , 0x91 , 0x01 ),
197
+ 250000 : (0x40 , 0xb5 , 0x01 ),
198
+ 200000 : (0x00 , 0xb6 , 0x04 ),
199
+ 125000 : (0x01 , 0xac , 0x03 ),
200
+ 100000 : (0x01 , 0xb6 , 0x04 ),
201
+ 95000 : (0x41 , 0xbe , 0x04 ),
202
+ 83300 : (0x02 , 0xAC , 0x03 ),
203
+ 80000 : (0x04 , 0x9a , 0x01 ),
204
+ 50000 : (0x03 , 0xb6 , 0x04 ),
205
+ 40000 : (0x04 , 0xb6 , 0x04 ),
206
+ 33000 : (0x0a , 0x9a , 0x02 ),
207
+ 31250 : (0x07 , 0xac , 0x03 ),
208
+ 25000 : (0x07 , 0xB6 , 0x04 ),
209
+ 20000 : (0x09 , 0xb6 , 0x04 ),
210
+ 10000 : (0x13 , 0xb6 , 0x04 ),
211
+ 5000 : (0x27 , 0xb6 , 0x04 ),
212
+ 666000 : (0x00 , 0x88 , 0x01 ),
213
+ }
189
214
}
190
215
191
216
@@ -217,6 +242,7 @@ def __init__(
217
242
cs_pin ,
218
243
* ,
219
244
baudrate : int = 250000 ,
245
+ xtal_frequency : int = 16000000 ,
220
246
loopback : bool = False ,
221
247
silent : bool = False ,
222
248
auto_restart : bool = False ,
@@ -228,6 +254,7 @@ def __init__(
228
254
:param ~digitalio.DigitalInOut cs_pin: SPI bus enable pin
229
255
:param int baudrate: The bit rate of the bus in Hz, using a 16Mhz crystal. All devices on\
230
256
the bus must agree on this value. Defaults to 250000.
257
+ :param bool xtal_frequency: MCP2515 crystal frequency. Valid values are: 16000000 and 8000000. Defaults to 16000000 (16MHz).\
231
258
:param bool loopback: Receive only packets sent from this device, and send only to this\
232
259
device. Requires that `silent` is also set to `True`, but only prevents transmission to\
233
260
other devices. Otherwise the send/receive behavior is normal.
@@ -262,9 +289,9 @@ def __init__(
262
289
self ._mode = None
263
290
self ._bus_state = BusState .ERROR_ACTIVE
264
291
self ._baudrate = baudrate
292
+ self ._xtal_frequency = xtal_frequency
265
293
self ._loopback = loopback
266
294
self ._silent = silent
267
- self ._baudrate = baudrate
268
295
269
296
self ._init_buffers ()
270
297
self .initialize ()
@@ -604,9 +631,11 @@ def _get_tx_buffer(self):
604
631
return tx_buffer
605
632
606
633
def _set_baud_rate (self ):
634
+ # ******* set baud rate ***********
635
+ if self ._xtal_frequency is not 16000000 and self ._xtal_frequency is not 8000000 :
636
+ raise RuntimeError ("Incorrect xtal frequency" )
607
637
608
- # *******8 set baud rate ***********
609
- cnf1 , cnf2 , cnf3 = _BAUD_RATES [self .baudrate ]
638
+ cnf1 , cnf2 , cnf3 = _BAUD_RATES [self ._xtal_frequency ][self .baudrate ]
610
639
611
640
self ._set_register (_CNF1 , cnf1 )
612
641
self ._set_register (_CNF2 , cnf2 )
0 commit comments