add si/htu

This commit is contained in:
Edward Emelianov 2022-09-29 22:41:13 +03:00
parent e1ad685d4b
commit d5ba933c30
16 changed files with 1085 additions and 0 deletions

57
SI7005_HTU21D/Makefile Normal file
View File

@ -0,0 +1,57 @@
# run `make DEF=...` to add extra defines
PROGRAM := sihtu
LDFLAGS := -fdata-sections -ffunction-sections -Wl,--gc-sections -Wl,--discard-all
LDFLAGS += -lusefull_macros -lm
SRCS := $(wildcard *.c)
DEFINES := $(DEF) -D_GNU_SOURCE -D_XOPEN_SOURCE=1111
OBJDIR := mk
CFLAGS += -O2 -Wall -Wextra -Wno-trampolines -std=gnu99
OBJS := $(addprefix $(OBJDIR)/, $(SRCS:%.c=%.o))
DEPS := $(OBJS:.o=.d)
TARGFILE := $(OBJDIR)/TARGET
CC = gcc
#TARGET := RELEASE
ifeq ($(shell test -e $(TARGFILE) && echo -n yes),yes)
TARGET := $(file < $(TARGFILE))
else
TARGET := RELEASE
endif
ifeq ($(TARGET), DEBUG)
.DEFAULT_GOAL := debug
endif
release: $(PROGRAM)
debug: CFLAGS += -DEBUG -Werror
debug: TARGET := DEBUG
debug: $(PROGRAM)
$(TARGFILE): $(OBJDIR)
@echo -e "\t\tTARGET: $(TARGET)"
@echo "$(TARGET)" > $(TARGFILE)
$(PROGRAM) : $(TARGFILE) $(OBJS)
@echo -e "\t\tLD $(PROGRAM)"
$(CC) $(OBJS) $(LDFLAGS) -o $(PROGRAM)
$(OBJDIR):
@mkdir $(OBJDIR)
ifneq ($(MAKECMDGOALS),clean)
-include $(DEPS)
endif
$(OBJDIR)/%.o: %.c
@echo -e "\t\tCC $<"
$(CC) $< -MD -c $(LDFLAGS) $(CFLAGS) $(DEFINES) -o $@
clean:
@echo -e "\t\tCLEAN"
@rm -rf $(OBJDIR) 2>/dev/null || true
xclean: clean
@rm -f $(PROGRAM)
.PHONY: clean xclean

2
SI7005_HTU21D/Readme Normal file
View File

@ -0,0 +1,2 @@
Code to work with I2C T/RH sensors SI7005 or HTU21D (can't work together due to identical
addresses)

166
SI7005_HTU21D/htu21d.c Normal file
View File

