# 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 v1.16.

## calc_busparamstq()

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

  Calculate closest matching busparameters.

  The device clock frequency, `clk_freq`, can be obtained via `ChannelData.clock_info.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=33.5,
      ... clk_freq=clock_info.frequency())
      >>> params
      BusParamsTq(tq=170, prop=107, phase1=31, phase2=31, sjw=57, prescaler=1)

  Note: Paramters are calculated with prescaler = 1, minimum sjw returned is 1.

  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, 0-100 (%) clk_freq (`float`) – Device clock frequency (Hz) target_prop_tq (`int`, Optional) – Wanted propagation segment (time quanta)
  Returns: | `BusParamsTq` – Calculated bus parameters

  New in version v1.16.

## 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 v1.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 v1.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.

  `sync_jump_width`()

    Return sync jump width (SJW) in percentage.