Skip to content

Commit

Permalink
Merge pull request #632 from cmorganBE/cmm_sht4x_fix
Browse files Browse the repository at this point in the history
sht4x - Fix sporadic measurement errors by applying TIME_TO_TICKS() m…
  • Loading branch information
UncleRus authored May 16, 2024
2 parents c6da185 + ad7ea5b commit 315294a
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions components/sht4x/sht4x.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@

#define I2C_FREQ_HZ 1000000 // 1MHz

// due to the fact that ticks can be smaller than portTICK_PERIOD_MS, one and
// a half tick period added to the duration to be sure that waiting time for
// the results is long enough
#define TIME_TO_TICKS(ms) (1 + ((ms) + (portTICK_PERIOD_MS-1) + portTICK_PERIOD_MS/2 ) / portTICK_PERIOD_MS)

static const char *TAG = "sht4x";

#define CMD_RESET 0x94
Expand Down Expand Up @@ -224,7 +229,7 @@ esp_err_t sht4x_init(sht4x_t *dev)
dev->heater = SHT4X_HEATER_OFF;

sht4x_raw_data_t s;
CHECK(exec_cmd(dev, CMD_SERIAL, pdMS_TO_TICKS(10), s));
CHECK(exec_cmd(dev, CMD_SERIAL, TIME_TO_TICKS(10), s));
dev->serial = ((uint32_t)s[0] << 24) | ((uint32_t)s[1] << 16) | ((uint32_t)s[3] << 8) | s[4];

return sht4x_reset(dev);
Expand Down Expand Up @@ -272,7 +277,7 @@ size_t sht4x_get_measurement_duration(sht4x_t *dev)
{
if (!dev) return 0;

size_t res = pdMS_TO_TICKS(get_duration_ms(dev));
size_t res = TIME_TO_TICKS(get_duration_ms(dev));
return res == 0 ? 1 : res;
}

Expand Down

0 comments on commit 315294a

Please sign in to comment.