Skip to content

Commit

Permalink
added "Sensor ID"
Browse files Browse the repository at this point in the history
  • Loading branch information
pvvx committed Mar 27, 2023
1 parent 4871182 commit d9971d4
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 24 deletions.
Binary file modified ATC_v43.bin
Binary file not shown.
Binary file modified BTH_v43.bin
Binary file not shown.
Binary file modified CGDK2_v43.bin
Binary file not shown.
Binary file modified CGG1M_v43.bin
Binary file not shown.
Binary file modified CGG1_v43.bin
Binary file not shown.
Binary file modified MHO_C401N_v43.bin
Binary file not shown.
Binary file modified MHO_C401_v43.bin
Binary file not shown.
5 changes: 5 additions & 0 deletions src/cmd_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,10 @@ void cmd_parser(void * p) {
send_buf[1] = 0xff; // Error cmd
olen = 2;
}
} else if (cmd == CMD_ID_SEN_ID) { // Get sensor ID
memcpy(&send_buf[1], &sensor_id, sizeof(sensor_id));
olen = sizeof(sensor_id) + 1;


// Debug commands (unsupported in different versions!):
} else if (cmd == CMD_ID_EEP_RW && len > 2) {
Expand All @@ -592,6 +596,7 @@ void cmd_parser(void * p) {
flash_write_cfg(&cfg, EEP_ID_CFG, sizeof(cfg));
ble_send_cfg();
ble_connected |= BIT(CONNECTED_FLG_RESET_OF_DISCONNECT); // reset device on disconnect

} else {
send_buf[1] = 0xff; // Error cmd
olen = 2;
Expand Down
3 changes: 2 additions & 1 deletion src/cmd_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ enum {
CMD_ID_GDEVS = 0x02, // Get address devises
CMD_ID_I2C_SCAN = 0x03, // I2C scan
CMD_ID_I2C_UTR = 0x04, // Universal I2C/SMBUS read-write
CMD_ID_SEN_ID = 0x05, // Get sensor ID
CMD_ID_DEV_MAC = 0x10, // Get/Set MAC [+RandMAC], [size][mac[6][randmac[2]]]
CMD_ID_MI_DNAME = 0x11, // Get/Set Mi key: DevNameId, [size]["\0"+miDevName]
CMD_ID_MI_TBIND = 0x12, // Get/Set Mi keys: Token & Bind, [size][keys]
Expand Down Expand Up @@ -36,7 +37,7 @@ enum {
// Debug commands (unsupported in different versions!):
CMD_ID_EEP_RW = 0xDC, // Get/set EEP
CMD_ID_LR_RESET = 0xDD, // Reset Long Range
CMD_ID_DEBUG = 0xDE // Test/Debug
CMD_ID_DEBUG = 0xDE // Test/Debug

} CMD_ID_KEYS;

Expand Down
2 changes: 2 additions & 0 deletions src/sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ extern volatile uint32_t timer_measure_cb; // time start measure

#define SHTC3_I2C_ADDR 0x70
#define SHT4x_I2C_ADDR 0x44
#define SHT4xB_I2C_ADDR 0x45

extern uint8_t sensor_i2c_addr;
extern uint32_t sensor_id;

void init_sensor(void);
void start_measure_sensor_deep_sleep(void);
Expand Down
70 changes: 47 additions & 23 deletions src/sensors.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#define SHTC3_MEASURE_CS 0xA27C // Measurement commands, Clock Stretching, Normal Mode, Read T First
#define SHTC3_LPMEASURE 0x9C60 // Measurement commands, Clock Stretching Disabled, Low Power Mode, Read T First
#define SHTC3_LPMEASURE_CS 0x245C // Measurement commands, Clock Stretching, Low Power Mode, Read T First
#define SHTC3_GET_ID 0xC8EF // read ID register

// Sensor SHT4x https://www.sensirion.com/fileadmin/user_upload/customers/sensirion/Dokumente/2_Humidity_Sensors/Datasheets/Sensirion_Humidity_Sensors_Datasheet.pdf
//#define SHT4x_I2C_ADDR 0x44
Expand All @@ -33,11 +34,13 @@
#define SHT4x_MEASURE_HI_us 7000 // 6.9..8.2 ms
#define SHT4x_MEASURE_LO 0xE0 // Measurement commands, Clock Stretching Disabled, Low Power Mode, Read T First
#define SHT4x_MEASURE_LO_us 1700 // 1.7 ms
#define SHT4x_GET_ID 0x89 // read ID register

#define CRC_POLYNOMIAL 0x131 // P(x) = x^8 + x^5 + x^4 + 1 = 100110001

RAM volatile uint32_t timer_measure_cb;
RAM uint8_t sensor_i2c_addr;
RAM uint32_t sensor_id;

static _attribute_ram_code_ void send_sensor_word(uint16_t cmd) {
if ((reg_clk_en0 & FLD_CLK0_I2C_EN)==0)
Expand Down Expand Up @@ -77,35 +80,68 @@ static void soft_reset_sensor(void) {
if (sensor_i2c_addr == (SHTC3_I2C_ADDR << 1)) {
send_sensor_word(SHTC3_SOFT_RESET); // Soft reset command
sleep_us(SHTC3_SOFT_RESET_us); // 240 us
} else if ((sensor_i2c_addr) == SHT4x_I2C_ADDR << 1) {
} else if (sensor_i2c_addr) {
send_sensor_byte(SHT4x_SOFT_RESET); // Soft reset command
sleep_us(SHT4x_SOFT_RESET_us); // max 1 ms
}
}

_attribute_ram_code_
uint8_t sensor_crc(uint8_t crc) {
int i;
for(i = 8; i > 0; i--) {
if (crc & 0x80)
crc = (crc << 1) ^ (CRC_POLYNOMIAL & 0xff);
else
crc = (crc << 1);
}
return crc;
}

uint32_t get_sensor_id(void) {
static const uint8_t utr_c3_gid[] = { 0x02,0x03,SHTC3_I2C_ADDR << 1, SHTC3_GET_ID & 0xff, (SHTC3_GET_ID >> 8) & 0xff };
static const uint8_t utr_4x_gid[] = { 0x01,0x06,SHT4x_I2C_ADDR << 1, SHT4x_GET_ID };
uint8_t buf_id[6];
uint32_t id = 0;
if(sensor_i2c_addr == (SHTC3_I2C_ADDR << 1)) {
if(!I2CBusUtr(&buf_id, (i2c_utr_t *)&utr_c3_gid, sizeof(utr_c3_gid) - 3)
&& buf_id[2] == sensor_crc(buf_id[1] ^ sensor_crc(buf_id[0] ^ 0xff))) { // = 0x5b
id = (buf_id[0] << 8) | buf_id[1]; // = 0x8708
}
} else if(sensor_i2c_addr) {
if(!I2CBusUtr(&buf_id, (i2c_utr_t *)&utr_4x_gid, sizeof(utr_4x_gid) - 3)
&& buf_id[2] == sensor_crc(buf_id[1] ^ sensor_crc(buf_id[0] ^ 0xff))
&& buf_id[5] == sensor_crc(buf_id[4] ^ sensor_crc(buf_id[3] ^ 0xff))
) {
id = (buf_id[3] << 24) | (buf_id[4] << 16) | (buf_id[0] << 8) | buf_id[1];
}
}
return id;
}

static int check_sensor(void) {
if ((sensor_i2c_addr = (uint8_t) scan_i2c_addr(SHTC3_I2C_ADDR << 1)) != 0) {
cfg.hw_cfg.shtc3 = 1; // = 1 - sensor SHTC3
} else {
sensor_i2c_addr = (uint8_t) scan_i2c_addr(SHT4x_I2C_ADDR << 1);
cfg.hw_cfg.shtc3 = 0; // = 0 - sensor SHT4x or ?
if(!sensor_i2c_addr)
sensor_i2c_addr = (uint8_t) scan_i2c_addr(SHT4xB_I2C_ADDR << 1);
}
soft_reset_sensor();
// no i2c sensor ? sensor_i2c_addr = 0
return sensor_i2c_addr;
}

void init_sensor(void) {
// START byte
scan_i2c_addr(1);
// Reset command using the general call address
send_sensor_byte(0x06);
//scan_i2c_addr(0);
send_i2c_byte(0, 0x06); // Reset command using the general call address
sleep_us(SHTC3_WAKEUP_us); // 240 us
// Wake-up command of the SHTC3 sensor
sensor_i2c_addr = SHTC3_I2C_ADDR << 1;
send_sensor_word(SHTC3_WAKEUP);
send_i2c_word(SHTC3_I2C_ADDR << 1, SHTC3_WAKEUP);
sleep_us(SHTC3_WAKEUP_us); // 240 us
check_sensor();
sensor_id = get_sensor_id();
}

_attribute_ram_code_ __attribute__((optimize("-Os"))) int read_sensor_cb(void) {
Expand Down Expand Up @@ -138,24 +174,12 @@ _attribute_ram_code_ __attribute__((optimize("-Os"))) int read_sensor_cb(void) {
data = reg_i2c_di;
reg_i2c_ctrl = FLD_I2C_CMD_DI | FLD_I2C_CMD_READ_ID;
_temp = data << 8;
crc = data ^ 0xff;
for(i = 8; i > 0; i--) {
if (crc & 0x80)
crc = (crc << 1) ^ (CRC_POLYNOMIAL & 0xff);
else
crc = (crc << 1);
}
crc = sensor_crc(data ^ 0xff);
while (reg_i2c_status & FLD_I2C_CMD_BUSY);
data = reg_i2c_di;
reg_i2c_ctrl = FLD_I2C_CMD_DI | FLD_I2C_CMD_READ_ID;
_temp |= data;
crc ^= data;
for(i = 8; i > 0; i--) {
if (crc & 0x80)
crc = (crc << 1) ^ (CRC_POLYNOMIAL & 0xff);
else
crc = (crc << 1);
}
crc = sensor_crc(crc ^ data);
while (reg_i2c_status & FLD_I2C_CMD_BUSY);
data = reg_i2c_di;
reg_i2c_ctrl = FLD_I2C_CMD_DI | FLD_I2C_CMD_READ_ID;
Expand Down Expand Up @@ -196,7 +220,7 @@ _attribute_ram_code_ void start_measure_sensor_deep_sleep(void) {
#else
send_sensor_word(SHTC3_MEASURE);
#endif
} else if (sensor_i2c_addr == (SHT4x_I2C_ADDR << 1)) {
} else if (sensor_i2c_addr) {
send_sensor_byte(SHT4x_MEASURE_HI);
} else
return;
Expand All @@ -212,7 +236,7 @@ _attribute_ram_code_ void start_measure_sensor_low_power(void) {
#else
send_sensor_word(SHTC3_LPMEASURE);
#endif
} else if ((sensor_i2c_addr) == SHT4x_I2C_ADDR << 1) {
} else if (sensor_i2c_addr) {
send_sensor_byte(SHT4x_MEASURE_LO);
sleep_us(SHT4x_MEASURE_LO_us); // 1700 us
}
Expand Down

0 comments on commit d9971d4

Please sign in to comment.