@ -0,0 +1,166 @@
/*
* This file is part of the sihtu project.
* Copyright 2022 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <usefull_macros.h>
#include "htu21d.h"
#include "i2c.h"
// read with no hold master!
// REGISTERS/COMANDS:
#define HTU21_READ_TEMP (0xF3)
#define HTU21_READ_HUMID (0xF5)
#define HTU21_READ_USERREG (0xE7)
#define HTU21_WRITE_USERREG (0xE6)
#define HTU21_SOFT_RESET (0xFE)
// status mask & val
#define HTU21_STATUS_MASK (0x03)
#define HTU21_HUMID_FLAG (0x02)
// user reg fields
#define HTU21_REG_VBAT (0x40)
#define HTU21_REG_D1 (0x80)
#define HTU21_REG_D0 (0x01)
#define HTU21_REG_HTR (0x04)
#define HTU21_REG_ODIS (0x02)
#define HTU21_REG_VBAT (0x40)
#define HTU21_REG_DEFVAL (0x02)
static HTU21D_status htustatus = HTU21D_RELAX;
static float Tmeasured, Hmeasured;
static double lastw = 0.; // last time of measurements start
HTU21D_status HTU21D_get_status(){
return htustatus;
}
static int writecmd(uint8_t cmd){
if(1 != write(i2c_getfd(), &cmd, 1)) return FALSE;
return TRUE;
}
/**
* @brief HTU21D_read_ID - read user register and compare with default
* @return TRUE if all OK
*/
int HTU21D_read_ID(){
if(htustatus != HTU21D_RELAX) return FALSE;
uint8_t ID;
if(!i2c_read_reg8(HTU21_READ_USERREG, &ID)){
DBG("Can't read HTU_REG_ID");
return FALSE;
}
DBG("HTU, reg: 0x%02x", ID);
if(ID != HTU21_REG_DEFVAL){
DBG("Not HTU21D or need reloading\n");
writecmd(HTU21_SOFT_RESET);
return FALSE;
}
return TRUE;
}
int HTU21D_startmeasure(){
htustatus = HTU21D_BUSY;
if(!writecmd(HTU21_READ_TEMP)){
htustatus = HTU21D_ERR;
return FALSE;
}
DBG("Wait for T\n");
lastw = dtime();
return TRUE;
}
static void HTU21D_cmdH(){
htustatus = HTU21D_BUSY;
if(!writecmd(HTU21_READ_HUMID)){
htustatus = HTU21D_ERR;
return;
}
DBG("Wait for H, dt=%g", dtime() - lastw);
lastw = dtime();
}
int HTU21D_getTH(float *T, float *H){
if(htustatus != HTU21D_RDY) return FALSE;
if(T) *T = Tmeasured;
if(H) *H = Hmeasured;
htustatus = HTU21D_RELAX;
return TRUE;
}
#define SHIFTED_DIVISOR 0x988000 //This is the 0x0131 polynomial shifted to farthest left of three bytes
// check CRC, return 0 if all OK
static uint32_t htu_check_crc(uint8_t *crc){
DBG("HTU check CRC\n");
uint32_t remainder = (crc[0] << 16) | (crc[1] << 8) | crc[2];
uint32_t divsor = (uint32_t)SHIFTED_DIVISOR;
int i;
for(i = 0; i < 16; i++) {
if (remainder & (uint32_t)1 << (23 - i))
remainder ^= divsor;
divsor >>= 1;
}
return remainder;
}
void HTU21D_process(){
uint8_t d[3];
if(htustatus != HTU21D_BUSY) return;
if(3 != read(i2c_getfd(), d, 3)){ // NACK'ed - not ready
if(dtime() - lastw > HTU21D_CONVTIMEOUT){
DBG("Wait too long -> err");
htustatus = HTU21D_ERR;
return;
}
return;
}
DBG("Got: 0x%02x, 0x%02x, 0x%02x", d[0], d[1], d[2]);
if(htu_check_crc(d)){
htustatus = HTU21D_ERR;
DBG("CRC failed\n");
return;
}
uint16_t TH = (uint16_t)((d[0]<<8) | d[1]);;
if(!(TH & HTU21_HUMID_FLAG)){ // temperature measured
Tmeasured = -46.85 + 175.72*(TH & 0xFFFC)/65536.;
DBG("T=%.1f", Tmeasured);
HTU21D_cmdH();
}else{ // humidity measured
Hmeasured = -6.f + 125.f*(TH & 0xFFFC)/65536.;
DBG("H=%.1f", Hmeasured);
htustatus = HTU21D_RDY;
}
}
int HTU21D_heater(int ON){
if(htustatus != HTU21D_RELAX) return FALSE;
uint8_t val;
if(!i2c_read_reg8(HTU21_READ_USERREG, &val)){
DBG("Can't read userreg");
return FALSE;
}
DBG("REG: 0x%02x", val);
if(ON) val |= HTU21_REG_HTR;
else val &= ~HTU21_REG_HTR;
DBG("REG -> 0x%02x", val);
if(!i2c_write_reg8(HTU21_WRITE_USERREG, val)){
DBG("Can't write write userreg");
return FALSE;
}
return TRUE;
}

38
SI7005_HTU21D/htu21d.h Normal file
View File

