Reading 14 bytes of factory calibration for the TM8801 in the ESP32

48 Views Asked by At

I have been having issues reading back 14 bytes of factory calibration in the TMF8001. I am currently using an ESP32 to program it.

This is that the datasheet says to do:

The factory calibration sequence is started by issuing to the following command to the TMF8X0X:

S 41 W 10 0A P

After this factory calibration is started. You can wait for the interrupt to get triggered, or poll the register 0x1E until it reads back as 0x0A:

S 41 W 1e Sr 41 R A N P

or have to wait a maximum time of 2 seconds.

Now read back and store the 14 bytes of factory calibration.

S 41 W 20 Sr 41 R A A A A A A A A A A A A A N P

This is what I've been trying:

   //Factory calibration 
    i2c_master_start(meas_read17_cmd);
    i2c_master_write_byte(meas_read17_cmd, (0x41 << 1) | WRITE_BIT, ACK_CHECK_EN);
    i2c_master_write_byte(meas_read17_cmd, 0x10, ACK_CHECK_EN);
    i2c_master_write_byte(meas_read17_cmd, 0x0A, ACK_CHECK_EN);
    i2c_master_stop(meas_read17_cmd);
    ret = i2c_master_cmd_begin(i2c_num, meas_read17_cmd, 1000/portTICK_PERIOD_MS);
    i2c_cmd_link_delete(meas_read17_cmd);
    printf("TOF ret 17: %02x\n", ret);  

    vTaskDelay(5000/portTICK_PERIOD_MS); // wait 100ms for measurement to complete

    //Polling 0x1E register until it reads back 0x0A
    i2c_master_start(meas_read20_cmd);
    i2c_master_write_byte(meas_read20_cmd, (0x41 << 1) | WRITE_BIT, ACK_CHECK_EN);
    i2c_master_write_byte(meas_read20_cmd, 0x1E, ACK_CHECK_EN);
    i2c_master_start(meas_read20_cmd);
    i2c_master_write_byte(meas_read20_cmd, (0x41 << 1) | READ_BIT, ACK_CHECK_EN);
    i2c_master_read_byte(meas_read20_cmd, poll_register_2, NACK_VAL);
    i2c_master_stop(meas_read20_cmd);
    ret = i2c_master_cmd_begin(i2c_num, meas_read20_cmd, 1000/portTICK_PERIOD_MS);
    i2c_cmd_link_delete(meas_read20_cmd);
    printf("TOF ret 20: %02x\n", ret);    

    vTaskDelay(100/portTICK_PERIOD_MS); // wait 100ms for measurement to complete

    //Read back and store 14 bytes of factory calibration
    i2c_master_start(meas_read19_cmd);
    i2c_master_write_byte(meas_read19_cmd, (0x41 << 1) | WRITE_BIT, ACK_CHECK_EN);
    i2c_master_write_byte(meas_read19_cmd, 0x20, ACK_CHECK_EN);
    i2c_master_start(meas_read19_cmd);
    i2c_master_write_byte(meas_read19_cmd, (0x41 << 1) | READ_BIT, ACK_CHECK_EN);
    i2c_master_read_byte(meas_read19_cmd, reg4_1, ACK_VAL);
    i2c_master_read_byte(meas_read19_cmd, reg4_2, ACK_VAL);
    i2c_master_read_byte(meas_read19_cmd, reg4_3, ACK_VAL);
    i2c_master_read_byte(meas_read19_cmd, reg4_4, ACK_VAL);
    i2c_master_read_byte(meas_read19_cmd, reg4_5, ACK_VAL);
    i2c_master_read_byte(meas_read19_cmd, reg4_6, ACK_VAL);
    i2c_master_read_byte(meas_read19_cmd, reg4_7, ACK_VAL);
    i2c_master_read_byte(meas_read19_cmd, reg4_8, ACK_VAL);
    i2c_master_read_byte(meas_read19_cmd, reg4_9, ACK_VAL);
    i2c_master_read_byte(meas_read19_cmd, reg4_10, ACK_VAL);
    i2c_master_read_byte(meas_read19_cmd, reg4_11, ACK_VAL);
    i2c_master_read_byte(meas_read19_cmd, reg4_12, ACK_VAL);
    i2c_master_read_byte(meas_read19_cmd, reg4_13, ACK_VAL);
    i2c_master_read_byte(meas_read19_cmd, reg4_14, NACK_VAL);
    i2c_master_stop(meas_read19_cmd);
    ret = i2c_master_cmd_begin(i2c_num, meas_read19_cmd, 1000/portTICK_PERIOD_MS);
    i2c_cmd_link_delete(meas_read19_cmd);
    printf("TOF ret 19: %02x\n", ret);    

    vTaskDelay(100/portTICK_PERIOD_MS); // wait 100ms for measurement to complete

This is the output in my terminal:

BOOT 14: c0
TOF POLL REGISTER 1 (EXP 0x41): 41
TOF POLL REGISTER 2 (EXP 0x0A): 00
REG4_1: 00
REG4_2: 00
REG4_3: 00
REG4_4: 00
REG4_5: 00
REG4_6: 00
REG4_7: 00
REG4_8: 00
REG4_9: 00
REG4_10: 00
REG4_11: 00
REG4_12: 00
REG4_13: 00
REG4_14: 00

Any help will be greatly appreciate it. Thank you.

0

There are 0 best solutions below