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 to configure – CAN1 or CAN2. |
|
| Current controller state:
|
| Bitwise error code flags, or 0 when no error is pending:
| |
| CAN controller transmit error counter. | |
| CAN controller receive error counter. | |
| Number of frames processed since the interface was initialized. | |
| Nominal (arbitration phase) bit rate currently configured, in bit/s. | |
| FD data-phase bit rate currently configured, in bit/s (0 when FD/BRS is not enabled). | |
| Last CAN bus status change reason:
|
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.