Skip to content

Commit

Permalink
Fix a couple missed issues from the fix_warnings PR (#393)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
skot authored Oct 9, 2024
1 parent 2c13f23 commit 2c41cc9
Show file tree
Hide file tree
Showing 19 changed files with 251 additions and 151 deletions.
2 changes: 1 addition & 1 deletion components/asic/bm1366.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion components/asic/bm1368.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion components/asic/bm1370.c
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
2 changes: 1 addition & 1 deletion components/asic/bm1397.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion components/asic/include/bm1366.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion components/asic/include/bm1368.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions components/asic/include/bm1370.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion components/asic/include/bm1397.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -27,6 +28,7 @@ INCLUDE_DIRS
"."
"tasks"
"http_server"
"self_test"
"../components/asic/include"
"../components/connect/include"
"../components/dns_server/include"
Expand Down
2 changes: 1 addition & 1 deletion main/global_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
142 changes: 27 additions & 115 deletions main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
{
Expand All @@ -40,132 +42,47 @@ void app_main(void)
//wait for I2C to init
vTaskDelay(100 / portTICK_PERIOD_MS);

ESP_ERROR_CHECK(nvs_flash_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;
//Init ADC
ADC_init();

//initialize the ESP32 NVS
if (NVSDevice_init() != ESP_OK){
ESP_LOGE(TAG, "Failed to init NVS");
return;
}
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 !

//parse the NVS config into GLOBAL_STATE
if (NVSDevice_parse_config(&GLOBAL_STATE) != ESP_OK) {
ESP_LOGE(TAG, "Failed to parse NVS config");
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) {
//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();

Expand Down Expand Up @@ -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;
Expand Down
Loading

0 comments on commit 2c41cc9

Please sign in to comment.