* @brief Write up to 8 bits at an arbitrary bit offset. * * The stored value is at most 8 bits, but can be stored at an offset of between 0 and 7 bits so may * span two separate bytes in memory. * * @param value The value to write. * @param bitcount The number of bits to write, starting from LSB. * @param bitoffset The bit offset to store at, between 0 and
| 444 | * @param[in,out] ptr The data pointer to write to. |
| 445 | */ |
| 446 | static inline void write_bits( |
| 447 | unsigned int value, |
| 448 | unsigned int bitcount, |
| 449 | unsigned int bitoffset, |
| 450 | uint8_t ptr[2] |
| 451 | ) { |
| 452 | unsigned int mask = (1 << bitcount) - 1; |
| 453 | value &= mask; |
| 454 | ptr += bitoffset >> 3; |
| 455 | bitoffset &= 7; |
| 456 | value <<= bitoffset; |
| 457 | mask <<= bitoffset; |
| 458 | mask = ~mask; |
| 459 | |
| 460 | ptr[0] &= mask; |
| 461 | ptr[0] |= value; |
| 462 | ptr[1] &= mask >> 8; |
| 463 | ptr[1] |= value >> 8; |
| 464 | } |
| 465 | |
| 466 | /** |
| 467 | * @brief Read up to 16 bits from two bytes. |