Skip to content

Commit

Permalink
migration and calibration
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Wilson committed Nov 14, 2024
1 parent 522ab2c commit 772dfd5
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 8 deletions.
2 changes: 2 additions & 0 deletions main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ SRCS
"nvs_device.c"
"./http_server/http_server.c"
"./self_test/self_test.c"
"./migration/migration.c"
"./tasks/stratum_task.c"
"./tasks/create_jobs_task.c"
"./tasks/asic_task.c"
Expand All @@ -29,6 +30,7 @@ INCLUDE_DIRS
"tasks"
"http_server"
"self_test"
"migration"
"../components/asic/include"
"../components/connect/include"
"../components/dns_server/include"
Expand Down
3 changes: 3 additions & 0 deletions main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "adc.h"
#include "nvs_device.h"
#include "self_test.h"
#include "migration.h"

static GlobalState GLOBAL_STATE = {
.extranonce_str = NULL,
Expand Down Expand Up @@ -63,6 +64,8 @@ void app_main(void)
vTaskDelay(60 * 60 * 1000 / portTICK_PERIOD_MS);
}

run_migrations((void *) &GLOBAL_STATE);

SYSTEM_init_system(&GLOBAL_STATE);

// pull the wifi credentials and hostname out of NVS
Expand Down
108 changes: 108 additions & 0 deletions main/migration/migration.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#include "global_state.h"
#include "TPS546.h"
#include "esp_log.h"
#include "nvs_config.h"
#include "nvs_flash.h"
#include "vcore.h"
#include "TPS546.h"
#include "EMC2101.h"
#include "oled.h"
#include "string.h"

static const char * TAG = "migrations";

static void do_temp_sensor_calibration(GlobalState * GLOBAL_STATE){

EMC2101_init(nvs_config_get_u16(NVS_CONFIG_INVERT_FAN_POLARITY, 1));
EMC2101_set_fan_speed(1);
EMC2101_configure_ideality(EMC2101_GAMMA_DEF_IDEALITY);
EMC2101_configure_beta_compensation(EMC2101_GAMMA_DEF_BETA);

GLOBAL_STATE->ASIC_TASK_MODULE.active_jobs = malloc(sizeof(bm_job *) * 128);
GLOBAL_STATE->valid_jobs = malloc(sizeof(uint8_t) * 128);
for (int i = 0; i < 128; i++) {
GLOBAL_STATE->ASIC_TASK_MODULE.active_jobs[i] = NULL;
GLOBAL_STATE->valid_jobs[i] = 0;
}
gpio_set_direction(GPIO_NUM_10, GPIO_MODE_OUTPUT);
gpio_set_level(GPIO_NUM_10, 0);


uint8_t result = VCORE_init(GLOBAL_STATE);
VCORE_set_voltage(nvs_config_get_u16(NVS_CONFIG_ASIC_VOLTAGE, CONFIG_ASIC_VOLTAGE) / 1000.0, GLOBAL_STATE);

SERIAL_init();


//Setup and init is done, shut vcore down and do calibration
(GLOBAL_STATE->ASIC_functions.init_fn)(0, GLOBAL_STATE->asic_count);
// Set vcore to 0
VCORE_set_voltage(0.0, GLOBAL_STATE);

vTaskDelay(1000 / portTICK_PERIOD_MS);

float air_temp = EMC2101_get_internal_temp();
//Reads high
float asic_temp_old = 256;
float asic_temp_new = EMC2101_get_external_temp();

//If our most recent reading is more than 0.25C colder than the last, we're still cooling down
while(asic_temp_new + 0.25 < asic_temp_old){
ESP_LOGI(TAG, "Cooling... %fC => %fC",asic_temp_old, asic_temp_new);
vTaskDelay(30000 / portTICK_PERIOD_MS);
asic_temp_old = asic_temp_new;
asic_temp_new = EMC2101_get_external_temp();
ESP_LOGI(TAG, "Cooling... %fC => %fC",asic_temp_old, asic_temp_new);
}

float offset = asic_temp_new - air_temp;
ESP_LOGI(TAG, "Temp Offset: %f", offset);

if(offset > 50){
SystemModule * module = &GLOBAL_STATE->SYSTEM_MODULE;
if (OLED_status()) {
memset(module->oled_buf, 0, 20);
snprintf(module->oled_buf, 20, "INVALID OFFSET");
OLED_writeString(0, 2, module->oled_buf);
}
vTaskDelay(60000 / portTICK_PERIOD_MS);
return;
}
//Multiply by 10 to add some precision, divide by 10 when get_u16
nvs_config_set_u16(NVS_CONFIG_EXTERNAL_TEMP_OFFSET, offset * 10);

esp_restart();
}

