From 97ba31974fac84012a08b614a5a2e3eb321bd8e4 Mon Sep 17 00:00:00 2001 From: jweigele Date: Sat, 13 Apr 2024 07:52:08 -0700 Subject: [PATCH] bugfix for i2c jank --- aqi/main/aqi.c | 25 +++++++++++++++++++------ aqi/main/aqi.h | 1 + 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/aqi/main/aqi.c b/aqi/main/aqi.c index b27f5b4..e5ccbfa 100644 --- a/aqi/main/aqi.c +++ b/aqi/main/aqi.c @@ -535,7 +535,7 @@ bool init_rht(){ .i2c_port = 0, .scl_io_num = CONFIG_RHT_SCL_PIN, .sda_io_num = CONFIG_RHT_SDA_PIN, - .glitch_ignore_cnt = 0, + .glitch_ignore_cnt = 7, .flags.enable_internal_pullup = false, //true, }; @@ -553,10 +553,15 @@ bool init_rht(){ i2c_device_config_t dev_cfg = { .dev_addr_length = I2C_ADDR_BIT_LEN_7, .device_address = CONFIG_RHT_ADDRESS, - .scl_speed_hz = 400000, + .scl_speed_hz = 100000, }; ESP_ERROR_CHECK(i2c_master_bus_add_device(bus_handle, &dev_cfg, &rht_handle)); + /*ESP_LOGE(TAG, "reset bus"); + uint8_t send_cmd[1] = CMD_SOFT_RESET; + esp_err_t check_done = i2c_master_transmit(rht_handle, send_cmd, sizeof(send_cmd), 100); + ESP_LOGE(TAG, "done: %s", esp_err_to_name(check_done));*/ + return true; } @@ -569,16 +574,24 @@ static short get_data_rht(){ ESP_LOGI(TAG, "fetching rht data"); //ESP_ERROR_CHECK(i2c_master_transmit_receive(rht_handle, send_cmd, 2, buf, 6, -1)); - esp_err_t tx_check = i2c_master_transmit(rht_handle, send_cmd, sizeof(send_cmd), -1); + esp_err_t tx_check = i2c_master_transmit(rht_handle, send_cmd, sizeof(send_cmd), 100); ESP_LOGE(TAG, "i2c transmit status is %s", esp_err_to_name(tx_check)); if (tx_check != ESP_OK){ + ESP_LOGE(TAG, "reset bus"); + uint8_t send_cmd[1] = CMD_SOFT_RESET; + esp_err_t check_done = i2c_master_transmit(rht_handle, send_cmd, sizeof(send_cmd), 100); + ESP_LOGE(TAG, "done: %s", esp_err_to_name(check_done)); return -1; } // delay to allow measurement time to finish vTaskDelay( pdMS_TO_TICKS(10) ); - esp_err_t rx_check = i2c_master_receive(rht_handle, buf, 6, -1); + esp_err_t rx_check = i2c_master_receive(rht_handle, buf, 6, 100); ESP_LOGE(TAG, "i2c receive status is %s", esp_err_to_name(rx_check)); if (rx_check != ESP_OK){ + ESP_LOGE(TAG, "reset bus"); + uint8_t send_cmd[1] = CMD_SOFT_RESET; + esp_err_t check_done = i2c_master_transmit(rht_handle, send_cmd, sizeof(send_cmd), 100); + ESP_LOGE(TAG, "done: %s", esp_err_to_name(check_done)); return -1; } @@ -594,9 +607,9 @@ static short get_data_rht(){ // doesn't really matter when we start the counting loop, so just block until we have the semaphore // (should only be unavailable if another task is updating, or more likely we're generating the report) - xSemaphoreTake(report_data.mutex, portMAX_DELAY); + //xSemaphoreTake(report_data.mutex, portMAX_DELAY); report_data.humidity = sensor_rh; - xSemaphoreGive(report_data.mutex); + //xSemaphoreGive(report_data.mutex); return -1; diff --git a/aqi/main/aqi.h b/aqi/main/aqi.h index 57e1a45..1c412c4 100644 --- a/aqi/main/aqi.h +++ b/aqi/main/aqi.h @@ -112,6 +112,7 @@ static const char *TAG = "aqi"; #ifdef CONFIG_RHT_ENABLED #include "driver/i2c_master.h" #define CMD_HIGH_PRECISION_READ {0xFD} +#define CMD_SOFT_RESET {0x94} #endif #ifdef CONFIG_CO2_UART_ENABLED -- 2.30.2