//指定长度位写入指定数据
/**
* This function writes multi-bit value of registers for mpu6xxx
*
* @param dev the pointer of device driver structure
* @param reg the register for mpu6xxx
* @param start_bit the start position of the register
* @param len number of bits to write
* @param data value to write
*
* @return the writing status, RT_EOK reprensents writing multi-bit value of registers successfully.
*/ //指定长度位写入指定数据
static rt_err_t mpu6xxx_write_bits(struct mpu6xxx_device *dev, rt_uint8_t reg, rt_uint8_t start_bit, rt_uint8_t len, rt_uint8_t data)
{
rt_uint8_t byte, mask;
rt_err_t res;
res = mpu6xxx_read_regs(dev, reg, 1, &byte);//先读出来
if (res != RT_EOK)
{
return res;
}
mask = ((1 << len) - 1) << (start_bit - len + 1);//切换到要复位的位上去
data <<= (start_bit - len + 1); // shift data into correct position 数据也移动到要写入的位上去
data &= mask; // zero all non-important bits in data 所有不重要的位归零 要写入的数据,不重要的位归零,以防写入其他数据
byte &= ~(mask); // 将现有字节中的所有重要位归零 读出来的数据归零
byte |= data; // combine data with existing byte 写入
return mpu6xxx_write_reg(dev, reg, byte);
}