From 2c41cc92557a71099f1c4650900fd899d7bbbab1 Mon Sep 17 00:00:00 2001 From: Skot Date: Wed, 9 Oct 2024 11:52:55 -0500 Subject: [PATCH] Fix a couple missed issues from the fix_warnings PR (#393) * move ADC_init() before start_http_server() because the UI reads the ADC * split SYSTEM_init() into separate functions. cleaned up nvs init. * fix my dumb INA260_init() memory leak * SYSTEM_init_peripherals() cleanup --- components/asic/bm1366.c | 2 +- components/asic/bm1368.c | 2 +- components/asic/bm1370.c | 2 +- components/asic/bm1397.c | 2 +- components/asic/include/bm1366.h | 2 +- components/asic/include/bm1368.h | 2 +- components/asic/include/bm1370.h | 3 +- components/asic/include/bm1397.h | 2 +- main/CMakeLists.txt | 2 + main/global_state.h | 2 +- main/main.c | 142 ++++--------------- main/nvs_device.c | 147 ++++++++++++++++++++ main/nvs_device.h | 12 ++ main/self_test/self_test.c | 10 ++ main/self_test/{self_text.h => self_test.h} | 1 + main/system.c | 69 +++++---- main/system.h | 1 + main/tasks/power_management_task.c | 2 - main/vcore.c | 1 - 19 files changed, 253 insertions(+), 153 deletions(-) create mode 100644 main/nvs_device.c create mode 100644 main/nvs_device.h rename main/self_test/{self_text.h => self_test.h} (65%) diff --git a/components/asic/bm1366.c b/components/asic/bm1366.c index 691aac56a..e7b340af2 100644 --- a/components/asic/bm1366.c +++ b/components/asic/bm1366.c @@ -456,7 +456,7 @@ static uint8_t _send_init(uint64_t frequency, uint16_t asic_count) // unsigned char init137[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x14, 0x00, 0x00, 0x00, 0xFF, 0x08}; // _send_simple(init137, 11); - BM1366_set_job_difficulty_mask(BM1366_INITIAL_DIFFICULTY); + BM1366_set_job_difficulty_mask(BM1366_ASIC_DIFFICULTY); unsigned char init138[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x54, 0x00, 0x00, 0x00, 0x03, 0x1D}; _send_simple(init138, 11); diff --git a/components/asic/bm1368.c b/components/asic/bm1368.c index 52e501237..b6b71b6be 100644 --- a/components/asic/bm1368.c +++ b/components/asic/bm1368.c @@ -291,7 +291,7 @@ uint8_t BM1368_init(uint64_t frequency, uint16_t asic_count) vTaskDelay(pdMS_TO_TICKS(500)); } - BM1368_set_job_difficulty_mask(BM1368_INITIAL_DIFFICULTY); + BM1368_set_job_difficulty_mask(BM1368_ASIC_DIFFICULTY); do_frequency_ramp_up((float)frequency); diff --git a/components/asic/bm1370.c b/components/asic/bm1370.c index 18c917141..d96d6e9a0 100644 --- a/components/asic/bm1370.c +++ b/components/asic/bm1370.c @@ -264,7 +264,7 @@ static uint8_t _send_init(uint64_t frequency, uint16_t asic_count) //set ticket mask // unsigned char init11[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x14, 0x00, 0x00, 0x00, 0xFF, 0x08}; // _send_simple(init11, 11); - BM1370_set_job_difficulty_mask(BM1370_INITIAL_DIFFICULTY); + BM1370_set_job_difficulty_mask(BM1370_ASIC_DIFFICULTY); //Analog Mux Control unsigned char init12[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x54, 0x00, 0x00, 0x00, 0x03, 0x1D}; diff --git a/components/asic/bm1397.c b/components/asic/bm1397.c index d88e95079..7dccaeb74 100644 --- a/components/asic/bm1397.c +++ b/components/asic/bm1397.c @@ -257,7 +257,7 @@ static uint8_t _send_init(uint64_t frequency, uint16_t asic_count) unsigned char init4[9] = {0x00, CORE_REGISTER_CONTROL, 0x80, 0x00, 0x80, 0x74}; // init4 - init_4_? _send_BM1397((TYPE_CMD | GROUP_ALL | CMD_WRITE), init4, 6, BM1937_SERIALTX_DEBUG); - BM1397_set_job_difficulty_mask(256); + BM1397_set_job_difficulty_mask(BM1397_ASIC_DIFFICULTY); unsigned char init5[9] = {0x00, PLL3_PARAMETER, 0xC0, 0x70, 0x01, 0x11}; // init5 - pll3_parameter _send_BM1397((TYPE_CMD | GROUP_ALL | CMD_WRITE), init5, 6, BM1937_SERIALTX_DEBUG); diff --git a/components/asic/include/bm1366.h b/components/asic/include/bm1366.h index a8010ec76..afa417cb6 100644 --- a/components/asic/include/bm1366.h +++ b/components/asic/include/bm1366.h @@ -8,7 +8,7 @@ #define ASIC_BM1366_JOB_FREQUENCY_MS 2000 #define CRC5_MASK 0x1F -#define BM1366_INITIAL_DIFFICULTY 256 +#define BM1366_ASIC_DIFFICULTY 256 #define BM1366_SERIALTX_DEBUG false #define BM1366_SERIALRX_DEBUG false diff --git a/components/asic/include/bm1368.h b/components/asic/include/bm1368.h index f9046b93c..227b806da 100644 --- a/components/asic/include/bm1368.h +++ b/components/asic/include/bm1368.h @@ -8,7 +8,7 @@ #define ASIC_BM1368_JOB_FREQUENCY_MS 500 #define CRC5_MASK 0x1F -#define BM1368_INITIAL_DIFFICULTY 256 +#define BM1368_ASIC_DIFFICULTY 256 #define BM1368_SERIALTX_DEBUG false #define BM1368_SERIALRX_DEBUG false diff --git a/components/asic/include/bm1370.h b/components/asic/include/bm1370.h index 3456b1708..7cfd6fdf8 100644 --- a/components/asic/include/bm1370.h +++ b/components/asic/include/bm1370.h @@ -8,8 +8,7 @@ #define ASIC_BM1370_JOB_FREQUENCY_MS 500 #define CRC5_MASK 0x1F - -#define BM1370_INITIAL_DIFFICULTY 256 +#define BM1370_ASIC_DIFFICULTY 256 #define BM1370_SERIALTX_DEBUG true #define BM1370_SERIALRX_DEBUG false diff --git a/components/asic/include/bm1397.h b/components/asic/include/bm1397.h index 95ea4aae9..11b5e61af 100644 --- a/components/asic/include/bm1397.h +++ b/components/asic/include/bm1397.h @@ -8,7 +8,7 @@ #define ASIC_BM1397_JOB_FREQUENCY_MS 20 //not currently used #define CRC5_MASK 0x1F -#define BM1397_INITIAL_DIFFICULTY 256 +#define BM1397_ASIC_DIFFICULTY 256 #define BM1937_SERIALTX_DEBUG false #define BM1937_SERIALRX_DEBUG false diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 4d05decd5..7d00f0be0 100755 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -14,6 +14,7 @@ SRCS "TPS546.c" "vcore.c" "work_queue.c" + "nvs_device.c" "./http_server/http_server.c" "./self_test/self_test.c" "./tasks/stratum_task.c" @@ -27,6 +28,7 @@ INCLUDE_DIRS "." "tasks" "http_server" + "self_test" "../components/asic/include" "../components/connect/include" "../components/dns_server/include" diff --git a/main/global_state.h b/main/global_state.h index f88710494..8ac051e15 100644 --- a/main/global_state.h +++ b/main/global_state.h @@ -89,7 +89,7 @@ typedef struct uint16_t voltage_domain; AsicFunctions ASIC_functions; double asic_job_frequency_ms; - uint32_t initial_ASIC_difficulty; + uint32_t ASIC_difficulty; work_queue stratum_queue; work_queue ASIC_jobs_queue; diff --git a/main/main.c b/main/main.c index 0d6e5159e..dbc467bf9 100644 --- a/main/main.c +++ b/main/main.c @@ -17,6 +17,9 @@ #include "stratum_task.h" #include "user_input_task.h" #include "i2c_bitaxe.h" +#include "adc.h" +#include "nvs_device.h" +#include "self_test.h" static GlobalState GLOBAL_STATE = { .extranonce_str = NULL, @@ -27,7 +30,6 @@ static GlobalState GLOBAL_STATE = { }; static const char * TAG = "bitaxe"; -static const double NONCE_SPACE = 4294967296.0; // 2^32 void app_main(void) { @@ -40,132 +42,47 @@ void app_main(void) //wait for I2C to init vTaskDelay(100 / portTICK_PERIOD_MS); - ESP_ERROR_CHECK(nvs_flash_init()); + //Init ADC + ADC_init(); - GLOBAL_STATE.POWER_MANAGEMENT_MODULE.frequency_value = nvs_config_get_u16(NVS_CONFIG_ASIC_FREQ, CONFIG_ASIC_FREQUENCY); - ESP_LOGI(TAG, "NVS_CONFIG_ASIC_FREQ %f", (float)GLOBAL_STATE.POWER_MANAGEMENT_MODULE.frequency_value); - - GLOBAL_STATE.device_model_str = nvs_config_get_string(NVS_CONFIG_DEVICE_MODEL, ""); - if (strcmp(GLOBAL_STATE.device_model_str, "max") == 0) { - ESP_LOGI(TAG, "DEVICE: Max"); - GLOBAL_STATE.device_model = DEVICE_MAX; - GLOBAL_STATE.asic_count = 1; - GLOBAL_STATE.voltage_domain = 1; - } else if (strcmp(GLOBAL_STATE.device_model_str, "ultra") == 0) { - ESP_LOGI(TAG, "DEVICE: Ultra"); - GLOBAL_STATE.device_model = DEVICE_ULTRA; - GLOBAL_STATE.asic_count = 1; - GLOBAL_STATE.voltage_domain = 1; - } else if (strcmp(GLOBAL_STATE.device_model_str, "supra") == 0) { - ESP_LOGI(TAG, "DEVICE: Supra"); - GLOBAL_STATE.device_model = DEVICE_SUPRA; - GLOBAL_STATE.asic_count = 1; - GLOBAL_STATE.voltage_domain = 1; - } else if (strcmp(GLOBAL_STATE.device_model_str, "gamma") == 0) { - ESP_LOGI(TAG, "DEVICE: Gamma"); - GLOBAL_STATE.device_model = DEVICE_GAMMA; - GLOBAL_STATE.asic_count = 1; - GLOBAL_STATE.voltage_domain = 1; - } else { - ESP_LOGE(TAG, "Invalid DEVICE model"); - // maybe should return here to now execute anything with a faulty device parameter ! - // this stops crashes/reboots and allows dev testing without an asic - GLOBAL_STATE.device_model = DEVICE_UNKNOWN; - GLOBAL_STATE.asic_count = -1; - GLOBAL_STATE.voltage_domain = 1; - } - GLOBAL_STATE.board_version = atoi(nvs_config_get_string(NVS_CONFIG_BOARD_VERSION, "000")); - ESP_LOGI(TAG, "Found Device Model: %s", GLOBAL_STATE.device_model_str); - ESP_LOGI(TAG, "Found Board Version: %d", GLOBAL_STATE.board_version); - - GLOBAL_STATE.asic_model_str = nvs_config_get_string(NVS_CONFIG_ASIC_MODEL, ""); - if (strcmp(GLOBAL_STATE.asic_model_str, "BM1366") == 0) { - ESP_LOGI(TAG, "ASIC: %dx BM1366 (%" PRIu64 " cores)", GLOBAL_STATE.asic_count, BM1366_CORE_COUNT); - GLOBAL_STATE.asic_model = ASIC_BM1366; - AsicFunctions ASIC_functions = {.init_fn = BM1366_init, - .receive_result_fn = BM1366_proccess_work, - .set_max_baud_fn = BM1366_set_max_baud, - .set_difficulty_mask_fn = BM1366_set_job_difficulty_mask, - .send_work_fn = BM1366_send_work, - .set_version_mask = BM1366_set_version_mask}; - //GLOBAL_STATE.asic_job_frequency_ms = (NONCE_SPACE / (double) (GLOBAL_STATE.POWER_MANAGEMENT_MODULE.frequency_value * BM1366_CORE_COUNT * 1000)) / (double) GLOBAL_STATE.asic_count; // version-rolling so Small Cores have different Nonce Space - GLOBAL_STATE.asic_job_frequency_ms = ASIC_BM1366_JOB_FREQUENCY_MS; //2000ms - GLOBAL_STATE.initial_ASIC_difficulty = BM1366_INITIAL_DIFFICULTY; - - GLOBAL_STATE.ASIC_functions = ASIC_functions; - } else if (strcmp(GLOBAL_STATE.asic_model_str, "BM1370") == 0) { - ESP_LOGI(TAG, "ASIC: %dx BM1370 (%" PRIu64 " cores)", GLOBAL_STATE.asic_count, BM1370_CORE_COUNT); - GLOBAL_STATE.asic_model = ASIC_BM1370; - AsicFunctions ASIC_functions = {.init_fn = BM1370_init, - .receive_result_fn = BM1370_proccess_work, - .set_max_baud_fn = BM1370_set_max_baud, - .set_difficulty_mask_fn = BM1370_set_job_difficulty_mask, - .send_work_fn = BM1370_send_work, - .set_version_mask = BM1370_set_version_mask}; - //GLOBAL_STATE.asic_job_frequency_ms = (NONCE_SPACE / (double) (GLOBAL_STATE.POWER_MANAGEMENT_MODULE.frequency_value * BM1370_CORE_COUNT * 1000)) / (double) GLOBAL_STATE.asic_count; // version-rolling so Small Cores have different Nonce Space - GLOBAL_STATE.asic_job_frequency_ms = ASIC_BM1370_JOB_FREQUENCY_MS; //500ms - GLOBAL_STATE.initial_ASIC_difficulty = BM1370_INITIAL_DIFFICULTY; - - GLOBAL_STATE.ASIC_functions = ASIC_functions; - } else if (strcmp(GLOBAL_STATE.asic_model_str, "BM1368") == 0) { - ESP_LOGI(TAG, "ASIC: %dx BM1368 (%" PRIu64 " cores)", GLOBAL_STATE.asic_count, BM1368_CORE_COUNT); - GLOBAL_STATE.asic_model = ASIC_BM1368; - AsicFunctions ASIC_functions = {.init_fn = BM1368_init, - .receive_result_fn = BM1368_proccess_work, - .set_max_baud_fn = BM1368_set_max_baud, - .set_difficulty_mask_fn = BM1368_set_job_difficulty_mask, - .send_work_fn = BM1368_send_work, - .set_version_mask = BM1368_set_version_mask}; - //GLOBAL_STATE.asic_job_frequency_ms = (NONCE_SPACE / (double) (GLOBAL_STATE.POWER_MANAGEMENT_MODULE.frequency_value * BM1368_CORE_COUNT * 1000)) / (double) GLOBAL_STATE.asic_count; // version-rolling so Small Cores have different Nonce Space - GLOBAL_STATE.asic_job_frequency_ms = ASIC_BM1368_JOB_FREQUENCY_MS; //500ms - GLOBAL_STATE.initial_ASIC_difficulty = BM1368_INITIAL_DIFFICULTY; - - GLOBAL_STATE.ASIC_functions = ASIC_functions; - } else if (strcmp(GLOBAL_STATE.asic_model_str, "BM1397") == 0) { - ESP_LOGI(TAG, "ASIC: %dx BM1397 (%" PRIu64 " cores)", GLOBAL_STATE.asic_count, BM1397_SMALL_CORE_COUNT); - GLOBAL_STATE.asic_model = ASIC_BM1397; - AsicFunctions ASIC_functions = {.init_fn = BM1397_init, - .receive_result_fn = BM1397_proccess_work, - .set_max_baud_fn = BM1397_set_max_baud, - .set_difficulty_mask_fn = BM1397_set_job_difficulty_mask, - .send_work_fn = BM1397_send_work, - .set_version_mask = BM1397_set_version_mask}; - GLOBAL_STATE.asic_job_frequency_ms = (NONCE_SPACE / (double) (GLOBAL_STATE.POWER_MANAGEMENT_MODULE.frequency_value * BM1397_SMALL_CORE_COUNT * 1000)) / (double) GLOBAL_STATE.asic_count; // no version-rolling so same Nonce Space is splitted between Small Cores - GLOBAL_STATE.initial_ASIC_difficulty = BM1397_INITIAL_DIFFICULTY; - - GLOBAL_STATE.ASIC_functions = ASIC_functions; - } else { - ESP_LOGE(TAG, "Invalid ASIC model"); - AsicFunctions ASIC_functions = {.init_fn = NULL, - .receive_result_fn = NULL, - .set_max_baud_fn = NULL, - .set_difficulty_mask_fn = NULL, - .send_work_fn = NULL}; - GLOBAL_STATE.ASIC_functions = ASIC_functions; - // maybe should return here to now execute anything with a faulty device parameter ! + //initialize the ESP32 NVS + if (NVSDevice_init() != ESP_OK){ + ESP_LOGE(TAG, "Failed to init NVS"); + return; } - bool is_max = GLOBAL_STATE.asic_model == ASIC_BM1397; - uint64_t best_diff = nvs_config_get_u64(NVS_CONFIG_BEST_DIFF, 0); - uint16_t should_self_test = nvs_config_get_u16(NVS_CONFIG_SELF_TEST, 0); - if (should_self_test == 1 && !is_max && best_diff < 1) { + //parse the NVS config into GLOBAL_STATE + if (NVSDevice_parse_config(&GLOBAL_STATE) != ESP_OK) { + ESP_LOGE(TAG, "Failed to parse NVS config"); + return; + } + + //should we run the self test? + if (should_test(&GLOBAL_STATE)) { self_test((void *) &GLOBAL_STATE); vTaskDelay(60 * 60 * 1000 / portTICK_PERIOD_MS); } + SYSTEM_init_system(&GLOBAL_STATE); + // pull the wifi credentials and hostname out of NVS char * wifi_ssid = nvs_config_get_string(NVS_CONFIG_WIFI_SSID, WIFI_SSID); char * wifi_pass = nvs_config_get_string(NVS_CONFIG_WIFI_PASS, WIFI_PASS); char * hostname = nvs_config_get_string(NVS_CONFIG_HOSTNAME, HOSTNAME); // copy the wifi ssid to the global state - strncpy(GLOBAL_STATE.SYSTEM_MODULE.ssid, - wifi_ssid, - sizeof(GLOBAL_STATE.SYSTEM_MODULE.ssid)); + strncpy(GLOBAL_STATE.SYSTEM_MODULE.ssid, wifi_ssid, sizeof(GLOBAL_STATE.SYSTEM_MODULE.ssid)); GLOBAL_STATE.SYSTEM_MODULE.ssid[sizeof(GLOBAL_STATE.SYSTEM_MODULE.ssid)-1] = 0; // init and connect to wifi wifi_init(wifi_ssid, wifi_pass, hostname); + + SYSTEM_init_peripherals(&GLOBAL_STATE); + + xTaskCreate(SYSTEM_task, "SYSTEM_task", 4096, (void *) &GLOBAL_STATE, 3, NULL); + xTaskCreate(POWER_MANAGEMENT_task, "power mangement", 8192, (void *) &GLOBAL_STATE, 10, NULL); + + //start the API for AxeOS start_rest_server((void *) &GLOBAL_STATE); EventBits_t result_bits = wifi_connect(); @@ -195,11 +112,6 @@ void app_main(void) free(wifi_pass); free(hostname); - SYSTEM_init_system(&GLOBAL_STATE); - - xTaskCreate(SYSTEM_task, "SYSTEM_task", 4096, (void *) &GLOBAL_STATE, 3, NULL); - xTaskCreate(POWER_MANAGEMENT_task, "power mangement", 8192, (void *) &GLOBAL_STATE, 10, NULL); - // set the startup_done flag GLOBAL_STATE.SYSTEM_MODULE.startup_done = true; GLOBAL_STATE.new_stratum_version_rolling_msg = false; diff --git a/main/nvs_device.c b/main/nvs_device.c new file mode 100644 index 000000000..123e6fbc0 --- /dev/null +++ b/main/nvs_device.c @@ -0,0 +1,147 @@ +#include +#include "esp_event.h" +#include "esp_log.h" +#include "nvs_flash.h" +#include "nvs_config.h" +#include "nvs_device.h" + +#include "connect.h" +#include "global_state.h" + +static const char * TAG = "nvs_device"; +static const double NONCE_SPACE = 4294967296.0; // 2^32 + + +esp_err_t NVSDevice_init(void) { + esp_err_t err = nvs_flash_init(); + if (err == ESP_ERR_NVS_NO_FREE_PAGES) { + ESP_ERROR_CHECK(nvs_flash_erase()); + err = nvs_flash_init(); + } + return err; +} + +esp_err_t NVSDevice_get_wifi_creds(GlobalState * GLOBAL_STATE, char ** wifi_ssid, char ** wifi_pass, char ** hostname) { + // pull the wifi credentials and hostname out of NVS + *wifi_ssid = nvs_config_get_string(NVS_CONFIG_WIFI_SSID, WIFI_SSID); + *wifi_pass = nvs_config_get_string(NVS_CONFIG_WIFI_PASS, WIFI_PASS); + *hostname = nvs_config_get_string(NVS_CONFIG_HOSTNAME, HOSTNAME); + + // copy the wifi ssid to the global state + strncpy(GLOBAL_STATE->SYSTEM_MODULE.ssid, *wifi_ssid, sizeof(GLOBAL_STATE->SYSTEM_MODULE.ssid)); + GLOBAL_STATE->SYSTEM_MODULE.ssid[sizeof(GLOBAL_STATE->SYSTEM_MODULE.ssid)-1] = 0; + + return ESP_OK; +} + +esp_err_t NVSDevice_parse_config(GlobalState * GLOBAL_STATE) { + + GLOBAL_STATE->POWER_MANAGEMENT_MODULE.frequency_value = nvs_config_get_u16(NVS_CONFIG_ASIC_FREQ, CONFIG_ASIC_FREQUENCY); + ESP_LOGI(TAG, "NVS_CONFIG_ASIC_FREQ %f", (float)GLOBAL_STATE->POWER_MANAGEMENT_MODULE.frequency_value); + + GLOBAL_STATE->device_model_str = nvs_config_get_string(NVS_CONFIG_DEVICE_MODEL, ""); + if (strcmp(GLOBAL_STATE->device_model_str, "max") == 0) { + ESP_LOGI(TAG, "DEVICE: Max"); + GLOBAL_STATE->device_model = DEVICE_MAX; + GLOBAL_STATE->asic_count = 1; + GLOBAL_STATE->voltage_domain = 1; + } else if (strcmp(GLOBAL_STATE->device_model_str, "ultra") == 0) { + ESP_LOGI(TAG, "DEVICE: Ultra"); + GLOBAL_STATE->device_model = DEVICE_ULTRA; + GLOBAL_STATE->asic_count = 1; + GLOBAL_STATE->voltage_domain = 1; + } else if (strcmp(GLOBAL_STATE->device_model_str, "supra") == 0) { + ESP_LOGI(TAG, "DEVICE: Supra"); + GLOBAL_STATE->device_model = DEVICE_SUPRA; + GLOBAL_STATE->asic_count = 1; + GLOBAL_STATE->voltage_domain = 1; + } else if (strcmp(GLOBAL_STATE->device_model_str, "gamma") == 0) { + ESP_LOGI(TAG, "DEVICE: Gamma"); + GLOBAL_STATE->device_model = DEVICE_GAMMA; + GLOBAL_STATE->asic_count = 1; + GLOBAL_STATE->voltage_domain = 1; + } else { + ESP_LOGE(TAG, "Invalid DEVICE model"); + // maybe should return here to now execute anything with a faulty device parameter ! + // this stops crashes/reboots and allows dev testing without an asic + GLOBAL_STATE->device_model = DEVICE_UNKNOWN; + GLOBAL_STATE->asic_count = -1; + GLOBAL_STATE->voltage_domain = 1; + + return ESP_FAIL; + } + + GLOBAL_STATE->board_version = atoi(nvs_config_get_string(NVS_CONFIG_BOARD_VERSION, "000")); + ESP_LOGI(TAG, "Found Device Model: %s", GLOBAL_STATE->device_model_str); + ESP_LOGI(TAG, "Found Board Version: %d", GLOBAL_STATE->board_version); + + GLOBAL_STATE->asic_model_str = nvs_config_get_string(NVS_CONFIG_ASIC_MODEL, ""); + if (strcmp(GLOBAL_STATE->asic_model_str, "BM1366") == 0) { + ESP_LOGI(TAG, "ASIC: %dx BM1366 (%" PRIu64 " cores)", GLOBAL_STATE->asic_count, BM1366_CORE_COUNT); + GLOBAL_STATE->asic_model = ASIC_BM1366; + AsicFunctions ASIC_functions = {.init_fn = BM1366_init, + .receive_result_fn = BM1366_proccess_work, + .set_max_baud_fn = BM1366_set_max_baud, + .set_difficulty_mask_fn = BM1366_set_job_difficulty_mask, + .send_work_fn = BM1366_send_work, + .set_version_mask = BM1366_set_version_mask}; + //GLOBAL_STATE.asic_job_frequency_ms = (NONCE_SPACE / (double) (GLOBAL_STATE.POWER_MANAGEMENT_MODULE.frequency_value * BM1366_CORE_COUNT * 1000)) / (double) GLOBAL_STATE.asic_count; // version-rolling so Small Cores have different Nonce Space + GLOBAL_STATE->asic_job_frequency_ms = 2000; //ms + GLOBAL_STATE->ASIC_difficulty = BM1366_ASIC_DIFFICULTY; + + GLOBAL_STATE->ASIC_functions = ASIC_functions; + } else if (strcmp(GLOBAL_STATE->asic_model_str, "BM1370") == 0) { + ESP_LOGI(TAG, "ASIC: %dx BM1370 (%" PRIu64 " cores)", GLOBAL_STATE->asic_count, BM1370_CORE_COUNT); + GLOBAL_STATE->asic_model = ASIC_BM1370; + AsicFunctions ASIC_functions = {.init_fn = BM1370_init, + .receive_result_fn = BM1370_proccess_work, + .set_max_baud_fn = BM1370_set_max_baud, + .set_difficulty_mask_fn = BM1370_set_job_difficulty_mask, + .send_work_fn = BM1370_send_work, + .set_version_mask = BM1370_set_version_mask}; + //GLOBAL_STATE.asic_job_frequency_ms = (NONCE_SPACE / (double) (GLOBAL_STATE.POWER_MANAGEMENT_MODULE.frequency_value * BM1370_CORE_COUNT * 1000)) / (double) GLOBAL_STATE.asic_count; // version-rolling so Small Cores have different Nonce Space + GLOBAL_STATE->asic_job_frequency_ms = 500; //ms + GLOBAL_STATE->ASIC_difficulty = BM1370_ASIC_DIFFICULTY; + + GLOBAL_STATE->ASIC_functions = ASIC_functions; + } else if (strcmp(GLOBAL_STATE->asic_model_str, "BM1368") == 0) { + ESP_LOGI(TAG, "ASIC: %dx BM1368 (%" PRIu64 " cores)", GLOBAL_STATE->asic_count, BM1368_CORE_COUNT); + GLOBAL_STATE->asic_model = ASIC_BM1368; + AsicFunctions ASIC_functions = {.init_fn = BM1368_init, + .receive_result_fn = BM1368_proccess_work, + .set_max_baud_fn = BM1368_set_max_baud, + .set_difficulty_mask_fn = BM1368_set_job_difficulty_mask, + .send_work_fn = BM1368_send_work, + .set_version_mask = BM1368_set_version_mask}; + //GLOBAL_STATE.asic_job_frequency_ms = (NONCE_SPACE / (double) (GLOBAL_STATE.POWER_MANAGEMENT_MODULE.frequency_value * BM1368_CORE_COUNT * 1000)) / (double) GLOBAL_STATE.asic_count; // version-rolling so Small Cores have different Nonce Space + GLOBAL_STATE->asic_job_frequency_ms = 500; //ms + GLOBAL_STATE->ASIC_difficulty = BM1368_ASIC_DIFFICULTY; + + GLOBAL_STATE->ASIC_functions = ASIC_functions; + } else if (strcmp(GLOBAL_STATE->asic_model_str, "BM1397") == 0) { + ESP_LOGI(TAG, "ASIC: %dx BM1397 (%" PRIu64 " cores)", GLOBAL_STATE->asic_count, BM1397_SMALL_CORE_COUNT); + GLOBAL_STATE->asic_model = ASIC_BM1397; + AsicFunctions ASIC_functions = {.init_fn = BM1397_init, + .receive_result_fn = BM1397_proccess_work, + .set_max_baud_fn = BM1397_set_max_baud, + .set_difficulty_mask_fn = BM1397_set_job_difficulty_mask, + .send_work_fn = BM1397_send_work, + .set_version_mask = BM1397_set_version_mask}; + GLOBAL_STATE->asic_job_frequency_ms = (NONCE_SPACE / (double) (GLOBAL_STATE->POWER_MANAGEMENT_MODULE.frequency_value * BM1397_SMALL_CORE_COUNT * 1000)) / (double) GLOBAL_STATE->asic_count; // no version-rolling so same Nonce Space is splitted between Small Cores + GLOBAL_STATE->ASIC_difficulty = BM1397_ASIC_DIFFICULTY; + + GLOBAL_STATE->ASIC_functions = ASIC_functions; + } else { + ESP_LOGE(TAG, "Invalid ASIC model"); + AsicFunctions ASIC_functions = {.init_fn = NULL, + .receive_result_fn = NULL, + .set_max_baud_fn = NULL, + .set_difficulty_mask_fn = NULL, + .send_work_fn = NULL}; + GLOBAL_STATE->ASIC_functions = ASIC_functions; + // maybe should return here to not execute anything with a faulty device parameter ! + return ESP_FAIL; + } + + return ESP_OK; +} \ No newline at end of file diff --git a/main/nvs_device.h b/main/nvs_device.h new file mode 100644 index 000000000..9f6e6e4e0 --- /dev/null +++ b/main/nvs_device.h @@ -0,0 +1,12 @@ +#ifndef MAIN_NVS_DEVICE_H +#define MAIN_NVS_DEVICE_H + +#include +#include "esp_err.h" +#include "global_state.h" + +esp_err_t NVSDevice_init(void); +esp_err_t NVSDevice_parse_config(GlobalState *); +esp_err_t NVSDevice_get_wifi_creds(GlobalState *, char **, char **, char **); + +#endif // MAIN_NVS_DEVICE_H diff --git a/main/self_test/self_test.c b/main/self_test/self_test.c index 868b23cd6..9cc090d0b 100644 --- a/main/self_test/self_test.c +++ b/main/self_test/self_test.c @@ -20,6 +20,16 @@ static const char * TAG = "self_test"; +bool should_test(GlobalState * GLOBAL_STATE) { + bool is_max = GLOBAL_STATE->asic_model == ASIC_BM1397; + uint64_t best_diff = nvs_config_get_u64(NVS_CONFIG_BEST_DIFF, 0); + uint16_t should_self_test = nvs_config_get_u16(NVS_CONFIG_SELF_TEST, 0); + if (should_self_test == 1 && !is_max && best_diff < 1) { + return true; + } + return false; +} + static void display_msg(char * msg, GlobalState * GLOBAL_STATE) { SystemModule * module = &GLOBAL_STATE->SYSTEM_MODULE; diff --git a/main/self_test/self_text.h b/main/self_test/self_test.h similarity index 65% rename from main/self_test/self_text.h rename to main/self_test/self_test.h index e694fc52a..7e441ca51 100644 --- a/main/self_test/self_text.h +++ b/main/self_test/self_test.h @@ -2,5 +2,6 @@ #define SELF_TEST_H_ void self_test(void * pvParameters); +bool should_test(GlobalState * GLOBAL_STATE); #endif \ No newline at end of file diff --git a/main/system.c b/main/system.c index 21058d4b3..3394f542a 100644 --- a/main/system.c +++ b/main/system.c @@ -1,24 +1,3 @@ -#include "system.h" - -#include "esp_log.h" - -#include "i2c_bitaxe.h" -#include "EMC2101.h" -//#include "INA260.h" -#include "adc.h" -#include "connect.h" -#include "led_controller.h" -#include "nvs_config.h" -#include "oled.h" -#include "vcore.h" - -#include "driver/gpio.h" -#include "esp_app_desc.h" -#include "esp_netif.h" -#include "esp_timer.h" -#include "esp_wifi.h" -#include "lwip/inet.h" - #include #include #include @@ -30,13 +9,33 @@ #include "freertos/task.h" #include "freertos/queue.h" #include "driver/gpio.h" +#include "esp_log.h" + +#include "driver/gpio.h" +#include "esp_app_desc.h" +#include "esp_netif.h" +#include "esp_timer.h" +#include "esp_wifi.h" +#include "lwip/inet.h" + +#include "system.h" +#include "i2c_bitaxe.h" +#include "EMC2101.h" +#include "INA260.h" +#include "adc.h" +#include "connect.h" +#include "led_controller.h" +#include "nvs_config.h" +#include "oled.h" +#include "vcore.h" + + static const char * TAG = "SystemModule"; static void _suffix_string(uint64_t, char *, size_t, int); static esp_netif_t * netif; -static esp_netif_ip_info_t ip_info; QueueHandle_t user_input_queue; @@ -98,11 +97,15 @@ void SYSTEM_init_system(GlobalState * GLOBAL_STATE) // set the wifi_status to blank memset(module->wifi_status, 0, 20); +} + +void SYSTEM_init_peripherals(GlobalState * GLOBAL_STATE) { // Initialize the core voltage regulator VCORE_init(GLOBAL_STATE); VCORE_set_voltage(nvs_config_get_u16(NVS_CONFIG_ASIC_VOLTAGE, CONFIG_ASIC_VOLTAGE) / 1000.0, GLOBAL_STATE); + //init the EMC2101, if we have one switch (GLOBAL_STATE->device_model) { case DEVICE_MAX: case DEVICE_ULTRA: @@ -113,6 +116,20 @@ void SYSTEM_init_system(GlobalState * GLOBAL_STATE) default: } + //initialize the INA260, if we have one. + switch (GLOBAL_STATE->device_model) { + case DEVICE_MAX: + case DEVICE_ULTRA: + case DEVICE_SUPRA: + if (GLOBAL_STATE->board_version < 402) { + // Initialize the LED controller + INA260_init(); + } + break; + case DEVICE_GAMMA: + default: + } + vTaskDelay(500 / portTICK_PERIOD_MS); // Ensure overheat_mode config exists @@ -121,6 +138,7 @@ void SYSTEM_init_system(GlobalState * GLOBAL_STATE) ESP_LOGE(TAG, "Failed to ensure overheat_mode config"); } + //Init the OLED switch (GLOBAL_STATE->device_model) { case DEVICE_MAX: case DEVICE_ULTRA: @@ -146,8 +164,6 @@ void SYSTEM_init_system(GlobalState * GLOBAL_STATE) _init_connection(GLOBAL_STATE); } - - void SYSTEM_task(void * pvParameters) { GlobalState * GLOBAL_STATE = (GlobalState *) pvParameters; @@ -270,7 +286,7 @@ void SYSTEM_notify_found_nonce(GlobalState * GLOBAL_STATE, double found_diff, ui // Calculate the time difference in seconds with sub-second precision // hashrate = (nonce_difficulty * 2^32) / time_to_find - module->historical_hashrate[module->historical_hashrate_rolling_index] = GLOBAL_STATE->initial_ASIC_difficulty; + module->historical_hashrate[module->historical_hashrate_rolling_index] = GLOBAL_STATE->ASIC_difficulty; module->historical_hashrate_time_stamps[module->historical_hashrate_rolling_index] = esp_timer_get_time(); module->historical_hashrate_rolling_index = (module->historical_hashrate_rolling_index + 1) % HISTORY_LENGTH; @@ -314,6 +330,7 @@ void SYSTEM_notify_found_nonce(GlobalState * GLOBAL_STATE, double found_diff, ui static void _show_overheat_screen(GlobalState * GLOBAL_STATE) { SystemModule * module = &GLOBAL_STATE->SYSTEM_MODULE; + esp_netif_ip_info_t ip_info; switch (GLOBAL_STATE->device_model) { case DEVICE_MAX: @@ -460,6 +477,8 @@ static void _update_system_info(GlobalState * GLOBAL_STATE) static void _update_esp32_info(GlobalState * GLOBAL_STATE) { SystemModule * module = &GLOBAL_STATE->SYSTEM_MODULE; + + esp_netif_ip_info_t ip_info; uint32_t free_heap_size = esp_get_free_heap_size(); uint16_t vcore = VCORE_get_voltage_mv(GLOBAL_STATE); diff --git a/main/system.h b/main/system.h index d8782dc5a..46a25400a 100644 --- a/main/system.h +++ b/main/system.h @@ -4,6 +4,7 @@ #include "global_state.h" void SYSTEM_init_system(GlobalState * GLOBAL_STATE); +void SYSTEM_init_peripherals(GlobalState * GLOBAL_STATE); void SYSTEM_task(void * parameters); void SYSTEM_notify_accepted_share(GlobalState * GLOBAL_STATE); diff --git a/main/tasks/power_management_task.c b/main/tasks/power_management_task.c index 68a76b9be..c7eb7b995 100644 --- a/main/tasks/power_management_task.c +++ b/main/tasks/power_management_task.c @@ -134,8 +134,6 @@ void POWER_MANAGEMENT_task(void * pvParameters) // The power reading from the TPS546 is only it's output power. So the rest of the Bitaxe power is not accounted for. power_management->power += SUPRA_POWER_OFFSET; // Add offset for the rest of the Bitaxe power. TODO: this better. } else { - INA260_init(); - if (INA260_installed() == true) { power_management->voltage = INA260_read_voltage(); power_management->current = INA260_read_current(); diff --git a/main/vcore.c b/main/vcore.c index f91dec197..71cd18af8 100644 --- a/main/vcore.c +++ b/main/vcore.c @@ -37,7 +37,6 @@ uint8_t VCORE_init(GlobalState * global_state) { // case DEVICE_HEX: default: } - ADC_init(); return result; }