Browse Source

htu21: temp reading, buggy?

master
Franz Levin 2 years ago
parent
commit
53907ed50a
3 changed files with 165 additions and 0 deletions
  1. +3
    -0
      drivers.h
  2. +125
    -0
      htu21.c
  3. +37
    -0
      htu21.h

+ 3
- 0
drivers.h View File

@ -49,6 +49,7 @@ typedef struct driver_sensor {
#include "drivers/bmp180.h"
#include "drivers/bmp280.h"
#include "drivers/tsl2561.h"
#include "drivers/htu21.h"
// Global, constant variables for function pointers
// Busses
@ -57,6 +58,7 @@ extern const driver_bus_type_t bus_i2c;
extern const driver_sensor_t sensor_bmp180;
extern const driver_sensor_t sensor_bmp280;
extern const driver_sensor_t sensor_tsl2561;
extern const driver_sensor_t sensor_htu21;
// Data buses
@ -76,6 +78,7 @@ union driver_params {
bmp180_t bmp180;
bmp280_t bmp280;
tsl2561_t tsl2561;
htu21_t htu21;
};
struct driver {


+ 125
- 0
htu21.c View File

@ -0,0 +1,125 @@
/*
"THE BEER/MATE-WARE LICENSE":
<xil@xil.se> wrote this file. As long as you retain this notice you
can do whatever you want with this stuff. If we meet some day, and you think
this stuff is worth it, you can buy us a ( > 0 ) beer/mate in return - The Xil TEAM
*/
#include <stdint.h>
#include <stdbool.h>
#include "drivers/i2c.h"
#include "drivers/htu21.h"
#include "drivers/drivers.h"
#define I2C_ADDRESS 0x40
#define HTU21_TRIGGER_TEMP 0xF3
#define HTU21_TRIGGER_HUMIDITY 0xF5
#define HTU21_I2C_RETRIES 20
static uint16_t ICACHE_FLASH_ATTR byteswap16(uint16_t u16)
{
uint8_t* u8;
u8 = (uint8_t*)&u16;
return (u8[0] << 8) | (u8[1] << 0);
}
static int16_t ICACHE_FLASH_ATTR htu21_read_temp(htu21_t* htu21)
{
float temp;
int res;
int retries;
uint8_t buffer[3];
if (!htu21->initilized) {
return 0;
}
buffer[0] = HTU21_TRIGGER_TEMP;
retries = HTU21_I2C_RETRIES;
do {
res = i2c_transmit_no_stop(htu21->i2c, I2C_ADDRESS, buffer, 1);
retries--;
} while (res != 0 && retries > 0);
htu21->retry1 = HTU21_I2C_RETRIES - retries;
if (res != 0)
{
return 0;
}
retries = HTU21_I2C_RETRIES;
do {
htu21->polls++;
I2C_DELAY_US(10);
res = i2c_receive(htu21->i2c, I2C_ADDRESS, buffer, 2);
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 = -46.85 + 175.72 * temp/(1<<16);
// return value in 0.1C
return temp * 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);
return true;
}
bool ICACHE_FLASH_ATTR htu21_print(driver_params_t* params, driver_print_data_t* print)
{
htu21_t* htu21 = &params->htu21;
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;
}
bool ICACHE_FLASH_ATTR htu21_init(driver_params_t* params, driver_bus_t* bus)
{
htu21_t* htu21 = &params->htu21;
i2c_t* i2c = &bus->params.i2c;
htu21->i2c = i2c;
htu21->initilized = true;
return true;
}
// Global variable with function pointers
const driver_sensor_t sensor_htu21 = {
.init = htu21_init,
.read = htu21_read,
.print = htu21_print,
};

+ 37
- 0
htu21.h View File

@ -0,0 +1,37 @@
/*
"THE BEER/MATE-WARE LICENSE":
<xil@xil.se> wrote this file. As long as you retain this notice you
can do whatever you want with this stuff. If we meet some day, and you think
this stuff is worth it, you can buy us a ( > 0 ) beer/mate in return - The Xil TEAM
*/
#ifndef HTU21_H
#define HTU21_H
#include <stdint.h>
#include <stdbool.h>
#include "drivers/i2c.h"
typedef struct {
// Configuration
// Internal data
bool initilized;
// calibration data
// busses
i2c_t* i2c;
// 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