Skip to content

Commit 8844856

Browse files
Iosif MacesanuMaureenHelm
authored andcommitted
drivers: sensor: Add SI7060 temperature sensor driver
Add SI7060 temperature sensor driver Signed-off-by: Iosif Macesanu <[email protected]>
1 parent 18d236f commit 8844856

File tree

9 files changed

+191
-0
lines changed

9 files changed

+191
-0
lines changed

drivers/sensor/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ add_subdirectory_ifdef(CONFIG_QDEC_NRFX qdec_nrfx)
5050
add_subdirectory_ifdef(CONFIG_TEMP_NRF5 nrf5)
5151
add_subdirectory_ifdef(CONFIG_SHT3XD sht3xd)
5252
add_subdirectory_ifdef(CONFIG_SI7006 si7006)
53+
add_subdirectory_ifdef(CONFIG_SI7060 si7060)
5354
add_subdirectory_ifdef(CONFIG_STTS751 stts751)
5455
add_subdirectory_ifdef(CONFIG_SX9500 sx9500)
5556
add_subdirectory_ifdef(CONFIG_TH02 th02)

drivers/sensor/Kconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ source "drivers/sensor/sht3xd/Kconfig"
125125

126126
source "drivers/sensor/si7006/Kconfig"
127127

128+
source "drivers/sensor/si7060/Kconfig"
129+
128130
source "drivers/sensor/stts751/Kconfig"
129131

130132
source "drivers/sensor/sx9500/Kconfig"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
zephyr_library()
4+
5+
zephyr_library_sources_ifdef(CONFIG_SI7060 si7060.c)

drivers/sensor/si7060/Kconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Kconfig - SI7060 temperature sensor configuration options #
2+
#
3+
# Copyright (c) 2019 Actinius
4+
#
5+
# SPDX-License-Identifier: Apache-2.0
6+
#
7+
8+
menuconfig SI7060
9+
bool "SI7060 Temperature Sensor"
10+
depends on (I2C && HAS_DTS_I2C)
11+
help
12+
Enable driver for SI7060 temperature sensors.