@ -0,0 +1,38 @@
/*
* This file is part of the sihtu project.
* Copyright 2022 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* 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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <stdint.h>
#define HTU21D_CONVTIMEOUT (2.0)
typedef enum{
HTU21D_BUSY, // measurement in progress
HTU21D_ERR, // error in I2C
HTU21D_RELAX, // relaxed state
HTU21D_RDY, // data ready - can get it
} HTU21D_status;
HTU21D_status HTU21D_get_status();
int HTU21D_read_ID();
void HTU21D_process();
int HTU21D_startmeasure();
int HTU21D_getTH(float *T, float *H);
int HTU21D_heater(int ON);

257
SI7005_HTU21D/i2c.c Normal file
View File

@ -0,0 +1,257 @@
/*
* This file is part of the sihtu project.
* Copyright 2022 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <asm/ioctl.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include <usefull_macros.h>
static uint8_t lastaddr = 0;
static int I2Cfd = -1;
int i2c_getfd(){return I2Cfd;}
/**
* @brief i2c_read_reg8 - read 8-bit addressed register (8 bit)
* @param regaddr - register address
* @param data - data read
* @return state
*/
int i2c_read_reg8(uint8_t regaddr, uint8_t *data){
if(I2Cfd < 1) return FALSE;
struct i2c_smbus_ioctl_data args;
union i2c_smbus_data sd;
args.read_write = I2C_SMBUS_READ;
args.command = regaddr;
args.size = I2C_SMBUS_BYTE_DATA;
args.data = &sd;
if(ioctl(I2Cfd, I2C_SMBUS, &args) < 0){
WARN("i2c_read_reg8, ioctl()");
return FALSE;
}
if(data) *data = sd.byte;
return TRUE;
}
/**
* @brief i2c_write_reg8 - write to 8-bit addressed register
* @param regaddr - address
* @param data - data
* @return state
*/
int i2c_write_reg8(uint8_t regaddr, uint8_t data){
if(I2Cfd < 1) return FALSE;
struct i2c_smbus_ioctl_data args;
union i2c_smbus_data sd;
sd.byte = data;
args.read_write = I2C_SMBUS_WRITE;
args.command = regaddr;
args.size = I2C_SMBUS_BYTE_DATA;
args.data = &sd;
if(ioctl(I2Cfd, I2C_SMBUS, &args) < 0){
WARN("i2c_write_reg8, ioctl()");
return FALSE;
}
uint8_t t;
if(!i2c_read_reg8(regaddr, &t)) return FALSE;
if(t != data){
WARNX("Read not same thing that write: 0x%02x instead of 0x%02x", t, data);
return FALSE;
}
return TRUE;
}
/**
* @brief i2c_read_reg16 - read 16-bit addressed register (to 16-bit data)
* @param regaddr - address
* @param data - data
* @return state
*/
int i2c_read_reg16(uint16_t regaddr, uint16_t *data){
if(I2Cfd < 1) return FALSE;
struct i2c_msg m[2];
struct i2c_rdwr_ioctl_data x = {.msgs = m, .nmsgs = 2};
m[0].addr = lastaddr; m[1].addr = lastaddr;
m[0].flags = 0;
m[1].flags = I2C_M_RD;
m[0].len = 2; m[1].len = 2;
uint8_t a[2], d[2] = {0};
a[0] = regaddr >> 8;
a[1] = regaddr & 0xff;
m[0].buf = a; m[1].buf = d;
if(ioctl(I2Cfd, I2C_RDWR, &x) < 0){
WARN("i2c_read_reg16, ioctl()");
return FALSE;
}
if(data) *data = (uint16_t)((d[0] << 8) | (d[1]));
return TRUE;
}
/**
* @brief i2c_write_reg16 - write 16-bit data value to 16-bit addressed register
* @param regaddr - address
* @param data - data to write
* @return state
*/
int i2c_write_reg16(uint16_t regaddr, uint16_t data){
if(I2Cfd < 1) return FALSE;
union i2c_smbus_data d;
d.block[0] = 3;
d.block[1] = regaddr & 0xff;
d.block[2] = data >> 8;
d.block[3] = data & 0xff;
struct i2c_smbus_ioctl_data args;
args.read_write = I2C_SMBUS_WRITE;
args.command = regaddr >> 8;
args.size = I2C_SMBUS_I2C_BLOCK_DATA;
args.data = &d;
if(ioctl(I2Cfd, I2C_SMBUS, &args) < 0){
WARN("i2c_write_reg16, ioctl()");
return FALSE;
}
/* printf("Block: ");
for(int i = 0; i < 4; ++i) printf("0x%02x ", d.block[i]);
printf("\n");*/
return TRUE;
}
/**
* @brief i2c_set_slave_address - set current slave address
* @param addr - address
* @return state
*/
int i2c_set_slave_address(uint8_t addr){
if(I2Cfd < 1) return FALSE;
if(ioctl(I2Cfd, I2C_SLAVE, addr) < 0){
WARN("i2c_set_slave_address, ioctl()");
return FALSE;
}
lastaddr = addr;
return TRUE;
}
/**
* @brief i2c_open - open I2C device
* @param path - full path to device
* @return state
*/
int i2c_open(const char *path){
if(I2Cfd > 0) close(I2Cfd);
I2Cfd = open(path, O_RDWR);
if(I2Cfd < 1){
WARN("i2c_open, open()");
return FALSE;
}
return TRUE;
}
void i2c_close(){
if(I2Cfd > 0) close(I2Cfd);
}
#if 0
// Don't work :(
/**
* @brief read_regN8 - read up to I2C_SMBUS_BLOCK_MAX bytes from 8-bit addressed register
* @param regaddr - address
* @param data - data to read
* @param N - amount of bytes
* @return state
*/
static int read_regN8(uint8_t regaddr, uint8_t *data, uint16_t N){
if(I2Cfd < 1 || N > I2C_SMBUS_BLOCK_MAX || N == 0 || !data) return FALSE;
struct i2c_smbus_ioctl_data args = {0};
union i2c_smbus_data sd = {0};
sd.block[0] = N;
DBG("block: %d, %d, %d", sd.block[0], sd.block[1], sd.block[2]);
DBG("Try to get %d bytes from 0x%02x", N, regaddr);
args.read_write = I2C_SMBUS_READ;
args.command = regaddr;
args.size = I2C_SMBUS_BLOCK_DATA;
args.data = &sd;
if(ioctl(I2Cfd, I2C_SMBUS, &args) < 0){
WARN("read_regN8, ioctl()");
return FALSE;
}
DBG("block: %d, %d, %d", sd.block[0], sd.block[1], sd.block[2]);
memcpy(data, sd.block+1, N);
return TRUE;
}
#endif
/**
* @brief read_data16 - read data from 16-bit addressed register
* @param regaddr - address
* @param N - amount of bytes
* @param array - data read
* @return state
*/
int i2c_read_data16(uint16_t regaddr, uint16_t N, uint8_t *array){
if(I2Cfd < 1 || N == 0 || !array) return FALSE;
struct i2c_msg m[2];
struct i2c_rdwr_ioctl_data x = {.msgs = m, .nmsgs = 2};
m[0].addr = lastaddr; m[1].addr = lastaddr;
m[0].flags = 0;
m[1].flags = I2C_M_RD;
m[0].len = 2; m[1].len = N;
uint8_t a[2];
a[0] = regaddr >> 8;
a[1] = regaddr & 0xff;
m[0].buf = a; m[1].buf = array;
if(ioctl(I2Cfd, I2C_RDWR, &x) < 0){
WARN("i2c_read_data16, ioctl()");
return FALSE;
}
return TRUE;
}
/**
* @brief read_data8 - read data from 8-bit addressed register
* @param regaddr - address
* @param N - amount of bytes
* @param array - data read
* @return state
*/
int i2c_read_data8(uint8_t regaddr, uint16_t N, uint8_t *array){
if(I2Cfd < 1 || N < 1 || N+regaddr > 0xff || !array) return FALSE;
#if 0
uint16_t rest = N;
do{
uint8_t l = (rest > I2C_SMBUS_BLOCK_MAX) ? I2C_SMBUS_BLOCK_MAX : (uint8_t)rest;
if(!read_regN8(regaddr, array, l)){
DBG("can't read");
return FALSE;
}
rest -= l;
regaddr += l;
array += l;
}while(rest);
#endif
for(uint16_t i = 0; i < N; ++i){
if(!i2c_read_reg8((uint8_t)(regaddr+i), array++)){
DBG("can't read @%dth byte", i);
return FALSE;
}
}
return TRUE;
}

