Skip to content

Commit 76ef38d

Browse files
committed
add docstrings
1 parent 8754b15 commit 76ef38d

File tree

1 file changed

+77
-6
lines changed

1 file changed

+77
-6
lines changed

can/bit_timing.py

Lines changed: 77 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,20 @@
44

55

66
class BitTiming(Mapping):
7-
"""Representation of a bit timing configuration.
7+
"""Representation of a bit timing configuration for a CAN 2.0 bus.
88
9-
The class can be constructed in various ways, depending on the information
10-
available or the capabilities of the interfaces that need to be supported.
11-
12-
The preferred way is using bitrate, CAN clock frequency, TSEG1, TSEG2, SJW::
9+
The class can be constructed in multiple ways, depending on the information
10+
available. The preferred way is using bitrate, CAN clock frequency, tseg1, tseg2 and sjw::
1311
1412
can.BitTiming(bitrate=1_000_000, f_clock=8_000_000, tseg1=5, tseg2=1, sjw=1)
1513
16-
It is also possible specify BTR registers directly::
14+
It is also possible to specify BTR registers::
1715
1816
can.BitTiming.from_registers(f_clock=8_000_000, btr0=0x00, btr1=0x14)
17+
18+
or to calculate the timings for a given sample point::
19+
20+
can.BitTiming.from_sample_point(f_clock=16_000_000, bitrate=500_000, sample_point=81.25)
1921
"""
2022

2123
def __init__(
@@ -305,6 +307,35 @@ def __iter__(self) -> Iterator[str]:
305307

306308

307309
class BitTimingFd(Mapping):
310+
"""Representation of a bit timing configuration for a CAN FD bus.
311+
312+
The class can be constructed in multiple ways, depending on the information
313+
available. The preferred way is using bitrate, CAN clock frequency, tseg1, tseg2 and sjw
314+
for both the arbitration (nominal) and data phase::
315+
316+
can.BitTimingFd(
317+
f_clock=80_000_000,
318+
nom_bitrate=1_000_000,
319+
nom_tseg1=59,
320+
nom_tseg2=20,
321+
nom_sjw=10,
322+
data_bitrate=8_000_000,
323+
data_tseg1=6,
324+
data_tseg2=3,
325+
data_sjw=2,
326+
)
327+
328+
or to calculate the timings for a given pair of arbitration and data sample points::
329+
330+
can.BitTimingFd.from_sample_point(
331+
f_clock=80_000_000,
332+
nom_bitrate=1_000_000,
333+
nom_sample_point=75.0,
334+
data_bitrate=8_000_000,
335+
data_sample_point=70.0,
336+
)
337+
"""
338+
308339
def __init__(
309340
self,
310341
f_clock: int,
@@ -416,6 +447,20 @@ def from_sample_point(
416447
data_bitrate: int,
417448
data_sample_point: float,
418449
) -> "BitTimingFd":
450+
"""Create a BitTimingFd instance for a given nominal/data sample point pair.
451+
452+
:param int f_clock:
453+
The CAN system clock frequency in Hz.
454+
Usually the oscillator frequency divided by 2.
455+
:param int nom_bitrate:
456+
Nominal bitrate in bit/s.
457+
:param int nom_sample_point:
458+
The sample point value of the arbitration phase in percent.
459+
:param int data_bitrate:
460+
Data bitrate in bit/s.
461+
:param int data_sample_point:
462+
The sample point value of the data phase in percent.
463+
"""
419464
if nom_sample_point < 50.0:
420465
raise ValueError(
421466
f"nom_sample_point (={nom_sample_point}) must not be below 50%."
@@ -499,58 +544,84 @@ def from_sample_point(
499544

500545
@property
501546
def nom_bitrate(self) -> int:
547+
"""Nominal (arbitration phase) bitrate."""
502548
return self["nom_bitrate"]
503549

504550
@property
505551
def nom_brp(self) -> int:
552+
"""Prescaler value for the arbitration phase."""
506553
return int(round(self.f_clock / (self.nom_bitrate * self.nbt)))
507554

508555
@property
509556
def nbt(self) -> int:
557+
"""Number of time quanta in a bit of the arbitration phase."""
510558
return 1 + self.nom_tseg1 + self.nom_tseg2
511559

512560
@property
513561
def nom_tseg1(self) -> int:
562+
"""Time segment 1 value of the arbitration phase.
563+
564+
This is the sum of the propagation time segment and the phase buffer segment 1.
565+
"""
514566
return self["nom_tseg1"]
515567

516568
@property
517569
def nom_tseg2(self) -> int:
570+
"""Time segment 2 value of the arbitration phase. Also known as phase buffer segment 2."""
518571
return self["nom_tseg2"]
519572

520573
@property
521574
def nom_sjw(self) -> int:
575+
"""Synchronization jump width of the arbitration phase.
576+
577+
The phase buffer segments may be shortened or lengthened by this value.
578+
"""
522579
return self["nom_sjw"]
523580

524581
@property
525582
def nom_sample_point(self) -> float:
583+
"""Sample point of the arbitration phase in percent."""
526584
return 100.0 * (1 + self.nom_tseg1) / (1 + self.nom_tseg1 + self.nom_tseg2)
527585

528586
@property
529587
def data_bitrate(self) -> int:
588+
"""Bitrate of the data phase in bit/s."""
530589
return self["data_bitrate"]
531590

532591
@property
533592
def data_brp(self) -> int:
593+
"""Prescaler value for the data phase."""
534594
return int(round(self.f_clock / (self.data_bitrate * self.dbt)))
535595

536596
@property
537597
def dbt(self) -> int:
598+
"""Number of time quanta in a bit of the data phase."""
538599
return 1 + self.data_tseg1 + self.data_tseg2
539600

540601
@property
541602
def data_tseg1(self) -> int:
603+
"""TSEG1 value of the data phase.
604+
605+
This is the sum of the propagation time segment and the phase buffer segment 1.
606+
"""
542607
return self["data_tseg1"]
543608

544609
@property
545610
def data_tseg2(self) -> int:
611+
"""TSEG2 value of the data phase. Also known as phase buffer segment 2."""
546612
return self["data_tseg2"]
547613

548614
@property
549615
def data_sjw(self) -> int:
616+
"""Synchronization jump width of the data phase.
617+
618+
The phase buffer segments may be shortened or lengthened by this value.
619+
"""
550620
return self["data_sjw"]
551621

552622
@property
553623
def data_sample_point(self) -> float:
624+
"""Sample point of the data phase in percent."""
554625
return 100.0 * (1 + self.data_tseg1) / (1 + self.data_tseg1 + self.data_tseg2)
555626

556627
@property

0 commit comments

Comments
 (0)