From bc365e630158f418f8096e2aef3ec22aba2af8ef Mon Sep 17 00:00:00 2001 From: Georges Palauqui Date: Fri, 7 Jun 2024 14:36:15 +0200 Subject: [PATCH] make a better usage of vcore module --- main/http_server/http_server.c | 3 ++- main/self_test/self_test.c | 10 +++++----- main/system.c | 22 ++++++++++++++-------- main/vcore.c | 29 ++++++++++++++++++++--------- main/vcore.h | 4 +++- 5 files changed, 44 insertions(+), 24 deletions(-) diff --git a/main/http_server/http_server.c b/main/http_server/http_server.c index 71cb19c..407f612 100644 --- a/main/http_server/http_server.c +++ b/main/http_server/http_server.c @@ -13,6 +13,7 @@ #include "freertos/task.h" #include "global_state.h" #include "nvs_config.h" +#include "vcore.h" #include #include #include @@ -368,7 +369,7 @@ static esp_err_t GET_system_info(httpd_req_t * req) cJSON_AddNumberToObject(root, "freeHeap", esp_get_free_heap_size()); cJSON_AddNumberToObject(root, "coreVoltage", nvs_config_get_u16(NVS_CONFIG_ASIC_VOLTAGE, CONFIG_ASIC_VOLTAGE)); - cJSON_AddNumberToObject(root, "coreVoltageActual", ADC_get_vcore()); + cJSON_AddNumberToObject(root, "coreVoltageActual", VCORE_get_voltage_mv(GLOBAL_STATE)); cJSON_AddNumberToObject(root, "frequency", nvs_config_get_u16(NVS_CONFIG_ASIC_FREQ, CONFIG_ASIC_FREQUENCY)); cJSON_AddStringToObject(root, "ssid", ssid); cJSON_AddStringToObject(root, "hostname", hostname); diff --git a/main/self_test/self_test.c b/main/self_test/self_test.c index f2f183b..85f6a68 100644 --- a/main/self_test/self_test.c +++ b/main/self_test/self_test.c @@ -33,9 +33,9 @@ static bool power_consumption_pass() return false; } -static bool core_voltage_pass() +static bool core_voltage_pass(GlobalState * global_state) { - uint16_t core_voltage = ADC_get_vcore(); + uint16_t core_voltage = VCORE_get_voltage_mv(global_state); ESP_LOGI(TAG, "Voltage: %u", core_voltage); if (core_voltage > 1100 && core_voltage < 1300) { @@ -67,8 +67,8 @@ void self_test(void * pvParameters) ESP_ERROR_CHECK(i2c_master_init()); ESP_LOGI(TAG, "I2C initialized successfully"); - ADC_init(); - VCORE_set_voltage(nvs_config_get_u16(NVS_CONFIG_ASIC_VOLTAGE, CONFIG_ASIC_VOLTAGE) / 1000.0, *GLOBAL_STATE); + VCORE_init(GLOBAL_STATE); + VCORE_set_voltage(nvs_config_get_u16(NVS_CONFIG_ASIC_VOLTAGE, CONFIG_ASIC_VOLTAGE) / 1000.0, GLOBAL_STATE); EMC2101_init(nvs_config_get_u16(NVS_CONFIG_INVERT_FAN_POLARITY, 1)); EMC2101_set_fan_speed(1); @@ -179,7 +179,7 @@ void self_test(void * pvParameters) free(GLOBAL_STATE->ASIC_TASK_MODULE.active_jobs); free(GLOBAL_STATE->valid_jobs); - if (!core_voltage_pass()) { + if (!core_voltage_pass(GLOBAL_STATE)) { if (OLED_status()) { memset(module->oled_buf, 0, 20); snprintf(module->oled_buf, 20, "POWER: FAIL"); diff --git a/main/system.c b/main/system.c index 00a99ff..363cb20 100644 --- a/main/system.c +++ b/main/system.c @@ -82,12 +82,17 @@ static void _init_system(GlobalState * global_state, SystemModule * module) ESP_ERROR_CHECK(i2c_master_init()); ESP_LOGI(TAG, "I2C initialized successfully"); - ADC_init(); - - VCORE_set_voltage(nvs_config_get_u16(NVS_CONFIG_ASIC_VOLTAGE, CONFIG_ASIC_VOLTAGE) / 1000.0, *global_state); - - EMC2101_init(nvs_config_get_u16(NVS_CONFIG_INVERT_FAN_POLARITY, 1)); + VCORE_init(global_state); + VCORE_set_voltage(nvs_config_get_u16(NVS_CONFIG_ASIC_VOLTAGE, CONFIG_ASIC_VOLTAGE) / 1000.0, global_state); + switch (global_state->device_model) { + case DEVICE_MAX: + case DEVICE_ULTRA: + case DEVICE_SUPRA: + EMC2101_init(nvs_config_get_u16(NVS_CONFIG_INVERT_FAN_POLARITY, 1)); + break; + default: + } vTaskDelay(500 / portTICK_PERIOD_MS); @@ -174,11 +179,12 @@ static void _update_system_info(GlobalState * GLOBAL_STATE) } } -static void _update_esp32_info(SystemModule * module) +static void _update_esp32_info(GlobalState * GLOBAL_STATE) { + SystemModule * module = &GLOBAL_STATE->SYSTEM_MODULE; uint32_t free_heap_size = esp_get_free_heap_size(); - uint16_t vcore = ADC_get_vcore(); + uint16_t vcore = VCORE_get_voltage_mv(GLOBAL_STATE); if (OLED_status()) { @@ -405,7 +411,7 @@ void SYSTEM_task(void * pvParameters) _update_system_info(GLOBAL_STATE); break; case 2: - _update_esp32_info(module); + _update_esp32_info(GLOBAL_STATE); break; } diff --git a/main/vcore.c b/main/vcore.c index 9a92f15..7bd2798 100644 --- a/main/vcore.c +++ b/main/vcore.c @@ -3,6 +3,7 @@ #include "esp_log.h" #include "vcore.h" +#include "adc.h" #include "DS4432U.h" // DS4432U Transfer function constants for Bitaxe board @@ -16,6 +17,10 @@ static const char *TAG = "vcore.c"; +void VCORE_init(GlobalState * global_state) { + ADC_init(); +} + /** * @brief ds4432_tps40305_bitaxe_voltage_to_reg takes a voltage and returns a register setting for the DS4432U to get that voltage on the TPS40305 * careful with this one!! @@ -44,19 +49,25 @@ static uint8_t ds4432_tps40305_bitaxe_voltage_to_reg(float vout) return reg; } -bool VCORE_set_voltage(float core_voltage, GlobalState global_state) +bool VCORE_set_voltage(float core_voltage, GlobalState * global_state) { uint8_t reg_setting; - if ((global_state.device_model == DEVICE_MAX) || - (global_state.device_model == DEVICE_ULTRA) || - (global_state.device_model == DEVICE_SUPRA)) { - reg_setting = ds4432_tps40305_bitaxe_voltage_to_reg(core_voltage); - ESP_LOGI(TAG, "Set ASIC voltage = %.3fV [0x%02X]", core_voltage, reg_setting); - DS4432U_set_current_code(0, reg_setting); /// eek! + switch (global_state->device_model) { + case DEVICE_MAX: + case DEVICE_ULTRA: + case DEVICE_SUPRA: + reg_setting = ds4432_tps40305_bitaxe_voltage_to_reg(core_voltage); + ESP_LOGI(TAG, "Set ASIC voltage = %.3fV [0x%02X]", core_voltage, reg_setting); + DS4432U_set_current_code(0, reg_setting); /// eek! + break; + // case DEVICE_HEX: + default: } - // can make other fancy 'else if' based on other device_model or specific version that have different HW to set VCore value - // for example DEVICE_HEX will have a different way to set VCore using TPS546... return true; } + +uint16_t VCORE_get_voltage_mv(GlobalState * global_state) { + return ADC_get_vcore(); +} diff --git a/main/vcore.h b/main/vcore.h index db517e7..0ee7e77 100644 --- a/main/vcore.h +++ b/main/vcore.h @@ -3,6 +3,8 @@ #include "global_state.h" -bool VCORE_set_voltage(float core_voltage, GlobalState global_state); +void VCORE_init(GlobalState * global_state); +bool VCORE_set_voltage(float core_voltage, GlobalState * global_state); +uint16_t VCORE_get_voltage_mv(GlobalState * global_state); #endif /* VCORE_H_ */