# MLX90632 eeprom write problem

I am using an MLX90632 (MLX90632-S-LD-BCB-000) infrared temperature sensor to build an application which reads an object's temperature among other things. MLX90632 manual

The MLX90632 comes with a default refresh rate of 2 Hz which is unacceptably low for my application. Supposedly, the sensor has configurable refresh rate, which is configured by writing specific values at 2 specific memory addresses in its eeprom memory.

The datasheet says that the eeprom should be unlocked with a command, the specific memory positions should be erased by writing 0x0000 to them and then the eeprom should be unlocked again and write the actual values.

I had written the program below to do this task but it does not seem to work, when i read the sensor's refresh rate,after changing it, it has the default rate (eeprom contents not changed).

I have also seen Sparkfun's driver which has the same implementation as mine and they have put a comment there:

Requires unlocking the EEPROM, writing 0x0000, unlocking again, then writing value. The datasheet doesn't go a good job of explaining how writing to EEPROM works. This should work but doesn't. It seems the IC is very sensitive to I2C traffic while the sensor is recording the new EEPROM.

For this reason instead of polling the sensor to check its eeprom_busy bit i just put a delay, long enough for the memory operation to finish before trying anything else. Still does not work.

I wonder if anyone has managed to write new values to this sensor's eeprom memory and how they managed it. Is there anything wrong with the logic in my implementation?

Note: The program sends what it is supposed to send via i2c to the sensor - this has been checked with a logic analyzer - I am just looking for errors in the process

int main (void){

// initialize mcu code etc...
// ...
// ...

rate = mlx90632_get_refresh_rate();  //the rate we get here is 2Hz
mlx90632_set_refresh_rate_1Hz  ();  //write the new refresh rate to sensor's eeprom

// mlx90632_reset();   // a reset won't help here either

rate = mlx90632_get_refresh_rate();  //after changing the rate, still gets 2 Hz refresh rate

}

/*-------------------------------------------------------------------------*/

// function to set the refresh rate to 1 Hz
int32_t mlx90632_set_refresh_rate_1Hz  ( void ){

uint32_t meas;

meas = 0x810D811D;  //value supposed to be written to configure the 1Hz refresh rate

// put to sleep mode before writing to eeprom
mlx90632_mode_type previous_mode = mlx90632_get_mode();
if (previous_mode == continuous_mode)
mlx90632_set_mode(sleep_step_mode);

//EE_MEAS_1 and EE_MEAS_2 are two 16-bit registers, needed to be written for the configuration
//  EE_MEAS_2 is right after EE_MEAS_1. We write the incrementally as one 32-bit register
if ( mlx90632_write_eeprom32( EE_MEAS_1, meas ) < 0 )
return -1;

mlx90632_set_mode(previous_mode);

}

/*-------------------------------------------------------------------------*/

int32_t mlx90632_write_eeprom32( uint16_t address, uint32_t value ){

while( mlx90632_eeprom_busy() ){;}    //wait until sensor is free

if ( mlx90632_unlock_eeprom() < 0 )     // unlock eeprom for writing
return -1;

// erase eeprom register before writing
if ( mlx90632_write_reg32(address, 0x0000) < 0 ) return -1;     //write a 32 bit value to the eeprom starting from  "address"
nrf_delay_ms(500);
//while( mlx90632_eeprom_busy() ){;}

if ( mlx90632_unlock_eeprom() < 0 )
return -1;

// actually write the desired value
if ( mlx90632_write_reg32(address, value) < 0 ) return -1;
nrf_delay_ms(500);
//while( mlx90632_eeprom_busy() ){;}    // to do: add timeout?

return 0;
}

/*-------------------------------------------------------------------------*/