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
This commit is contained in:
Skot
2024-10-09 11:52:55 -05:00
committed by GitHub
parent 2c13f23b54
commit 2c41cc9255
19 changed files with 253 additions and 153 deletions

View File

@@ -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);

View File

@@ -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);

View File

@@ -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};

View File

@@ -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);

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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"

View File

@@ -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;

View File

@@ -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;

147
main/nvs_device.c Normal file
View File

@@ -0,0 +1,147 @@
#include <string.h>
#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;
}

12
main/nvs_device.h Normal file
View File

@@ -0,0 +1,12 @@
#ifndef MAIN_NVS_DEVICE_H
#define MAIN_NVS_DEVICE_H
#include <stdbool.h>
#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

View File

@@ -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;

View File

@@ -2,5 +2,6 @@
#define SELF_TEST_H_
void self_test(void * pvParameters);
bool should_test(GlobalState * GLOBAL_STATE);
#endif

View File

@@ -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 <inttypes.h>
#include <math.h>
#include <stdint.h>
@@ -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);

View File

@@ -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);

View File

@@ -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();

View File

@@ -37,7 +37,6 @@ uint8_t VCORE_init(GlobalState * global_state) {
// case DEVICE_HEX:
default:
}
ADC_init();
return result;
}