CAN out - CAN output mailbox

The CAN_out block implements an output mailbox that supports sending messages using the CAN bus protocol. To receive messages, the CAN in block should be used instead. The supported CAN protocol variants are as follows:

  • CAN 2.0A: standard format (11-bit identifier), up to 8 bytes payload
  • CAN 2.0B: extended format (29-bit identifier), up to 8 bytes payload
  • CAN FD: standard and extended format, up to 64 bytes payload

CAN 2.0A/B are supported on all imperix controllers, provided that the related hardware exists. However, CAN FD (flexible data) is only supported on the B-Box 4. The detailed compatibility list is shown below.

CANopen is a high-level protocol (OSI layer 7), which should not be confused with CAN (OSI layers 1 and 2). It is currently not supported on imperix equipment.

ControllerHardware resourcesCAN 2.0ACAN 2.0BCAN FD
B-Box 42x channels (one RJ45 socket for each)
Max. 5 Mbps (FD)
YesYesYes
B-Box 3 (RCP)1x channel (one RJ45 socket)
Max. 1 Mbps
YesYesNo
B-Board 3 (PRO)1x channel (Tx/Rx pins or D-sub on EVM)
Max. 1 Mbps
YesYesNo
B-Box microN/ANoNoNo
TPI 80321x channel (two common RJ45 sockets)
Max. 1 Mbps
YesYesNo
Supported variants of the CAN protocol on imperix controllers

Parameters

Addressing

  • Physical port: selects the port on which the mailbox operate (Port A or Port B). Port B is only available on B-Box 4. For all the other devices, Port A must be selected.
  • Frame format: selects between Standard (11-bit identifier) and Extended (29-bit identifier).
  • Identifier (ID): sets the CAN identifier.
  • Enable CAN FD Bit Rate Switch (BRS): Activates the CAN-FD extension to enable the communication at a higher data bitrate (see also CAN bus configuration below).

Communication parameters

  • Signal(s) type: defines the data type accepted in the data input (int8, int16, int32, uint8, uint16, uint32, float32, or float64).
  • Number of signals: specifies the vector size of the data to be sent.
  • Byte order: defines the byte order in which the data will be sent. Either little-endian or big-endian.
  • Data transmission mode: selects when the data is sent.
    • On-demand: the user manually triggers the message transmissions.
    • Periodically: the message is sent periodically, whether the data has been changed or not.
  • Tx Frequency (Hz): sets the data transmission frequency when the Data transmission mode is set to Periodically.

CAN bus configuration

In Simulink, the CAN bus configuration is performed from within the CAN block. In PLECS, the configuration is performed in the Target tab of Coder options windows (Coder -> Coder options, or Ctrl+Alt+B).

  • The bitrate is the rate at which bits are transmitted on the bus (up to 1 Mbps),
  • The data bitrate is the increased bitrate used to transmit the CAN FD frame payload when the Bit Rate Switch (BRS) is enabled (up to 5 Mbps).
CAN bus configuration in Simulink
CAN bus configuration in PLECS

Simulink block

Signal specification

  • The data input signal supports a vector of data. The accepted data type is configured by Signal type parameter. The vector length can be configured with Number of signals parameter.
  • The second input is the send data signal. It is used to initiate a data transmission when the on-demand mode has been selected. Data is sent upon a rising edge on this signal.

Mask

PLECS block

Signal specification

  • The data input signal supports a vector of data. The accepted data type is configured by Signal type parameter. The vector length can be configured with Number of signals parameter.
  • The second input is the send data signal. It is used to initiate a data transmission when the on-demand mode has been selected. Data is sent upon a rising edge on this signal.

Mask

C++ functions

void Can_ConfigureCanBus(unsigned int baudrate);Code language: C++ (cpp)

Configures the baud rate of the CAN bus. Baud rates up to 1 Mbit/s are supported.

It has to be called in UserInit().

Parameters

  • baudrate: Baud rate of the CAN bus in bit/s (up to 1’000’000 bit/s)
bool Can_ConfigureOutputMailbox(unsigned int maildboxId, unsigned int canAddress, unsigned int dataLength, float maxTxFrequency = 100, tEndianness endianness = LITTLE_ENDIAN);Code language: C++ (cpp)