drivers/sensor/si7060/si7060.c

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/*
2+
* Copyright (c) 2019 Actinius
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <device.h>
8+
#include <drivers/i2c.h>
9+
#include <drivers/sensor.h>
10+
#include <logging/log.h>
11+
12+
#include "si7060.h"
13+
14+
#define SIGN_BIT_MASK 0x7F
15+
16+
LOG_MODULE_REGISTER(si7060, CONFIG_SENSOR_LOG_LEVEL);
17+
18+
struct si7060_data {
19+
struct device *i2c_dev;
20+
u16_t temperature;
21+
};
22+
23+
static int si7060_reg_read(struct si7060_data *drv_data, u8_t reg,
24+
u8_t *val)
25+
{
26+
if (i2c_reg_read_byte(drv_data->i2c_dev,
27+
DT_INST_0_SILABS_SI7060_BASE_ADDRESS, reg, val)) {
28+
return -EIO;
29+
}
30+
31+
return 0;
32+
}
33+
34+
static int si7060_reg_write(struct si7060_data *drv_data, u8_t reg,
35+
u8_t val)
36+
{
37+
return i2c_reg_write_byte(drv_data->i2c_dev,
38+
DT_INST_0_SILABS_SI7060_BASE_ADDRESS, reg, val);
39+
}
40+
41+
static int si7060_sample_fetch(struct device *dev, enum sensor_channel chan)
42+
{
43+
struct si7060_data *drv_data = dev->driver_data;
44+
45+
if (si7060_reg_write(drv_data, SI7060_REG_CONFIG,
46+
SI7060_ONE_BURST_VALUE) != 0) {
47+
return -EIO;
48+
}
49+
50+
int retval;
51+
u8_t dspsigm;
52+
u8_t dspsigl;
53+
54+
retval = si7060_reg_read(drv_data, SI7060_REG_TEMP_HIGH,
55+
&dspsigm);
56+
retval += si7060_reg_read(drv_data, SI7060_REG_TEMP_LOW,
57+
&dspsigl);
58+
59+
if (retval == 0) {
60+
drv_data->temperature = (256 * (dspsigm & SIGN_BIT_MASK))
61+
+ dspsigl;
62+
} else {
63+
LOG_ERR("Read register err");
64+
}
65+
66+
LOG_DBG("Sample_fetch retval: %d", retval);
67+
68+
return retval;
69+
}
70+
71+
static int si7060_channel_get(struct device *dev, enum sensor_channel chan,
72+
struct sensor_value *val)
73+
{
74+
struct si7060_data *drv_data = dev->driver_data;
75+
s32_t uval;
76+
77+
if (chan == SENSOR_CHAN_AMBIENT_TEMP) {
78+
uval = ((55 * 160) + (drv_data->temperature - 16384)) >> 4;
79+
val->val1 = uval / 10;
80+
val->val2 = (uval % 10) * 100000;
81+
82+
LOG_DBG("Temperature = val1:%d, val2:%d", val->val1, val->val2);
83+
84+
return 0;
85+
} else {
86+
return -ENOTSUP;
87+
}
88+
}
89+
90+
static const struct sensor_driver_api si7060_api = {
91+
.sample_fetch = &si7060_sample_fetch,
92+
.channel_get = &si7060_channel_get,
93+
};
94+
95+
static int si7060_chip_init(struct device *dev)
96+
{
97+
struct si7060_data *drv_data = dev->driver_data;
98+
u8_t value;
99+
100+
drv_data->i2c_dev = device_get_binding(
101+
DT_INST_0_SILABS_SI7060_BUS_NAME);
102+
103+
if (!drv_data->i2c_dev) {
104+
LOG_ERR("Failed to get pointer to %s device!",
105+
DT_INST_0_SILABS_SI7060_BUS_NAME);
106+
return -EINVAL;
107+
}
108+
109+
if (si7060_reg_read(drv_data, SI7060_REG_CHIP_INFO,
110+
&value) != 0) {
111+
return -EIO;
112+
}
113+
114+
if ((value >> 4) != SI7060_CHIP_ID_VALUE) {
115+
LOG_ERR("Bad chip id 0x%x", value);
116+
return -ENOTSUP;
117+
}
118+
119+
return 0;
120+
}
121+
122+
static int si7060_init(struct device *dev)
123+
{
124+
if (si7060_chip_init(dev) < 0) {
125+
return -EINVAL;
126+
}
127+
128+
return 0;
129+
}
130+
131+
static struct si7060_data si_data;
132+
133+
DEVICE_AND_API_INIT(si7060, DT_INST_0_SILABS_SI7060_LABEL, si7060_init,
134+
&si_data, NULL, POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, &si7060_api);

drivers/sensor/si7060/si7060.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright (c) 2019 Actinius
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef _SI7060_H
8+
#define _SI7060_H
9+
10+
/* Si7060 register addresses */
11+
#define SI7060_REG_CHIP_INFO 0xC0
12+
#define SI7060_REG_TEMP_HIGH 0xC1
13+
#define SI7060_REG_TEMP_LOW 0xC2
14+
#define SI7060_REG_CONFIG 0xC4
15+
16+
#define SI7060_CHIP_ID_VALUE 0x01
17+
#define SI7060_ONE_BURST_VALUE 0x04
18+
19+
#endif /* _SI7060_H */
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Copyright (c) 2019, Actinius
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
title: Si7060 Temperature sensor
5+
6+
description: >
7+
This is a representation of Si7060 Temperature sensor
8+
9+
compatible: "silabs,si7060"
10+
11+
include: i2c-device.yaml

tests/drivers/build_all/dts_fixup.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,12 @@
180180
#define DT_INST_0_SILABS_SI7006_BUS_NAME ""
181181
#endif
182182

183+
#ifndef DT_INST_0_SILABS_SI7060_LABEL
184+
#define DT_INST_0_SILABS_SI7060_LABEL ""
185+
#define DT_INST_0_SILABS_SI7060_BASE_ADDRESS 0x00
186+
#define DT_INST_0_SILABS_SI7060_BUS_NAME ""
187+
#endif
188+
183189
#ifndef DT_INST_0_ST_LIS2DS12_LABEL
184190
#define DT_INST_0_ST_LIS2DS12_LABEL ""
185191
#define DT_INST_0_ST_LIS2DS12_BUS_NAME ""

tests/drivers/build_all/sensors_i_z.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ CONFIG_MCP9808=y
2626
CONFIG_MPU6050=y
2727
CONFIG_SHT3XD=y
2828
CONFIG_SI7006=y
29+
CONFIG_SI7060=y
2930
CONFIG_SX9500=y
3031
CONFIG_TEMP_NRF5=y
3132
CONFIG_TH02=y

0 commit comments

Comments
 (0)