/** * struct sht4x_data - All the data required to operate an SHT4X chip * @client: the i2c client associated with the SHT4X * @lock: a mutex that is used to prevent parallel access to the i2c client * @heating_complete: the time that the last heating finished * @data_pending: true if and only if there are measurements to retrieve after heating * @heater_power: the power at which the heater will be started * @heater_time: the time for which the heater will remain turned on * @valid: validity of fields below * @update_interval: the minimum poll interval * @last_updated: the previous time that the SHT4X was polled * @temperature: the latest temperature value received from the SHT4X * @humidity: the latest humidity value received from the SHT4X
*/ struct sht4x_data { struct i2c_client *client; struct mutex lock; /* atomic read data updates */ unsignedlong heating_complete; /* in jiffies */ bool data_pending;
u32 heater_power; /* in milli-watts */
u32 heater_time; /* in milli-seconds */ bool valid; /* validity of fields below */ long update_interval; /* in milli-seconds */ long last_updated; /* in jiffies */
s32 temperature;
s32 humidity;
};
/** * sht4x_read_values() - read and parse the raw data from the SHT4X * @data: the struct sht4x_data to use for the lock * Return: 0 if successful, -ERRNO if not
*/ staticint sht4x_read_values(struct sht4x_data *data)
{ int ret = 0;
u16 t_ticks, rh_ticks; unsignedlong next_update; struct i2c_client *client = data->client;
u8 crc;
u8 cmd[SHT4X_CMD_LEN] = {SHT4X_CMD_MEASURE_HPM};
u8 raw_data[SHT4X_RESPONSE_LENGTH]; unsignedlong curr_jiffies;
mutex_lock(&data->lock);
curr_jiffies = jiffies; if (time_before(curr_jiffies, data->heating_complete))
msleep(jiffies_to_msecs(data->heating_complete - curr_jiffies));
/* sht4x_interval_read() - read the minimum poll interval in milliseconds */ static size_t sht4x_interval_read(struct sht4x_data *data, long *val)
{
*val = data->update_interval; return 0;
}
/* sht4x_temperature1_read() - read the temperature in millidegrees */ staticint sht4x_temperature1_read(struct sht4x_data *data, long *val)
{ int ret;
ret = sht4x_read_values(data); if (ret < 0) return ret;
*val = data->temperature;
return 0;
}
/* sht4x_humidity1_read() - read a relative humidity in millipercent */ staticint sht4x_humidity1_read(struct sht4x_data *data, long *val)
{ int ret;
ret = sht4x_read_values(data); if (ret < 0) return ret;
*val = data->humidity;
return 0;
}
static umode_t sht4x_hwmon_visible(constvoid *data, enum hwmon_sensor_types type,
u32 attr, int channel)
{ switch (type) { case hwmon_temp: case hwmon_humidity: return 0444; case hwmon_chip: return 0644; default: return 0;
}
}
staticint sht4x_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
u32 attr, int channel, long *val)
{ struct sht4x_data *data = dev_get_drvdata(dev);
switch (type) { case hwmon_temp: return sht4x_temperature1_read(data, val); case hwmon_humidity: return sht4x_humidity1_read(data, val); case hwmon_chip: return sht4x_interval_read(data, val); default: return -EOPNOTSUPP;
}
}
staticint sht4x_hwmon_write(struct device *dev, enum hwmon_sensor_types type,
u32 attr, int channel, long val)
{ struct sht4x_data *data = dev_get_drvdata(dev);
/* * we require full i2c support since the sht4x uses multi-byte read and * writes as well as multi-byte commands which are not supported by * the smbus protocol
*/ if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) return -EOPNOTSUPP;
data = devm_kzalloc(device, sizeof(*data), GFP_KERNEL); if (!data) return -ENOMEM;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.