Skip to content

Commit

Permalink
correct Read sensor ID SHT4x
Browse files Browse the repository at this point in the history
  • Loading branch information
pvvx committed Mar 27, 2023
1 parent d9971d4 commit b9856cc
Show file tree
Hide file tree
Showing 9 changed files with 12 additions and 8 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.
8 changes: 3 additions & 5 deletions src/i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,12 @@ int I2CBusUtr(void * outdata, i2c_utr_t * tr, unsigned int wrlen) {
if(--wrlen == cntstart && ret == 0) { // + send start & id
if(tr->mode & 0x80) {
reg_i2c_ctrl = FLD_I2C_CMD_STOP;
while(reg_i2c_status & FLD_I2C_CMD_BUSY);
}
else {
} else {
// hw reset I2C
reg_rst0 = FLD_RST0_I2C;
reg_rst0 = 0;
}
while(reg_i2c_status & FLD_I2C_CMD_BUSY);
reg_i2c_id = tr->wrdata[0] | FLD_I2C_WRITE_READ_BIT;
// start + id
reg_i2c_ctrl = FLD_I2C_CMD_START | FLD_I2C_CMD_ID;
Expand All @@ -168,8 +167,7 @@ int I2CBusUtr(void * outdata, i2c_utr_t * tr, unsigned int wrlen) {
}
if(ret == 0) {
while(rdlen) {
rdlen--;
if(rdlen == 0 && (tr->rdlen & 0x80) == 0)
if(--rdlen == 0 && (tr->rdlen & 0x80) == 0)
reg_i2c_ctrl = FLD_I2C_CMD_DI | FLD_I2C_CMD_READ_ID | FLD_I2C_CMD_ACK;
else
reg_i2c_ctrl = FLD_I2C_CMD_DI | FLD_I2C_CMD_READ_ID;
Expand Down
12 changes: 9 additions & 3 deletions src/sensors.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ uint8_t sensor_crc(uint8_t 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 };
i2c_utr_t utr;
uint8_t buf_id[6];
uint32_t id = 0;
if(sensor_i2c_addr == (SHTC3_I2C_ADDR << 1)) {
Expand All @@ -109,11 +109,17 @@ uint32_t get_sensor_id(void) {
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)
if(!send_i2c_byte(sensor_i2c_addr, SHT4x_GET_ID)) {
utr.mode = 0;
utr.rdlen = 6;
utr.wrdata[0] = sensor_i2c_addr | 1;
sleep_us(SHT4x_SOFT_RESET_us);
if(I2CBusUtr(&buf_id, (i2c_utr_t *)&utr, 0) == 0
&& 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];
id = (buf_id[3] << 24) | (buf_id[4] << 16) | (buf_id[0] << 8) | buf_id[1];
}
}
}
return id;
Expand Down

0 comments on commit b9856cc

Please sign in to comment.