mirror of
https://github.com/eddyem/eddys_snippets.git
synced 2026-03-22 09:40:55 +03:00
Add SI7005
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user