Main Content

CAN Frame Structure, Byte Order, and Bit Counting

This page explains the AUTOSAR and Vehicle Network Toolbox™ behavior regarding CAN frame structure. It refers to the AUTOSAR CP System Template R23-11 for specific items, but these concepts are general and hold for other versions of the standard.

Byte Order (Endianness) and Bit Counting

Byte order (or endianness) refers to the order in which bytes are stored in a digital word. The term "Big Endian" (Motorola® byte order) refers to the most significant byte being stored first. Conversely, "Little Endian" (Intel® byte order) refers to the least significant byte being stored first.

Bit counting refers to the ordered sequence of bits of a binary number within a given byte layout. The term "most significant bit first" (MSb first) or "least significant bit first" (LSb first) refers to the start bit of the bit sequence being the MSb or the LSb, respectively. Bit numbering can be specified independently of the byte order.

Vehicle Network Toolbox Convention

In the blocks that pack and unpack signals, Vehicle Network Toolbox conventionally utilizes the "LSb first" bit counting, irrespective of the byte order.For example, consider a 2-byte word; in the word, bits and bytes are numbered as follows:

Byte index

A 10-bit number may be stored in several different ways within the word. One possible way to store the number with Little Endian byte order is the following:

Byte index for little endian

where the start bit is bit 2 (the LSb) and the last bit is bit 11 (the MSb). Note that, since the byte order is Little Endian, the least significant byte is stored first (in byte 0).

Another possible way to store the same number, but with Big Endian bit order, is the following:

Byte index for big endian

where the start bit is bit 11 (the LSb) and the last bit is bit 4 (the MSb). Note that, since the byte order is Big Endian, the most significant byte is stored first (in byte 0).

AUTOSAR Convention

The AUTOSAR standard specifies that a different bit counting be used for each byte order. In particular, if the Little Endian byte order is chosen, then the bit counting is LSb first; if the Big Endian byte order is used instead, then the bit counting is MSb first.

This convention implies that, when Little Endian byte order is used, the start bit is effectively the same for Vehicle Network Toolbox and AUTOSAR (i.e., the LSb).

When Big Endian is used instead, then the start bit is different. In the Big Endian example above, AUTOSAR would consider the start bit to be bit 4 (the MSb) and the last bit to be bit 11 (the LSb).

Structure of a CAN Frame in AUTOSAR vs DBC

The AUTOSAR standard leverages the concept of Protocol Data Unit (PDU) in the description of automotive networks, in particular CAN buses.

According to the AUTOSAR description, a CAN Frame is an array of bytes with a given maximum length depending on the protocol (8 bytes for regular CAN, 64 bytes for CAN FD). The standard allows CAN Frames to contain a single PDU. The most common type of PDU is the ISignalIPdu, a PDU that carries one or more ISignals. (Other PDU types are used depending on the context; a noteworthy example is that of a ContainerIPdu - a PDU that can carry other PDUs - which is generally used in CAN FD Frames to transmit more than one ISignalIPdu in a Frame.)

An ISignal is a sequence of bits, representing the binary value of a signal to be transmitted over the bus.

Therefore, unlike the common definition of a CAN Frame in a CAN database (DBC file), which maps directly signals to frames, AUTOSAR provides more flexibility. In AUTOSAR, ISignals need to be mapped to ISignalIPdus first; then, ISignalIPdus are mapped to Frames. These two successive steps are impacted by the choice of the byte order and of the bit counting.

Layout of AUTOSAR Signals in a PDU

The AUTOSAR entity ISignalToIPduMapping specifies the way in which an ISignal is positioned within an ISignalIPdu. In this respect, the key attributes of the ISignalToIPduMapping are:

  • The corresponding ISignal, which is another AUTOSAR entity defining (among other parameters) the length of the signal.

  • The packingByteOrder, whose values can be "mostSignificantByteLast" (i.e. Little Endian), "mostSignificantByteFirst" (i.e., Big Endian) and "Opaque" (not supported by Vehicle Network Toolbox).

  • The startPosition, that is the start bit position of the signal in the PDU, i.e., the LSb in case of Little Endian byte order, and the MSb in case of Big Endian byte order.

As an example, considering the following bit layout, corresponding to that of an ISignalIPdu of length 2 bytes:

PDU byte index

In this case, the relevant parameters according to the AUTOSAR conventions are:

  • ISignal.length = 10

  • ISignalIPduMapping.packingByteOrder = mostSignificantByteFirst (Big Endian)

  • ISignalIPduMapping.startPosition = 5 (the MSb)

Layout of AUTOSAR PDUs in a Frame

The AUTOSAR entity PduToFrameMapping specifies the way in which a PDU is positioned within a Frame. In this respect, the key attributes of the PduToFrameMapping are:

  • The packingByteOrder, whose values can be "mostSignificantByteLast" (i.e., Little Endian) or "mostSignificantByteFirst" (i.e., "Big Endian")

  • The startPosition, that is the start bit position of the ISignalIPdu, i.e., the LSb in case of Little Endian byte order, and the MSb in case of Big Endian byte order.

Even though these two parameters have the same name as in the ISignalToIPduMapping, they refer to the byte order and the bit position of the PDU, not of the signal.

In addition, note that PDUs must be byte-aligned within a frame. Thus, if the byte order is Little Endian, the allowed values for startPosition are 0, 8, 16, ..., while if the byte order is Big endian then the allowed values for startPosition are 7, 15, 23, ...

