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 into a uint64_t.
19 * source - the bytes in question.
20 * offset - the starting index of the bit field (beginning from 0).
21 * bit_count - the width of the bit field to extract.
22 * big_endian - 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 big_endian == 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 = get_bit_field(data, 2, 4);
47 * Returns the value of the requested bit field, right aligned in a uint64_t.
49 uint64_t get_bit_field(uint64_t source, const uint16_t offset,
50 const uint16_t bit_count, const bool big_endian);
52 /* Public: Return a single nibble from the payload, with range checking.
54 * source - the source payload.
55 * nibble_index - the index of the nibble to retreive. The leftmost nibble is
57 * big_endian - if the data passed in is little endian, set this to false and it
58 * will be flipped before grabbing the bit field.
60 * Returns the retreived nibble, right aligned in a uint8_t.
62 uint8_t eightbyte_get_nibble(const uint64_t source, const uint8_t nibble_index,
63 const bool big_endian);
65 /* Public: Return a single byte from the payload, with range checking.
67 * source - the source byte array.
68 * byte_index - the index of the byte to retreive. The leftmost byte is index 0.
69 * big_endian - if the data passed in is little endian, set this to false and it
70 * will be flipped before grabbing the bit field.
72 * Returns the retreived byte.
74 uint8_t eightbyte_get_byte(const uint64_t source, const uint8_t byte_index,
75 const bool big_endian);
77 /* Public: Set the bit field in the given data array to the new value.
79 * destination - a byte array with size at least offset + bit_count.
80 * value - the value to set in the bit field.
81 * offset - the starting index of the bit field (beginning from 0).
82 * bit_count - the number of bits to set in the data.
84 * Returns true if the bit_count is enough to fully represent the value, and
85 * false if it will not fit.
87 bool set_bit_field(uint64_t* destination, uint64_t value, const uint16_t offset,
88 const uint16_t bit_count);
90 /* Private: Determine the index of the last bit used.
92 uint8_t find_end_bit(const uint16_t num_bits);