void run_migrations(void * pvParameters){

GlobalState * GLOBAL_STATE = (GlobalState *) pvParameters;



switch (GLOBAL_STATE->device_model) {
case DEVICE_MAX:
case DEVICE_ULTRA:
case DEVICE_SUPRA:
case DEVICE_GAMMA:
//Check if temp offset has been configured
if(nvs_config_get_u16(NVS_CONFIG_EXTERNAL_TEMP_OFFSET, 999) != 999){
return;
}

if (!OLED_init()) {
ESP_LOGE(TAG, "OLED init failed!");
} else {
ESP_LOGI(TAG, "OLED init success!");
// clear the oled screen
OLED_fill(0);
OLED_writeString(0, 0, "CALIBRATION...");
}

do_temp_sensor_calibration(GLOBAL_STATE);

break;
default:
}

}
6 changes: 6 additions & 0 deletions main/migration/migration.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef MIGRATION_H_
#define MIGRATION_H_

void run_migrations(void * pvParameters);

#endif
16 changes: 8 additions & 8 deletions main/self_test/self_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,6 @@ void self_test(void * pvParameters)

// Calibrate the temp sensor if need be
switch (GLOBAL_STATE->device_model) {
case DEVICE_MAX:
case DEVICE_ULTRA:
case DEVICE_SUPRA:
case DEVICE_GAMMA:
// Init the asic and send 0 freq so we don't ramp up and produce any heat. This will init the temp sensor.
(GLOBAL_STATE->ASIC_functions.init_fn)(0, GLOBAL_STATE->asic_count);
Expand Down Expand Up @@ -255,21 +252,24 @@ void self_test(void * pvParameters)

float offset = asic_temp_new - air_temp;
ESP_LOGI(TAG, "Temp Offset: %f", offset);

if(offset > 50){
display_msg("OFST 2 BIG", GLOBAL_STATE);
return;
}
//Multiply by 10 to add some precision, divide by 10 when get_u16
nvs_config_set_u16(NVS_CONFIG_EXTERNAL_TEMP_OFFSET, offset * 10);

//Re init the vcore and asic to proceed with testing
VCORE_init(GLOBAL_STATE);
VCORE_set_voltage(nvs_config_get_u16(NVS_CONFIG_ASIC_VOLTAGE, CONFIG_ASIC_VOLTAGE) / 1000.0, GLOBAL_STATE);
chips_detected = (GLOBAL_STATE->ASIC_functions.init_fn)(GLOBAL_STATE->POWER_MANAGEMENT_MODULE.frequency_value, GLOBAL_STATE->asic_count);
ESP_LOGI(TAG, "%u chips detected, %u expected", chips_detected, GLOBAL_STATE->asic_count);
break;
default:
//init the vcore and asic to proceed with testing
chips_detected = (GLOBAL_STATE->ASIC_functions.init_fn)(GLOBAL_STATE->POWER_MANAGEMENT_MODULE.frequency_value, GLOBAL_STATE->asic_count);
ESP_LOGI(TAG, "%u chips detected, %u expected", chips_detected, GLOBAL_STATE->asic_count);
}

//init the vcore and asic to proceed with testing
chips_detected = (GLOBAL_STATE->ASIC_functions.init_fn)(GLOBAL_STATE->POWER_MANAGEMENT_MODULE.frequency_value, GLOBAL_STATE->asic_count);
ESP_LOGI(TAG, "%u chips detected, %u expected", chips_detected, GLOBAL_STATE->asic_count);

int baud = (*GLOBAL_STATE->ASIC_functions.set_max_baud_fn)();
vTaskDelay(10 / portTICK_PERIOD_MS);
Expand Down

0 comments on commit 772dfd5

Please sign in to comment.