can_bit_rate_scan()

can_bit_rate_scan()

Description

Probes the bus connected to the given CAN interface against the supported CAN Bitrate values and reports the detected bit rate together with the resulting controller status in a can_status structure. Useful for auto-detecting the bit rate of a bus whose configuration is not known ahead of time.

Syntax

can_bit_rate_scan(can_interface, can_status);

Parameters

Parameter

 

Description

can_interface

 

CAN interface to configure – CAN1 or CAN2.

can_status

.state

Current controller state:

  • CAN_STATE_ERROR_ACTIVE - RX/TX error count < 96

  • CAN_STATE_ERROR_WARNING - RX/TX error count < 128

  • CAN_STATE_ERROR_PASSIVE - RX/TX error count < 256

  • CAN_STATE_BUS_OFF - RX/TX error count >= 256

  • CAN_STATE_STOPPED - Device is stopped

.error

Bitwise error code flags, or 0 when no error is pending:

  • CAN_PROTOCOL_NO_ERROR

  • CAN_PROTOCOL_STUFF_ERROR

  • CAN_PROTOCOL_FORM_ERROR

  • CAN_PROTOCOL_ACK_ERROR

  • CAN_PROTOCOL_RECESSIVE_ERROR

  • CAN_PROTOCOL_DOMINANT_ERROR

  • CAN_PROTOCOL_CRC_ERROR

  • CAN_PROTOCOL_SOFTWARE_ERROR

.tx_error_count

CAN controller transmit error counter.

.rx_error_count

CAN controller receive error counter.

.frame_count

Number of frames processed since the interface was initialized.

.bit_rate

Nominal (arbitration phase) bit rate currently configured, in bit/s.

.data_bit_rate

FD data-phase bit rate currently configured, in bit/s (0 when FD/BRS is not enabled).

.status_flags

Last CAN bus status change reason:

  • CAN_STATUS_NO_CHANGE - No change in status detected

  • CAN_STATUS_NEW_STATE - .state state value changed

  • CAN_STATUS_NEW_RX_ARB_ERR - Protocol error in arbitration phase detected

  • CAN_STATUS_NEW_RX_DATA_ERR - Protocol error in data phase detected

  • CAN_STATUS_NEW_RX_ERR - Protocol error in received frame detected

  • CAN_STATUS_NEW_TX_ERR - .tx_error_countincremented

  • CAN_STATUS_NEW_FRAME - new frame received

  • CAN_STATUS_NEW_BIT_RATE - .bit_rate value changed

Returns

Return value

Return explanation

SCRIPT_OPERATION_SUCCESS

The operation completed successfully.

SCRIPT_OPERATION_FAILED

The operation could not be completed (invalid state, hardware error, etc.).

SCRIPT_NO_ACCESS

The CAN interface is already taken by other script.

SCRIPT_PARAM_INVALID

One or more passed parameters are invalid.

Function call example

#include <io> #include <read> #include <float> #include <string> #include <core> #include <write> #include <define> #include <socket> #include <can> #define CAN_BUS CAN1 new can_status[.state, .error, .tx_error_count, .rx_error_count, .frame_count, .bit_rate, .data_bit_rate, .status_flags]; main() { can_bit_rate_scan(CAN_BUS, can_status); for(;;) { Delay(1000); } }

Notes and Warnings

  • Run can_bit_rate_scan before can_init/can_fd_init when the bus bit rate is unknown, then initialize the interface with the detected value.

  • Scanning takes the interface offline while it cycles through candidate rates; avoid calling it while the bus is expected to be carrying live traffic that must not be missed.