Skip to content
Permalink
Browse files

driver/sensor: add STTS751 temperature sensor

Add support to STM STTS751 temperature sensor.

Signed-off-by: Armando Visconti <armando.visconti@st.com>
  • Loading branch information...
avisconti authored and galak committed Jul 16, 2019
1 parent dea40b6 commit 9e5c97edaea7e8ec1811672ae366c98daaa67fb0
@@ -146,6 +146,7 @@
/drivers/sensor/lis*/ @avisconti
/drivers/sensor/lps*/ @avisconti
/drivers/sensor/lsm*/ @avisconti
/drivers/sensor/st*/ @avisconti
/drivers/serial/uart_altera_jtag_hal.c @wentongwu
/drivers/serial/*ns16550* @gnuless
/drivers/serial/Kconfig.litex @mateusz-holenko @kgugala @pgielda
@@ -48,6 +48,7 @@ add_subdirectory_ifdef(CONFIG_PMS7003 pms7003)
add_subdirectory_ifdef(CONFIG_QDEC_NRFX qdec_nrfx)
add_subdirectory_ifdef(CONFIG_TEMP_NRF5 nrf5)
add_subdirectory_ifdef(CONFIG_SHT3XD sht3xd)
add_subdirectory_ifdef(CONFIG_STTS751 stts751)
add_subdirectory_ifdef(CONFIG_SX9500 sx9500)
add_subdirectory_ifdef(CONFIG_TH02 th02)
add_subdirectory_ifdef(CONFIG_TMP007 tmp007)
@@ -121,6 +121,8 @@ source "drivers/sensor/qdec_nrfx/Kconfig"

source "drivers/sensor/sht3xd/Kconfig"

source "drivers/sensor/stts751/Kconfig"

source "drivers/sensor/sx9500/Kconfig"

source "drivers/sensor/th02/Kconfig"
@@ -0,0 +1,11 @@
# ST Microelectronics STTS751 temperature sensor
#
# Copyright (c) 2019 STMicroelectronics
#
# SPDX-License-Identifier: Apache-2.0
#
zephyr_library()

zephyr_library_sources_ifdef(CONFIG_STTS751 stts751.c)
zephyr_library_sources_ifdef(CONFIG_STTS751 stts751_i2c.c)
zephyr_library_sources_ifdef(CONFIG_STTS751_TRIGGER stts751_trigger.c)
@@ -0,0 +1,92 @@
# ST Microelectronics STTS751 temperature sensor
#
# Copyright (c) 2019 STMicroelectronics
#
# SPDX-License-Identifier: Apache-2.0
#

menuconfig STTS751
bool "STTS751 temperature sensor"
depends on (I2C && HAS_DTS_I2C)
select HAS_STMEMSC
select USE_STDC_STTS751
help
Enable driver for STTS751 I2C-based temperature sensor.

if STTS751

choice STTS751_TRIGGER_MODE
prompt "Trigger mode"
default STTS751_TRIGGER_GLOBAL_THREAD
help
Specify the type of triggering to be used by the driver.

config STTS751_TRIGGER_NONE
bool "No trigger"

config STTS751_TRIGGER_GLOBAL_THREAD
bool "Use global thread"
depends on GPIO
select STTS751_TRIGGER

config STTS751_TRIGGER_OWN_THREAD
bool "Use own thread"
depends on GPIO
select STTS751_TRIGGER

endchoice # STTS751_TRIGGER_MODE

config STTS751_TRIGGER
bool

config STTS751_THREAD_PRIORITY
int "Thread priority"
depends on STTS751_TRIGGER_OWN_THREAD
default 10
help
Priority of thread used by the driver to handle interrupts.

config STTS751_THREAD_STACK_SIZE
int "Thread stack size"
depends on STTS751_TRIGGER_OWN_THREAD
default 1024
help
Stack size of thread used by the driver to handle interrupts.

menu "Attributes"

config STTS751_TEMP_HI_THRESHOLD
depends on STTS751_TRIGGER
int "High temperature threshold alarm"
default 50
help
HIGH temperature threshold to trigger an alarm

config STTS751_TEMP_LO_THRESHOLD
depends on STTS751_TRIGGER
int "Low temperature threshold alarm"
default 10
help
LOW temperature threshold to trigger an alarm

config STTS751_SAMPLING_RATE
int "Output data rate"
range 0 9
default 4
help
Sensor output data rate expressed in conversions per second.
Data rates supported by the chip are:
0: 1 conv every 16 sec
1: 1 conv every 8 sec
2: 1 conv every 4 sec
3: 1 conv every 2 sec
4: 1 conv every sec
5: 2 convs every sec
6: 4 convs every sec
7: 8 convs every sec
8: 16 convs every sec
9: 32 convs every sec

endmenu

endif # STTS751
@@ -0,0 +1,215 @@
/* ST Microelectronics STTS751 temperature sensor
*
* Copyright (c) 2019 STMicroelectronics
*
* SPDX-License-Identifier: Apache-2.0
*
* Datasheet:
* https://www.st.com/resource/en/datasheet/stts751.pdf
*/

#include <sensor.h>
#include <kernel.h>
#include <device.h>
#include <init.h>
#include <sys/byteorder.h>
#include <sys/__assert.h>
#include <logging/log.h>

#include "stts751.h"

#define LOG_LEVEL CONFIG_SENSOR_LOG_LEVEL
LOG_MODULE_REGISTER(STTS751);

