tedyapo / arduino-MLX90393

Arduino library for MLX90393 magnetometer sensor
MIT License
49 stars 26 forks source link

Potential cache bug #50

Open tesshellebrekers opened 3 years ago

tesshellebrekers commented 3 years ago

Hello,

I recently noticed that my conversion time was not always calculating properly for different DIG_FILT and OSR settings. This led me to find a potential error in cache_t struct in the .h file.

  struct cache_t {
    enum { SIZE = 3, ALL_DIRTY_MASK = 1 << (SIZE+1) - 1};
    uint8_t dirty;
    uint16_t reg[SIZE];
  } cache;

The ALL_DIRTY_MASK reads as binary 1000. I believe it should be 0111, to track the three setting registers, and whether the current cache information is accurate.

Some other hints I found along the way: Line 70 does not update with 1000:
cache.dirty |= cache_t::ALL_DIRTY_MASK & (1<<address);

Line 78 is always being anded with 0: cache.dirty &= ~(1 << address);

I changed the struct to:

  struct cache_t {
    enum { SIZE = 3, ALL_DIRTY_MASK = (1 << SIZE) - 1};
    uint8_t dirty;
    uint16_t reg[SIZE];
  } cache;

To get a binary mask of 0111. Conversion at higher data rates seems to be working better for me this way.