Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | /*
* Copyright (c) 2016 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <device.h>
#include <i2c.h>
#include <nanokernel.h>
#include <sensor.h>
#include <misc/__assert.h>
#include "sensor_sht3xd.h"
/*
* CRC algorithm parameters were taken from the
* "Checksum Calculation" section of the datasheet.
*/
static uint8_t sht3xd_compute_crc(uint16_t value)
{
uint8_t buf[2] = {value >> 8, value & 0xFF};
uint8_t crc = 0xFF;
uint8_t polynom = 0x31;
int i, j;
for (i = 0; i < 2; ++i) {
crc = crc ^ buf[i];
for (j = 0; j < 8; ++j) {
if (crc & 0x80) {
crc = (crc << 1) ^ polynom;
} else {
crc = crc << 1;
}
}
}
return crc;
}
int sht3xd_write_command(struct sht3xd_data *drv_data, uint16_t cmd)
{
uint8_t tx_buf[2] = {cmd >> 8, cmd & 0xFF};
return i2c_write(drv_data->i2c, tx_buf, sizeof(tx_buf),
SHT3XD_I2C_ADDRESS);
}
int sht3xd_write_reg(struct sht3xd_data *drv_data, uint16_t cmd,
uint16_t val)
{
uint8_t tx_buf[5];
tx_buf[0] = cmd >> 8;
tx_buf[1] = cmd & 0xFF;
tx_buf[2] = val >> 8;
tx_buf[3] = val & 0xFF;
tx_buf[4] = sht3xd_compute_crc(val);
return i2c_write(drv_data->i2c, tx_buf, sizeof(tx_buf),
SHT3XD_I2C_ADDRESS);
}
static int sht3xd_sample_fetch(struct device *dev, enum sensor_channel chan)
{
struct sht3xd_data *drv_data = dev->driver_data;
uint8_t rx_buf[6];
uint16_t t_sample, rh_sample;
__ASSERT(chan == SENSOR_CHAN_ALL);
uint8_t tx_buf[2] = {
SHT3XD_CMD_FETCH >> 8,
SHT3XD_CMD_FETCH & 0xFF
};
struct i2c_msg msgs[2] = {
{
.buf = tx_buf,
.len = sizeof(tx_buf),
.flags = I2C_MSG_WRITE | I2C_MSG_RESTART,
},
{
.buf = rx_buf,
.len = sizeof(rx_buf),
.flags = I2C_MSG_READ | I2C_MSG_STOP,
},
};
if (i2c_transfer(drv_data->i2c, msgs, 2, SHT3XD_I2C_ADDRESS) < 0) {
SYS_LOG_DBG("Failed to read data sample!");
return -EIO;
}
t_sample = (rx_buf[0] << 8) | rx_buf[1];
if (sht3xd_compute_crc(t_sample) != rx_buf[2]) {
SYS_LOG_DBG("Received invalid temperature CRC!");
return -EIO;
}
rh_sample = (rx_buf[3] << 8) | rx_buf[4];
if (sht3xd_compute_crc(rh_sample) != rx_buf[5]) {
SYS_LOG_DBG("Received invalid relative humidity CRC!");
return -EIO;
}
drv_data->t_sample = t_sample;
drv_data->rh_sample = rh_sample;
return 0;
}
static int sht3xd_channel_get(struct device *dev,
enum sensor_channel chan,
struct sensor_value *val)
{
struct sht3xd_data *drv_data = dev->driver_data;
uint64_t tmp;
/*
* See datasheet "Conversion of Signal Output" section
* for more details on processing sample data.
*/
if (chan == SENSOR_CHAN_TEMP) {
/* val = -45 + 175 * sample / (2^16 -1) */
tmp = 175 * (uint64_t)drv_data->t_sample;
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
val->val1 = (int32_t)(tmp / 0xFFFF) - 45;
val->val2 = (1000000 * (tmp % 0xFFFF)) / 0xFFFF;
} else if (chan == SENSOR_CHAN_HUMIDITY) {
/* val = 100000 * sample / (2^16 -1) */
tmp = 100000 * (uint64_t)drv_data->rh_sample;
val->type = SENSOR_VALUE_TYPE_INT_PLUS_MICRO;
val->val1 = tmp / 0xFFFF;
val->val2 = (1000000 * (tmp % 0xFFFF)) / 0xFFFF;
} else {
return -ENOTSUP;
}
return 0;
}
static struct sensor_driver_api sht3xd_driver_api = {
#ifdef CONFIG_SHT3XD_TRIGGER
.attr_set = sht3xd_attr_set,
.trigger_set = sht3xd_trigger_set,
#endif
.sample_fetch = sht3xd_sample_fetch,
.channel_get = sht3xd_channel_get,
};
static int sht3xd_init(struct device *dev)
{
struct sht3xd_data *drv_data = dev->driver_data;
drv_data->i2c = device_get_binding(CONFIG_SHT3XD_I2C_MASTER_DEV_NAME);
if (drv_data->i2c == NULL) {
SYS_LOG_DBG("Failed to get pointer to %s device!",
CONFIG_SHT3XD_I2C_MASTER_DEV_NAME);
return -EINVAL;
}
/* clear status register */
if (sht3xd_write_command(drv_data, SHT3XD_CMD_CLEAR_STATUS) < 0) {
SYS_LOG_DBG("Failed to clear status register!");
return -EIO;
}
sys_thread_busy_wait(SHT3XD_CLEAR_STATUS_WAIT_USEC);
/* set periodic measurement mode */
if (sht3xd_write_command(drv_data,
sht3xd_measure_cmd[SHT3XD_MPS_IDX][SHT3XD_REPEATABILITY_IDX])
< 0) {
SYS_LOG_DBG("Failed to set measurement mode!");
return -EIO;
}
sys_thread_busy_wait(sht3xd_measure_wait[SHT3XD_REPEATABILITY_IDX]);
#ifdef CONFIG_SHT3XD_TRIGGER
if (sht3xd_init_interrupt(dev) < 0) {
SYS_LOG_DBG("Failed to initialize interrupt");
return -EIO;
}
#endif
dev->driver_api = &sht3xd_driver_api;
return 0;
}
struct sht3xd_data sht3xd_driver;
DEVICE_INIT(sht3xd, CONFIG_SHT3XD_NAME, sht3xd_init, &sht3xd_driver,
NULL, SECONDARY, CONFIG_SHT3XD_INIT_PRIORITY);
|