add test AHT30
This commit is contained in:
parent
1091013c1f
commit
1a15f7b3a0
6 changed files with 149 additions and 67 deletions
|
|
@ -34,8 +34,9 @@ int cmd_parser(uint8_t * obuf, uint8_t * ibuf, uint32_t len) {
|
|||
obuf[1] = 0; // no err
|
||||
if (cmd == CMD_ID_DEVID) { // Get DEV_ID
|
||||
pdev_id_t p = (pdev_id_t) obuf;
|
||||
//p->revision = 0; // already = 0
|
||||
p->hw_version = 0x01;
|
||||
// p->pid = CMD_ID_DEV_ID;
|
||||
// p->revision = 0; // уже = 0
|
||||
p->hw_version = DEVICE;
|
||||
p->sw_version = APP_VERSION;
|
||||
p->dev_spec_data = 0;
|
||||
p->services = 0;
|
||||
|
|
|
|||
|
|
@ -68,7 +68,7 @@ enum CMD_ID_KEYS {
|
|||
|
||||
// CMD_ID_DEV_ID
|
||||
typedef struct _dev_id_t{
|
||||
uint8_t pid; // packet identifier = CMD_ID_DEV_ID
|
||||
uint8_t pid; // packet identifier = CMD_ID_DEVID
|
||||
uint8_t revision; // protocol version/revision
|
||||
uint16_t hw_version; // hardware version
|
||||
uint16_t sw_version; // software version (BCD)
|
||||
|
|
|
|||
|
|
@ -75,12 +75,12 @@ void test_config(void) {
|
|||
void load_eep_config(void) {
|
||||
if(!flash_supported_eep_ver(0, APP_VERSION)) {
|
||||
osal_memcpy(&cfg, &def_cfg, sizeof(cfg));
|
||||
osal_memcpy(&thsensor_cfg.coef, &def_thcoef, sizeof(thsensor_cfg.coef));
|
||||
} else {
|
||||
if (flash_read_cfg(&cfg, EEP_ID_CFG, sizeof(cfg)) != sizeof(cfg))
|
||||
osal_memcpy(&cfg, &def_cfg, sizeof(cfg));
|
||||
if (flash_read_cfg(&thsensor_cfg.coef, EEP_ID_CFS, sizeof(thsensor_cfg.coef)) != sizeof(thsensor_cfg.coef))
|
||||
osal_memcpy(&thsensor_cfg.coef, &def_thcoef, sizeof(thsensor_cfg.coef));
|
||||
if(flash_read_cfg(&thsensor_cfg.coef, EEP_ID_CFS, sizeof(thsensor_cfg.coef)) != sizeof(thsensor_cfg.coef)) {
|
||||
osal_memset(&thsensor_cfg.coef, 0, sizeof(thsensor_cfg.coef));
|
||||
}
|
||||
}
|
||||
test_config();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,12 +11,30 @@
|
|||
#include "types.h"
|
||||
|
||||
#ifndef APP_VERSION
|
||||
#define APP_VERSION 0x05 // BCD
|
||||
#define APP_VERSION 0x06 // BCD
|
||||
#endif
|
||||
|
||||
#define DEVICE_THB2 1
|
||||
#define DEVICE_BTH01 2
|
||||
#define DEVICE_TH05 3
|
||||
/*
|
||||
#define BOARD_LYWSD03MMC_B14 0 // number used for BLE firmware
|
||||
#define BOARD_MHO_C401 1
|
||||
#define BOARD_CGG1 2
|
||||
#define BOARD_LYWSD03MMC_B19 3 // number used for BLE firmware
|
||||
#define BOARD_LYWSD03MMC_DEVBIS 3 // ver https://github.com/devbis/z03mmc
|
||||
#define BOARD_LYWSD03MMC_B16 4 // number used for BLE firmware
|
||||
#define BOARD_WATERMETER 4 // ver https://github.com/slacky1965/watermeter_zed
|
||||
#define BOARD_LYWSD03MMC_B17 5 // number used for BLE firmware
|
||||
#define BOARD_CGDK2 6
|
||||
#define BOARD_CGG1N 7 // 2022
|
||||
#define BOARD_MHO_C401N 8 // 2022
|
||||
#define BOARD_MJWSD05MMC 9
|
||||
#define BOARD_LYWSD03MMC_B15 10 // number used for BLE firmware
|
||||
#define BOARD_MHO_C122 11
|
||||
#define BOARD_TNK 16 // Water tank controller (not yet published at the moment)
|
||||
#define BOARD_TS0201_TZ3000 17
|
||||
#define BOARD_TS0202_TZ3000 18 // ?
|
||||
*/
|
||||
#define DEVICE_THB2 19
|
||||
#define DEVICE_BTH01 20
|
||||
#define DEVICE_TH05 21
|
||||
|
||||
#ifndef DEVICE
|
||||
#define DEVICE DEVICE_THB2
|
||||
|
|
|
|||
|
|
@ -109,6 +109,20 @@ struct __attribute__((packed)) _cht8305_config_t{
|
|||
|
||||
#define CHT8305_ID 0x5959
|
||||
|
||||
|
||||
/*
|
||||
---------------------------------------
|
||||
Датчик влажности AHT25
|
||||
---------------------------------------
|
||||
*/
|
||||
#define AHT30_I2C_ADDR 0x38
|
||||
|
||||
#define AHT30_CMD_INI 0x0E1 // Initialization Command
|
||||
#define AHT30_CMD_TMS 0x0AC // Trigger Measurement Command
|
||||
#define AHT30_DATA_TMS 0x3300 // Trigger Measurement data
|
||||
#define AHT30_CMD_RST 0x0BA // Soft Reset Command
|
||||
|
||||
|
||||
typedef struct _measured_data_t {
|
||||
uint16_t count;
|
||||
int16_t temp; // x 0.01 C
|
||||
|
|
@ -125,6 +139,9 @@ typedef struct _thsensor_coef_t {
|
|||
} thsensor_coef_t;
|
||||
|
||||
extern const thsensor_coef_t def_thcoef;
|
||||
#if DEVICE == DEVICE_BTH01
|
||||
extern const thsensor_coef_t def_thcoef_aht30;
|
||||
#endif
|
||||
|
||||
typedef struct _thsensor_cfg_t {
|
||||
thsensor_coef_t coef;
|
||||
|
|
@ -132,7 +149,7 @@ typedef struct _thsensor_cfg_t {
|
|||
uint32_t id;
|
||||
uint16_t _id[2];
|
||||
};
|
||||
uint8_t i2c_addr;
|
||||
uint8_t i2c_addr;
|
||||
} thsensor_cfg_t;
|
||||
|
||||
extern measured_data_t measured_data;
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@
|
|||
|
||||
#include "types.h"
|
||||
#include "config.h"
|
||||
#include "OSAL.h"
|
||||
#include "gpio.h"
|
||||
#include "rom_sym_def.h"
|
||||
#include "i2c.h"
|
||||
|
|
@ -16,18 +17,33 @@
|
|||
measured_data_t measured_data;
|
||||
thsensor_cfg_t thsensor_cfg;
|
||||
|
||||
const thsensor_coef_t def_thcoef = {
|
||||
#if DEVICE == DEVICE_THB2
|
||||
|
||||
const thsensor_coef_t def_thcoef = {
|
||||
.temp_k = 25606,
|
||||
.humi_k = 20000,
|
||||
#elif DEVICE == DEVICE_BTH01
|
||||
.temp_k = 16500,
|
||||
.humi_k = 10000,
|
||||
#endif
|
||||
.temp_z = 0,
|
||||
.humi_z = 0
|
||||
};
|
||||
|
||||
#elif DEVICE == DEVICE_BTH01
|
||||
|
||||
const thsensor_coef_t def_thcoef = {
|
||||
.temp_k = 16500,
|
||||
.humi_k = 10000,
|
||||
.temp_z = -4000,
|
||||
.humi_z = 0
|
||||
};
|
||||
|
||||
const thsensor_coef_t def_thcoef_aht30 = {
|
||||
.temp_k = 1250,
|
||||
.humi_k = 625,
|
||||
.temp_z = -5000,
|
||||
.humi_z = 0
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
void init_i2c(void) {
|
||||
hal_gpio_fmux_set(I2C_SCL, FMUX_IIC0_SCL);
|
||||
hal_gpio_fmux_set(I2C_SDA, FMUX_IIC0_SDA);
|
||||
|
|
@ -189,7 +205,7 @@ int send_i2c_wreg(uint8 addr, uint8 reg, uint16 data) {
|
|||
while(1) {
|
||||
if(pi2cdev->IC_RAW_INTR_STAT & 0x200)// check tx empty
|
||||
break;
|
||||
if(osal_sys_tick - to > 2)
|
||||
if(osal_sys_tick - to > I2C_WAIT_ms)
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
|
|
@ -201,12 +217,16 @@ __ATTR_SECTION_XIP__ void init_sensor(void) {
|
|||
send_i2c_byte(0, 0x06); // Reset command using the general call address
|
||||
WaitMs(SENSOR_RESET_TIMEOUT_ms);
|
||||
thsensor_cfg.i2c_addr = CHT8310_I2C_ADDR0;
|
||||
read_i2c_bytes(thsensor_cfg.i2c_addr, CHT8310_REG_MID, (uint8 *)&thsensor_cfg._id[0], 2); // 0x5959
|
||||
read_i2c_bytes(thsensor_cfg.i2c_addr, CHT8310_REG_VID, (uint8 *)&thsensor_cfg._id[1], 2); // 0x8215
|
||||
//WaitMs(1);
|
||||
if(adv_wrk.measure_interval_ms >= 5000) // > 5 sec
|
||||
send_i2c_wreg(CHT8310_I2C_ADDR0, CHT8310_REG_CRT, 0x0300); // Set conversion ratio 5 sec
|
||||
// else 1 sec
|
||||
if(!read_i2c_bytes(thsensor_cfg.i2c_addr, CHT8310_REG_MID, (uint8 *)&thsensor_cfg._id[0], 2) // 0x5959
|
||||
&& !read_i2c_bytes(thsensor_cfg.i2c_addr, CHT8310_REG_VID, (uint8 *)&thsensor_cfg._id[1], 2)) { // 0x8215
|
||||
if(adv_wrk.measure_interval_ms >= 5000) // > 5 sec
|
||||
send_i2c_wreg(CHT8310_I2C_ADDR0, CHT8310_REG_CRT, 0x0300); // Set conversion ratio 5 sec
|
||||
// else 1 sec
|
||||
if(!thsensor_cfg.coef.temp_k) {
|
||||
osal_memcpy(&thsensor_cfg.coef, &def_thcoef, sizeof(thsensor_cfg.coef));
|
||||
}
|
||||
} else
|
||||
thsensor_cfg.i2c_addr = 0;
|
||||
#elif DEVICE == DEVICE_BTH01
|
||||
#define USE_DEFAULT_SETS_SENSOR 0 // for CHT8305
|
||||
#if USE_DEFAULT_SETS_SENSOR
|
||||
|
|
@ -215,22 +235,30 @@ __ATTR_SECTION_XIP__ void init_sensor(void) {
|
|||
hal_gpio_write(GPIO_SPWR, 1);
|
||||
#endif
|
||||
thsensor_cfg.i2c_addr = CHT8305_I2C_ADDR0;
|
||||
if(!read_i2c_bytes(thsensor_cfg.i2c_addr, CHT8310_REG_MID, (uint8 *)&thsensor_cfg._id[0], 2) // 0x5959
|
||||
&& !read_i2c_bytes(thsensor_cfg.i2c_addr, CHT8310_REG_VID, (uint8 *)&thsensor_cfg._id[1], 2)) { // 0x8305
|
||||
if((!read_i2c_bytes(thsensor_cfg.i2c_addr, CHT8310_REG_MID, (uint8 *)&thsensor_cfg._id[0], 2)) // 0x5959
|
||||
&& (!read_i2c_bytes(thsensor_cfg.i2c_addr, CHT8310_REG_VID, (uint8 *)&thsensor_cfg._id[1], 2))) { // 0x8305
|
||||
#if !USE_DEFAULT_SETS_SENSOR
|
||||
// Soft reset command
|
||||
send_i2c_wreg(thsensor_cfg.i2c_addr, CHT8305_REG_CFG,
|
||||
CHT8305_CFG_SOFT_RESET
|
||||
| CHT8305_CFG_MODE
|
||||
);
|
||||
CHT8305_CFG_SOFT_RESET | CHT8305_CFG_MODE);
|
||||
WaitMs(SENSOR_RESET_TIMEOUT_ms);
|
||||
// Configure
|
||||
send_i2c_wreg(thsensor_cfg.i2c_addr, CHT8305_REG_CFG,
|
||||
CHT8305_CFG_MODE
|
||||
);
|
||||
send_i2c_wreg(thsensor_cfg.i2c_addr, CHT8305_REG_CFG, CHT8305_CFG_MODE );
|
||||
#endif
|
||||
// WaitMs(SENSOR_MEASURING_TIMEOUT_ms);
|
||||
send_i2c_byte(thsensor_cfg.i2c_addr, CHT8305_REG_TMP); // start measure T/H
|
||||
if(!thsensor_cfg.coef.temp_k) {
|
||||
osal_memcpy(&thsensor_cfg.coef, &def_thcoef, sizeof(thsensor_cfg.coef));
|
||||
}
|
||||
|
||||
} else {
|
||||
thsensor_cfg.i2c_addr = AHT30_I2C_ADDR;
|
||||
if(send_i2c_wreg(thsensor_cfg.i2c_addr, AHT30_CMD_TMS, AHT30_DATA_TMS))
|
||||
thsensor_cfg.i2c_addr = 0;
|
||||
else
|
||||
if(!thsensor_cfg.coef.temp_k) {
|
||||
osal_memcpy(&thsensor_cfg.coef, &def_thcoef_aht30, sizeof(thsensor_cfg.coef));
|
||||
}
|
||||
}
|
||||
#endif // DEVICE == DEVICE_BTH01
|
||||
deinit_i2c();
|
||||
|
|
@ -238,46 +266,64 @@ __ATTR_SECTION_XIP__ void init_sensor(void) {
|
|||
|
||||
|
||||
int read_sensor(void) {
|
||||
uint8 reg_data[4];
|
||||
init_i2c();
|
||||
if(thsensor_cfg.i2c_addr) {
|
||||
init_i2c();
|
||||
#if DEVICE == DEVICE_THB2
|
||||
int32 _r32;
|
||||
int16 _r16;
|
||||
_r32 = read_i2c_bytes(thsensor_cfg.i2c_addr, CHT8310_REG_TMP, reg_data, 2);
|
||||
//? WaitUs(100);
|
||||
_r32 |= read_i2c_bytes(thsensor_cfg.i2c_addr, CHT8310_REG_HMD, ®_data[2], 2);
|
||||
deinit_i2c();
|
||||
if (!_r32) {
|
||||
_r16 = (reg_data[0] << 8) | reg_data[1];
|
||||
measured_data.temp = ((int32)(_r16 * thsensor_cfg.coef.temp_k + 0x7fff) >> 16) + thsensor_cfg.coef.temp_z;; // x 0.01 C
|
||||
_r32 = ((reg_data[2] << 8) | reg_data[3]) & 0x7fff;
|
||||
measured_data.humi = ((uint32)(_r32 * thsensor_cfg.coef.humi_k + 0x7fff) >> 16) + + thsensor_cfg.coef.humi_z; // x 0.01 %
|
||||
if (measured_data.humi < 0)
|
||||
measured_data.humi = 0;
|
||||
else if (measured_data.humi > 9999)
|
||||
measured_data.humi = 9999;
|
||||
measured_data.count++;
|
||||
return 0;
|
||||
}
|
||||
#elif DEVICE == DEVICE_BTH01
|
||||
uint16_t _temp;
|
||||
if (!read_noreg_i2c_bytes(thsensor_cfg.i2c_addr, reg_data, 4)) {
|
||||
_temp = (reg_data[0] << 8) | reg_data[1];
|
||||
measured_data.temp = ((uint32_t)(_temp * thsensor_cfg.coef.temp_k) >> 16) - 4000 + thsensor_cfg.coef.temp_z; // x 0.01 C
|
||||
_temp = (reg_data[2] << 8) | reg_data[3];
|
||||
measured_data.humi = ((uint32_t)(_temp * thsensor_cfg.coef.humi_k) >> 16) + thsensor_cfg.coef.humi_z; // x 0.01 %
|
||||
if (measured_data.humi < 0)
|
||||
measured_data.humi = 0;
|
||||
else if (measured_data.humi > 9999)
|
||||
measured_data.humi = 9999;
|
||||
send_i2c_byte(thsensor_cfg.i2c_addr, CHT8305_REG_TMP); // start measure T/H
|
||||
uint8 reg_data[4];
|
||||
int32 _r32;
|
||||
int16 _r16;
|
||||
_r32 = read_i2c_bytes(thsensor_cfg.i2c_addr, CHT8310_REG_TMP, reg_data, 2);
|
||||
//? WaitUs(100);
|
||||
_r32 |= read_i2c_bytes(thsensor_cfg.i2c_addr, CHT8310_REG_HMD, ®_data[2], 2);
|
||||
deinit_i2c();
|
||||
return 0;
|
||||
}
|
||||
// deinit_i2c();
|
||||
if (!_r32) {
|
||||
_r16 = (reg_data[0] << 8) | reg_data[1];
|
||||
measured_data.temp = ((int32)(_r16 * thsensor_cfg.coef.temp_k) >> 16) + thsensor_cfg.coef.temp_z;; // x 0.01 C
|
||||
_r32 = ((reg_data[2] << 8) | reg_data[3]) & 0x7fff;
|
||||
measured_data.humi = ((uint32)(_r32 * thsensor_cfg.coef.humi_k) >> 16) + thsensor_cfg.coef.humi_z; // x 0.01 %
|
||||
if (measured_data.humi < 0)
|
||||
measured_data.humi = 0;
|
||||
else if (measured_data.humi > 9999)
|
||||
measured_data.humi = 9999;
|
||||
measured_data.count++;
|
||||
return 0;
|
||||
}
|
||||
#elif DEVICE == DEVICE_BTH01
|
||||
uint8 reg_data[8];
|
||||
if(thsensor_cfg.i2c_addr == AHT30_I2C_ADDR) {
|
||||
if(!read_noreg_i2c_bytes(thsensor_cfg.i2c_addr, reg_data, 7)
|
||||
&& (reg_data[0] & 0x80) == 0) { // busy
|
||||
send_i2c_wreg(thsensor_cfg.i2c_addr, AHT30_CMD_TMS, AHT30_DATA_TMS);
|
||||
deinit_i2c();
|
||||
uint32_t _temp;
|
||||
_temp = ((reg_data[3] & 0x0F) << 16) | (reg_data[4] << 8) | reg_data[5];
|
||||
measured_data.temp = ((uint32_t)(_temp * thsensor_cfg.coef.temp_k) >> 16) + thsensor_cfg.coef.temp_z; // x 0.01 C
|
||||
_temp = (reg_data[1] << 12) | (reg_data[2] << 4) | (reg_data[3] >> 4);
|
||||
measured_data.humi = ((uint32_t)(_temp * thsensor_cfg.coef.humi_k) >> 16) + thsensor_cfg.coef.humi_z; // x 0.01 %
|
||||
if (measured_data.humi < 0)
|
||||
measured_data.humi = 0;
|
||||
else if (measured_data.humi > 9999)
|
||||
measured_data.humi = 9999;
|
||||
return 0;
|
||||
}
|
||||
} else if(!read_noreg_i2c_bytes(thsensor_cfg.i2c_addr, reg_data, 4)) {
|
||||
send_i2c_byte(thsensor_cfg.i2c_addr, CHT8305_REG_TMP); // start measure T/H
|
||||
deinit_i2c();
|
||||
uint16_t _temp;
|
||||
_temp = (reg_data[0] << 8) | reg_data[1];
|
||||
measured_data.temp = ((uint32_t)(_temp * thsensor_cfg.coef.temp_k) >> 16) + thsensor_cfg.coef.temp_z; // x 0.01 C
|
||||
_temp = (reg_data[2] << 8) | reg_data[3];
|
||||
measured_data.humi = ((uint32_t)(_temp * thsensor_cfg.coef.humi_k) >> 16) + thsensor_cfg.coef.humi_z; // x 0.01 %
|
||||
if (measured_data.humi < 0)
|
||||
measured_data.humi = 0;
|
||||
else if (measured_data.humi > 9999)
|
||||
measured_data.humi = 9999;
|
||||
return 0;
|
||||
}
|
||||
#else
|
||||
#error "DEVICE Not released!"
|
||||
#endif
|
||||
}
|
||||
init_sensor();
|
||||
return 1;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue