Bus Parameters¶
calc_bitrate()¶
-
canlib.canlib.busparams.calc_bitrate(target_bitrate, clk_freq)[source]¶ Calculate nearest available bitrate
Parameters: - target_bitrate (
int) – Wanted bitrate (bit/s) - clk_freq (
int) – Device clock frequency (Hz)
Returns: The returned tuple is a
(bitrate, tq)named tuple of –bitrate(int): Available bitrate, could be a rounded value (bit/s)tq(int): Number of time quanta in one bit
New in version 1.16.
- target_bitrate (
calc_busparamstq()¶
-
canlib.canlib.busparams.calc_busparamstq(target_bitrate, target_sample_point, target_sync_jump_width, clk_freq, target_prop_tq=None, prescaler=1)[source]¶ Calculate closest matching busparameters.
The device clock frequency,
clk_freq, can be obtained viaClockInfo.frequency():>>> chd = canlib.ChannelData(channel_number=0) >>> clock_info = chd.clock_info >>> clock_info.frequency() 80000000.0
Now call
calc_busparamstqwith target values, and aBusParamsTqobject will be returned:>>> params = calc_busparamstq( ... target_bitrate=470_000, ... target_sample_point=82, ... target_sync_jump_width=15.3, ... clk_freq=clock_info.frequency()) >>> params BusParamsTq(tq=170, prop=107, phase1=31, phase2=31, sjw=26, prescaler=1)
A target number of time quanta in the propagation segment can also be specified by the user.
The returned BusParamsTq may not be valid on all devices. If
Error.NOT_IMPLEMENTEDis encountered when trying to set the bitrate with the returned BusParamsTq, provide a prescaler argument higher than one and retry. This will lower the total number of time quanta in the bit and thus make the BusParamsTq valid.Example
>>> params = calc_busparamstq( ... target_bitrate=470_000, ... target_sample_point=82, ... target_sync_jump_width=15.3, ... clk_freq=clock_info.frequency(), ... target_prop_tq=50, ... prescaler=2) >>> params BusParamsTq(tq=85, prop=25, phase1=44, phase2=15, sjw=13, prescaler=2)
Note
- Minimum sjw returned is 1, maximum sjw is min(phase1, phase2).
Parameters: - target_bitrate (
float) – Wanted bitrate (bit/s) - target_sample_point (
float) – Wanted sample point in percentage (0-100) - target_sync_jump_width (
float) – Wanted sync jump width in percentage (0-100) - clk_freq (
float) – Device clock frequency (Hz) - target_prop_tq (
int, Optional) – Wanted propagation segment (time quanta) - prescaler (
int, Optional) – Wanted prescaler (at most 2 for CAN FD)
Returns: BusParamsTq– Calculated bus parametersNew in version 1.16.
Changed in version 1.17.
to_BusParamsTq()¶
-
canlib.canlib.busparams.to_BusParamsTq(clk_freq, bus_param, prescaler=1, data=False)[source]¶ Convert
BitrateSettingortupletoBusParamsTq.The device clock frequency,
clk_freq, can be obtained viaClockInfo.frequency():>>> chd = canlib.ChannelData(channel_number=0) >>> clock_info = chd.clock_info >>> clock_info.frequency() 80000000.0
Parameters: - clk_freq (
float) – Clock frequency of device. - bus_param (
BitrateSettingortuple) –BitrateSettingobject or - (freq, tseg1, tseg2, sjw) `tuple` to convert.
- prescaler (
int) – The prescaler to use in the createdBusParamsTq - object.
- data (
bool) – Set to True if the resultingBusParamsTqshould be - used for CAN FD data bitrate parameters.
Returns: BusParamsTqobject with equivalent settings as the input argument.New in version 1.17.
- clk_freq (
to_BitrateSetting()¶
-
canlib.canlib.busparams.to_BitrateSetting(clk_freq, bus_param)[source]¶ Convert
BusParamsTqtoBitrateSetting.The device clock frequency,
clk_freq, can be obtained viaClockInfo.frequency():>>> chd = canlib.ChannelData(channel_number=0) >>> clock_info = chd.clock_info >>> clock_info.frequency() 80000000.0
Parameters: - clk_freq (
float) – Clock frequency of device. - bus_param (
BusParamsTq) –BusParamsTqobject to convert.
Returns: BitrateSettingobject with equivalent settings as the input argument.New in version 1.17.
- clk_freq (
ClockInfo¶
BusParamsTq¶
-
class
canlib.canlib.busparams.BusParamsTq(tq, phase1, phase2, sjw, prescaler=1, prop=None)[source]¶ Holds parameters for busparameters in number of time quanta.
If you don’t want to specify the busparameters in time quanta directly, you may use
calc_busparamstqwhich returns an object of this class.>>> params = calc_busparamstq( ... target_bitrate=470_000, ... target_sample_point=82, ... target_sync_jump_width=33.5, ... clk_freq=clk_freq) >>> params BusParamsTq(tq=170, prop=107, phase1=31, phase2=31, sjw=57, prescaler=1)
You may now query for the actual Sample Point and Sync Jump Width expressed as percentages of total bit time quanta:
>>> params.sample_point() 81.76470588235294
>>> params.sync_jump_width() 33.52941176470588
If you supply the clock frequency, you may also calculate the corresponding bitrate:
>>> params.bitrate(clk_freq=80_000_000) 470588.23529411765
Parameters: - tq (
int) – Number of time quanta in one bit. - phase1 (
int) – Number of time quanta in Phase Segment 1. - phase2 (
int) – Number of time quanta in Phase Segment 2. - sjw (
int) – Number of time quanta in Sync Jump Width. - prescaler (
int) – Prescaler value (1-2 to enable auto in CAN FD) - prop (
int, optional) – Number of time quanta in Propagation Segment.
New in version 1.16.
- tq (
BusParamHelper¶
-
class
canlib.canlib.channel.BusParamHelper(channel)[source]¶ Helper class for setting and getting bus parameters. Automatically converts supported objects to
BusParamsTq.-
bitrate_to_BusParamsTq(freq_a, freq_d=None)[source]¶ Calculate bus parameters based on predefined frequencies.
This function uses the
BitrateandBitrateFDvalues to create bus parameters for use with the new bus parameter API.Parameters: Returns: (
nominal,None) for classic CAN, wherenominalis aBusParamsTqobject.(
nominal,data) for CAN FD, where bothnominalanddataareBusParamsTqobjects, representing the arbitration bitrate and the data bitrate, respectively.New in version 1.17.
-
get()[source]¶ Get current bus parameters.
Returns: ( nominal,data) tuple wherenominalanddataare the bus parameters asBusParamsTqobjects. data is None for classic CAN channels.
-
set(nominal, data=None)[source]¶ Set bus parameters. Both arguments must be of the same type.
Parameters: - nominal (
BusParamsTqorBitrateSetting) – Specifies bus parameters for classic CAN or arbitration phase parameters for CAN FD. - data (
BusParamsTqorBitrateSetting) – Specifies data rate parameters for CAN FD.
- nominal (
-
BusParamHelperLegacy¶
-
class
canlib.canlib.channel.BusParamHelperLegacy(channel)[source]¶ Helper class for setting and getting bus parameters. Automatically converts
BitrateSettingobjects to (freq, tseg1, tseg2, sjw) tuples.-
get()[source]¶ Get bus parameters.
Returns: ( nominal,data) tuple where nominal and data are the bus parameters asBitrateSettingobjects. data is None for classic CAN channels.
-
set(nominal, data=None)[source]¶ Set bus parameters.
Parameters: - nominal (
BitrateSetting) – Specifies bus parameters for classic CAN or arbitration phase parameters for CAN FD. - data (
BusParamsTqorBitrateSetting) – Specifies data rate parameters for CAN FD.
- nominal (
-
BitrateSetting¶
-
class
canlib.canlib.busparams.BitrateSetting(freq, tseg1, tseg2, sjw, nosamp=1, syncMode=0)[source]¶ Class that holds bitrate setting.
Parameters: - freq – Bitrate in bit/s.
- tseg1 – Number of quanta from (but not including) the Sync Segment to the sampling point.
- tseg2 – Number of quanta from the sampling point to the end of the bit.
- sjw – The Synchronization Jump Width.
- nosamp – The number of sampling points, only 1 is supported.
- syncMode – Unsupported and ignored.
New in version 1.17.