2017-03-30 17:34:03 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/sensors/hts221.c
|
|
|
|
*
|
2021-11-15 08:04:55 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you 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
|
2017-03-30 17:34:03 +02:00
|
|
|
*
|
2021-11-15 08:04:55 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2017-03-30 17:34:03 +02:00
|
|
|
*
|
2021-11-15 08:04:55 +01:00
|
|
|
* 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.
|
2017-03-30 17:34:03 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2019-11-30 00:37:39 +01:00
|
|
|
|
2017-03-30 17:34:03 +02:00
|
|
|
#include <sys/types.h>
|
2021-05-18 08:59:14 +02:00
|
|
|
#include <assert.h>
|
2017-03-30 17:34:03 +02:00
|
|
|
#include <debug.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <poll.h>
|
|
|
|
#include <errno.h>
|
2019-11-30 00:37:39 +01:00
|
|
|
|
2017-03-30 17:34:03 +02:00
|
|
|
#include <nuttx/arch.h>
|
|
|
|
#include <nuttx/i2c/i2c_master.h>
|
|
|
|
#include <nuttx/irq.h>
|
|
|
|
#include <nuttx/kmalloc.h>
|
2022-09-06 08:18:45 +02:00
|
|
|
#include <nuttx/mutex.h>
|
2019-11-30 00:37:39 +01:00
|
|
|
#include <nuttx/signal.h>
|
2017-03-30 17:34:03 +02:00
|
|
|
#include <nuttx/random.h>
|
|
|
|
|
|
|
|
#include <nuttx/sensors/hts221.h>
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-Processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-08-24 17:22:36 +02:00
|
|
|
#ifdef CONFIG_HTS221_DEBUG
|
2017-03-30 17:34:03 +02:00
|
|
|
# define hts221_dbg(x, ...) _info(x, ##__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
# define hts221_dbg(x, ...) sninfo(x, ##__VA_ARGS__)
|
|
|
|
#endif
|
|
|
|
|
2017-06-01 14:19:54 +02:00
|
|
|
#ifndef CONFIG_HTS221_I2C_FREQUENCY
|
|
|
|
# define CONFIG_HTS221_I2C_FREQUENCY 400000
|
|
|
|
#endif
|
|
|
|
|
2017-03-30 17:34:03 +02:00
|
|
|
#define HTS221_WHO_AM_I 0x0f
|
|
|
|
#define HTS221_AV_CONF 0x10
|
|
|
|
#define HTS221_CTRL_REG1 0x20
|
|
|
|
#define HTS221_CTRL_REG2 0x21
|
|
|
|
#define HTS221_CTRL_REG3 0x22
|
|
|
|
#define HTS221_STATUS_REG 0x27
|
|
|
|
#define HTS221_HUM_OUT_L 0x28
|
|
|
|
#define HTS221_HUM_OUT_H 0x29
|
|
|
|
#define HTS221_TEMP_OUT_L 0x2a
|
|
|
|
#define HTS221_TEMP_OUT_H 0x2b
|
|
|
|
|
|
|
|
/* Calibration registers */
|
|
|
|
|
|
|
|
#define HTS221_CALIB_H0_RH_X2 0x30
|
|
|
|
#define HTS221_CALIB_H1_RH_X2 0x31
|
|
|
|
#define HTS221_CALIB_T0_DEGC_X8 0x32
|
|
|
|
#define HTS221_CALIB_T1_DEGC_X8 0x33
|
|
|
|
#define HTS221_CALIB_T1_T0_MSB 0x35
|
|
|
|
#define HTS221_CALIB_H0T0_OUT_L 0x36
|
|
|
|
#define HTS221_CALIB_H0T0_OUT_H 0x37
|
|
|
|
#define HTS221_CALIB_H1T0_OUT_L 0x3a
|
|
|
|
#define HTS221_CALIB_H1T0_OUT_H 0x3b
|
|
|
|
#define HTS221_CALIB_T0_OUT_L 0x3c
|
|
|
|
#define HTS221_CALIB_T0_OUT_H 0x3d
|
|
|
|
#define HTS221_CALIB_T1_OUT_L 0x3e
|
|
|
|
#define HTS221_CALIB_T1_OUT_H 0x3f
|
|
|
|
|
|
|
|
/* HTS221_CTRL_REG1 */
|
|
|
|
|
|
|
|
#define HTS221_CTRL_REG1_PD (1 << 7)
|
|
|
|
#define HTS221_CTRL_REG1_BDU (1 << 2)
|
|
|
|
|
|
|
|
/* HTS221_CTRL_REG2 */
|
|
|
|
|
|
|
|
#define HTS221_CTRL_REG2_BOOT (1 << 7)
|
|
|
|
#define HTS221_CTRL_REG2_ONE_SHOT (1 << 0)
|
|
|
|
|
|
|
|
/* HTS221_CTRL_REG3 */
|
|
|
|
|
|
|
|
#define HTS221_CTRL_REG3_DRDY_L_H (1 << 7)
|
|
|
|
#define HTS221_CTRL_REG3_PP_OD (1 << 6)
|
|
|
|
#define HTS221_CTRL_REG3_DRDY_EN (1 << 2)
|
|
|
|
|
|
|
|
/* HTS221_STATUS_REG */
|
|
|
|
|
|
|
|
#define HTS221_STATUS_REG_H_DA (1 << 1)
|
|
|
|
#define HTS221_STATUS_REG_T_DA (1 << 0)
|
|
|
|
|
|
|
|
#define HTS221_I2C_RETRIES 10
|
|
|
|
|
|
|
|
/****************************************************************************
|
2019-12-07 02:07:34 +01:00
|
|
|
* Private Function Prototypes
|
2020-03-30 23:06:15 +02:00
|
|
|
****************************************************************************/
|
2017-03-30 17:34:03 +02:00
|
|
|
|
|
|
|
static int hts221_open(FAR struct file *filep);
|
|
|
|
static int hts221_close(FAR struct file *filep);
|
|
|
|
static ssize_t hts221_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen);
|
|
|
|
static ssize_t hts221_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t buflen);
|
|
|
|
static int hts221_ioctl(FAR struct file *filep, int cmd, unsigned long arg);
|
|
|
|
static int hts221_poll(FAR struct file *filep, FAR struct pollfd *fds,
|
|
|
|
bool setup);
|
|
|
|
|
|
|
|
/****************************************************************************
|
2019-12-07 02:07:34 +01:00
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
2017-03-30 17:34:03 +02:00
|
|
|
|
|
|
|
struct hts221_dev_s
|
|
|
|
{
|
2024-08-25 01:21:12 +02:00
|
|
|
FAR struct i2c_master_s *i2c;
|
2017-03-30 17:34:03 +02:00
|
|
|
uint8_t addr;
|
2024-08-25 01:21:12 +02:00
|
|
|
FAR hts221_config_t *config;
|
2022-09-06 08:18:45 +02:00
|
|
|
mutex_t devlock;
|
2017-03-30 17:34:03 +02:00
|
|
|
volatile bool int_pending;
|
2024-08-25 01:21:12 +02:00
|
|
|
FAR struct pollfd *fds[CONFIG_HTS221_NPOLLWAITERS];
|
2017-03-30 17:34:03 +02:00
|
|
|
struct
|
|
|
|
{
|
|
|
|
int16_t t0_out;
|
|
|
|
int16_t t1_out;
|
|
|
|
int16_t h0_t0_out;
|
|
|
|
int16_t h1_t0_out;
|
|
|
|
unsigned int t0_x8:10;
|
|
|
|
unsigned int t1_x8:10;
|
|
|
|
uint8_t h0_x2;
|
|
|
|
uint8_t h1_x2;
|
|
|
|
} calib;
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
2019-12-07 02:07:34 +01:00
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
2017-03-30 17:34:03 +02:00
|
|
|
|
|
|
|
static const struct file_operations g_humidityops =
|
|
|
|
{
|
|
|
|
hts221_open, /* open */
|
|
|
|
hts221_close, /* close */
|
|
|
|
hts221_read, /* read */
|
|
|
|
hts221_write, /* write */
|
|
|
|
NULL, /* seek */
|
2019-05-22 02:57:54 +02:00
|
|
|
hts221_ioctl, /* ioctl */
|
2023-01-02 14:02:51 +01:00
|
|
|
NULL, /* mmap */
|
2023-01-02 18:06:12 +01:00
|
|
|
NULL, /* truncate */
|
2019-05-22 02:57:54 +02:00
|
|
|
hts221_poll /* poll */
|
2017-03-30 17:34:03 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
2019-12-07 02:07:34 +01:00
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
2017-03-30 17:34:03 +02:00
|
|
|
|
2017-03-31 13:53:43 +02:00
|
|
|
static int hts221_do_transfer(FAR struct hts221_dev_s *priv,
|
2017-03-30 17:34:03 +02:00
|
|
|
FAR struct i2c_msg_s *msgv,
|
|
|
|
size_t nmsg)
|
|
|
|
{
|
|
|
|
int ret = -EIO;
|
|
|
|
int retries;
|
|
|
|
|
|
|
|
for (retries = 0; retries < HTS221_I2C_RETRIES; retries++)
|
|
|
|
{
|
2017-03-31 13:53:43 +02:00
|
|
|
ret = I2C_TRANSFER(priv->i2c, msgv, nmsg);
|
2017-03-30 17:34:03 +02:00
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Some error. Try to reset I2C bus and keep trying. */
|
2019-12-07 02:07:34 +01:00
|
|
|
|
2017-03-30 17:34:03 +02:00
|
|
|
#ifdef CONFIG_I2C_RESET
|
|
|
|
if (retries == HTS221_I2C_RETRIES - 1)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-05-15 15:22:17 +02:00
|
|
|
ret = I2C_RESET(priv->i2c);
|
2017-03-30 17:34:03 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-05-15 15:22:17 +02:00
|
|
|
hts221_dbg("I2C_RESET failed: %d\n", ret);
|
2017-03-30 17:34:03 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hts221_dbg("xfer failed: %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-03-31 13:53:43 +02:00
|
|
|
static int32_t hts221_write_reg8(FAR struct hts221_dev_s *priv,
|
2017-03-30 17:34:03 +02:00
|
|
|
const uint8_t *command)
|
|
|
|
{
|
|
|
|
struct i2c_msg_s msgv[2] =
|
|
|
|
{
|
|
|
|
{
|
2017-06-01 14:19:54 +02:00
|
|
|
.frequency = CONFIG_HTS221_I2C_FREQUENCY,
|
|
|
|
.addr = priv->addr,
|
|
|
|
.flags = 0,
|
|
|
|
.buffer = (FAR void *)&command[0],
|
|
|
|
.length = 1
|
2017-03-30 17:34:03 +02:00
|
|
|
},
|
|
|
|
{
|
2017-06-01 14:19:54 +02:00
|
|
|
.frequency = CONFIG_HTS221_I2C_FREQUENCY,
|
|
|
|
.addr = priv->addr,
|
2018-08-03 16:51:55 +02:00
|
|
|
.flags = I2C_M_NOSTART,
|
2017-06-01 14:19:54 +02:00
|
|
|
.buffer = (FAR void *)&command[1],
|
|
|
|
.length = 1
|
2017-03-30 17:34:03 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-03-31 13:53:43 +02:00
|
|
|
return hts221_do_transfer(priv, msgv, 2);
|
2017-03-30 17:34:03 +02:00
|
|
|
}
|
|
|
|
|
2017-03-31 13:53:43 +02:00
|
|
|
static int hts221_read_reg(FAR struct hts221_dev_s *priv,
|
2017-03-30 17:34:03 +02:00
|
|
|
FAR const uint8_t *command, FAR uint8_t *value)
|
|
|
|
{
|
|
|
|
struct i2c_msg_s msgv[2] =
|
|
|
|
{
|
|
|
|
{
|
2017-06-01 14:19:54 +02:00
|
|
|
.frequency = CONFIG_HTS221_I2C_FREQUENCY,
|
|
|
|
.addr = priv->addr,
|
|
|
|
.flags = 0,
|
|
|
|
.buffer = (FAR void *)command,
|
|
|
|
.length = 1
|
2017-03-30 17:34:03 +02:00
|
|
|
},
|
|
|
|
{
|
2017-06-01 14:19:54 +02:00
|
|
|
.frequency = CONFIG_HTS221_I2C_FREQUENCY,
|
|
|
|
.addr = priv->addr,
|
|
|
|
.flags = I2C_M_READ,
|
|
|
|
.buffer = value,
|
|
|
|
.length = 1
|
2017-03-30 17:34:03 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-03-31 13:53:43 +02:00
|
|
|
return hts221_do_transfer(priv, msgv, 2);
|
2017-03-30 17:34:03 +02:00
|
|
|
}
|
|
|
|
|
2017-03-31 13:53:43 +02:00
|
|
|
static int hts221_get_id(FAR struct hts221_dev_s *priv, uint8_t *value)
|
2017-03-30 17:34:03 +02:00
|
|
|
{
|
|
|
|
int ret = OK;
|
|
|
|
uint8_t cmd = HTS221_WHO_AM_I;
|
|
|
|
|
|
|
|
ret = hts221_read_reg(priv, &cmd, value);
|
|
|
|
|
|
|
|
hts221_dbg("Who am I request: 0x%02X\n", *value);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hts221_cfgr_resolution(FAR struct hts221_dev_s *priv,
|
|
|
|
FAR hts221_settings_t *settings)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
uint8_t value;
|
|
|
|
const uint8_t addr = HTS221_AV_CONF;
|
|
|
|
uint8_t regval = 0;
|
2019-12-07 02:07:34 +01:00
|
|
|
uint8_t cmd[2] =
|
|
|
|
{
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
2017-03-30 17:34:03 +02:00
|
|
|
const uint8_t mask = 0x3f;
|
|
|
|
|
|
|
|
ret = hts221_read_reg(priv, &addr, ®val);
|
|
|
|
hts221_dbg("Default resolution: 0x%02X\n", regval);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
value = (uint8_t)settings->humid_resol |
|
|
|
|
((uint8_t)settings->temp_resol << 3);
|
|
|
|
regval &= ~mask;
|
|
|
|
cmd[0] = addr;
|
|
|
|
cmd[1] = regval | value;
|
|
|
|
hts221_dbg("New resolution: 0x%02X\n", cmd[1]);
|
|
|
|
|
|
|
|
ret = hts221_write_reg8(priv, cmd);
|
|
|
|
|
|
|
|
hts221_dbg("Resolution changed: temp=%d humid=%d ret=%d\n",
|
|
|
|
settings->temp_resol, settings->humid_resol, ret);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hts221_config_ctrl_reg3(FAR struct hts221_dev_s *priv,
|
|
|
|
FAR hts221_settings_t *settings)
|
|
|
|
{
|
|
|
|
int ret = OK;
|
|
|
|
uint8_t regval = 0;
|
|
|
|
uint8_t addr = HTS221_CTRL_REG3;
|
|
|
|
const uint8_t mask = 0xc4;
|
2019-12-07 02:07:34 +01:00
|
|
|
uint8_t data_to_write[2] =
|
|
|
|
{
|
|
|
|
0
|
|
|
|
};
|
2017-03-30 17:34:03 +02:00
|
|
|
|
|
|
|
ret = hts221_read_reg(priv, &addr, ®val);
|
2017-12-20 17:11:52 +01:00
|
|
|
hts221_dbg("CTRL_REG%d: 0x%02X\n", 3, regval);
|
2017-03-30 17:34:03 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
regval &= ~mask;
|
2020-03-30 23:06:15 +02:00
|
|
|
regval |= (uint8_t)(settings->is_high_edge ?
|
|
|
|
0 : HTS221_CTRL_REG3_DRDY_L_H);
|
2017-03-30 17:34:03 +02:00
|
|
|
regval |= (uint8_t)(settings->is_open_drain ? HTS221_CTRL_REG3_PP_OD : 0);
|
|
|
|
regval |= (uint8_t)(settings->is_data_rdy ? HTS221_CTRL_REG3_DRDY_EN : 0);
|
|
|
|
|
|
|
|
data_to_write[0] = addr;
|
|
|
|
data_to_write[1] = regval;
|
|
|
|
|
|
|
|
ret = hts221_write_reg8(priv, data_to_write);
|
2017-12-20 17:11:52 +01:00
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
ret = hts221_read_reg(priv, &addr, ®val);
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
hts221_dbg("wrote 0x%02X => CTRL_REG%d: 0x%02X\n",
|
|
|
|
data_to_write[1], 3, regval);
|
|
|
|
}
|
|
|
|
}
|
2017-03-30 17:34:03 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hts221_config_ctrl_reg2(FAR struct hts221_dev_s *priv,
|
|
|
|
FAR hts221_settings_t *settings)
|
|
|
|
{
|
|
|
|
int ret = OK;
|
|
|
|
uint8_t regval = 0;
|
|
|
|
uint8_t addr = HTS221_CTRL_REG2;
|
|
|
|
const uint8_t mask = 0x80;
|
2019-12-07 02:07:34 +01:00
|
|
|
uint8_t data_to_write[2] =
|
|
|
|
{
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
2017-12-20 17:11:52 +01:00
|
|
|
int retries = 5;
|
|
|
|
|
|
|
|
if (!settings->is_boot)
|
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
2017-03-30 17:34:03 +02:00
|
|
|
|
|
|
|
ret = hts221_read_reg(priv, &addr, ®val);
|
2017-12-20 17:11:52 +01:00
|
|
|
hts221_dbg("CTRL_REG%d: 0x%02X\n", 2, regval);
|
|
|
|
|
2017-03-30 17:34:03 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
regval &= ~mask;
|
2017-12-20 17:11:52 +01:00
|
|
|
regval |= HTS221_CTRL_REG2_BOOT;
|
2017-03-30 17:34:03 +02:00
|
|
|
|
|
|
|
data_to_write[0] = addr;
|
|
|
|
data_to_write[1] = regval;
|
|
|
|
|
|
|
|
ret = hts221_write_reg8(priv, data_to_write);
|
2017-12-20 17:11:52 +01:00
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
/* Wait until boot bit is cleared. */
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
ret = hts221_read_reg(priv, &addr, ®val);
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
hts221_dbg("wrote 0x%02X => CTRL_REG%d: 0x%02X\n",
|
|
|
|
data_to_write[1], 2, regval);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((regval & HTS221_CTRL_REG2_BOOT) == 0)
|
|
|
|
{
|
|
|
|
/* After boot bit is cleared, wait additional 5 msec as
|
|
|
|
* recommended in HTS221 application note.
|
|
|
|
*/
|
|
|
|
|
|
|
|
up_mdelay(5);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-11-30 00:37:39 +01:00
|
|
|
nxsig_usleep(10 * 1000);
|
2017-12-20 17:11:52 +01:00
|
|
|
retries--;
|
|
|
|
}
|
|
|
|
while (retries);
|
|
|
|
|
|
|
|
if (ret >= 0 && (regval & HTS221_CTRL_REG2_BOOT) != 0)
|
|
|
|
{
|
|
|
|
ret = -ETIMEDOUT;
|
|
|
|
}
|
|
|
|
}
|
2017-03-30 17:34:03 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hts221_config_ctrl_reg1(FAR struct hts221_dev_s *priv,
|
2017-03-31 13:53:43 +02:00
|
|
|
FAR hts221_settings_t *settings)
|
2017-03-30 17:34:03 +02:00
|
|
|
{
|
|
|
|
int ret = OK;
|
|
|
|
uint8_t regval = 0;
|
|
|
|
uint8_t addr = HTS221_CTRL_REG1;
|
|
|
|
const uint8_t mask = 0x87;
|
2019-12-07 02:07:34 +01:00
|
|
|
uint8_t data_to_write[2] =
|
|
|
|
{
|
|
|
|
0
|
|
|
|
};
|
2017-03-30 17:34:03 +02:00
|
|
|
|
|
|
|
ret = hts221_read_reg(priv, &addr, ®val);
|
2017-12-20 17:11:52 +01:00
|
|
|
hts221_dbg("CTRL_REG%d: 0x%02X\n", 1, regval);
|
2017-03-30 17:34:03 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
regval &= ~mask;
|
2019-12-07 02:07:34 +01:00
|
|
|
regval |= (uint8_t)(settings->odr & 0xff);
|
|
|
|
regval |= (uint8_t)(settings->is_bdu ? HTS221_CTRL_REG1_BDU : 0);
|
2017-03-30 17:34:03 +02:00
|
|
|
|
|
|
|
data_to_write[0] = addr;
|
|
|
|
data_to_write[1] = regval;
|
|
|
|
|
|
|
|
ret = hts221_write_reg8(priv, data_to_write);
|
2017-12-20 17:11:52 +01:00
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
ret = hts221_read_reg(priv, &addr, ®val);
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
hts221_dbg("wrote 0x%02X => CTRL_REG%d: 0x%02X\n",
|
|
|
|
data_to_write[1], 1, regval);
|
|
|
|
}
|
|
|
|
}
|
2017-03-30 17:34:03 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hts221_power_on_off(FAR struct hts221_dev_s *priv, bool on)
|
|
|
|
{
|
|
|
|
int ret = OK;
|
|
|
|
uint8_t regval = 0;
|
|
|
|
uint8_t addr = HTS221_CTRL_REG1;
|
|
|
|
uint8_t data_to_write[2];
|
|
|
|
|
|
|
|
ret = hts221_read_reg(priv, &addr, ®val);
|
2017-12-20 17:11:52 +01:00
|
|
|
hts221_dbg("CTRL_REG%d: 0x%02X\n", 1, regval);
|
2017-03-30 17:34:03 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (on)
|
|
|
|
{
|
|
|
|
regval |= HTS221_CTRL_REG1_PD;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
regval &= ~HTS221_CTRL_REG1_PD;
|
|
|
|
}
|
2019-12-07 02:07:34 +01:00
|
|
|
|
2017-03-30 17:34:03 +02:00
|
|
|
data_to_write[0] = addr;
|
|
|
|
data_to_write[1] = regval;
|
|
|
|
|
|
|
|
ret = hts221_write_reg8(priv, data_to_write);
|
2017-12-20 17:11:52 +01:00
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
ret = hts221_read_reg(priv, &addr, ®val);
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
hts221_dbg("wrote 0x%02X => CTRL_REG%d: 0x%02X\n",
|
|
|
|
data_to_write[1], 1, regval);
|
|
|
|
}
|
|
|
|
}
|
2017-03-30 17:34:03 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hts221_config(FAR struct hts221_dev_s *priv,
|
2017-03-31 13:53:43 +02:00
|
|
|
FAR hts221_settings_t *cfgr)
|
2017-03-30 17:34:03 +02:00
|
|
|
{
|
|
|
|
int ret = OK;
|
|
|
|
|
2017-12-20 17:11:52 +01:00
|
|
|
ret = hts221_config_ctrl_reg2(priv, cfgr); /* Performs sensor reset. */
|
2017-03-30 17:34:03 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
2017-12-20 17:11:52 +01:00
|
|
|
ret = hts221_cfgr_resolution(priv, cfgr);
|
2017-03-30 17:34:03 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
2017-12-20 17:11:52 +01:00
|
|
|
ret = hts221_config_ctrl_reg3(priv, cfgr);
|
2017-03-30 17:34:03 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = hts221_config_ctrl_reg1(priv, cfgr);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hts221_start_conversion(FAR struct hts221_dev_s *priv)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
uint8_t addr = HTS221_CTRL_REG2;
|
|
|
|
uint8_t data_to_write[2];
|
|
|
|
|
|
|
|
ret = hts221_power_on_off(priv, true);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
data_to_write[0] = addr;
|
|
|
|
data_to_write[1] = (uint8_t) HTS221_CTRL_REG2_ONE_SHOT;
|
|
|
|
|
|
|
|
ret = hts221_write_reg8(priv, data_to_write);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
hts221_dbg("Cannot start conversion\n");
|
|
|
|
ret = ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hts221_check_status(FAR struct hts221_dev_s *priv,
|
2017-03-31 13:53:43 +02:00
|
|
|
FAR hts221_status_t *status)
|
2017-03-30 17:34:03 +02:00
|
|
|
{
|
|
|
|
int ret = OK;
|
|
|
|
uint8_t addr = HTS221_STATUS_REG;
|
|
|
|
const uint8_t humid_mask = 0x02;
|
|
|
|
const uint8_t temp_mask = 0x01;
|
|
|
|
uint8_t regval = 0;
|
|
|
|
|
|
|
|
ret = hts221_read_reg(priv, &addr, ®val);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
status->is_humid_ready = ((regval & humid_mask) ? true : false);
|
|
|
|
status->is_temp_ready = ((regval & temp_mask) ? true : false);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hts221_read_raw_data(FAR struct hts221_dev_s *priv,
|
2017-03-31 13:53:43 +02:00
|
|
|
FAR hts221_raw_data_t *data)
|
2017-03-30 17:34:03 +02:00
|
|
|
{
|
|
|
|
int ret = OK;
|
|
|
|
uint8_t addr_humid_low = HTS221_HUM_OUT_L;
|
|
|
|
uint8_t addr_humid_high = HTS221_HUM_OUT_H;
|
|
|
|
uint8_t addr_temp_low = HTS221_TEMP_OUT_L;
|
|
|
|
uint8_t addr_temp_high = HTS221_TEMP_OUT_H;
|
2017-12-20 17:11:52 +01:00
|
|
|
irqstate_t flags;
|
2017-03-30 17:34:03 +02:00
|
|
|
|
|
|
|
ret = hts221_read_reg(priv, &addr_humid_low, &data->humid_low_bits);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = hts221_read_reg(priv, &addr_humid_high, &data->humid_high_bits);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = hts221_read_reg(priv, &addr_temp_low, &data->temp_low_bits);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = hts221_read_reg(priv, &addr_temp_high, &data->temp_high_bits);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add low-order bytes to entropy pool. */
|
|
|
|
|
2020-03-30 23:06:15 +02:00
|
|
|
add_sensor_randomness(((uint32_t)data->humid_low_bits << 8) |
|
|
|
|
data->temp_low_bits);
|
2017-03-30 17:34:03 +02:00
|
|
|
|
|
|
|
flags = enter_critical_section();
|
|
|
|
priv->int_pending = false;
|
|
|
|
leave_critical_section(flags);
|
|
|
|
|
|
|
|
hts221_dbg("Humid: 0x%02X, 0x%02X Temper: 0x%02X 0x%02X\n",
|
|
|
|
data->humid_high_bits, data->humid_low_bits,
|
|
|
|
data->temp_high_bits, data->temp_low_bits);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hts221_load_calibration_data(FAR struct hts221_dev_s *priv)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
uint8_t addr;
|
|
|
|
uint8_t t0_degc_x8 = 0;
|
|
|
|
uint8_t t1_degc_x8 = 0;
|
|
|
|
uint8_t t1_t0_msb = 0;
|
|
|
|
uint8_t t0_out_lsb = 0;
|
|
|
|
uint8_t t0_out_msb = 0;
|
|
|
|
uint8_t t1_out_lsb = 0;
|
|
|
|
uint8_t t1_out_msb = 0;
|
|
|
|
uint8_t h0_rh_x2 = 0;
|
|
|
|
uint8_t h1_rh_x2 = 0;
|
|
|
|
uint8_t h0t0_out_lsb = 0;
|
|
|
|
uint8_t h0t0_out_msb = 0;
|
|
|
|
uint8_t h1t0_out_lsb = 0;
|
|
|
|
uint8_t h1t0_out_msb = 0;
|
|
|
|
|
|
|
|
addr = HTS221_CALIB_T0_DEGC_X8;
|
|
|
|
ret = hts221_read_reg(priv, &addr, &t0_degc_x8);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
addr = HTS221_CALIB_T1_DEGC_X8;
|
|
|
|
ret = hts221_read_reg(priv, &addr, &t1_degc_x8);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
addr = HTS221_CALIB_T1_T0_MSB;
|
|
|
|
ret = hts221_read_reg(priv, &addr, &t1_t0_msb);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
addr = HTS221_CALIB_T0_OUT_L;
|
|
|
|
ret = hts221_read_reg(priv, &addr, &t0_out_lsb);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
addr = HTS221_CALIB_T0_OUT_H;
|
|
|
|
ret = hts221_read_reg(priv, &addr, &t0_out_msb);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
addr = HTS221_CALIB_T1_OUT_L;
|
|
|
|
ret = hts221_read_reg(priv, &addr, &t1_out_lsb);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
addr = HTS221_CALIB_T1_OUT_H;
|
|
|
|
ret = hts221_read_reg(priv, &addr, &t1_out_msb);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
addr = HTS221_CALIB_H0_RH_X2;
|
|
|
|
ret = hts221_read_reg(priv, &addr, &h0_rh_x2);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
addr = HTS221_CALIB_H1_RH_X2;
|
|
|
|
ret = hts221_read_reg(priv, &addr, &h1_rh_x2);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
addr = HTS221_CALIB_H0T0_OUT_L;
|
|
|
|
ret = hts221_read_reg(priv, &addr, &h0t0_out_lsb);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
addr = HTS221_CALIB_H0T0_OUT_H;
|
|
|
|
ret = hts221_read_reg(priv, &addr, &h0t0_out_msb);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
addr = HTS221_CALIB_H1T0_OUT_L;
|
|
|
|
ret = hts221_read_reg(priv, &addr, &h1t0_out_lsb);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
addr = HTS221_CALIB_H1T0_OUT_H;
|
|
|
|
ret = hts221_read_reg(priv, &addr, &h1t0_out_msb);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->calib.t0_x8 = t0_degc_x8 | ((t1_t0_msb & 0x3) << 8);
|
|
|
|
priv->calib.t1_x8 = t1_degc_x8 | ((t1_t0_msb & (0x3 << 2)) << (8 - 2));
|
|
|
|
priv->calib.t0_out = (uint16_t) (t0_out_lsb | (t0_out_msb << 8));
|
|
|
|
priv->calib.t1_out = (uint16_t) (t1_out_lsb | (t1_out_msb << 8));
|
|
|
|
priv->calib.h0_x2 = h0_rh_x2;
|
|
|
|
priv->calib.h1_x2 = h1_rh_x2;
|
|
|
|
priv->calib.h0_t0_out = (uint16_t) (h0t0_out_lsb | (h0t0_out_msb << 8));
|
|
|
|
priv->calib.h1_t0_out = (uint16_t) (h1t0_out_lsb | (h1t0_out_msb << 8));
|
|
|
|
|
|
|
|
hts221_dbg("calib.t0_x8: %d\n", priv->calib.t0_x8);
|
|
|
|
hts221_dbg("calib.t1_x8: %d\n", priv->calib.t1_x8);
|
|
|
|
hts221_dbg("calib.t0_out: %d\n", priv->calib.t0_out);
|
|
|
|
hts221_dbg("calib.t1_out: %d\n", priv->calib.t1_out);
|
|
|
|
hts221_dbg("calib.h0_x2: %d\n", priv->calib.h0_x2);
|
|
|
|
hts221_dbg("calib.h1_x2: %d\n", priv->calib.h1_x2);
|
|
|
|
hts221_dbg("calib.h0_t0_out: %d\n", priv->calib.h0_t0_out);
|
|
|
|
hts221_dbg("calib.h1_t0_out: %d\n", priv->calib.h1_t0_out);
|
|
|
|
|
|
|
|
/* As calibration coefficients are unique to each sensor device,
|
|
|
|
* they are a good candidate to be added to entropy pool.
|
|
|
|
*/
|
|
|
|
|
2024-08-25 01:21:12 +02:00
|
|
|
up_rngaddentropy(RND_SRC_HW, (FAR uint32_t *)&priv->calib,
|
2017-03-30 17:34:03 +02:00
|
|
|
sizeof(priv->calib) / sizeof(uint32_t));
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hts221_calculate_temperature(FAR struct hts221_dev_s *priv,
|
|
|
|
FAR int *temperature,
|
|
|
|
FAR hts221_raw_data_t *raw_data)
|
|
|
|
{
|
|
|
|
int16_t t_out = (raw_data->temp_high_bits << 8) | raw_data->temp_low_bits;
|
|
|
|
int x0 = priv->calib.t0_out;
|
|
|
|
int x1 = priv->calib.t1_out;
|
|
|
|
int y0 = priv->calib.t0_x8;
|
|
|
|
int y1 = priv->calib.t1_x8;
|
|
|
|
int x = t_out;
|
|
|
|
int64_t y;
|
|
|
|
int x1_x0_diff;
|
|
|
|
|
|
|
|
x1_x0_diff = x1 - x0;
|
|
|
|
|
|
|
|
y = (y0 * x1_x0_diff + (y1 - y0) * (x - x0));
|
|
|
|
y *= HTS221_TEMPERATURE_PRECISION;
|
|
|
|
y /= x1_x0_diff * 8;
|
|
|
|
|
|
|
|
*temperature = (int)y;
|
|
|
|
|
|
|
|
hts221_dbg("Interpolation data temper: %d\n", *temperature);
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hts221_calculate_humidity(FAR struct hts221_dev_s *priv,
|
|
|
|
FAR unsigned int *humidity,
|
|
|
|
FAR hts221_raw_data_t *raw_data)
|
|
|
|
{
|
2020-03-30 23:06:15 +02:00
|
|
|
int16_t h_out;
|
2017-03-30 17:34:03 +02:00
|
|
|
int x0 = priv->calib.h0_t0_out;
|
|
|
|
int x1 = priv->calib.h1_t0_out;
|
|
|
|
int y0 = priv->calib.h0_x2;
|
|
|
|
int y1 = priv->calib.h1_x2;
|
2020-03-30 23:06:15 +02:00
|
|
|
int x;
|
2017-03-30 17:34:03 +02:00
|
|
|
int64_t y;
|
|
|
|
int x1_x0_diff;
|
|
|
|
|
2020-03-30 23:06:15 +02:00
|
|
|
h_out = (raw_data->humid_high_bits << 8) | raw_data->humid_low_bits;
|
|
|
|
x = h_out;
|
2017-03-30 17:34:03 +02:00
|
|
|
x1_x0_diff = x1 - x0;
|
|
|
|
|
2020-03-30 23:06:15 +02:00
|
|
|
y = (y0 * x1_x0_diff + (y1 - y0) * (x - x0));
|
2017-03-30 17:34:03 +02:00
|
|
|
y *= HTS221_HUMIDITY_PRECISION;
|
|
|
|
y /= x1_x0_diff * 2;
|
|
|
|
|
|
|
|
*humidity = (int)y;
|
|
|
|
|
|
|
|
hts221_dbg("Interpolation data humidity: %d\n", *humidity);
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hts221_read_convert_data(FAR struct hts221_dev_s *priv,
|
|
|
|
FAR hts221_conv_data_t *data)
|
|
|
|
{
|
|
|
|
int ret = OK;
|
|
|
|
hts221_raw_data_t raw_data;
|
|
|
|
|
|
|
|
ret = hts221_read_raw_data(priv, &raw_data);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = hts221_calculate_temperature(priv, &data->temperature, &raw_data);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
hts221_dbg("Temperature calculated\n");
|
|
|
|
|
|
|
|
ret = hts221_calculate_humidity(priv, &data->humidity, &raw_data);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
hts221_dbg("Humidity calculated\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-08-24 17:22:36 +02:00
|
|
|
#ifdef CONFIG_HTS221_DEBUG
|
2017-03-30 17:34:03 +02:00
|
|
|
static int hts221_dump_registers(FAR struct hts221_dev_s *priv)
|
|
|
|
{
|
|
|
|
int ret = OK;
|
|
|
|
uint8_t av_addr = HTS221_AV_CONF;
|
|
|
|
uint8_t ctrl_reg1_addr = HTS221_CTRL_REG1;
|
|
|
|
uint8_t ctrl_reg2_addr = HTS221_CTRL_REG2;
|
|
|
|
uint8_t ctrl_reg3_addr = HTS221_CTRL_REG3;
|
|
|
|
uint8_t regval = 0;
|
|
|
|
|
|
|
|
ret = hts221_read_reg(priv, &av_addr, ®val);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
hts221_dbg("AV_CONF_REG: 0x%02X\n", regval);
|
|
|
|
|
|
|
|
ret = hts221_read_reg(priv, &ctrl_reg1_addr, ®val);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
hts221_dbg("CTRL_REG_1: 0x%02X\n", regval);
|
|
|
|
|
|
|
|
ret = hts221_read_reg(priv, &ctrl_reg2_addr, ®val);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
hts221_dbg("CTRL_REG_2: 0x%02X\n", regval);
|
|
|
|
|
|
|
|
ret = hts221_read_reg(priv, &ctrl_reg3_addr, ®val);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
hts221_dbg("CTRL_REG_3: 0x%02X\n", regval);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int hts221_open(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct hts221_dev_s *priv = inode->i_private;
|
2020-03-30 23:06:15 +02:00
|
|
|
int ret;
|
2017-10-04 23:22:27 +02:00
|
|
|
|
|
|
|
/* Get exclusive access */
|
2017-03-30 17:34:03 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->devlock);
|
2020-03-30 23:06:15 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2017-03-30 17:34:03 +02:00
|
|
|
|
|
|
|
priv->config->set_power(priv->config, true);
|
|
|
|
priv->config->irq_enable(priv->config, true);
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->devlock);
|
2017-03-30 17:34:03 +02:00
|
|
|
hts221_dbg("Sensor is powered on\n");
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hts221_close(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct hts221_dev_s *priv = inode->i_private;
|
2017-10-04 23:22:27 +02:00
|
|
|
int ret;
|
2017-03-30 17:34:03 +02:00
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
/* Get exclusive access */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->devlock);
|
2020-03-30 23:06:15 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2017-03-30 17:34:03 +02:00
|
|
|
|
|
|
|
priv->config->irq_enable(priv->config, false);
|
|
|
|
ret = hts221_power_on_off(priv, false);
|
|
|
|
priv->config->set_power(priv->config, false);
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->devlock);
|
2017-03-30 17:34:03 +02:00
|
|
|
hts221_dbg("CLOSED\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t hts221_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct hts221_dev_s *priv = inode->i_private;
|
|
|
|
hts221_conv_data_t data;
|
2017-10-04 23:22:27 +02:00
|
|
|
ssize_t length = 0;
|
|
|
|
int ret;
|
2017-03-30 17:34:03 +02:00
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
/* Get exclusive access */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->devlock);
|
2020-03-30 23:06:15 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return (ssize_t)ret;
|
|
|
|
}
|
2017-03-30 17:34:03 +02:00
|
|
|
|
|
|
|
ret = hts221_read_convert_data(priv, &data);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
hts221_dbg("cannot read data: %d\n", ret);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* This interface is mainly intended for easy debugging in nsh. */
|
|
|
|
|
|
|
|
length = snprintf(buffer, buflen, "%d %u\n",
|
|
|
|
data.temperature, data.humidity);
|
|
|
|
if (length > buflen)
|
|
|
|
{
|
|
|
|
length = buflen;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->devlock);
|
2017-03-30 17:34:03 +02:00
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t hts221_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t buflen)
|
|
|
|
{
|
|
|
|
ssize_t length = 0;
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hts221_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct hts221_dev_s *priv = inode->i_private;
|
2017-10-04 23:22:27 +02:00
|
|
|
int ret;
|
2017-03-30 17:34:03 +02:00
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
/* Get exclusive access */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->devlock);
|
2020-03-30 23:06:15 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2017-03-30 17:34:03 +02:00
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case SNIOC_GET_DEV_ID:
|
|
|
|
ret = hts221_get_id(priv, (FAR uint8_t *) arg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SNIOC_CFGR:
|
|
|
|
ret = hts221_config(priv, (FAR hts221_settings_t *) arg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SNIOC_START_CONVERSION:
|
|
|
|
ret = hts221_start_conversion(priv);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SNIOC_CHECK_STATUS_REG:
|
|
|
|
ret = hts221_check_status(priv, (FAR hts221_status_t *) arg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SNIOC_READ_RAW_DATA:
|
|
|
|
ret = hts221_read_raw_data(priv, (FAR hts221_raw_data_t *) arg);
|
|
|
|
break;
|
|
|
|
|
2017-08-24 17:22:36 +02:00
|
|
|
#ifdef CONFIG_HTS221_DEBUG
|
2017-03-30 17:34:03 +02:00
|
|
|
case SNIOC_DUMP_REGS:
|
|
|
|
ret = hts221_dump_registers(priv);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
case SNIOC_READ_CONVERT_DATA:
|
|
|
|
ret = hts221_read_convert_data(priv, (FAR hts221_conv_data_t *) arg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2017-05-15 15:22:17 +02:00
|
|
|
ret = -ENOTTY;
|
2017-03-30 17:34:03 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->devlock);
|
2017-03-30 17:34:03 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool hts221_sample(FAR struct hts221_dev_s *priv)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
hts221_status_t status =
|
2020-03-30 23:06:15 +02:00
|
|
|
{
|
|
|
|
.is_humid_ready = false,
|
|
|
|
.is_temp_ready = false
|
|
|
|
};
|
2017-03-30 17:34:03 +02:00
|
|
|
|
|
|
|
ret = hts221_check_status(priv, &status);
|
|
|
|
if (ret < 0)
|
2019-12-07 02:07:34 +01:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2017-03-30 17:34:03 +02:00
|
|
|
|
|
|
|
return status.is_humid_ready || status.is_temp_ready;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hts221_poll(FAR struct file *filep, FAR struct pollfd *fds,
|
|
|
|
bool setup)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode;
|
|
|
|
FAR struct hts221_dev_s *priv;
|
2017-12-20 17:11:52 +01:00
|
|
|
irqstate_t flags;
|
2020-01-02 17:49:34 +01:00
|
|
|
int ret = OK;
|
2017-10-04 23:22:27 +02:00
|
|
|
int i;
|
2017-03-30 17:34:03 +02:00
|
|
|
|
2023-08-28 11:17:05 +02:00
|
|
|
DEBUGASSERT(fds);
|
2017-03-30 17:34:03 +02:00
|
|
|
inode = filep->f_inode;
|
|
|
|
|
2023-08-28 11:17:05 +02:00
|
|
|
DEBUGASSERT(inode->i_private);
|
2023-08-29 06:16:49 +02:00
|
|
|
priv = inode->i_private;
|
2017-03-30 17:34:03 +02:00
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
/* Get exclusive access */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->devlock);
|
2020-03-30 23:06:15 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2017-03-30 17:34:03 +02:00
|
|
|
|
|
|
|
if (setup)
|
|
|
|
{
|
|
|
|
/* Ignore waits that do not include POLLIN */
|
|
|
|
|
|
|
|
if ((fds->events & POLLIN) == 0)
|
|
|
|
{
|
|
|
|
ret = -EDEADLK;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This is a request to set up the poll. Find an available slot for
|
|
|
|
* the poll structure reference.
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (i = 0; i < CONFIG_HTS221_NPOLLWAITERS; i++)
|
|
|
|
{
|
|
|
|
/* Find an available slot */
|
|
|
|
|
|
|
|
if (!priv->fds[i])
|
|
|
|
{
|
|
|
|
/* Bind the poll structure and this slot */
|
|
|
|
|
|
|
|
priv->fds[i] = fds;
|
|
|
|
fds->priv = &priv->fds[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i >= CONFIG_HTS221_NPOLLWAITERS)
|
|
|
|
{
|
|
|
|
fds->priv = NULL;
|
|
|
|
ret = -EBUSY;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
flags = enter_critical_section();
|
|
|
|
if (priv->int_pending || hts221_sample(priv))
|
|
|
|
{
|
2023-11-19 12:19:53 +01:00
|
|
|
poll_notify(&fds, 1, POLLIN);
|
2017-03-30 17:34:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
leave_critical_section(flags);
|
|
|
|
}
|
|
|
|
else if (fds->priv)
|
|
|
|
{
|
|
|
|
/* This is a request to tear down the poll. */
|
|
|
|
|
2024-08-25 01:21:12 +02:00
|
|
|
FAR struct pollfd **slot = (FAR struct pollfd **)fds->priv;
|
2017-03-30 17:34:03 +02:00
|
|
|
DEBUGASSERT(slot != NULL);
|
|
|
|
|
|
|
|
/* Remove all memory of the poll setup */
|
|
|
|
|
|
|
|
*slot = NULL;
|
|
|
|
fds->priv = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->devlock);
|
2017-03-30 17:34:03 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hts221_int_handler(int irq, FAR void *context, FAR void *arg)
|
|
|
|
{
|
2017-03-31 13:31:35 +02:00
|
|
|
FAR struct hts221_dev_s *priv = (FAR struct hts221_dev_s *)arg;
|
|
|
|
|
|
|
|
DEBUGASSERT(priv != NULL);
|
2017-03-30 17:34:03 +02:00
|
|
|
|
2017-03-31 13:31:35 +02:00
|
|
|
priv->int_pending = true;
|
2017-03-30 17:34:03 +02:00
|
|
|
hts221_dbg("Hts221 interrupt\n");
|
2022-09-19 05:08:57 +02:00
|
|
|
poll_notify(priv->fds, CONFIG_HTS221_NPOLLWAITERS, POLLIN);
|
2017-03-30 17:34:03 +02:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int hts221_register(FAR const char *devpath, FAR struct i2c_master_s *i2c,
|
|
|
|
uint8_t addr, FAR hts221_config_t *config)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
FAR struct hts221_dev_s *priv;
|
|
|
|
|
2023-08-28 09:39:47 +02:00
|
|
|
priv = kmm_zalloc(sizeof(struct hts221_dev_s));
|
2017-03-30 17:34:03 +02:00
|
|
|
|
|
|
|
if (!priv)
|
|
|
|
{
|
|
|
|
hts221_dbg("Memory cannot be allocated for hts221 sensor");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->addr = addr;
|
|
|
|
priv->i2c = i2c;
|
|
|
|
priv->config = config;
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_init(&priv->devlock);
|
2017-03-30 17:34:03 +02:00
|
|
|
|
2017-10-20 16:33:44 +02:00
|
|
|
priv->config->set_power(priv->config, true);
|
|
|
|
|
2017-03-30 17:34:03 +02:00
|
|
|
ret = hts221_load_calibration_data(priv);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-10-16 18:50:59 +02:00
|
|
|
nxmutex_destroy(&priv->devlock);
|
2017-03-30 17:34:03 +02:00
|
|
|
kmm_free(priv);
|
|
|
|
hts221_dbg("Cannot calibrate hts221 sensor\n");
|
2022-10-16 18:50:59 +02:00
|
|
|
return ret;
|
2017-03-30 17:34:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = register_driver(devpath, &g_humidityops, 0666, priv);
|
|
|
|
|
|
|
|
hts221_dbg("Registered with %d\n", ret);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-10-16 18:50:59 +02:00
|
|
|
nxmutex_destroy(&priv->devlock);
|
2017-03-30 17:34:03 +02:00
|
|
|
kmm_free(priv);
|
|
|
|
hts221_dbg("Error occurred during the driver registering\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (priv->config->irq_clear)
|
|
|
|
{
|
|
|
|
priv->config->irq_clear(priv->config);
|
|
|
|
}
|
|
|
|
|
2017-03-31 13:31:35 +02:00
|
|
|
priv->config->irq_attach(priv->config, hts221_int_handler, priv);
|
2017-03-30 17:34:03 +02:00
|
|
|
priv->config->irq_enable(priv->config, false);
|
2017-10-20 16:33:44 +02:00
|
|
|
priv->config->set_power(priv->config, false);
|
2017-03-30 17:34:03 +02:00
|
|
|
return OK;
|
|
|
|
}
|