can_init()
Description
Initializes a CAN interface for classic (non-FD) operation at a single, fixed bit rate and installs the receive buffer that frames are queued into before an rx event fires. Must be called once, before any other can_* function is used on the same interface.
Syntax
can_init(can_interface, can_ctrl_mode, bit_rate, can_rx_buffer[]);
Parameters
Parameter | Description |
| CAN interface to initialize – CAN1 or CAN2. |
| CAN_CTRL_MODE_NORMAL - Standard operating mode: the controller participates fully in bus arbitration and acknowledges frames. These options can be passed as a bitwise OR of one or more flags, thus controlling retransmission behavior and whether Normal mode is enabled. |
| Bus bit rate, in bit/s – 20000, 33333, 50000, 62500, 83333, 100000, 111111, 125000, 250000, 500000, 666666, 1000000 |
| Array reserved by the script to hold incoming raw CAN frame data; sized to the maximum amount of buffering required (can_rx_buffer{4096} in the example). |
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 | CAN Bus already initialized. |
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_rx_buffer{4096};
main()
{
can_init(CAN_BUS, CAN_CTRL_MODE_NORMAL | CAN_CTRL_MODE_ONE_SHOT, 500000, can_rx_buffer);
for(;;)
{
Delay(1000);
}
}Notes and Warnings
Use can_init for classic CAN buses. For CAN FD buses that need Bit Rate Switching and a separate data-phase rate, use can_fd_init instead.