# Bus Parameters

## calc_bitrate()

`canlib.canlib.busparams.``calc_bitrate`(target_bitrate, clk_freq)

  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.

## calc_busparamstq()

`canlib.canlib.busparams.``calc_busparamstq`(target_bitrate, target_sample_point, target_sync_jump_width, clk_freq, target_prop_tq=None, prescaler=1)

  Calculate closest matching busparameters.

  The device clock frequency, `clk_freq`, can be obtained via `ClockInfo.frequency()`:

      >>> chd = canlib.ChannelData(channel_number=0)
      >>> clock_info = chd.clock_info
      >>> clock_info.frequency()
      80000000.0

  Now call `calc_busparamstq` with target values, and a `BusParamsTq` object 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_IMPLEMENTED` is 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 parameters

  New in version 1.16.

  Changed in version 1.17.

## to_BusParamsTq()

`canlib.canlib.busparams.``to_BusParamsTq`(clk_freq, bus_param, prescaler=1, data=False)

  Convert `BitrateSetting` or `tuple` to `BusParamsTq`.

  The device clock frequency, `clk_freq`, can be obtained via `ClockInfo.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 (`BitrateSetting` or `tuple`) – `BitrateSetting` object or (freq, tseg1, tseg2, sjw) `tuple` to convert. prescaler (`int`) – The prescaler to use in the created `BusParamsTq` object. data (`bool`) – Set to True if the resulting `BusParamsTq` should be used for CAN FD data bitrate parameters.
  Returns: | `BusParamsTq` object with equivalent settings as the input argument.

  New in version 1.17.

## to_BitrateSetting()

`canlib.canlib.busparams.``to_BitrateSetting`(clk_freq, bus_param)

  Convert `BusParamsTq` to `BitrateSetting`.

  The device clock frequency, `clk_freq`, can be obtained via `ClockInfo.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`) – `BusParamsTq` object to convert.
  Returns: | `BitrateSetting` object with equivalent settings as the input argument.

  New in version 1.17.

## ClockInfo

class `canlib.canlib.busparams.``ClockInfo`(numerator, denominator, power_of_ten, accuracy)

  Information about clock a oscillator

  The clock frequency is set in the form:

  > frequency = numerator / denominator * 10 ** power_of_ten +/- accuracy

  New in version 1.16.

  `frequency`()

    Returns an approximation of the clock frequency as a float.

## BusParamsTq

class `canlib.canlib.busparams.``BusParamsTq`(tq, phase1, phase2, sjw, prescaler=1, prop=None)

  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_busparamstq` which 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.

  `bitrate`(clk_freq)

    Return bitrate assuming the given clock frequency

    Parameters: | clk_freq (`int`) – Clock frequency (in Hz)

  `sample_point`()

    Return sample point in percentage.

  `sample_point_ns`(clk_freq)

    Return sample point in ns.

    New in version 1.17.

  `sync_jump_width`()

    Return sync jump width (SJW) in percentage.

## BusParamHelper

class `canlib.canlib.channel.``BusParamHelper`(channel)

  Helper class for setting and getting bus parameters. Automatically converts supported objects to `BusParamsTq`.

  `bitrate_to_BusParamsTq`(freq_a, freq_d=None)

    Calculate bus parameters based on predefined frequencies.

    This function uses the `Bitrate` and `BitrateFD` values to create bus parameters for use with the new bus parameter API.

    Parameters: | freq_a (`Bitrate` or `BitrateFD`) – The bitrate for classic CAN channels specified as a `Bitrate`, or the arbitration bitrate for CAN FD channels specified as a `BitrateFD`. freq_d (`BitrateFD`) – Data bitrate for CAN FD channels.
    Returns: | (`nominal`, `None`) for classic CAN, where `nominal` is a `BusParamsTq` object. (`nominal`, `data`) for CAN FD, where both `nominal` and `data` are `BusParamsTq` objects, representing the arbitration bitrate and the data bitrate, respectively.

    New in version 1.17.

  `get`()

    Get current bus parameters.

    Returns: | (`nominal`, `data`) tuple where `nominal` and `data` are the bus parameters as `BusParamsTq` objects. data is None for classic CAN channels.

  `set`(nominal, data=None)

    Set bus parameters. Both arguments must be of the same type.

    Parameters: | nominal (`BusParamsTq` or `BitrateSetting`) – Specifies bus parameters for classic CAN or arbitration phase parameters for CAN FD. data (`BusParamsTq` or `BitrateSetting`) – Specifies data rate parameters for CAN FD.

## BusParamHelperLegacy

class `canlib.canlib.channel.``BusParamHelperLegacy`(channel)

  Helper class for setting and getting bus parameters. Automatically converts `BitrateSetting` objects to (freq, tseg1, tseg2, sjw) tuples.

  `get`()

    Get bus parameters.

    Returns: | (`nominal`, `data`) tuple where nominal and data are the bus parameters as `BitrateSetting` objects. data is None for classic CAN channels.

  `set`(nominal, data=None)

    Set bus parameters.

    Parameters: | nominal (`BitrateSetting`) – Specifies bus parameters for classic CAN or arbitration phase parameters for CAN FD. data (`BusParamsTq` or `BitrateSetting`) – Specifies data rate parameters for CAN FD.

# BitrateSetting

class `canlib.canlib.busparams.``BitrateSetting`(freq, tseg1, tseg2, sjw, nosamp=1, syncMode=0)

  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.

  classmethod `from_predefined`(bitrate)

    Create a BitrateSetting object using one of the `Bitrate` or `BitrateFD` enumerations.