From 914b92675800e0da460234bc0f62d7d67099804f Mon Sep 17 00:00:00 2001 From: Ben Date: Wed, 14 Jun 2023 13:31:25 -0400 Subject: [PATCH] power soft start --- components/bm1397/bm1397.c | 8 ++++---- components/bm1397/include/bm1397.h | 2 +- main/miner.c | 10 ++++++++-- main/tasks/power_management_task.c | 25 ++++++++++++++++--------- 4 files changed, 29 insertions(+), 16 deletions(-) diff --git a/components/bm1397/bm1397.c b/components/bm1397/bm1397.c index 112ffcf8..ef538f1d 100644 --- a/components/bm1397/bm1397.c +++ b/components/bm1397/bm1397.c @@ -178,7 +178,7 @@ void BM1397_send_hash_frequency(float frequency) { } -static void _send_init(void) { +static void _send_init(u_int64_t frequency) { //send serial data vTaskDelay(SLEEP_TIME / portTICK_RATE_MS); @@ -208,7 +208,7 @@ static void _send_init(void) { BM1397_set_default_baud(); - BM1397_send_hash_frequency(BM1397_FREQUENCY); + BM1397_send_hash_frequency(frequency); } @@ -236,7 +236,7 @@ static void _send_read_address(void) { } -void BM1397_init(void) { +void BM1397_init(u_int64_t frequency) { ESP_LOGI(TAG, "Initializing BM1397"); gpio_pad_select_gpio(BM1397_RST_PIN); @@ -248,7 +248,7 @@ void BM1397_init(void) { //send the init command _send_read_address(); - _send_init(); + _send_init(frequency); } diff --git a/components/bm1397/include/bm1397.h b/components/bm1397/include/bm1397.h index b5f758e0..9acffb00 100644 --- a/components/bm1397/include/bm1397.h +++ b/components/bm1397/include/bm1397.h @@ -65,7 +65,7 @@ struct __attribute__((__packed__)) nonce_response { uint8_t crc; }; -void BM1397_init(void); +void BM1397_init(u_int64_t frequency); void BM1397_send_init(void); void BM1397_send_work(struct job_packet *job); diff --git a/main/miner.c b/main/miner.c index 9e63552b..acb5d67d 100755 --- a/main/miner.c +++ b/main/miner.c @@ -19,7 +19,11 @@ static GlobalState GLOBAL_STATE = { .extranonce_str = NULL, .extranonce_2_len = 0, .abandon_work = 0, - .version_mask = 0 + .version_mask = 0, + .POWER_MANAGEMENT_MODULE = { + .frequency_multiplier = 1, + .frequency_value = BM1397_FREQUENCY/3 + } }; @@ -28,6 +32,8 @@ static const char *TAG = "miner"; void app_main(void) { ESP_LOGI(TAG, "Welcome to the bitaxe!"); + //wait between 0 and 5 seconds for multiple units + vTaskDelay(rand() % 5001 / portTICK_RATE_MS); ESP_ERROR_CHECK(nvs_flash_init()); //ESP_ERROR_CHECK(esp_netif_init()); //ESP_ERROR_CHECK(esp_event_loop_create_default()); @@ -47,7 +53,7 @@ void app_main(void) SERIAL_init(); - BM1397_init(); + BM1397_init(GLOBAL_STATE.POWER_MANAGEMENT_MODULE.frequency_value); xTaskCreate(stratum_task, "stratum admin", 8192, (void*)&GLOBAL_STATE, 5, NULL); xTaskCreate(create_jobs_task, "stratum miner", 8192, (void*)&GLOBAL_STATE, 10, NULL); diff --git a/main/tasks/power_management_task.c b/main/tasks/power_management_task.c index a1ebe338..5fbc8281 100644 --- a/main/tasks/power_management_task.c +++ b/main/tasks/power_management_task.c @@ -30,18 +30,18 @@ static float _fbound(float value, float lower_bound, float upper_bound) return value; } -void _power_init(PowerManagementModule * power_management){ - power_management->frequency_multiplier = 1; - power_management->frequency_value = BM1397_FREQUENCY; +// void _power_init(PowerManagementModule * power_management){ +// power_management->frequency_multiplier = 1; +// power_management->frequency_value = BM1397_FREQUENCY; -} +// } void POWER_MANAGEMENT_task(void * pvParameters){ GlobalState *GLOBAL_STATE = (GlobalState*)pvParameters; //bm1397Module * bm1397 = &GLOBAL_STATE->BM1397_MODULE; PowerManagementModule * power_management = &GLOBAL_STATE->POWER_MANAGEMENT_MODULE; - _power_init(power_management); + // _power_init(power_management); int last_frequency_increase = 0; while(1){ @@ -73,6 +73,8 @@ void POWER_MANAGEMENT_task(void * pvParameters){ } } + + power_management->frequency_multiplier = lowest_multiplier; @@ -82,6 +84,12 @@ void POWER_MANAGEMENT_task(void * pvParameters){ // TODO: Turn the chip off } + // reinitialize after coming off some low voltage + if(power_management->frequency_value <= 50 && target_frequency > 50){ + power_management->frequency_value = target_frequency; + BM1397_init(target_frequency); + } + if(power_management->frequency_value > target_frequency){ power_management->frequency_value = target_frequency; @@ -90,14 +98,13 @@ void POWER_MANAGEMENT_task(void * pvParameters){ ESP_LOGI(TAG, "target %f, Freq %f, Temp %f, Power %f", target_frequency, power_management->frequency_value, power_management->chip_temp, power_management->power); }else{ if( - last_frequency_increase > 250 && + last_frequency_increase > 120 && power_management->frequency_value != BM1397_FREQUENCY ){ - float add = power_management->frequency_value - (((power_management->frequency_value * 9.0) + target_frequency)/10.0); - power_management->frequency_value += _fbound(add, 2 , 10); + power_management->frequency_value += _fbound(target_frequency, 2 , 15); BM1397_send_hash_frequency(power_management->frequency_value); ESP_LOGI(TAG, "target %f, Freq %f, Temp %f, Power %f", target_frequency, power_management->frequency_value, power_management->chip_temp, power_management->power); - last_frequency_increase = 125; + last_frequency_increase = 60; }else{ last_frequency_increase++; }