As an example, consider the following 2-byte-long ISignalIPdu stored in a 4-byte-long Frame:

AUTOSAR frame byte index

According to the AUTOSAR convention, the parameters are:

  • PduToFrameMapping.packingByteOrder = mostSignificantByteFirst (Big Endian)

  • PduToFrameMapping.startPosition = 15 (MSb)

Mapping Signals to Frames: AUTOSAR vs Vehicle Network Toolbox

The intermediate mapping between ISignalIPdus and Frames as well as ISignals and ISignalIPdus makes it less intuitive to comprehend the way the start bit is computed in Vehicle Network Toolbox as opposed to AUTOSAR. The following four examples are representative of the possible situations.

PDU with Little Endian Byte Order Carrying a Signal with Little Endian Byte Order

Consider the following 2-byte-long ISignalIPdu stored in a 4-byte-long Frame. The byte order being Little Endian, the start bit is bit 8 (the LSb) and the last bit is bit 23 (the MSb). This PDU carries an ISignal, itself stored with Little Endian byte order, whose start bit (relative to the PDU) is bit 2, and whose bit length is 10 bits.

Frame byte index of little endianness

Vehicle Network Toolbox considers the signal start bit as the LSb of the signal with respect to the frame, irrespective of the signal's byte order. (This is the interpretation of start bit in, e.g., CAN Pack and CAN Unpack block masks.)

As far as bit counting is concerned, in this case the definition of start bit of the signal in AUTOSAR is equivalent to that of Vehicle Network Toolbox, because the byte order of the signal is Little Endian. However, the start bit of the signal is, in AUTOSAR, given relative to the start bit of the PDU, which is itself stored in Little Endian byte order, so its start bit is the LSb of the PDU. Therefore, the start bit is computed by Vehicle Network Toolbox as follows (pseudo-code):

if PduToFrameMapping.byteOrder == mostSignificantByteLast && ISignalIPduMapping.byteOrder == mostSignificantByteLast
startBit = PduToFrameMapping.startPosition + ISignalIPduMapping.startPosition
end

In the example, as shown in the figure, in Vehicle Network Toolbox the start bit of the signal is bit 10.

PDU with Little Endian Byte Order Carrying a Signal with Big Endian Byte Order

Consider the following 2-byte-long ISignalIPdu stored in a 4-byte-long Frame. The byte order being Little Endian, the start bit is bit 8 (the LSb) and the last bit is bit 23 (the MSb). This PDU carries an ISignal, stored with Big Endian byte order, whose start bit (relative to the PDU) is bit 4, and whose bit length is 10 bits.

Frame byte index of mixed endianness

In this case, the byte order of the signal being Big Endian, the bit counting convention differs between AUTOSAR and Vehicle Network Toolbox. However, the byte order of the PDU is Little Endian, so its startPosition can be added to that of the signal's MSb to determine the MSb relative to the frame.

The final step is that of determining the LSb (relative to the frame) given the MSb (relative to the frame). This can be accomplished with the following standard algorithm (pseudo-code):

function LSb = computeLSbGivenMSb(MSb, bitLength, frameLength)

i = rem(MSb, 8);
j = fix(MSb/8);

MSb_aux = 8*(frameLength - j - 1) + i;
LSb_aux = MSb_aux - bitLength + 1;

ii = rem(LSb_aux, 8);
jj = frameLength - fix(LSb_aux/8) - 1;

LSb = ii + 8*jj;
end

This algorithm given the value of bit 19 as the LSb, which corresponds to the signal's start bit according to the convention in Vehicle Network Toolbox.

PDU with Big Endian Byte Order Carrying a Signal with Little Endian Byte Order

Consider the following 2-byte-long ISignalIPdu stored in a 4-byte-long Frame. The byte order being Big Endian, the start bit is bit 15 (the MSb) and the last bit is bit 16 (the LSb), as shown in the figure. This PDU carries an ISignal, stored with Little Endian byte order, whose start bit (relative to the PDU) is bit 2, and whose bit length is 10 bits.

Frame byte index of mixed endianness

Note that the PDU metadata define the bytes belonging to the PDU. Note also that the byte order (of both the signal and the PDU) is given relative to the absolute byte positions of the Frame. Given the metadata, it is then possible to determine the start bit of the signal according to the Vehicle Network Toolbox convention as follows (pseudo-code):

startBit = PduToFrameMapping.startPosition - 7 + ISignalToIPduMapping.startPosition

PDU with Big Endian Byte Order Carrying a Signal with Big Endian Byte Order

Consider the following 2-byte-long ISignalIPdu stored in a 4-byte-long Frame. The byte order being Big Endian, the start bit is bit 15 (the MSb) and the last bit is bit 16 (the LSb), as shown in the figure. This PDU carries an ISignal, stored with Big Endian byte order, whose start bit relative to the PDU is bit 4 (the MSb), and whose bit length is 10 bits.

Frame byte index of big endianness

In this case, it is possible to determine the MSb of the signal relative to the frame via the following:

MSb = PduToFrameMapping.startPosition - 7 + ISignalToIPduMapping.startPosition;

From the MSb, it is possible to compute the LSb based on the signal bit length and the frame length, as shown before in the function computeLSbGivenMSb.

See Also

Blocks