Skip to content

Incorrect bit masking (e.g. data &= ~0xfc ) in various functions #4

@pmichaud

Description

@pmichaud

There appear to be several places where the wrong bits are being masked/reset to zero in the LIS331.cpp library.

For example, at

data &= ~0xfc; // clear the low two bits of the register
there's a line of code that reads:

data &= ~0xfc; // clear the low two bits of the register

Actually, this is clearing all bits except the low two bits of the register. The correct statement would be either data &= ~0x03; or data &= 0xfc; (note the presence/absence of tildes in each of these versions).

Similarly this occurs in

data &= ~0xe7; // clear bits 4:3 of the register

data &= ~0xe7; // clear bits 4:3 of the register

which should be data &= ~0x18;.

I found these examples/bugs when looking at the code for LIS331::setFullScale(), which should be clearing bits 5:4 but actually clears everything except those bits:

void LIS331::setFullScale(fs_range range)
{
  uint8_t data; 
  LIS331_read(CTRL_REG4, &data, 1);
  data &= ~0xcf;
  data |= range<<4;
  LIS331_write(CTRL_REG4, &data, 1);
}

The third statement should read data &= ~0x30;.

Thanks,

Pm

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions