From 7f85861d6c9e1a95b6c87cfdcc4949e1d6be931f Mon Sep 17 00:00:00 2001 From: Edward Emelianov Date: Sat, 11 Oct 2025 21:10:55 +0300 Subject: [PATCH] Add SI7005 --- I2Csensors/BMP180.c | 7 +- I2Csensors/PCA9548 | 16 +++++ I2Csensors/SI7005.c | 154 ++++++++++++++++++++++++++++++++++++++++ I2Csensors/SI7005.h | 21 ++++++ I2Csensors/aht.c | 64 ++++++++++++++--- I2Csensors/aht.h | 1 + I2Csensors/main.c | 12 ++++ I2Csensors/sensor.c | 7 +- I2Csensors/sensor.files | 2 + I2Csensors/sensor.h | 10 +-- 10 files changed, 276 insertions(+), 18 deletions(-) create mode 100755 I2Csensors/PCA9548 create mode 100644 I2Csensors/SI7005.c create mode 100644 I2Csensors/SI7005.h diff --git a/I2Csensors/BMP180.c b/I2Csensors/BMP180.c index ba00077..4bc78b4 100644 --- a/I2Csensors/BMP180.c +++ b/I2Csensors/BMP180.c @@ -254,7 +254,7 @@ static int BMP180_getdata(sensor_data_t *d){ } static sensor_props_t BMP180_props(){ - sensor_props_t p = {.T = 1, .H = 0, .P = 1}; + sensor_props_t p = {.T = 1, .P = 1}; return p; } @@ -263,11 +263,16 @@ static uint8_t address(uint8_t new){ return addr; } +static int s_heater(int _U_ on){ + return FALSE; +} + sensor_t BMP180 = { .name = "BMP180", .address = address, .init = BMP180_init, .start = BMP180_start, + .heater = s_heater, .process = BMP180_process, .properties = BMP180_props, .get_data = BMP180_getdata diff --git a/I2Csensors/PCA9548 b/I2Csensors/PCA9548 new file mode 100755 index 0000000..e68744a --- /dev/null +++ b/I2Csensors/PCA9548 @@ -0,0 +1,16 @@ +#!/bin/bash + +if [[ $# != 1 || $((-(-$1))) != $1 ]]; then + echo "Usage: $0 cnannel" >&2 + exit 1 +fi + +if [[ $1 < 0 || $1 > 7 ]]; then + echo "Channel number should be from 0 to 7" >&2 + exit 1 +fi + +echo $((1 << $1)) + +i2ctransfer -y 6 w1@0x70 $((1 << $1)) +i2cdetect -y -r 6 \ No newline at end of file diff --git a/I2Csensors/SI7005.c b/I2Csensors/SI7005.c new file mode 100644 index 0000000..6f323ce --- /dev/null +++ b/I2Csensors/SI7005.c @@ -0,0 +1,154 @@ +/* + * Copyright 2025 Edward V. Emelianov . + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include + +#include "i2c.h" +#include "SI7005.h" + +static uint8_t addr = 0x40; +static double Tmeasured, Hmeasured; +static sensor_status_t status = SENS_NOTINIT; + +#define SI7005_REGSTATUS 0 +#define SI7005_STATUSNRDY 1 +#define SI7005_REGDATA 1 +#define SI7005_REGCONFIG 3 +#define SI7005_CONFFAST (1<<5) +#define SI7005_CONFTEMP (1<<4) +#define SI7005_CONFHEAT (1<<1) +#define SI7005_CONFSTART (1<<0) +#define SI7005_REGID 0x11 + +#define SI7005_ID 0x50 + +static int s_init(){ + uint8_t ID; + status = SENS_NOTINIT; + if(!i2c_read_reg8(SI7005_REGID, &ID)){ + DBG("Can't read SI_REG_ID"); + return FALSE; + } + DBG("SI, device ID: 0x%02x", ID); + if(ID != SI7005_ID){ + DBG("Not SI7005\n"); + return FALSE; + } + status = SENS_RELAX; + return TRUE; +} + +static int s_start(){ + if(status != SENS_RELAX) return FALSE; + status = SENS_BUSY; + if(!i2c_write_reg8(SI7005_REGCONFIG, SI7005_CONFTEMP | SI7005_CONFSTART)){ + DBG("Can't write start Tmeas"); + status = SENS_ERR; + return FALSE; + } + DBG("Wait for T\n"); + return TRUE; +} + +// start humidity measurement +static sensor_status_t si7005_cmdH(){ + status = SENS_BUSY; + if(!i2c_write_reg8(SI7005_REGCONFIG, SI7005_CONFSTART)){ + DBG("Can't write start Hmeas"); + return (status = SENS_ERR); + } + DBG("Wait for H"); + return status; +} + +static sensor_status_t s_process(){ + uint8_t c, d[3]; + if(status != SENS_BUSY) return status; + if(!i2c_read_raw(d, 3)){ + DBG("Can't read status"); + return (status = SENS_ERR); + } + //DBG("Status: 0x%02x, H: 0x%02x, L: 0x%02x", d[0], d[1], d[2]); + if(!i2c_read_reg8(SI7005_REGCONFIG, &c)){ + DBG("Can't read config"); + return (status = SENS_ERR); + } + //DBG("Config: 0x%02x", c); + if(d[0] & SI7005_STATUSNRDY){ // not ready yet + return status; + } + uint16_t TH = (uint16_t)((d[1]<<8) | d[2]); + if(c & SI7005_CONFTEMP){ // temperature measured + TH >>= 2; + Tmeasured = TH/32. - 50.; + DBG("T=%.2f", Tmeasured); + return si7005_cmdH(); + }else{ // humidity measured + TH >>= 4; + Hmeasured = TH/16.f - 24.f; + DBG("H=%.1f", Hmeasured); + status = SENS_RDY; + } + return status; +} + +static int s_getdata(sensor_data_t *d){ + if(!d || status != SENS_RDY) return FALSE; + DBG("Measured T=%.1f, H=%.1f", Tmeasured, Hmeasured); + // correct T/H +#define A0 (-4.7844) +#define A1 (0.4008) +#define A2 (-0.00393) + d->H = Hmeasured - (A2*Hmeasured*Hmeasured + A1*Hmeasured + A0); + d->T = Tmeasured; + status = SENS_RELAX; + return TRUE; +} + +// turn heater on/off (1/0) +static int s_heater(int on){ + DBG("status=%d", status); + if(status != SENS_RELAX) return FALSE; + uint8_t reg = (on) ? SI7005_CONFHEAT : 0; + if(!i2c_write_reg8(SI7005_REGCONFIG, reg)){ + DBG("Can't write regconfig"); + return FALSE; + } + return TRUE; +} + +static sensor_props_t s_props(){ + sensor_props_t p = {.T = 1, .H = 1, .htr = 1}; + return p; +} + +static uint8_t address(uint8_t new){ + if(new) addr = new; + return addr; +} + +sensor_t SI7005 = { + .name = "SI7005", + .address = address, + .init = s_init, + .start = s_start, + .heater = s_heater, + .process = s_process, + .properties = s_props, + .get_data = s_getdata +}; diff --git a/I2Csensors/SI7005.h b/I2Csensors/SI7005.h new file mode 100644 index 0000000..a1869c5 --- /dev/null +++ b/I2Csensors/SI7005.h @@ -0,0 +1,21 @@ +/* + * Copyright 2025 Edward V. Emelianov . + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once +#include "sensor.h" + +extern sensor_t SI7005; diff --git a/I2Csensors/aht.c b/I2Csensors/aht.c index 022a2f4..0624437 100644 --- a/I2Csensors/aht.c +++ b/I2Csensors/aht.c @@ -36,6 +36,23 @@ static uint32_t rawH = 0, rawT = 0; #define AHT_CMD_INITIALIZE 0xE1 #define AHT_CMD_MEASURE 0xAC #define AHT_CMD_SOFT_RESET 0xBA +// max reset time +#define RST_TIME (20e-3) +// max data waiting time +#define DATA_TIME (75e-3) + +static sensor_status_t s_poll(){ + uint8_t b; + if(!i2c_read_raw(&b, 1)) return SENS_ERR; +#ifdef EBUG + if(b & 0x80) printf("BUSY "); + static const char *modes[] = {"NOR", "CYC", "CMD", "CMD"}; + printf("MODE=%s ", modes[(b >> 6)&3]); + printf("%sCALIBRATED\n", b & 8 ? "" : "NOT "); +#endif + if(b & 0x80) return SENS_BUSY; + return SENS_RELAX; +} static int s_init(){ status = SENS_NOTINIT; @@ -43,11 +60,25 @@ static int s_init(){ DBG("Can't reset"); return FALSE; } + double t0 = sl_dtime(), t; + while((t = sl_dtime()) - t0 < RST_TIME){ + if(SENS_RELAX == s_poll()) break; + usleep(1000); + } + if(t - t0 > RST_TIME) return SENS_ERR; + DBG("Reseted"); uint8_t data[3] = {AHT_CMD_INITIALIZE, 0x08, 0}; if(!i2c_write_raw(data, 3)){ DBG("Can't init"); return FALSE; } + t0 = sl_dtime(); + while((t = sl_dtime()) - t0 < RST_TIME){ + if(SENS_RELAX == s_poll()) break; + usleep(1000); + } + if(t - t0 > RST_TIME) return SENS_ERR; + DBG("Inited"); status = SENS_RELAX; return TRUE; } @@ -59,21 +90,16 @@ static int s_start(){ DBG("Can't start measuring"); return FALSE; } + DBG("Start @ %.3f", sl_dtime()); return TRUE; } static sensor_status_t s_process(){ + sensor_status_t s = s_poll(); + if(s != SENS_RELAX) return (status = s); uint8_t data[6]; - //if(!i2c_read_data8(0, 6, data)) return (status = SENS_ERR); if(!i2c_read_raw(data, 6)) return (status = SENS_ERR); -#ifdef EBUG - printf("Got data: "); for(int i = 0; i < 6; ++i) printf(" %02X", data[i]); printf("\n"); - if(data[0] & 0x80) printf("BUSY "); - static const char *modes[] = {"NOR", "CYC", "CMD", "CMD"}; - printf("MODE=%s ", modes[(data[0] >> 6)&3]); - printf("%sCALIBRATED\n", data[0] & 8 ? "" : "NOT "); -#endif - if(data[0] & 0x80) return (status = SENS_BUSY); // still measuring + DBG("Got @ %.3f", sl_dtime()); rawH = ((uint32_t)data[1] << 12) | ((uint32_t)data[2] << 4) | (data[3] >> 4); rawT = ((uint32_t)(data[3] & 0x0F) << 16) | ((uint32_t)data[4] << 8) | data[5]; DBG("rawH=%d, rawT=%d", rawH, rawT); @@ -84,11 +110,12 @@ static int s_getdata(sensor_data_t *d){ if(!d || status != SENS_RDY) return FALSE; d->T = rawT * 200.0 / 1048576.0 - 50.0; d->H = rawH * 100.0 / 1048576.0; + status = SENS_RELAX; return TRUE; } static sensor_props_t s_props(){ - sensor_props_t p = {.T = 1, .H = 1, .P = 0}; + sensor_props_t p = {.T = 1, .H = 1}; return p; } @@ -97,12 +124,29 @@ static uint8_t address(uint8_t new){ return addr; } +static int s_heater(int _U_ on){ + return FALSE; +} + sensor_t AHT10 = { .name = "AHT10", .private = ISAHT10, .address = address, .init = s_init, .start = s_start, + .heater = s_heater, + .process = s_process, + .properties = s_props, + .get_data = s_getdata +}; + +sensor_t AHT15 = { + .name = "AHT15", + .private = ISAHT15, + .address = address, + .init = s_init, + .start = s_start, + .heater = s_heater, .process = s_process, .properties = s_props, .get_data = s_getdata diff --git a/I2Csensors/aht.h b/I2Csensors/aht.h index a16d57b..117e477 100644 --- a/I2Csensors/aht.h +++ b/I2Csensors/aht.h @@ -20,3 +20,4 @@ #include "sensor.h" extern sensor_t AHT10; +extern sensor_t AHT15; diff --git a/I2Csensors/main.c b/I2Csensors/main.c index eb242ab..389cb35 100644 --- a/I2Csensors/main.c +++ b/I2Csensors/main.c @@ -28,10 +28,12 @@ typedef struct{ int list; int presmm; // pressure in mm instead of hPa int help; + int heater; // turn on/off heater (if present) } glob_pars; static glob_pars G = { .device = "/dev/i2c-6", + .heater = -1, }; static sl_option_t cmdlnopts[] = { @@ -41,6 +43,7 @@ static sl_option_t cmdlnopts[] = { {"sensor", NEED_ARG, NULL, 's', arg_string, APTR(&G.sensor), "sensor's name"}, {"list", NO_ARGS, NULL, 'l', arg_int, APTR(&G.list), "list all supported sensors"}, {"presmm", NO_ARGS, NULL, 'm', arg_int, APTR(&G.presmm), "pressure in mmHg instead of hPa"}, + {"heater", NEED_ARG, NULL, 'H', arg_int, APTR(&G.heater), "turn on/off heater (if present)"}, end_option }; @@ -86,6 +89,14 @@ int main(int argc, char **argv){ if(!sensors_open(G.device)) ERR("Can't open %s", G.device); const sensor_t* s = sensor_find(G.sensor); if(!s){ WARNX("Can't find sensor `%s` in supported list", G.sensor); goto clo; } + if(G.heater > -1){ + if(s->properties().htr && s->heater){ + if(!sensor_init(s, G.slaveaddr)) ERRX("Can't init device"); + if(!s->heater(G.heater)) WARNX("Cant run heater command"); + else green("Heater is %s\n", G.heater ? "on" : "off"); + }else ERRX("The sensor have no heater"); + return 0; + } if(!start(s, G.slaveaddr)) goto clo; while(1){ sensor_status_t status = s->process(); @@ -96,6 +107,7 @@ int main(int argc, char **argv){ WARNX("Error in measurement, try again"); if(!start(s, G.slaveaddr)) break; } + usleep(10000); } clo: diff --git a/I2Csensors/sensor.c b/I2Csensors/sensor.c index 2e23348..4b73a4a 100644 --- a/I2Csensors/sensor.c +++ b/I2Csensors/sensor.c @@ -23,9 +23,10 @@ #include "BMP180.h" #include "i2c.h" #include "sensor.h" +#include "SI7005.h" // NULL-terminated list of all supported sensors -static const sensor_t* supported_sensors[] = {&AHT10, &BMP180, NULL}; +static const sensor_t* supported_sensors[] = {&AHT10, &AHT15, &BMP180, &SI7005, NULL}; // just two stupid wrappers int sensors_open(const char *dev){ @@ -49,7 +50,7 @@ int sensor_init(const sensor_t *s, uint8_t address){ } double t0 = sl_dtime(); int result = FALSE; - while(sl_dtime() - t0 < SENS_TIMEOUT && !(result = s->init())) usleep(10000); + while(sl_dtime() - t0 < I2C_TIMEOUT && !(result = s->init())) usleep(10000); return result; } @@ -80,6 +81,6 @@ int sensor_start(const sensor_t *s){ if(!s) return FALSE; double t0 = sl_dtime(); int result = FALSE; - while(sl_dtime() - t0 < SENS_TIMEOUT && !(result = s->start())) usleep(10000); + while(sl_dtime() - t0 < I2C_TIMEOUT && !(result = s->start())) usleep(10000); return result; } diff --git a/I2Csensors/sensor.files b/I2Csensors/sensor.files index 953f39b..6fb4f88 100644 --- a/I2Csensors/sensor.files +++ b/I2Csensors/sensor.files @@ -1,5 +1,7 @@ BMP180.c BMP180.h +SI7005.c +SI7005.h aht.c aht.h i2c.c diff --git a/I2Csensors/sensor.h b/I2Csensors/sensor.h index 78b0cec..d65c157 100644 --- a/I2Csensors/sensor.h +++ b/I2Csensors/sensor.h @@ -20,7 +20,7 @@ #include // timeout of i2c waiting -#define SENS_TIMEOUT (2.) +#define I2C_TIMEOUT (2.) typedef enum{ SENS_NOTINIT, // wasn't inited @@ -31,9 +31,10 @@ typedef enum{ } sensor_status_t; typedef struct{ - uint8_t T : 1; // can temperature (degC) - uint8_t H : 1; // can humidity (percent) - uint8_t P : 1; // can pressure (hPa) + uint8_t T : 1; // can temperature (degC) + uint8_t H : 1; // can humidity (percent) + uint8_t P : 1; // can pressure (hPa) + uint8_t htr : 1; // have heater } sensor_props_t; typedef struct{ @@ -48,6 +49,7 @@ typedef struct{ uint8_t (*address)(uint8_t new);// set/get sensor's address (get - if `new`==0) int (*init)(); // init device - only @ start after POR int (*start)(); // start measuring + int (*heater)(int); // turn heater on/off (1/0) sensor_status_t (*process)(); // main polling process sensor_props_t (*properties)(); // get properties int (*get_data)(sensor_data_t*);// read data