Documentation

CAN Bit-Packing

CAN Bit-Packing block

Library

Simulink® Real-Time™ Library for CAN

Description

This block is for message data of type double. This block constructs CAN data frames, and its output port is ordinarily connected to an input port of a CAN Send driver block. The block has one output port of data type double (a scalar), which represents the data frame entity constructed by the signals entering the block at its input ports. The number of input ports depends on the setting in the block dialog box.

Do not use this block to construct CAN_MESSAGE data types or to prepare a remote frame. Use the CAN Pack block.

Block Parameters

Bit Patterns

Specify bit patterns. The data type entered in the control must be a MATLAB® cell array vector. The number of elements in the cell array define the number of input ports shown by this block instance. The cell array elements must be of type double array and define the position of each bit of the incoming value (data typed input port) in the outgoing double value (data frame).

From a data type perspective (input ports), the block behaves like a Simulink Sink block, and therefore the data types of the input ports are inherited from the driving blocks.

The sample time of the block is also inherited from the driving blocks. Therefore you do not need an explicit sample time in the block dialog box.

Example

The example in this topic describes the use of the Softing® CAN blocks with standard double data types for message storage. For an alternative and easier way to pack CAN_MESSAGE data types, use the CAN Pack block.

The functionality of the block is best explained by means of an example.

Assume that a node on the CAN network needs to receive a CAN message with identifier 156 having the following data frame content. The data frame must be 6 bytes long.

Byte 0

Function class of type uint8

Byte 1

Function subclass of type uint8 with reversed bit order

Byte 2

Reserved, all bits must be 1

Byte 3

Bit 0 must be 0, Bit 1 must be a boolean (flag), bits 2 to 7 must be bit 2 to 7 of an incoming int8 value (control)

Byte 4 and 5

Value of type int16

The bit pattern cell array, which bit-packs the data frame according to the above specification, can look as follows:

{ [0:7] , [15:-1:8] , [16:23] , [25] , [-1,-1,26:31] , [32:47] }

And the Simulink model simulating the required behavior would be as shown.

  • The function class is of type uint8, which has an example value of 112. This value becomes byte 0 (bits 0 to 7) of the data frame. Therefore the first bit (element 1 of double array [0:7]) gets bit 0 of the data frame, the second bit 1, and so on. It is easiest to define this mapping by the MATLAB colon operator (:).

  • The function subclass is of type uint8, which has an example value of 23. This value becomes byte 1 (bits 8:15) of the data frame but in reversed bit order. Therefore the first bit (element 1 of double array [15:-1:8]) gets bit 15, the second bit 14, and so on. It is easiest to define this mapping by the MATLAB colon operator (:) and an increment of -1.

  • The reserved byte 2 must have all bits set to 1. If a bit position in the outgoing data frame is not referenced by a bit pattern array element, the bit is 0 by default, but you cannot set them to 1 as the default. Therefore a uint8 constant with value 255 must be brought in externally. The constant 255 must get to bit position 16 to 23 (byte 2) of the outgoing data frame.

    Because bit 0 of data frame byte 3 (bit 24) must be 0, and 0 is the default bit value if not referenced by a bit pattern array element, you do not need to take an explicit action here.

  • The flag is of type boolean, which has an example value of 1. This value must become bit 1 of byte 3 (bit 25) of the data frame. Therefore the single bit (element 1 of double array [25]) must get bit 25 of the data frame.

  • The control is of type int8, which has an example value of 121. But only bits 2 to 7 must be mapped into the outgoing data frame or, in other words, bits 0 and 1 must be thrown away. Because indexing of incoming values starts with the first bit (bit 0), a special indexing value (-1) must be used to skip bit 0 and 1 of the incoming int8 value. Bits 2 to 7 are directly mapped to bits 2 to 7 of byte 3 (bits 26 to 31) of the outgoing data frame. This leads to the following bit pattern: [-1,-1,26:31].

  • The value is of type int16, which has an example value of -12270. This value must become byte 4 and 5 (bits 32 to 47) of the outgoing data frame. Therefore the first bit (element 1 of double array [32:47]) must get bit 32 of the data frame, the second bit 33, and so on. It is easiest to define this mapping by the MATLAB colon operator (:).

  • The output of the CAN Bit-Packing block consists of a double value representing the packed data types within the first six bytes. The last two bytes are zero. This means that even in the case where less than eight bytes are significant, the CAN data frame is represented by a double value (eight bytes). The value of the constructed floating-point double does not have a particular meaning but you still see it with a numerical display.

  • The data frame is then propagated to the CAN Send driver block and is sent as part of a CAN message having identifier 156. In the Send block's dialog box, the data frame size is 6 bytes, meaning that only the first six bytes of the incoming double value are transmitted as part of the CAN message.

Was this topic helpful?