40
SI7005_HTU21D/i2c.h Normal file
View File

@ -0,0 +1,40 @@
/*
* This file is part of the sihtu project.
* Copyright 2022 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* 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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <stdint.h>
#ifndef FALSE
#define FALSE 0
#endif
#ifndef TRUE
#define TRUE 1
#endif
int i2c_getfd();
int i2c_open(const char *path);
void i2c_close();
int i2c_set_slave_address(uint8_t addr);
int i2c_read_reg8(uint8_t regaddr, uint8_t *data);
int i2c_write_reg8(uint8_t regaddr, uint8_t data);
int i2c_read_data8(uint8_t regaddr, uint16_t N, uint8_t *array);
int i2c_read_reg16(uint16_t regaddr, uint16_t *data);
int i2c_write_reg16(uint16_t regaddr, uint16_t data);
int i2c_read_data16(uint16_t regaddr, uint16_t N, uint8_t *array);

129
SI7005_HTU21D/main.c Normal file
View File

@ -0,0 +1,129 @@
/*
* This file is part of the sihtu project.
* Copyright 2022 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <math.h>
#include <stdio.h>
#include <unistd.h>
#include <usefull_macros.h>
#include "htu21d.h"
#include "si7005.h"
#include "i2c.h"
#define DEVADDR (0x40)
typedef struct{
char *device;
int help;
int heater;
} glob_pars;
static glob_pars G = {.device = "/dev/i2c-3", .heater = -1};
static myoption cmdlnopts[] = {
{"help", NO_ARGS, NULL, 'h', arg_int, APTR(&G.help), _("show this help")},
{"device", NEED_ARG, NULL, 'd', arg_string, APTR(&G.device), _("I2C device path")},
{"heater", NEED_ARG, NULL, 'H', arg_int, APTR(&G.heater), _("turn on (>0) or off (0) heater")},
end_option
};
static void showd(float T, float H){
// Sonntag1990
#define dB 17.62f
#define dC 243.12f
if(T < -273.15f || H < 0.f) return;
float gamma = logf(H/100.f) + dB*T/(dC + T);
float Tdp = dC * gamma / (dB - gamma);
printf("T=%.1fC, H=%.1f%%, Tdp=%.1fC\n", T, H, Tdp);
static int i = 0;
if(++i > 3) signals(0);
}
static void processSI(){
static double t0 = 0.;
si7005_process();
si7005_status s = si7005_get_status();
if(s == SI7005_RELAX){
if(dtime() - t0 > 1.){
DBG("need to start measure");
if(si7005_startmeasure()) t0 = dtime();
}
}else if(s == SI7005_RDY){ // humidity can be shown
DBG("Got data");
float T, H;
if(!si7005_getTH(&T, &H)) return;
showd(T, H);
t0 = dtime();
}else if(s == SI7005_ERR){
DBG("got error");
if(si7005_startmeasure()) t0 = dtime();
}
}
static void processHTU(){
static double t0 = 0.;
HTU21D_process();
HTU21D_status s = HTU21D_get_status();
if(s == HTU21D_RELAX){
if(dtime() - t0 > 1.){
DBG("need to start measure");
if(HTU21D_startmeasure()) t0 = dtime();
}
}else if(s == HTU21D_RDY){ // humidity can be shown
DBG("Got data");
float T, H;
if(!HTU21D_getTH(&T, &H)) return;
showd(T, H);
t0 = dtime();
}else if(s == HTU21D_ERR){
DBG("got error");
if(HTU21D_startmeasure()) t0 = dtime();
}
}
int main(int argc, char **argv){
initial_setup();
parseargs(&argc, &argv, cmdlnopts);
if(G.help) showhelp(-1, cmdlnopts);
if(!i2c_open(G.device)) ERR("Can't open %s", G.device);
if(!i2c_set_slave_address((uint8_t)DEVADDR)){
WARN("Can't set slave address 0x%02x", DEVADDR);
goto clo;
}
//if(!i2c_read_reg8(0, NULL)) ERR("Can't connect!");
int si = 1; // == 0 if htu
if(!si7005_read_ID()){
DBG("Don't see SI7005");
si = 0;
if(!HTU21D_read_ID()){
ERRX("Neither SI7005 nor HTU21D not found");
}else while(!HTU21D_startmeasure()) usleep(1000);
}else while(!si7005_startmeasure()) usleep(1000);
if(G.heater > -1){
int ans = FALSE;
if(si) ans = si7005_heater(G.heater);
else ans = HTU21D_heater(G.heater);
if(!ans) WARNX("Can't turn on heater");
}
while(1){
if(si) processSI();
else processHTU();
}
clo:
i2c_close();
return 0;
}

168
SI7005_HTU21D/si7005.c Normal file
View File

@ -0,0 +1,168 @@
/*
* This file is part of the sihtu project.
* Copyright 2022 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <usefull_macros.h>
#include <unistd.h>
#include "i2c.h"
#include "si7005.h"
#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 si7005_status sistatus = SI7005_RELAX;
static float Tmeasured, Hmeasured;
static double lastw = 0.; // last time of measurements start
si7005_status si7005_get_status(){
return sistatus;
}
/**
* @brief si7005_read_ID - read device ID
* @return TRUE if all OK
*/
int si7005_read_ID(){
if(sistatus != SI7005_RELAX) return FALSE;
//i2c_write_reg8(SI7005_REGCONFIG, 0);
uint8_t ID;
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;
}
return TRUE;
}
/*
* start themperature reading @return TRUE if all OK
*/
int si7005_startmeasure(){
sistatus = SI7005_BUSY;
if(!i2c_write_reg8(SI7005_REGCONFIG, SI7005_CONFTEMP | SI7005_CONFSTART)){
DBG("Can't write start Tmeas");
sistatus = SI7005_ERR;
return FALSE;
}
DBG("Wait for T\n");
lastw = dtime();
return TRUE;
}
/*
* start humidity reading @return TRUE if all OK
*/
static void si7005_cmdH(){
sistatus = SI7005_BUSY;
i2c_write_reg8(SI7005_REGCONFIG, SI7005_CONFSTART);
if(!i2c_write_reg8(SI7005_REGCONFIG, SI7005_CONFSTART)){
DBG("Can't write start Hmeas");
sistatus = SI7005_ERR;
return;
}
DBG("Wait for H, dt=%g", dtime() - lastw);
lastw = dtime();
}
int si7005_getTH(float *T, float *H){
if(sistatus != SI7005_RDY) return FALSE;
DBG("dt=%g", dtime() - lastw);
DBG("Measured T=%.1f, H=%.1f", Tmeasured, Hmeasured);
// correct T/H
#define A0 (-4.7844f)
#define A1 (0.4008f)
#define A2 (-0.00393f)
if(H){
*H = Hmeasured - (A2*Hmeasured*Hmeasured + A1*Hmeasured + A0);
}
if(T) *T = Tmeasured;
sistatus = SI7005_RELAX;
return TRUE;
}
/*
* process state machine
*/
void si7005_process(){
uint8_t c, d[3];
if(sistatus != SI7005_BUSY) return;
if(3 != read(i2c_getfd(), d, 3)){
DBG("Can't read status");
sistatus = SI7005_ERR;
return;
}
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");
sistatus = SI7005_ERR;
return;
}
DBG("Config: 0x%02x", c);
if(d[0] & SI7005_STATUSNRDY){ // not ready yet
//if(c & SI7005_CONFSTART){
if(dtime() - lastw > SI7005_CONVTIMEOUT){
DBG("Wait too long -> err");
sistatus = SI7005_ERR;
return;
}
usleep(20000);
return;
}
uint16_t TH = (uint16_t)((d[1]<<8) | d[2]);
if(c & SI7005_CONFTEMP){ // temperature measured
TH >>= 2;
Tmeasured = TH/32.f - 50.f;
DBG("T=%.1f", Tmeasured);
si7005_cmdH();
}else{ // humidity measured
TH >>= 4;
Hmeasured = TH/16.f - 24.f;
DBG("H=%.1f", Hmeasured);
sistatus = SI7005_RDY;
}
}
/**
* @brief si7005_heater - turn on/off heater
* @param ON == 1 to turn on
* @return FALSE if failed
*/
int si7005_heater(int ON){
if(sistatus != SI7005_RELAX) return FALSE;
uint8_t reg = (ON) ? SI7005_CONFHEAT : 0;
if(!i2c_write_reg8(SI7005_REGCONFIG, reg)){
DBG("Can't write write regconfig");
return FALSE;
}
return TRUE;
}

