Set sensor as not present upon I2C communication errors
authorNathael Pajani <nathael.pajani@ed3l.fr>
Tue, 13 Sep 2016 23:22:00 +0000 (01:22 +0200)
committerNathael Pajani <nathael.pajani@ed3l.fr>
Fri, 10 Feb 2023 18:02:59 +0000 (19:02 +0100)
extdrv/tmp101_temp_sensor.c
extdrv/tsl256x_light_sensor.c
extdrv/veml6070_uv_sensor.c

index 3d1313e..47e13d9 100644 (file)
@@ -119,16 +119,18 @@ int tmp101_sensor_read(struct tmp101_sensor_config* conf, uint16_t* raw, int* de
                conf->last_accessed_register = TMP_REG_TEMPERATURE;
        }
 
-       if (ret == 2) {
-               if (raw != NULL) {
-                       *raw = byte_swap_16(temp);
-               }
-               if (deci_degrees != NULL) {
-                       *deci_degrees = tmp101_convert_to_deci_degrees(byte_swap_16(temp));
-               }
-               return 0;
+       if (ret != 2) {
+               conf->probe_ok = 0;
+               return ret;
        }
-       return ret;
+
+       if (raw != NULL) {
+               *raw = byte_swap_16(temp);
+       }
+       if (deci_degrees != NULL) {
+               *deci_degrees = tmp101_convert_to_deci_degrees(byte_swap_16(temp));
+       }
+       return 0;
 }
 
 
@@ -163,10 +165,11 @@ int tmp101_sensor_config(struct tmp101_sensor_config* conf)
        msleep(1);
        ret = i2c_write(conf->bus_num, cmd, 3, NULL);
        conf->last_accessed_register = TMP_REG_CONFIG;
-       if (ret == 3) {
-               return 0; /* Config success */
+       if (ret != 3) {
+               conf->probe_ok = 0;
+               return ret;
        }
-       return ret;
+       return 0; /* Config success */
 }
 
 /* Start a conversion when the sensor is in shutdown mode. */
@@ -183,10 +186,11 @@ int tmp101_sensor_start_conversion(struct tmp101_sensor_config* conf)
        cmd[2] |= TMP_ONE_SHOT_TRIGGER;
        ret = i2c_write(conf->bus_num, cmd, 3, NULL);
        conf->last_accessed_register = TMP_REG_CONFIG;
-       if (ret == 3) {
-               return 0; /* Conversion start success */
+       if (ret != 3) {
+               conf->probe_ok = 0;
+               return ret;
        }
-       return ret;
+       return 0; /* Conversion start success */
 }
 
 
index b08dc46..967613b 100644 (file)
@@ -83,6 +83,7 @@ int tsl256x_read_id(struct tsl256x_sensor_config* conf)
        }
        ret = i2c_read(conf->bus_num, cmd_buf, ID_BUF_SIZE, ctrl_buf, &id, 1);
        if (ret != 1) {
+               conf->probe_ok = 0;
                return ret;
        }
        return id;
@@ -114,6 +115,7 @@ int tsl256x_sensor_read(struct tsl256x_sensor_config* conf, uint16_t* comb, uint
 
        ret = i2c_read(conf->bus_num, cmd_buf, READ_BUF_SIZE, ctrl_buf, data, 4);
        if (ret != 4) {
+               conf->probe_ok = 0;
                return ret;
        }
        comb_raw = (data[0] & 0xFF) | ((data[1] << 8) & 0xFF00);
@@ -159,6 +161,7 @@ int tsl256x_configure(struct tsl256x_sensor_config* conf)
        }
        ret = i2c_write(conf->bus_num, cmd_buf, CONF_BUF_SIZE, NULL);
        if (ret != CONF_BUF_SIZE) {
+               conf->probe_ok = 0;
                return -EIO;
        }
        return 0;
index 5f019a3..abb6337 100644 (file)
@@ -73,6 +73,7 @@ int veml6070_sensor_read(struct veml6070_sensor_config* conf, uint16_t* uv_raw)
        cmd_buf = ((conf->addr + 2) | I2C_READ_BIT);
     ret = i2c_read(conf->bus_num, &cmd_buf, 1, NULL, &data, 1);
     if (ret != 1) {
+               conf->probe_ok = 0;
         return ret;
     }
     *uv_raw = ((uint16_t)data << 8) & 0xFF00;
@@ -81,6 +82,7 @@ int veml6070_sensor_read(struct veml6070_sensor_config* conf, uint16_t* uv_raw)
     cmd_buf = (conf->addr | I2C_READ_BIT);
     ret = i2c_read(conf->bus_num, &cmd_buf, 1, NULL, &data, 1);
     if (ret != 1) {
+               conf->probe_ok = 0;
         return ret;
     }
     *uv_raw |= (uint16_t)data & 0x00FF;
@@ -113,6 +115,7 @@ int veml6070_configure(struct veml6070_sensor_config* conf)
        msleep(1);
     ret = i2c_write(conf->bus_num, cmd_buf, CONF_BUF_SIZE, NULL);
     if (ret != CONF_BUF_SIZE) {
+               conf->probe_ok = 0;
         return -EIO;
     }
     return 0;