11 // TODO using uint64_t everywhere for CAN message payload is kind of cute, but
12 // in actuality a CAN message may have a smaller payload, and it makes all of
13 // these functions not applicable to other data sizes. It's also fairly
14 // inefficient on 32-bit platforms. how much work is it to switch vi-firmware
17 /* Public: Reads a subset of bits from a byte array.
19 * data - the bytes in question.
20 * startPos - the starting index of the bit field (beginning from 0).
21 * numBits - the width of the bit field to extract.
22 * bigEndian - if the data passed in is little endian, set this to false and it
23 * will be flipped before grabbing the bit field.
25 * Bit fields are positioned according to big-endian bit layout, but inside the
26 * bit field, values are represented as little-endian. Therefore, to get the bit
27 * field, we swap the overall byte order if bigEndian == false and
28 * use the value we find in the field (assuming the embedded platform is little
31 * For example, the bit layout of the value "42" (i.e. 00101010 set at position
32 * 14 with length 6 is:
34 * 000000000000001010100000000000000000000000000000000000000000000
36 * and the same value and position but with length 8 is:
38 * 000000000000000010101000000000000000000000000000000000000000000
40 * If the architecture where is code is running is little-endian, the input data
41 * will be swapped before grabbing the bit field.
45 * uint64_t value = getBitField(data, 2, 4);
47 * Returns the value of the requested bit field.
49 uint64_t getBitField(uint64_t data, const uint16_t startPos,
50 const uint16_t numBits, bool bigEndian);
52 /* Public: Set the bit field in the given data array to the new value.
54 * data - a byte array with size at least startPos + numBits.
55 * value - the value to set in the bit field.
56 * startPos - the starting index of the bit field (beginning from 0).
58 void setBitField(uint64_t* data, uint64_t value, const uint16_t startPos,
59 const uint16_t numBits);
61 /* Public: Retreive the nth byte out of 8 bytes in a uint64_t.
63 * source - the source data to retreive the byte from.
64 * byteNum - the index of the byte, starting at 0 and assuming big-endian order.
66 * Returns the requested byte from the source bytes.
68 uint8_t nthByte(const uint64_t source, const uint16_t byteNum);