static inline int stts751_set_odr_raw(struct device *dev, u8_t odr)
{
struct stts751_data *data = dev->driver_data;

return stts751_temp_data_rate_set(data->ctx, odr);
}

static int stts751_sample_fetch(struct device *dev,
enum sensor_channel chan)
{
struct stts751_data *data = dev->driver_data;
axis1bit16_t raw_temp;

__ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL);

if (stts751_temperature_raw_get(data->ctx, &raw_temp.i16bit) < 0) {
LOG_DBG("Failed to read sample");
return -EIO;
}

data->sample_temp = raw_temp.i16bit;

return 0;
}

static inline void stts751_temp_convert(struct sensor_value *val,
s16_t raw_val)
{
val->val1 = raw_val / 256;
val->val2 = ((s32_t)raw_val % 256) * 10000;
}

static int stts751_channel_get(struct device *dev,
enum sensor_channel chan,
struct sensor_value *val)
{
struct stts751_data *data = dev->driver_data;

if (chan == SENSOR_CHAN_AMBIENT_TEMP) {
stts751_temp_convert(val, data->sample_temp);
} else {
return -ENOTSUP;
}

return 0;
}

static const struct {
s32_t rate;
s32_t rate_dec;
} stts751_map[] = {
{0, 62500},
{0, 125000},
{0, 250000},
{0, 500000},
{1, 0},
{2, 0},
{4, 0},
{8, 0},
{16, 0},
{32, 0},
};

static int stts751_odr_set(struct device *dev,
const struct sensor_value *val)
{
int odr;

for (odr = 0; odr < ARRAY_SIZE(stts751_map); odr++) {
if (val->val1 == stts751_map[odr].rate &&
val->val2 == stts751_map[odr].rate_dec) {
break;
}
}

if (odr == ARRAY_SIZE(stts751_map)) {
LOG_DBG("bad frequency");
return -EINVAL;
}

if (stts751_set_odr_raw(dev, odr) < 0) {
LOG_DBG("failed to set sampling rate");
return -EIO;
}

return 0;
}

static int stts751_attr_set(struct device *dev, enum sensor_channel chan,
enum sensor_attribute attr,
const struct sensor_value *val)
{
if (chan != SENSOR_CHAN_ALL) {
LOG_WRN("attr_set() not supported on this channel.");
return -ENOTSUP;
}

switch (attr) {
case SENSOR_ATTR_SAMPLING_FREQUENCY:
return stts751_odr_set(dev, val);
default:
LOG_DBG("operation not supported.");
return -ENOTSUP;
}

return 0;
}

static const struct sensor_driver_api stts751_api_funcs = {
.attr_set = stts751_attr_set,
.sample_fetch = stts751_sample_fetch,
.channel_get = stts751_channel_get,
#if CONFIG_STTS751_TRIGGER
.trigger_set = stts751_trigger_set,
#endif
};

static int stts751_init_chip(struct device *dev)
{
struct stts751_data *data = dev->driver_data;
stts751_id_t chip_id;

if (stts751_device_id_get(data->ctx, &chip_id) < 0) {
LOG_DBG("Failed reading chip id");
return -EIO;
}

if (chip_id.manufacturer_id != STTS751_ID_MAN) {
LOG_DBG("Invalid chip id 0x%x", chip_id.manufacturer_id);
return -EIO;
}

if (stts751_set_odr_raw(dev, CONFIG_STTS751_SAMPLING_RATE) < 0) {
LOG_DBG("Failed to set sampling rate");
return -EIO;
}

if (stts751_resolution_set(data->ctx, STTS751_11bit) < 0) {
LOG_DBG("Failed to set resolution");
return -EIO;
}

return 0;
}

static int stts751_init(struct device *dev)
{
const struct stts751_config * const config = dev->config->config_info;
struct stts751_data *data = dev->driver_data;

data->bus = device_get_binding(config->master_dev_name);
if (!data->bus) {
LOG_DBG("bus master not found: %s", config->master_dev_name);
return -EINVAL;
}

config->bus_init(dev);

if (stts751_init_chip(dev) < 0) {
LOG_DBG("Failed to initialize chip");
return -EIO;
}

#ifdef CONFIG_STTS751_TRIGGER
if (stts751_init_interrupt(dev) < 0) {
LOG_ERR("Failed to initialize interrupt.");
return -EIO;
}
#endif

return 0;
}

static struct stts751_data stts751_data;

static const struct stts751_config stts751_config = {
.master_dev_name = DT_INST_0_ST_STTS751_BUS_NAME,
#ifdef CONFIG_STTS751_TRIGGER
.event_port = DT_INST_0_ST_STTS751_DRDY_GPIOS_CONTROLLER,
.event_pin = DT_INST_0_ST_STTS751_DRDY_GPIOS_PIN,
#endif
#if defined(DT_ST_STTS751_BUS_I2C)
.bus_init = stts751_i2c_init,
.i2c_slv_addr = DT_INST_0_ST_STTS751_BASE_ADDRESS,
#else
#error "BUS MACRO NOT DEFINED IN DTS"
#endif
};

DEVICE_AND_API_INIT(stts751, DT_INST_0_ST_STTS751_LABEL, stts751_init,
&stts751_data, &stts751_config, POST_KERNEL,
CONFIG_SENSOR_INIT_PRIORITY, &stts751_api_funcs);

0 comments on commit 9e5c97e

Please sign in to comment.
You can’t perform that action at this time.