add HTU21D

This commit is contained in:
Edward Emelianov 2021-02-14 23:50:12 +03:00
parent 4a75b43838
commit 5b9e4233e6
22 changed files with 276 additions and 15 deletions

View File

@ -1 +0,0 @@
Work with I2C humidity/temperature sensor SI7005

View File

@ -0,0 +1 @@
Work with I2C humidity/temperature sensors SI7005 & HTU21D

View File

@ -54,7 +54,7 @@ float ln(uint16_t RH){
}
if(idx < 0) idx = 0;
else if(idx > NKNOTS-1) idx = NKNOTS - 1;
USB_send("idx="); USB_send(u2str(idx)); USB_send("\n");
//USB_send("idx="); USB_send(u2str(idx)); USB_send("\n");
float l = Y[idx] + K[idx]*(RH - X[idx]);
#undef NKNOTS
return l;

View File

@ -0,0 +1,155 @@
/*
* This file is part of the si7005 project.
* Copyright 2021 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 "htu21d.h"
#include "i2c.h"
#define DEVID (0x40)
#ifdef EBUG
#include "usb.h"
#include "proto.h"
#define DBG(x) do{USB_send(x);}while(0)
#else
#define DBG(x)
#endif
// read with no hold master!
#define HTU21_READ_TEMP (0xF3)
#define HTU21_READ_HUMID (0xF5)
#define HTU21_READ_REG (0xE7)
#define HTU21_WRITE_REG (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)
typedef enum{
RELAX = 0,
WAITFORT, // T
WAITFORH // humidity
} HTU21D_state;
static HTU21D_state state = RELAX;
static HTU21D_status htustatus = HTU21D_RELAX;
static uint16_t TH; // data for T or H
HTU21D_status HTU21D_get_status(){
return htustatus;
}
//output in Cx10
int32_t HTU21D_getT(){
DBG("HTU getT\n");
htustatus = HTU21D_RELAX;
if(TH & HTU21_HUMID_FLAG) return -5000; // humidity measured
uint32_t a = TH & 0xFFFC;
a *= 17572;
a >>= 16;
int32_t val = (a - 4685)/10;
return val;
}
//output in %x10
uint32_t HTU21D_getH(){
DBG("HTU getH\n");
htustatus = HTU21D_RELAX;
if(!(TH & HTU21_HUMID_FLAG)) return 5000; // temperature measured
uint32_t a = TH & 0xFFFC;
a *= 1250;
a >>= 16;
a -= 60;
return a;
}
void HTU21D_setup(){
DBG("HTU setup\n");
htustatus = HTU21D_RELAX;
state = RELAX;
i2c_setup();
i2c_set_addr7(DEVID);
}
#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;
}
static int htusendcmd(uint8_t cmd){
DBG("htu cmd\n");
if(state != RELAX) return 1;
htustatus = HTU21D_BUSY;
if(I2C_OK != i2c_7bit_send_onebyte(cmd, 1)){
htustatus = HTU21D_ERR;
DBG("htu read err\n");
return 1;
}
return 0;
}
int HTU21D_cmdT(){
DBG("htu read T\n");
if(htusendcmd(HTU21_READ_TEMP)) return 1;
state = WAITFORT;
return 0;
}
int HTU21D_cmdH(){
DBG("htu read H\n");
if(htusendcmd(HTU21_READ_HUMID)) return 1;
state = WAITFORH;
return 0;
}
void HTU21D_process(){
uint8_t d[3];
if(state == RELAX) return;
if(state == WAITFORH || state == WAITFORT){ // poll RDY
if(I2C_OK != i2c_7bit_receive(d, 3)) return; // NACKed
DBG("HTU got H or T:");
DBG(u2str(d[0])); DBG(", ");
DBG(u2str(d[1])); DBG(", ");
DBG(u2str(d[2])); DBG("\n");
if(htu_check_crc(d)){
htustatus = HTU21D_ERR;
DBG("CRC failed\n");
state = RELAX;
return;
}
TH = (d[0] << 8) | d[1];
htustatus = (state == WAITFORH) ? HTU21D_HRDY : HTU21D_TRDY;
state = RELAX;
}
}

View File

@ -0,0 +1,42 @@
/*
* This file is part of the HTU21D project.
* Copyright 2021 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
#ifndef HTU21D_H__
#include <stm32f1.h>
typedef enum{
HTU21D_BUSY, // measurement in progress
HTU21D_ERR, // error in I2C
HTU21D_RELAX, // relaxed state
HTU21D_TRDY, // data ready - can get it
HTU21D_HRDY
} HTU21D_status;
HTU21D_status HTU21D_get_status();
void HTU21D_setup();
int HTU21D_read_ID(uint8_t *devid);
int HTU21D_cmdT();
int HTU21D_cmdH();
void HTU21D_process();
int32_t HTU21D_getT();
uint32_t HTU21D_getH();
#define HTU21D_H__
#endif // HTU21D_H__

View File

@ -170,19 +170,67 @@ i2c_status i2c_7bit_receive_twobytes(uint8_t *data){
//I2C_LINEWAIT();
I2C1->CR1 |= I2C_CR1_START | I2C_CR1_POS | I2C_CR1_ACK; // generate start sequence, set pos & ack
I2C_WAIT(I2C1->SR1 & I2C_SR1_SB); // wait for SB
DBG("2 Rx sb\n");
(void) I2C1->SR1; // clear SB
I2C1->DR = addr7r; // set address
I2C_WAIT(I2C1->SR1 & I2C_SR1_ADDR); // wait for ADDR flag
DBG("2 ADDR\n");
if(I2C1->SR1 & I2C_SR1_AF){ // NACK
ret = I2C_NACK;
goto eotr;
}
DBG("2 ACK\n");
(void) I2C1->SR2; // clear ADDR
I2C1->CR1 &= ~I2C_CR1_ACK; // clear ACK
I2C_WAIT(I2C1->SR1 & I2C_SR1_BTF); // wait for BTF
DBG("2 BTF\n");
I2C1->CR1 |= I2C_CR1_STOP; // program STOP
*data++ = I2C1->DR; *data = I2C1->DR; // read data & clear RxNE
ret = I2C_OK;
eotr:
return ret;
}
// receive any amount of bytes
i2c_status i2c_7bit_receive(uint8_t *data, uint16_t nbytes){
if(nbytes == 0) return I2C_HWPROBLEM;
I2C1->SR1 = 0; // clear previous NACK flag & other error flags
if(nbytes == 1) return i2c_7bit_receive_onebyte(data, 1);
else if(nbytes == 2) return i2c_7bit_receive_twobytes(data);
i2c_status ret = I2C_LINEBUSY;
//I2C_LINEWAIT();
I2C1->CR1 |= I2C_CR1_START | I2C_CR1_ACK; // generate start sequence, set pos & ack
I2C_WAIT(I2C1->SR1 & I2C_SR1_SB); // wait for SB
DBG("got SB\n");
(void) I2C1->SR1; // clear SB
I2C1->DR = addr7r; // set address
I2C_WAIT(I2C1->SR1 & I2C_SR1_ADDR); // wait for ADDR flag
DBG("send addr\n");
if(I2C1->SR1 & I2C_SR1_AF){ // NACK
DBG("NACKed\n");
ret = I2C_NACK;
goto eotr;
}
DBG("ACKed\n");
(void) I2C1->SR2; // clear ADDR
for(uint16_t x = nbytes - 3; x > 0; --x){
I2C_WAIT(I2C1->SR1 & I2C_SR1_RXNE); // wait next byte
*data++ = I2C1->DR; // get data
}
DBG("three left\n");
// three bytes remain to be read
I2C_WAIT(I2C1->SR1 & I2C_SR1_RXNE); // wait dataN-2
DBG("dataN-2\n");
I2C_WAIT(I2C1->SR1 & I2C_SR1_BTF); // wait for BTF
DBG("BTF\n");
I2C1->CR1 &= ~I2C_CR1_ACK; // clear ACK
*data++ = I2C1->DR; // read dataN-2
I2C1->CR1 |= I2C_CR1_STOP; // program STOP
*data++ = I2C1->DR; // read dataN-1
I2C_WAIT(I2C1->SR1 & I2C_SR1_RXNE); // wait next byte
*data = I2C1->DR; // read dataN
DBG("got it\n");
ret = I2C_OK;
eotr:
return ret;
}

View File

@ -35,6 +35,7 @@ i2c_status i2c_7bit_send_onebyte(uint8_t data, uint8_t stop);
i2c_status i2c_7bit_send(const uint8_t *data, int datalen);
i2c_status i2c_7bit_receive_onebyte(uint8_t *data, uint8_t stop);
i2c_status i2c_7bit_receive_twobytes(uint8_t *data);
i2c_status i2c_7bit_receive(uint8_t *data, uint16_t nbytes);
#define I2C_H__
#endif // I2C_H__

View File

@ -18,6 +18,7 @@
#include "dewpoint.h"
#include "hardware.h"
#include "htu21d.h"
#include "proto.h"
#include "si7005.h"
#include "usb.h"
@ -68,6 +69,7 @@ int main(void){
StartHSE();
hw_setup();
si7005_setup();
HTU21D_setup();
SysTick_Config(72000);
RCC->CSR |= RCC_CSR_RMVF; // remove reset flags
@ -85,17 +87,20 @@ int main(void){
}
usb_proc();
si7005_process();
HTU21D_process();
HTU21D_status h = HTU21D_get_status();
si7005_status s = si7005_get_status();
if(s == SI7005_HRDY){ // humidity can be shown
//USB_send(u2str(Tms)); USB_send("\n");
mH = si7005_getH();
if(s == SI7005_HRDY || h == HTU21D_HRDY){ // humidity can be shown
if(s == SI7005_HRDY) mH = si7005_getH();
else mH = HTU21D_getH();
USB_send("Hum=");
USB_send(u2str(mH));
USB_send("*10%\n");
showd(mT, mH);
}else if(s == SI7005_TRDY){ // T can be shown
//USB_send(u2str(Tms)); USB_send("\n");
mT = si7005_getT();
}else if(s == SI7005_TRDY || h == HTU21D_TRDY){ // T can be shown
if(s == SI7005_TRDY) mT = si7005_getT();
else mT = HTU21D_getT();
int32_t T = mT;
USB_send("T=");
if(T < 0){
@ -105,9 +110,10 @@ int main(void){
USB_send(u2str(T));
USB_send("*10degrC\n");
showd(mT, mH);
}else if(s == SI7005_ERR){
}else if(s == SI7005_ERR || h == HTU21D_ERR){
USB_send("Error in H/T measurement\n");
si7005_setup();
HTU21D_setup();
}
char *txt, *ans;

View File

@ -17,6 +17,7 @@
*/
#include "dewpoint.h"
#include "htu21d.h"
#include "proto.h"
#include "si7005.h"
#include "usb.h"
@ -156,10 +157,10 @@ char *getnum(const char *txt, uint32_t *N){
const char* helpmsg =
"'0' - reset I2C\n"
"'I' - read SI7005 device ID\n"
"'H' - start Humidity measurement\n"
"'hH' - start Humidity measurement (h-si,H-htu)\n"
"'N' - read number (0..1000) and show its ln\n"
"'R' - software reset\n"
"'T' - start Temperature measurement\n"
"'tT' - start Temperature measurement (t-si,T-htu)\n"
"'W' - test watchdog\n"
;
@ -169,6 +170,7 @@ const char *parse_cmd(const char *buf){
switch(*buf){
case '0':
si7005_setup();
HTU21D_setup();
return "Reset I2C\n";
break;
/*case 'p':
@ -185,16 +187,21 @@ const char *parse_cmd(const char *buf){
USB_send("\n");
}else USB_send("Can't read ID\n");
break;
case 'h':
if(si7005_cmdH()) USB_send("SI7005 not found\n");
break;
case 'H':
if(si7005_cmdH()) USB_send("Humid. read error\n");
if(HTU21D_cmdH()) USB_send("HTU21D not found\n");
break;
case 'R':
USB_send("Soft reset\n");
NVIC_SystemReset();
break;
case 't':
if(si7005_cmdT()) USB_send("SI7005 not found\n");
break;
case 'T':
if(si7005_cmdT()) USB_send("Temper. read error\n");
//USB_send(u2str(Tms)); USB_send("\n");
if(HTU21D_cmdT()) USB_send("HTU21D not found\n");
break;
case 'W':
USB_send("Wait for reboot\n");

View File

@ -83,6 +83,7 @@ int si7005_cmdT(){
sistatus = SI7005_BUSY;
i2c_status st = i2c_7bit_send(cmd, 2);
if(st != I2C_OK){
sistatus = SI7005_ERR;
return 1;
}
DBG("Wait for T\n");
@ -101,6 +102,7 @@ int si7005_cmdH(){
sistatus = SI7005_BUSY;
i2c_status st = i2c_7bit_send(cmd, 2);
if(st != I2C_OK){
sistatus = SI7005_ERR;
return 1;
}
state = WAITFORH;
@ -117,7 +119,7 @@ int32_t si7005_getT(){ // T*10
return d;
}
uint32_t si7005_getH(){ // hum * 10
if(sistatus != SI7005_HRDY) return 0;
if(sistatus != SI7005_HRDY) return 5000;
TH >>= 4;
uint32_t d = (TH*10)/16 - 240;
sistatus = SI7005_RELAX;