Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write EEPROM issue #45

Open
thanhhaiittalk opened this issue Jun 20, 2023 · 1 comment
Open

Write EEPROM issue #45

thanhhaiittalk opened this issue Jun 20, 2023 · 1 comment

Comments

@thanhhaiittalk
Copy link

thanhhaiittalk commented Jun 20, 2023

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
Copy link
Member

Letme commented Jun 21, 2023

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants