Skip to content

Commit d5eb647

Browse files
committed
Fix bitrate calculation for PCAN interfaces
1 parent 9514f24 commit d5eb647

1 file changed

Lines changed: 9 additions & 20 deletions

File tree

pycyphal/transport/can/media/pythoncan/_pythoncan.py

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -491,32 +491,21 @@ def _construct_pcan(parameters: _InterfaceParameters) -> can.ThreadSafeBus:
491491
if isinstance(parameters, _FDInterfaceParameters):
492492
if parameters.bitrate[0] == 0 or parameters.bitrate[1] == 0:
493493
raise InvalidMediaConfigurationError("Bitrate must be non-zero")
494-
# These magic numbers come from the settings of PCAN adapter.
495-
# They don't allow any direct baudrate settings, you have to set all lengths and value of the main frequency.
496-
# Bit lengths below are very universal and can be applied for almost every popular baudrate.
497-
# There is probably a better solution here, but it needs significantly more time to implement it.
498-
f_clock = 40000000
499-
nom_tseg1, nom_tseg2, nom_sjw = 3, 1, 1
500-
data_tseg1, data_tseg2, data_sjw = 3, 1, 1
501-
502-
nom_br = int(f_clock / parameters.bitrate[0] / (nom_tseg1 + nom_tseg2 + nom_sjw))
503-
data_br = int(f_clock / parameters.bitrate[1] / (data_tseg1 + data_tseg2 + data_sjw))
504-
# TODO: validate the result and see if it is within an acceptable range
505494

495+
timing = can.BitTimingFd.from_sample_point(
496+
f_clock=80_000_000, # TODO: 80 MHz is a good choice for high data rates, what about lower ones?
497+
nom_bitrate=parameters.bitrate[0],
498+
nom_sample_point=87.5,
499+
data_bitrate=parameters.bitrate[1],
500+
data_sample_point=87.5,
501+
)
502+
_logger.debug("PCAN timing solution: %s", timing)
506503
return (
507504
PythonCANBusOptions(),
508505
can.ThreadSafeBus(
509506
interface=parameters.interface_name,
510507
channel=parameters.channel_name,
511-
f_clock=f_clock,
512-
nom_brp=nom_br,
513-
data_brp=data_br,
514-
nom_tseg1=nom_tseg1,
515-
nom_tseg2=nom_tseg2,
516-
nom_sjw=nom_sjw,
517-
data_tseg1=data_tseg1,
518-
data_tseg2=data_tseg2,
519-
data_sjw=data_sjw,
508+
timing=timing,
520509
fd=True,
521510
),
522511
)

0 commit comments

Comments
 (0)