41
SI7005_HTU21D/si7005.h Normal file
View File

@ -0,0 +1,41 @@
/*
* This file is part of the sihtu project.
* Copyright 2022 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* 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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <stdint.h>
// conversion timeout (s)
#define SI7005_CONVTIMEOUT 2.0
typedef enum{
SI7005_BUSY, // measurement in progress
SI7005_ERR, // error in I2C
SI7005_RELAX, // relaxed state
SI7005_RDY, // data ready - can get it
} si7005_status;
si7005_status si7005_get_status();
int si7005_read_ID();
void si7005_process();
int si7005_startmeasure();
int si7005_getTH(float *T, float *H);
int si7005_heater(int ON);

View File

@ -0,0 +1 @@
-std=c17

View File

@ -0,0 +1,2 @@
// Add predefined macros for your project here. For example:
// #define THE_ANSWER 42

View File

@ -0,0 +1 @@
[General]

View File

@ -0,0 +1,174 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 6.0.0, 2022-09-28T21:44:32. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
<value type="QByteArray">{7bd84e39-ca37-46d3-be9d-99ebea85bc0d}</value>
</data>
<data>
<variable>ProjectExplorer.Project.ActiveTarget</variable>
<value type="int">0</value>
</data>
<data>
<variable>ProjectExplorer.Project.EditorSettings</variable>
<valuemap type="QVariantMap">
<value type="bool" key="EditorConfiguration.AutoIndent">true</value>
<value type="bool" key="EditorConfiguration.AutoSpacesForTabs">false</value>
<value type="bool" key="EditorConfiguration.CamelCaseNavigation">true</value>
<valuemap type="QVariantMap" key="EditorConfiguration.CodeStyle.0">
<value type="QString" key="language">Cpp</value>
<valuemap type="QVariantMap" key="value">
<value type="QByteArray" key="CurrentPreferences">CppGlobal</value>
</valuemap>
</valuemap>
<valuemap type="QVariantMap" key="EditorConfiguration.CodeStyle.1">
<value type="QString" key="language">QmlJS</value>
<valuemap type="QVariantMap" key="value">
<value type="QByteArray" key="CurrentPreferences">QmlJSGlobal</value>
</valuemap>
</valuemap>
<value type="int" key="EditorConfiguration.CodeStyle.Count">2</value>
<value type="QByteArray" key="EditorConfiguration.Codec">KOI8-R</value>
<value type="bool" key="EditorConfiguration.ConstrainTooltips">false</value>
<value type="int" key="EditorConfiguration.IndentSize">4</value>
<value type="bool" key="EditorConfiguration.KeyboardTooltips">false</value>
<value type="int" key="EditorConfiguration.MarginColumn">80</value>
<value type="bool" key="EditorConfiguration.MouseHiding">true</value>
<value type="bool" key="EditorConfiguration.MouseNavigation">true</value>
<value type="int" key="EditorConfiguration.PaddingMode">1</value>
<value type="bool" key="EditorConfiguration.PreferSingleLineComments">false</value>
<value type="bool" key="EditorConfiguration.ScrollWheelZooming">true</value>
<value type="bool" key="EditorConfiguration.ShowMargin">false</value>
<value type="int" key="EditorConfiguration.SmartBackspaceBehavior">0</value>
<value type="bool" key="EditorConfiguration.SmartSelectionChanging">true</value>
<value type="bool" key="EditorConfiguration.SpacesForTabs">true</value>
<value type="int" key="EditorConfiguration.TabKeyBehavior">0</value>
<value type="int" key="EditorConfiguration.TabSize">8</value>
<value type="bool" key="EditorConfiguration.UseGlobal">true</value>
<value type="bool" key="EditorConfiguration.UseIndenter">false</value>
<value type="int" key="EditorConfiguration.Utf8BomBehavior">1</value>
<value type="bool" key="EditorConfiguration.addFinalNewLine">true</value>
<value type="bool" key="EditorConfiguration.cleanIndentation">false</value>
<value type="bool" key="EditorConfiguration.cleanWhitespace">true</value>
<value type="QString" key="EditorConfiguration.ignoreFileTypes">*.md, *.MD, Makefile</value>
<value type="bool" key="EditorConfiguration.inEntireDocument">false</value>
<value type="bool" key="EditorConfiguration.skipTrailingWhitespace">true</value>
</valuemap>
</data>
<data>
<variable>ProjectExplorer.Project.PluginSettings</variable>
<valuemap type="QVariantMap">
<valuemap type="QVariantMap" key="AutoTest.ActiveFrameworks">
<value type="bool" key="AutoTest.Framework.Boost">true</value>
<value type="bool" key="AutoTest.Framework.CTest">false</value>
<value type="bool" key="AutoTest.Framework.Catch">true</value>
<value type="bool" key="AutoTest.Framework.GTest">true</value>
<value type="bool" key="AutoTest.Framework.QtQuickTest">true</value>
<value type="bool" key="AutoTest.Framework.QtTest">true</value>
</valuemap>
<valuemap type="QVariantMap" key="AutoTest.CheckStates"/>
<value type="int" key="AutoTest.RunAfterBuild">0</value>
<value type="bool" key="AutoTest.UseGlobal">true</value>
<valuelist type="QVariantList" key="ClangCodeModel.CustomCommandLineKey"/>
<value type="bool" key="ClangCodeModel.UseGlobalConfig">true</value>
<value type="QString" key="ClangCodeModel.WarningConfigId">Builtin.BuildSystem</value>
<valuemap type="QVariantMap" key="ClangTools">
<value type="bool" key="ClangTools.AnalyzeOpenFiles">true</value>
<value type="bool" key="ClangTools.BuildBeforeAnalysis">true</value>
<value type="QString" key="ClangTools.DiagnosticConfig">Builtin.DefaultTidyAndClazy</value>
<value type="int" key="ClangTools.ParallelJobs">2</value>
<valuelist type="QVariantList" key="ClangTools.SelectedDirs"/>
<valuelist type="QVariantList" key="ClangTools.SelectedFiles"/>
<valuelist type="QVariantList" key="ClangTools.SuppressedDiagnostics"/>
<value type="bool" key="ClangTools.UseGlobalSettings">true</value>
</valuemap>
</valuemap>
</data>
<data>
<variable>ProjectExplorer.Project.Target.0</variable>
<valuemap type="QVariantMap">
<value type="QString" key="DeviceType">Desktop</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Desktop</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Desktop</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">{65a14f9e-e008-4c1b-89df-4eaa4774b6e3}</value>
<value type="int" key="ProjectExplorer.Target.ActiveBuildConfiguration">0</value>
<value type="int" key="ProjectExplorer.Target.ActiveDeployConfiguration">0</value>
<value type="int" key="ProjectExplorer.Target.ActiveRunConfiguration">0</value>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.0">
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">/tmp/1/home/eddy/SI7005</value>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
<valuelist type="QVariantList" key="GenericProjectManager.GenericMakeStep.BuildTargets">
<value type="QString">all</value>
</valuelist>
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">GenericProjectManager.GenericMakeStep</value>
</valuemap>
<value type="int" key="ProjectExplorer.BuildStepList.StepsCount">1</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Сборка</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Сборка</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Build</value>
</valuemap>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.1">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
<valuelist type="QVariantList" key="GenericProjectManager.GenericMakeStep.BuildTargets">
<value type="QString">clean</value>
</valuelist>
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">GenericProjectManager.GenericMakeStep</value>
</valuemap>
<value type="int" key="ProjectExplorer.BuildStepList.StepsCount">1</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Очистка</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Очистка</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Clean</value>
</valuemap>
<value type="int" key="ProjectExplorer.BuildConfiguration.BuildStepListCount">2</value>
<value type="bool" key="ProjectExplorer.BuildConfiguration.ClearSystemEnvironment">false</value>
<valuelist type="QVariantList" key="ProjectExplorer.BuildConfiguration.CustomParsers"/>
<value type="bool" key="ProjectExplorer.BuildConfiguration.ParseStandardOutput">false</value>
<valuelist type="QVariantList" key="ProjectExplorer.BuildConfiguration.UserEnvironmentChanges"/>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">По умолчанию</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">GenericProjectManager.GenericBuildConfiguration</value>
</valuemap>
<value type="int" key="ProjectExplorer.Target.BuildConfigurationCount">1</value>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.DeployConfiguration.0">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
<value type="int" key="ProjectExplorer.BuildStepList.StepsCount">0</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Развёртывание</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Развёртывание</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Deploy</value>
</valuemap>
<value type="int" key="ProjectExplorer.BuildConfiguration.BuildStepListCount">1</value>
<valuemap type="QVariantMap" key="ProjectExplorer.DeployConfiguration.CustomData"/>
<value type="bool" key="ProjectExplorer.DeployConfiguration.CustomDataEnabled">false</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.DefaultDeployConfiguration</value>
</valuemap>
<value type="int" key="ProjectExplorer.Target.DeployConfigurationCount">1</value>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.RunConfiguration.0">
<valuelist type="QVariantList" key="CustomOutputParsers"/>
<value type="int" key="PE.EnvironmentAspect.Base">2</value>
<valuelist type="QVariantList" key="PE.EnvironmentAspect.Changes"/>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.CustomExecutableRunConfiguration</value>
<value type="QString" key="ProjectExplorer.RunConfiguration.BuildKey"></value>
<value type="bool" key="RunConfiguration.UseCppDebugger">false</value>
<value type="bool" key="RunConfiguration.UseCppDebuggerAuto">true</value>
<value type="bool" key="RunConfiguration.UseQmlDebugger">false</value>
<value type="bool" key="RunConfiguration.UseQmlDebuggerAuto">true</value>
</valuemap>
<value type="int" key="ProjectExplorer.Target.RunConfigurationCount">1</value>
</valuemap>
</data>
<data>
<variable>ProjectExplorer.Project.TargetCount</variable>
<value type="int">1</value>
</data>
<data>
<variable>ProjectExplorer.Project.Updater.FileVersion</variable>
<value type="int">22</value>
</data>
<data>
<variable>Version</variable>
<value type="int">22</value>
</data>
</qtcreator>

View File

@ -0,0 +1 @@
-std=c++17

View File

@ -0,0 +1,7 @@
htu21d.c
htu21d.h
i2c.c
i2c.h
main.c
si7005.c
si7005.h

View File

@ -0,0 +1 @@
.