Configures a CAN output mailbox.

It has to be called in UserInit().

Parameters

  • mailboxId: a unique ID used to distinguish mailboxes from each other. This ID must be unique throughout the code for all ETH and CAN input/output mailboxes.
  • canAddress: address to whom data will be sent. This parameter is also used as the CAN identifier. Therefore, it also represents the message priority. CAN address range is 0 to 2047 (11 bits).
  • dataLength: number of bytes of data to transmit (1 to 8 bytes).
  • maxTxFrequency: maximal frequency at which data can be sent. The frequency must be a multiple of the interrupt frequency. If the requested frequency is not achievable, it will be automatically set to the closest valid frequency.
  • endianness: defines the frame bytes order. (BIG_ENDIAN or LITTLE_ENDIAN)

Return value

  • bool: returns false if the configuration fails. The reason may be that too many output mailboxes were created or if canAddress is out of range.
// 64-bit (8 bytes) types: uint64, int64 and double
int Can_Write(unsigned int maildboxId, uint64_t data); //8 bytes
int Can_Write(unsigned int maildboxId, int64_t data); //8 bytes
int Can_Write(unsigned int maildboxId, double data); //8 bytesCode language: C++ (cpp)
// 32-bit (4 bytes) types: int32, uint32 and float
// dataLow  represents the bytes 0, 1, 2 and 3
// dataHigh represents the bytes 4, 5, 6 and 7
int Can_Write(unsigned int maildboxId, unsigned int dataLow, unsigned int dataHigh); //8 bytes
int Can_Write(unsigned int maildboxId, unsigned int dataLow); //4 bytes
int Can_Write(unsigned int maildboxId, int dataLow, int dataHigh); //8 bytes
int Can_Write(unsigned int maildboxId, int dataLow); //4 bytes
int Can_Write(unsigned int maildboxId, float dataLow, float dataHigh); //8 bytes
int Can_Write(unsigned int maildboxId, float dataLow); //4 bytesCode language: C++ (cpp)
// 16-bit (2 bytes) types: int16 and uint16
// dataLow     represents the bytes 0 and 1
// dataMedLow  represents the bytes 2 and 3
// dataMedHigh represents the bytes 4 and 5
// dataHigh    represents the bytes 6 and 7
int Can_Write(unsigned int maildboxId, uint16_t dataLow, uint16_t dataMedLow, uint16_t dataMedHigh, uint16_t dataHigh); //8 bytes
int Can_Write(unsigned int maildboxId, uint16_t dataLow, uint16_t dataMedLow, uint16_t dataMedHigh); //6 bytes
int Can_Write(unsigned int maildboxId, uint16_t dataLow, uint16_t dataMedLow); //4 bytes
int Can_Write(unsigned int maildboxId, uint16_t dataLow); //2 bytes
int Can_Write(unsigned int maildboxId, int16_t dataLow, int16_t dataMedLow, int16_t dataMedHigh, int16_t dataHigh); //8 bytes
int Can_Write(unsigned int maildboxId, int16_t dataLow, int16_t dataMedLow, int16_t dataMedHigh); //6 bytes
int Can_Write(unsigned int maildboxId, int16_t dataLow, int16_t dataMedLow); //4 bytes
int Can_Write(unsigned int maildboxId, int16_t dataLow); //2 bytesCode language: C++ (cpp)
// array of uint8
typedef struct {
  uint8_t data[8];
} tCanMsg;

int Can_Write(unsigned int maildboxId, tCanMsg& data); //1 to 8 bytesCode language: C++ (cpp)

These functions are used to send data on the CAN bus.

They have to be called during the control interrupt.

Parameters

  • maildboxId: a unique ID used to distinguish mailboxes from each other. This ID must be unique throughout the code for all ETH and CAN input/output mailboxes.
  • data: data which will be sent. Several data types and prototypes are available. Note that the prototype used will not affect the length of the data sent, only the dataLength in Can_ConfigureOutputMailbox() defines it.

Return value

  • int: returns 1 if the data has successfully been loaded in the write buffer. Returns 0 otherwise.