melexis / mlx90632-library

MLX90632 library for the Melexis 90632 Infra Red temperature sensor.
Apache License 2.0
42 stars 15 forks source link

Write EEPROM issue #45

Open thanhhaiittalk opened 1 year ago

thanhhaiittalk commented 1 year ago

I have a problem why trying to set Ha and Hb in EEPROM. Code below show my implementation:

#define MLX90632_EEPROM_WRITE_KEY (0x554C) /**< EEPROM write key */
#define MLX90632_EE_HA  (0x2481)               /**< Ha customer calibration value register 16bit */
#define MLX90632_EE_HB  (0x2482)               /**< Hb customer calibration value register 16bit */

BaseStatus_T MLX90632_WriteEEPROM(MLX90632_T *this, uint16_t addr, uint16_t data)
{
  BaseStatus_T ret;

  ret = MLX90632_WaitForDeviceNotBusy(this);
  if (ret != BS_OK)
    return ret;

  ret = MLX90632_EraseEEPROM(this, addr);
  if (ret != BS_OK)
    return ret;

  uint16_t eValue = 0;
  MLX90632_I2C_Read16(this, addr, &(eValue));

  ret = MLX90632_UnlockEEPROM(this);
  if (ret != BS_OK)
    return ret;
  HAL_DelayMs(100);

  ret = MLX90632_I2C_Write16(this, addr, data);
  if (ret != BS_OK)
    return ret;
  HAL_DelayMs(100);

  MLX90632_I2C_Read16(this, addr, &(eValue));

  ret = MLX90632_WaitForEEPROMNotBusy(this);
  if (ret != BS_OK)
    return ret;
  HAL_DelayMs(100);

  return ret;
}

STATIC BaseStatus_T MLX90632_EraseEEPROM(MLX90632_T *this, uint16_t addr)
{
  BaseStatus_T ret;
  ret = MLX90632_UnlockEEPROM(this);
  if (ret != BS_OK)
    return ret;

  ret = MLX90632_I2C_Write16(this, addr, 0x0000);
  if (ret != BS_OK)
    return ret;
  HAL_DelayMs(100);

  ret = MLX90632_WaitForEEPROMNotBusy(this);
  if (ret != BS_OK)
    return ret;

  HAL_DelayMs(100);

  return ret;
}

STATIC BaseStatus_T MLX90632_UnlockEEPROM(MLX90632_T *this)
{
  BaseStatus_T ret;

  ret = MLX90632_I2C_Write16(this, 0x3005, MLX90632_EEPROM_WRITE_KEY);
  HAL_DelayMs(100);
  return ret;
}

STATIC BaseStatus_T MLX90632_WaitForEEPROMNotBusy(MLX90632_T *this)
{
  uint16_t regStatus;
  BaseStatus_T ret;

  ret = MLX90632_I2C_Read16(this, MLX90632_REG_STATUS, &regStatus);
  if (ret != BS_OK)
    return ret;

  while (ret == BS_OK && (regStatus & MLX90632_STAT_EE_BUSY))
  {
    ret = MLX90632_I2C_Read16(this, MLX90632_REG_STATUS, &regStatus);
  }
  return ret;
}

STATIC BaseStatus_T MLX90632_WaitForDeviceNotBusy(MLX90632_T *this)
{
  uint16_t regStatus;
  BaseStatus_T ret;

  ret = MLX90632_I2C_Read16(this, MLX90632_REG_STATUS, &regStatus);
  if (ret != BS_OK)
    return ret;

  while (ret == BS_OK && (regStatus & MLX90632_STAT_BUSY))
  {
    ret = MLX90632_I2C_Read16(this, MLX90632_REG_STATUS, &regStatus);
  }
  return ret;
}

STATIC BaseStatus_T MLX90632_I2C_Write16(MLX90632_T *this, int16_t regAddr, uint16_t value)
{
  BaseStatus_T ret;
  uint8_t data[2];
  data[0] = (value >> 8) & 0xFF;
  data[1] = value & 0xFF;
  ret     = this->I2CWriteAt(this->DeviceAddress, regAddr, data, 2);
  return ret;
}

STATIC BaseStatus_T MLX90632_I2C_Read16(MLX90632_T *this, uint16_t regAddr, uint16_t *value)
{
  uint8_t data[2];
  BaseStatus_T ret;
  ret    = this->I2CReadAt(this->DeviceAddress, regAddr, data, 2);
  *value = data[1] | (data[0] << 8);
  return ret;
}

There are some functions relate to my work that need to set value for Ha and Hb. But after writing value to EEPROM, I read Ha and Hb again to re-check, it still holds old values (default value). I add some delay functions in effort of making sure writing operation completed. Please suggest me where I implement wrongly. Thanks a lot.

Letme commented 1 year ago

Hi, This does not seem like library code to me. Why aren't you using functions from this library? Did you confirm that the i2c read you have implemented there is indeed reading the correct endianess?