# HG changeset patch # User Michiel Broek # Date 1681911522 -7200 # Node ID 40231d010111f47c2f4186bf8a7d5191be64ff7a # Parent 5bd5f6668f719c3577e91a7ece8b3345a4c89e24 Global settings for the INA219 boards. diff -r 5bd5f6668f71 -r 40231d010111 main/task_ina219.c --- a/main/task_ina219.c Mon Apr 17 16:53:38 2023 +0200 +++ b/main/task_ina219.c Wed Apr 19 15:38:42 2023 +0200 @@ -9,6 +9,14 @@ static const char *TAG = "task_ina219"; +/* + * Global settings for the INA219 boards. + */ +#define BUS_RANGE INA219_BUS_RANGE_32V +#define GAIN_SHUNT INA219_GAIN_0_125 +#define U_RES INA219_RES_12BIT_4S +#define I_RES INA219_RES_12BIT_2S + SemaphoreHandle_t xSemaphoreINA219 = NULL; ///< Semaphore INA219 task EventGroupHandle_t xEventGroupINA219; ///< Events INA219 task INA219_State *ina219_state; ///< Public state for other tasks @@ -54,8 +62,7 @@ ina219_state->Battery.error = INA219_ERR_NONE; if (ina219_b_dev.i2c_dev.addr) { ESP_LOGI(TAG, "Configuring INA219 Battery"); - ESP_ERROR_CHECK(ina219_configure(&ina219_b_dev, INA219_BUS_RANGE_32V, INA219_GAIN_0_125, - INA219_RES_12BIT_1S, INA219_RES_12BIT_1S, INA219_MODE_CONT_SHUNT_BUS)); + ESP_ERROR_CHECK(ina219_configure(&ina219_b_dev, BUS_RANGE, GAIN_SHUNT, U_RES, I_RES, INA219_MODE_CONT_SHUNT_BUS)); ESP_LOGI(TAG, "Calibrating INA219 Battery"); ESP_ERROR_CHECK(ina219_calibrate(&ina219_b_dev, (float)I_MAX_CURRENT, (float)I_SHUNT_RESISTOR_MILLI_OHM / 1000.0f)); } @@ -66,8 +73,7 @@ ina219_state->Solar.error = INA219_ERR_NONE; if (ina219_s_dev.i2c_dev.addr) { ESP_LOGI(TAG, "Configuring INA219 Solar"); - ESP_ERROR_CHECK(ina219_configure(&ina219_s_dev, INA219_BUS_RANGE_32V, INA219_GAIN_0_125, - INA219_RES_12BIT_1S, INA219_RES_12BIT_1S, INA219_MODE_CONT_SHUNT_BUS)); + ESP_ERROR_CHECK(ina219_configure(&ina219_s_dev, BUS_RANGE, GAIN_SHUNT, U_RES, I_RES, INA219_MODE_CONT_SHUNT_BUS)); ESP_LOGI(TAG, "Calibrating INA219 Solar"); ESP_ERROR_CHECK(ina219_calibrate(&ina219_s_dev, (float)I_MAX_CURRENT, (float)I_SHUNT_RESISTOR_MILLI_OHM / 1000.0f)); } @@ -86,6 +92,19 @@ uxBits = xEventGroupWaitBits(xEventGroupINA219, TASK_INA219_REQUEST_POWER, pdFALSE, pdFALSE, portMAX_DELAY ); if (uxBits & TASK_INA219_REQUEST_POWER) { + /* + * We run the ESP32-C3 in Power Save mode, Dynamic Frequency Scaling. + * This means a wrong current measurement (too high) unless we let the CPU + * rest for a while. The INA219 runs in continuous mode so we get the + * results during the vTaskDelay(). + * We also run the INA219 boards in standby/power-on mode, so first turn + * them on. + */ + if (! ina219_state->Battery.fake) + ESP_ERROR_CHECK(ina219_configure(&ina219_b_dev, BUS_RANGE, GAIN_SHUNT, U_RES, I_RES, INA219_MODE_CONT_SHUNT_BUS)); + if (! ina219_state->Solar.fake) + ESP_ERROR_CHECK(ina219_configure(&ina219_s_dev, BUS_RANGE, GAIN_SHUNT, U_RES, I_RES, INA219_MODE_CONT_SHUNT_BUS)); + vTaskDelay(20 / portTICK_PERIOD_MS); /* * Four scenario's: @@ -96,15 +115,6 @@ * 4. Fake everything. */ if (! ina219_state->Battery.fake) { - /* - * We run the ESP32-C3 in Power Save mode, Dynamic Frequency Scaling. - * This means a wrong current measurement (too high) unless we let the CPU - * rest for a while. The INA219 runs in continuous mode so we get the - * results during the vTaskDelay(). - */ - ESP_ERROR_CHECK(ina219_configure(&ina219_b_dev, INA219_BUS_RANGE_32V, INA219_GAIN_0_125, - INA219_RES_12BIT_1S, INA219_RES_12BIT_1S, INA219_MODE_CONT_SHUNT_BUS)); - vTaskDelay(20 / portTICK_PERIOD_MS); ESP_ERROR_CHECK(ina219_get_bus_voltage(&ina219_b_dev, &bus_voltage)); vTaskDelay(10 / portTICK_PERIOD_MS); ESP_ERROR_CHECK(ina219_get_shunt_voltage(&ina219_b_dev, &shunt_voltage)); @@ -113,8 +123,7 @@ * a new measurement which usual gives the same results as the shunt voltage. * So just calculate the current. */ - ESP_ERROR_CHECK(ina219_configure(&ina219_b_dev, INA219_BUS_RANGE_32V, INA219_GAIN_0_125, - INA219_RES_12BIT_1S, INA219_RES_12BIT_1S, INA219_MODE_POWER_DOWN)); + ESP_ERROR_CHECK(ina219_configure(&ina219_b_dev, BUS_RANGE, GAIN_SHUNT, U_RES, I_RES, INA219_MODE_POWER_DOWN)); ESP_LOGI(TAG, "Battery VBUS: %.03f V, VSHUNT: %.02f mV, IBUS: %.01f mA", bus_voltage, shunt_voltage * 1000, shunt_voltage * 10000); } if (xSemaphoreTake(xSemaphoreINA219, 25) == pdTRUE) { @@ -137,14 +146,10 @@ } if (! ina219_state->Solar.fake) { - ESP_ERROR_CHECK(ina219_configure(&ina219_s_dev, INA219_BUS_RANGE_32V, INA219_GAIN_0_125, - INA219_RES_12BIT_1S, INA219_RES_12BIT_1S, INA219_MODE_CONT_SHUNT_BUS)); - vTaskDelay(20 / portTICK_PERIOD_MS); ESP_ERROR_CHECK(ina219_get_bus_voltage(&ina219_s_dev, &bus_voltage)); vTaskDelay(10 / portTICK_PERIOD_MS); ESP_ERROR_CHECK(ina219_get_shunt_voltage(&ina219_s_dev, &shunt_voltage)); - ESP_ERROR_CHECK(ina219_configure(&ina219_s_dev, INA219_BUS_RANGE_32V, INA219_GAIN_0_125, - INA219_RES_12BIT_1S, INA219_RES_12BIT_1S, INA219_MODE_POWER_DOWN)); + ESP_ERROR_CHECK(ina219_configure(&ina219_s_dev, BUS_RANGE, GAIN_SHUNT, U_RES, I_RES, INA219_MODE_POWER_DOWN)); ESP_LOGI(TAG, " Solar VBUS: %.03f V, VSHUNT: %.02f mV, IBUS: %.01f mA", bus_voltage, shunt_voltage * 1000, shunt_voltage * 10000); } if (xSemaphoreTake(xSemaphoreINA219, 25) == pdTRUE) {