This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TMP114: Unable to read data from the temperature sensor

Part Number: TMP114

Tool/software:

Hi TI team,

Need detailed step by step information on reading temperature from this sensor tmp114.

Getting the output as FFFF.

when i tried to write configuration register address - 0x03 's value as 0 and trying to read from the sensor writing to 0x00 and reading from that.

I'm attaching the base c code that we tried.

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>
#include <stdint.h>

#define TMP114_ADDRESS 0x49
#define I2C_BUS "/dev/i2c-0"

#define REG_TEMP_RESULT 0x00
#define REG_CONFIGURATION 0x03

struct tmp114_registers {
    uint16_t temp_result;
};

void read_register(int file, uint8_t reg, uint16_t *value) {
    uint8_t buffer[2] = {0};

    buffer[0] = reg;
    if (write(file, buffer, 1) != 1) {
        perror("Failed to move to register address\n");
        exit(1);
    }

    if (read(file, buffer, 2) != 2) {
        perror("Failed to read register data\n");
        exit(1);
    }

    *value = (buffer[0] << 8) | buffer[1];
}

void write_register(int file, uint8_t reg, uint16_t value) {
    uint8_t buffer[3] = {0};

    buffer[0] = reg;
    buffer[1] = value >> 8;
    buffer[2] = value & 0xFF;
    if (write(file, buffer, 3) != 3) {
        perror("Failed to write to register\n");
        exit(1);
    }
}

void configure_sensor(int file) {
    uint16_t config_value = 0x0000;
    write_register(file, REG_CONFIGURATION, config_value);
}

void read_temperature_result(int file, struct tmp114_registers *regs) {
    read_register(file, REG_TEMP_RESULT, &regs->temp_result);
}

void print_temperature_result(const struct tmp114_registers *regs) {
    printf("Temp_Result: %d\n", regs->temp_result);
}

int main() {
    int file;

    if ((file = open(I2C_BUS, O_RDWR)) < 0) {
        perror("Failed to open the I2C bus");
        exit(1);
    }

    if (ioctl(file, I2C_SLAVE, TMP114_ADDRESS) < 0) {
        perror("Failed to acquire bus access and/or talk to the slave");
        exit(1);
    }

    struct tmp114_registers regs;

    configure_sensor(file);

    while (1) {
        read_temperature_result(file, &regs);
        print_temperature_result(&regs);

        sleep(1);
    }
    close(file);
    return 0;
}

Please suggest further changes that need to be done to read the temperature data from this sensor...

Also if available, please provide a c code for the same.

Thanks & Regards
Sai Akhil C

  • Reads from TMP114 must be performed as one Write-Restart-Read transaction. This is different from most of the TMPxxx sensor portfolio. You may need to use SMBus Read Byte or Read Block commands to achieve this.

    At any rate, there is driver code for TMP114 at https://dev.ti.com/sysconfig/index.html?product=ascstudio&module=/ti/sensors/tempsensor/TMP114 but this will not solve the above problem for you, as it does not include HAL.

    ren

  • Hi Ren,
    Thanks for the response.

    1)Can you suggest what can be done next to solve this issue?
    2)Any references for the Read Block commands that you mentioned?
    3)Normal i2c driver in Linux cant be used to read the data from sensor?
    4)Please provide any reference of 'C' user space code if available to read the temp data.

    Regards,

    Sai Akhil

  • You need to use SMBus Read Word as described in the Linux documentation. https://docs.kernel.org/i2c/smbus-protocol.html

    ren

  • Hi Ren,

    Just need to confirm the procedure I am following to read the temperature from the tmp114
    -> The first thing to do is to configure the sensor so i am writing the data as 0000(required configuration bits) for the configuration register 0x03.

    -> Then i am trying to read the temp data by giving the register address as 0x00 (TEMP_RESULT) register.

    My doubt here is, Do i have to write something to the TEMP_RESULT register to read the data from that same register?

    Because its mentioned TEMP_RESULT register type is 'R'. So i just need to confirm the procedure that i am following is in the right path? or any changes in the way i am reading?

  • The first byte transmitted to TMP114 in a write transaction is always the register "pointer." The register addressed by this pointer will be written to by any subsequent bytes in the transaction. The I2C transaction can have an arbitrary number of bytes dependent only on the controller's will to continue clocking them. Of course, many target devices will just stop taking any action with the bytes beyond a certain number. 

    A transaction of the form Start, Address+Write, ACK, Data, ACK, Stop will only alter the pointer.

    A transaction of the form Start, Address+Write, ACK, Data1, ACK, Data2, ACK, Data3, Stop will write Data2 and Data3 to the register identified by Data1. This is applicable to TMP114's Configuration and Limit registers.

    If a 2 or 3 byte write transaction were performed on TMP114 using zero as the Data1 pointer byte, nothing will happen. As you've pointed out, the Temperature Register 0 is not writable, so Data2 and Data3 will not actually be stored by TMP114. 

    Most of the TMP portfolio of devices will retain the pointer byte written to them until power cycled. This means a single write transaction of S,Addr+W,A,Data,A,P could be used to set the pointer to the Configuration register, and from then on all read transactions of the form S,Addr+R,A,Data,A,P would return one byte of data from the Configuration. For this reason, it is customary to always set the pointer to the register you wish to read using a write transaction prior to performing any read transaction. This way you will never get data from an unexpected register.

    However, the TMP114 will not retain it's pointer. All read transactions with TMP114 must be of the form S,Addr+W,A,Sr,Addr+R,A,Data1,A,Data2,A,P. This is accomplished by the i2c_smbus_read_word_data() in the previously linked documentation. https://docs.kernel.org/i2c/smbus-protocol.html Since this Write/Read action must occur together in hardware timing, it can only be triggered by specific software function. It cannot be triggered by individual write and read software functions. 

    thanks,

    ren

  • Hi ren,

    Thanks for the detailed information. It helped, but while im reading the data im getting the junk values. I'm pasting the output here

    Writing Configuration is success
    Writing to read register is success
    starting reading
    command = 0, size = 3
    actual data = 26893, in hex = 0x690d
    raw temperature = 26893 in hex=0x690d
    Temperature: 210.10°C
    command = 0, size = 3
    actual data = 20749, in hex = 0x510d
    raw temperature = 20749 in hex=0x510d
    Temperature: 162.10°C
    command = 0, size = 3
    actual data = 10253, in hex = 0x280d
    raw temperature = 10253 in hex=0x280d
    Temperature: 80.10°C
    command = 0, size = 3
    actual data = 6157, in hex = 0x180d
    raw temperature = 6157 in hex=0x180d
    Temperature: 48.10°C
    command = 0, size = 3
    actual data = 3085, in hex = 0xc0d
    raw temperature = 3085 in hex=0xc0d
    Temperature: 24.10°C
    command = 0, size = 3
    actual data = 20237, in hex = 0x4f0d
    raw temperature = 20237 in hex=0x4f0d
    Temperature: 158.10°C

    As you suggested I have used the i2c_smbus_read_word_data(), maybe i am missing some configurations. So, need little help on the configuration side.

    I'm attaching the source code with the configurations that i used, please check

    /cfs-file/__key/communityserver-discussions-components-files/1023/tmp_5F00_read_5F00_smbus.c

    Thanks
    Akhil

  • The data returned by SMBus Read Word is byte swapped, as described in the linked documentation. The documentation suggests using i2c_smbus_read_word_swapped() instead. This would also be apparent if you read the Device ID register, since it always reports 0x1114. All of your reported temperatures are about 26C. 

    ren

  • Hi ren,
    Thanks for the support, it worked.

    Regards
    Akhil