* @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
| 450 | * @param[in,out] ptr The data pointer to write to. |
| 451 | */ |
| 452 | static inline void write_bits( |
| 453 | unsigned int value, |
| 454 | unsigned int bitcount, |
| 455 | unsigned int bitoffset, |
| 456 | uint8_t ptr[2] |
| 457 | ) { |
| 458 | unsigned int mask = (1 << bitcount) - 1; |
| 459 | value &= mask; |
| 460 | ptr += bitoffset >> 3; |
| 461 | bitoffset &= 7; |
| 462 | value <<= bitoffset; |
| 463 | mask <<= bitoffset; |
| 464 | mask = ~mask; |
| 465 | |
| 466 | ptr[0] &= mask; |
| 467 | ptr[0] |= value; |
| 468 | ptr[1] &= mask >> 8; |
| 469 | ptr[1] |= value >> 8; |
| 470 | } |
| 471 | |
| 472 | /** |
| 473 | * @brief Read up to 8 bits at an arbitrary bit offset. |