169
169
["CTRL_REG" , "STD_ID_REG" , "INT_FLAG_MASK" , "LOAD_CMD" , "SEND_CMD" ],
170
170
)
171
171
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
+
172
181
_BAUD_RATES = {
173
182
# This is magic, don't disturb the dragon
174
183
# expects a 16Mhz crystal
175
184
16000000 : {
176
- # CNF1, CNF2, CNF3
185
+ # CNF1, CNF2, CNF3
177
186
1000000 : (0x00 , 0xD0 , 0x82 ),
178
187
500000 : (0x00 , 0xF0 , 0x86 ),
179
188
250000 : (0x41 , 0xF1 , 0x85 ),
193
202
5000 : (0x3F , 0xFF , 0x87 ),
194
203
666000 : (0x00 , 0xA0 , 0x04 ),
195
204
},
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
198
226
8000000 : {
199
- # CNF1, CNF2, CNF3
227
+ # CNF1, CNF2, CNF3
200
228
500000 : (0x00 , 0x91 , 0x01 ),
201
229
250000 : (0x40 , 0xB5 , 0x01 ),
202
230
200000 : (0x00 , 0xB6 , 0x04 ),
@@ -246,7 +274,7 @@ def __init__(
246
274
cs_pin ,
247
275
* ,
248
276
baudrate : int = 250000 ,
249
- crystal_freq : Literal [8000000 , 16000000 ] = 16000000 ,
277
+ crystal_freq : Literal [8000000 , 10000000 , 16000000 ] = 16000000 ,
250
278
loopback : bool = False ,
251
279
silent : bool = False ,
252
280
auto_restart : bool = False ,
@@ -259,7 +287,7 @@ def __init__(
259
287
:param int baudrate: The bit rate of the bus in Hz, using a 16Mhz crystal. All devices on\
260
288
the bus must agree on this value. Defaults to 250000.
261
289
: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).\
263
291
:param bool loopback: Receive only packets sent from this device, and send only to this\
264
292
device. Requires that `silent` is also set to `True`, but only prevents transmission to\
265
293
other devices. Otherwise the send/receive behavior is normal.
@@ -637,7 +665,7 @@ def _get_tx_buffer(self):
637
665
638
666
def _set_baud_rate (self ):
639
667
# ******* set baud rate ***********
640
- if self ._crystal_freq not in ( 16000000 , 8000000 ) :
668
+ if self ._crystal_freq not in _BAUD_RATES :
641
669
raise ValueError (
642
670
f"Incorrect crystal frequency - must be one of: { tuple (_BAUD_RATES .keys ())} "
643
671
)
0 commit comments