bugfix for i2c jank
authorjweigele <jweigele@local>
Sat, 13 Apr 2024 14:52:08 +0000 (07:52 -0700)
committerjweigele <jweigele@local>
Sat, 13 Apr 2024 14:52:08 +0000 (07:52 -0700)
aqi/main/aqi.c
aqi/main/aqi.h

index b27f5b46c0258bcbfbd494a7555c1bcb44e818b1..e5ccbfad85969d7d811bdd232a3e3824add3432b 100644 (file)
@@ -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;
index 57e1a45edf3e336ab1a5b7669e29ff741adf98a1..1c412c4575aa1d6df0a6f2242daff7ff3912d785 100644 (file)
@@ -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