Browse Source

htu21: driver reads temp and humidity, no crc check

master
Franz Levin 2 years ago
parent
commit
c256eb2513
2 changed files with 54 additions and 22 deletions
  1. +54
    -17
      htu21.c
  2. +0
    -5
      htu21.h

+ 54
- 17
htu21.c View File

@ -17,7 +17,9 @@ this stuff is worth it, you can buy us a ( > 0 ) beer/mate in return - The Xil T
#define HTU21_TRIGGER_TEMP 0xF3
#define HTU21_TRIGGER_HUMIDITY 0xF5
#define HTU21_I2C_RETRIES 20
// Temperature measurement seems to take ~45ms
// Humidity measurement seems to take ~15ms
#define HTU21_I2C_RETRIES 100 // 1ms per retry
static uint16_t ICACHE_FLASH_ATTR byteswap16(uint16_t u16)
{
@ -45,10 +47,10 @@ static int16_t ICACHE_FLASH_ATTR htu21_read_temp(htu21_t* htu21)
retries = HTU21_I2C_RETRIES;
do {
res = i2c_transmit_no_stop(htu21->i2c, I2C_ADDRESS, buffer, 1);
res = i2c_transmit(htu21->i2c, I2C_ADDRESS, buffer, 1);
retries--;
I2C_DELAY_US(1000);
} while (res != 0 && retries > 0);
htu21->retry1 = HTU21_I2C_RETRIES - retries;
if (res != 0)
{
return 0;
@ -56,20 +58,16 @@ static int16_t ICACHE_FLASH_ATTR htu21_read_temp(htu21_t* htu21)
retries = HTU21_I2C_RETRIES;
do {
htu21->polls++;
I2C_DELAY_US(10);
res = i2c_receive(htu21->i2c, I2C_ADDRESS, buffer, 2);
I2C_DELAY_US(1000);
res = i2c_receive(htu21->i2c, I2C_ADDRESS, buffer, 3);
retries--;
} while (res != 0 && retries > 0);
htu21->retry2 = HTU21_I2C_RETRIES - retries;
if (res != 0)
{
return 0;
}
temp = *(uint16_t*)buffer;
temp = byteswap16(temp);
temp = byteswap16(*(uint16_t*)buffer);
temp = -46.85 + 175.72 * temp/(1<<16);
@ -77,15 +75,57 @@ static int16_t ICACHE_FLASH_ATTR htu21_read_temp(htu21_t* htu21)
return temp * 10;
}
static int16_t ICACHE_FLASH_ATTR htu21_read_humidity(htu21_t* htu21)
{
float humidity;
int res;
int retries;
uint8_t buffer[3];
if (!htu21->initilized) {
return 0;
}
buffer[0] = HTU21_TRIGGER_HUMIDITY;
retries = HTU21_I2C_RETRIES;
do {
res = i2c_transmit(htu21->i2c, I2C_ADDRESS, buffer, 1);
retries--;
I2C_DELAY_US(1000);
} while (res != 0 && retries > 0);
if (res != 0)
{
return 0;
}
retries = HTU21_I2C_RETRIES;
do {
I2C_DELAY_US(1000);
res = i2c_receive(htu21->i2c, I2C_ADDRESS, buffer, 3);
retries--;
} while (res != 0 && retries > 0);
if (res != 0)
{
return 0;
}
humidity = byteswap16(*(uint16_t*)buffer);
humidity = -6 + 125 * humidity/(1<<16);
// return value in 0.1%
return humidity * 10;
}
bool ICACHE_FLASH_ATTR htu21_read(driver_params_t* params)
{
htu21_t* htu21 = &params->htu21;
htu21->polls = 0;
htu21->retry1 = 0;
htu21->retry2 = 0;
htu21->temperature = htu21_read_temp(htu21);
htu21->humidity = htu21_read_humidity(htu21);
return true;
}
@ -97,9 +137,6 @@ bool ICACHE_FLASH_ATTR htu21_print(driver_params_t* params, driver_print_data_t*
print->print_string(print->data, print->index, print->i, "driver", "htu21");
print->print_value(print->data, print->index, print->i, "temp", &htu21->temperature, sizeof(htu21->temperature));
print->print_value(print->data, print->index, print->i, "humidity", &htu21->humidity, sizeof(htu21->humidity));
print->print_value(print->data, print->index, print->i, "polls", &htu21->polls, sizeof(htu21->polls));
print->print_value(print->data, print->index, print->i, "retry1", &htu21->retry1, sizeof(htu21->retry1));
print->print_value(print->data, print->index, print->i, "retry2", &htu21->retry2, sizeof(htu21->retry2));
return true;
}


+ 0
- 5
htu21.h View File

@ -27,11 +27,6 @@ typedef struct {
// collected data
int16_t humidity;
int16_t temperature;
// debug data
int16_t polls;
int16_t retry1;
int16_t retry2;
} htu21_t;
#endif // HTU21_H

Loading…
Cancel
Save