Skip to content

Commit df59e89

Browse files
committed
feat(srv/shtc3): Add SHTC3 temperature and humidity sensor driver service
1 parent baf9732 commit df59e89

File tree

6 files changed

+176
-0
lines changed

6 files changed

+176
-0
lines changed

services/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ menu "High level device drivers"
235235

236236
source "services/iis2iclx/Kconfig"
237237
source "services/gpio-led/Kconfig"
238+
source "services/shtc3/Kconfig"
238239

239240
endmenu
240241

services/shtc3/Kconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
config SERVICE_SHTC3
2+
bool "SHTC3 temperature and humidity sensor driver"
3+
default n

services/shtc3/SConscript

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Import("env")
2+
Import("objs")
3+
Import("conf")
4+
5+
if conf["SERVICE_SHTC3"] == "y":
6+
objs.append(env.Object(File(Glob("*.c"))))
7+
env.Append(CPPPATH = [Dir(".")])

services/shtc3/index.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
==================================================================
2+
SHTC3 temperature and humidity sensor
3+
==================================================================
4+
5+

services/shtc3/shtc3.c

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
#include <stdint.h>
2+
#include <stdlib.h>
3+
#include <stdbool.h>
4+
5+
#include <main.h>
6+
7+
#include <interfaces/i2c-bus.h>
8+
#include <interfaces/waveform_source.h>
9+
10+
#include "shtc3.h"
11+
12+
//#define DEBUG
13+
#define MODULE_NAME "shtc3"
14+
15+
16+
static shtc3_ret_t shtc3_cmd(Shtc3 *self, uint16_t cmd, uint8_t *ret, size_t len) {
17+
uint8_t txbuf[2] = {cmd >> 8, cmd & 0xff};
18+
19+
if (self->i2c->vmt->transfer(self->i2c, SHTC3_ADDR, txbuf, sizeof(txbuf), ret, len) != I2C_BUS_RET_OK) {
20+
return SHTC3_RET_FAILED;
21+
}
22+
23+
return SHTC3_RET_OK;
24+
}
25+
26+
27+
/**********************************************************************************************************************
28+
* Temperature Sensor API
29+
**********************************************************************************************************************/
30+
31+
static sensor_ret_t temp_sensor_value_f(Sensor *sensor, float *value) {
32+
Shtc3 *self = sensor->parent;
33+
34+
uint8_t d[3] = {0};
35+
if (shtc3_cmd(self, 0x7ca2, d, sizeof(d)) != SHTC3_RET_OK) {
36+
return SENSOR_RET_FAILED;
37+
}
38+
39+
float temp = -45 + 175 * ((d[0] << 8) | d[1]) / 65536;
40+
//float rh = 100 * ((d[3] << 8) | d[4]) / 65536;
41+
42+
if (value != NULL) {
43+
*value = temp;
44+
return SENSOR_RET_OK;
45+
}
46+
47+
return SENSOR_RET_FAILED;
48+
}
49+
50+
51+
static const struct sensor_vmt temp_sensor_vmt = {
52+
.value_f = temp_sensor_value_f,
53+
};
54+
55+
56+
const struct sensor_info temp_sensor_info = {
57+
.description = "PCB temperature",
58+
.unit = "°C",
59+
};
60+
61+
62+
/**********************************************************************************************************************
63+
* Relative humidity Sensor API
64+
**********************************************************************************************************************/
65+
66+
static sensor_ret_t rh_sensor_value_f(Sensor *sensor, float *value) {
67+
Shtc3 *self = sensor->parent;
68+
69+
uint8_t d[3] = {0};
70+
if (shtc3_cmd(self, 0x5c24, d, sizeof(d)) != SHTC3_RET_OK) {
71+
return SENSOR_RET_FAILED;
72+
}
73+
74+
float rh = 100.0f * ((d[0] << 8) | d[1]) / 65536;
75+
76+
if (value != NULL) {
77+
*value = rh;
78+
return SENSOR_RET_OK;
79+
}
80+
81+
return SENSOR_RET_FAILED;
82+
}
83+
84+
85+
static const struct sensor_vmt rh_sensor_vmt = {
86+
.value_f = rh_sensor_value_f,
87+
};
88+
89+
90+
const struct sensor_info rh_sensor_info = {
91+
.description = "PCB humidity",
92+
.unit = "%Rh",
93+
};
94+
95+
96+
97+
shtc3_ret_t shtc3_init(Shtc3 *self, I2cBus *i2c) {
98+
memset(self, 0, sizeof(Shtc3));
99+
self->i2c = i2c;
100+
101+
if (shtc3_cmd(self, 0xefc8, self->id, sizeof(self->id)) != SHTC3_RET_OK) {
102+
u_log(system_log, LOG_TYPE_ERROR, U_LOG_MODULE_PREFIX("SHTC3 sensor not found"));
103+
return SHTC3_RET_FAILED;
104+
}
105+
106+
if (!(((self->id[1] & 0x3f) == 0x07) && (self->id[0] & 0x08))) {
107+
u_log(system_log, LOG_TYPE_ERROR, U_LOG_MODULE_PREFIX("SHTC3 sensor probe failed (wrong ID)"));
108+
return SHTC3_RET_FAILED;
109+
}
110+
111+
self->temp.parent = self;
112+
self->temp.vmt = &temp_sensor_vmt;
113+
self->temp.info = &temp_sensor_info;
114+
115+
self->rh.parent = self;
116+
self->rh.vmt = &rh_sensor_vmt;
117+
self->rh.info = &rh_sensor_info;
118+
119+
u_log(system_log, LOG_TYPE_INFO, U_LOG_MODULE_PREFIX("initialized, id = 0x%02x, 0x%02x, 0x%02x"), self->id[0], self->id[1], self->id[2]);
120+
121+
return SHTC3_RET_OK;
122+
}
123+
124+
125+
shtc3_ret_t shtc3_free(Shtc3 *self) {
126+
(void)self;
127+
128+
return SHTC3_RET_OK;
129+
}
130+
131+
132+

services/shtc3/shtc3.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#pragma once
2+
3+
#include <stdint.h>
4+
#include <stdbool.h>
5+
#include <waveform_source.h>
6+
7+
#include <interfaces/i2c-bus.h>
8+
#include <interfaces/sensor.h>
9+
10+
#define SHTC3_ADDR 0x70
11+
12+
13+
typedef enum {
14+
SHTC3_RET_OK = 0,
15+
SHTC3_RET_FAILED = -1,
16+
} shtc3_ret_t;
17+
18+
typedef struct {
19+
I2cBus *i2c;
20+
Sensor temp;
21+
Sensor rh;
22+
uint8_t id[3];
23+
} Shtc3;
24+
25+
26+
shtc3_ret_t shtc3_init(Shtc3 *self, I2cBus *i2c);
27+
shtc3_ret_t shtc3_free(Shtc3 *self);
28+

0 commit comments

Comments
 (0)