From b700c6bbf00bf01a10b4de836d0c1439934fa5fd Mon Sep 17 00:00:00 2001 From: Benjamin Wilson Date: Fri, 20 Sep 2024 19:30:20 -0400 Subject: [PATCH] Fix overtemp and self tests for gamma (#343) --- components/asic/bm1366.c | 6 ++-- components/asic/bm1368.c | 2 +- components/asic/bm1370.c | 38 ++++++++++++--------- components/asic/bm1397.c | 5 ++- components/asic/include/bm1366.h | 2 +- components/asic/include/bm1370.h | 1 + components/asic/include/bm1397.h | 1 + main/EMC2101.c | 5 +++ main/global_state.h | 2 +- main/main.c | 10 +++++- main/self_test/self_test.c | 54 ++++++++++++++++++++++++++---- main/tasks/power_management_task.c | 6 ++-- 12 files changed, 99 insertions(+), 33 deletions(-) diff --git a/components/asic/bm1366.c b/components/asic/bm1366.c index a2186c381..536de030f 100644 --- a/components/asic/bm1366.c +++ b/components/asic/bm1366.c @@ -613,12 +613,12 @@ void BM1366_send_work(void * pvParameters, bm_job * next_bm_job) pthread_mutex_lock(&GLOBAL_STATE->valid_jobs_lock); GLOBAL_STATE->valid_jobs[job.job_id] = 1; - + pthread_mutex_unlock(&GLOBAL_STATE->valid_jobs_lock); + //debug sent jobs - this can get crazy if the interval is short - #if BM1368_DEBUG_JOBS + #if BM1366_DEBUG_JOBS ESP_LOGI(TAG, "Send Job: %02X", job.job_id); #endif - pthread_mutex_unlock(&GLOBAL_STATE->valid_jobs_lock); _send_BM1366((TYPE_JOB | GROUP_SINGLE | CMD_WRITE), &job, sizeof(BM1366_job), BM1366_DEBUG_WORK); } diff --git a/components/asic/bm1368.c b/components/asic/bm1368.c index a1563a493..802a763fc 100644 --- a/components/asic/bm1368.c +++ b/components/asic/bm1368.c @@ -348,11 +348,11 @@ void BM1368_send_work(void * pvParameters, bm_job * next_bm_job) pthread_mutex_lock(&GLOBAL_STATE->valid_jobs_lock); GLOBAL_STATE->valid_jobs[job.job_id] = 1; + pthread_mutex_unlock(&GLOBAL_STATE->valid_jobs_lock); #if BM1368_DEBUG_JOBS ESP_LOGI(TAG, "Send Job: %02X", job.job_id); #endif - pthread_mutex_unlock(&GLOBAL_STATE->valid_jobs_lock); _send_BM1368((TYPE_JOB | GROUP_SINGLE | CMD_WRITE), &job, sizeof(BM1368_job), BM1368_DEBUG_WORK); } diff --git a/components/asic/bm1370.c b/components/asic/bm1370.c index b38a4857f..bdf2c1a7f 100644 --- a/components/asic/bm1370.c +++ b/components/asic/bm1370.c @@ -96,7 +96,9 @@ static void _send_BM1370(uint8_t header, uint8_t * data, uint8_t data_len, bool } // send serial data - SERIAL_send(buf, total_length, packet_type == CMD_PACKET ? BM1370_SERIALTX_DEBUG : false); + if (SERIAL_send(buf, total_length, debug) == 0) { + ESP_LOGE(TAG, "Failed to send data to BM1370"); + } free(buf); } @@ -115,7 +117,7 @@ static void _send_chain_inactive(void) unsigned char read_address[2] = {0x00, 0x00}; // send serial data - _send_BM1370((TYPE_CMD | GROUP_ALL | CMD_INACTIVE), read_address, 2, false); + _send_BM1370((TYPE_CMD | GROUP_ALL | CMD_INACTIVE), read_address, 2, BM1370_SERIALTX_DEBUG); } static void _set_chip_address(uint8_t chipAddr) @@ -123,7 +125,7 @@ static void _set_chip_address(uint8_t chipAddr) unsigned char read_address[2] = {chipAddr, 0x00}; // send serial data - _send_BM1370((TYPE_CMD | GROUP_SINGLE | CMD_SETADDRESS), read_address, 2, false); + _send_BM1370((TYPE_CMD | GROUP_SINGLE | CMD_SETADDRESS), read_address, 2, BM1370_SERIALTX_DEBUG); } void BM1370_send_hash_frequency(int id, float target_freq, float max_diff) { @@ -174,7 +176,7 @@ void BM1370_send_hash_frequency(int id, float target_freq, float max_diff) { _send_BM1370(TYPE_CMD | GROUP_ALL | CMD_WRITE, freqbuf, 6, BM1370_SERIALTX_DEBUG); } - //ESP_LOGI(TAG, "Setting Frequency to %.2fMHz (%.2f)", target_freq, best_freq); + ESP_LOGI(TAG, "Setting Frequency to %.2fMHz (%.2f)", target_freq, best_freq); } static void do_frequency_ramp_up(float target_frequency) { @@ -189,7 +191,7 @@ static void do_frequency_ramp_up(float target_frequency) { float next_step = fminf(step, target_frequency - current); current += next_step; BM1370_send_hash_frequency(-1, current, 0.001); - vTaskDelay(pdMS_TO_TICKS(100)); + vTaskDelay(pdMS_TO_TICKS(10)); } } @@ -277,20 +279,20 @@ static uint8_t _send_init(uint64_t frequency, uint16_t asic_count) for (uint8_t i = 0; i < chip_counter; i++) { //Reg_A8 unsigned char set_a8_register[6] = {i * address_interval, 0xA8, 0x00, 0x07, 0x01, 0xF0}; - _send_BM1370((TYPE_CMD | GROUP_SINGLE | CMD_WRITE), set_a8_register, 6, false); + _send_BM1370((TYPE_CMD | GROUP_SINGLE | CMD_WRITE), set_a8_register, 6, BM1370_SERIALTX_DEBUG); //Misc Control unsigned char set_18_register[6] = {i * address_interval, 0x18, 0xF0, 0x00, 0xC1, 0x00}; - _send_BM1370((TYPE_CMD | GROUP_SINGLE | CMD_WRITE), set_18_register, 6, false); + _send_BM1370((TYPE_CMD | GROUP_SINGLE | CMD_WRITE), set_18_register, 6, BM1370_SERIALTX_DEBUG); //Core Register Control unsigned char set_3c_register_first[6] = {i * address_interval, 0x3C, 0x80, 0x00, 0x8B, 0x00}; - _send_BM1370((TYPE_CMD | GROUP_SINGLE | CMD_WRITE), set_3c_register_first, 6, false); + _send_BM1370((TYPE_CMD | GROUP_SINGLE | CMD_WRITE), set_3c_register_first, 6, BM1370_SERIALTX_DEBUG); //Core Register Control //unsigned char set_3c_register_second[6] = {i * address_interval, 0x3C, 0x80, 0x00, 0x80, 0x0C}; //from S21Pro dump unsigned char set_3c_register_second[6] = {i * address_interval, 0x3C, 0x80, 0x00, 0x80, 0x18}; - _send_BM1370((TYPE_CMD | GROUP_SINGLE | CMD_WRITE), set_3c_register_second, 6, false); + _send_BM1370((TYPE_CMD | GROUP_SINGLE | CMD_WRITE), set_3c_register_second, 6, BM1370_SERIALTX_DEBUG); //Core Register Control unsigned char set_3c_register_third[6] = {i * address_interval, 0x3C, 0x80, 0x00, 0x82, 0xAA}; - _send_BM1370((TYPE_CMD | GROUP_SINGLE | CMD_WRITE), set_3c_register_third, 6, false); + _send_BM1370((TYPE_CMD | GROUP_SINGLE | CMD_WRITE), set_3c_register_third, 6, BM1370_SERIALTX_DEBUG); } do_frequency_ramp_up(frequency); @@ -305,7 +307,7 @@ static uint8_t _send_init(uint64_t frequency, uint16_t asic_count) //unsigned char set_10_hash_counting[6] = {0x00, 0x10, 0x00, 0x00, 0x15, 0xA4}; //S21-Stock Default unsigned char set_10_hash_counting[6] = {0x00, 0x10, 0x00, 0x00, 0x1E, 0xB5}; //S21 Pro-Stock Default // unsigned char set_10_hash_counting[6] = {0x00, 0x10, 0x00, 0x0F, 0x00, 0x00}; //supposedly the "full" 32bit nonce range - _send_BM1370((TYPE_CMD | GROUP_ALL | CMD_WRITE), set_10_hash_counting, 6, false); + _send_BM1370((TYPE_CMD | GROUP_ALL | CMD_WRITE), set_10_hash_counting, 6, BM1370_SERIALTX_DEBUG); return chip_counter; } @@ -330,7 +332,7 @@ static void _send_read_address(void) unsigned char read_address[2] = {0x00, 0x00}; // send serial data - _send_BM1370((TYPE_CMD | GROUP_ALL | CMD_READ), read_address, 2, false); + _send_BM1370((TYPE_CMD | GROUP_ALL | CMD_READ), read_address, 2, BM1370_SERIALTX_DEBUG); } uint8_t BM1370_init(uint64_t frequency, uint16_t asic_count) @@ -354,7 +356,7 @@ int BM1370_set_default_baud(void) { // default divider of 26 (11010) for 115,749 unsigned char baudrate[9] = {0x00, MISC_CONTROL, 0x00, 0x00, 0b01111010, 0b00110001}; // baudrate - misc_control - _send_BM1370((TYPE_CMD | GROUP_ALL | CMD_WRITE), baudrate, 6, false); + _send_BM1370((TYPE_CMD | GROUP_ALL | CMD_WRITE), baudrate, 6, BM1370_SERIALTX_DEBUG); return 115749; } @@ -394,7 +396,7 @@ void BM1370_set_job_difficulty_mask(int difficulty) ESP_LOGI(TAG, "Setting ASIC difficulty mask to %d", difficulty); - _send_BM1370((TYPE_CMD | GROUP_ALL | CMD_WRITE), job_difficulty_mask, 6, false); + _send_BM1370((TYPE_CMD | GROUP_ALL | CMD_WRITE), job_difficulty_mask, 6, BM1370_SERIALTX_DEBUG); } static uint8_t id = 0; @@ -423,10 +425,14 @@ void BM1370_send_work(void * pvParameters, bm_job * next_bm_job) pthread_mutex_lock(&GLOBAL_STATE->valid_jobs_lock); GLOBAL_STATE->valid_jobs[job.job_id] = 1; - //ESP_LOGI(TAG, "Send Job: %02X", job.job_id); pthread_mutex_unlock(&GLOBAL_STATE->valid_jobs_lock); - _send_BM1370((TYPE_JOB | GROUP_SINGLE | CMD_WRITE), &job, sizeof(BM1370_job), false); + //debug sent jobs - this can get crazy if the interval is short + #if BM1370_DEBUG_JOBS + ESP_LOGI(TAG, "Send Job: %02X", job.job_id); + #endif + + _send_BM1370((TYPE_JOB | GROUP_SINGLE | CMD_WRITE), &job, sizeof(BM1370_job), BM1370_DEBUG_WORK); } asic_result * BM1370_receive_work(void) diff --git a/components/asic/bm1397.c b/components/asic/bm1397.c index e91327a4d..df917a36b 100644 --- a/components/asic/bm1397.c +++ b/components/asic/bm1397.c @@ -384,9 +384,12 @@ void BM1397_send_work(void *pvParameters, bm_job *next_bm_job) pthread_mutex_lock(&GLOBAL_STATE->valid_jobs_lock); GLOBAL_STATE->valid_jobs[job.job_id] = 1; - // ESP_LOGI(TAG, "Added Job: %i", job.job_id); pthread_mutex_unlock(&GLOBAL_STATE->valid_jobs_lock); + #if BM1397_DEBUG_JOBS + ESP_LOGI(TAG, "Send Job: %02X", job.job_id); + #endif + _send_BM1397((TYPE_JOB | GROUP_SINGLE | CMD_WRITE), &job, sizeof(job_packet), BM1397_DEBUG_WORK); } diff --git a/components/asic/include/bm1366.h b/components/asic/include/bm1366.h index 5f044d762..5555b4cdb 100644 --- a/components/asic/include/bm1366.h +++ b/components/asic/include/bm1366.h @@ -13,7 +13,7 @@ #define BM1366_SERIALTX_DEBUG false #define BM1366_SERIALRX_DEBUG false #define BM1366_DEBUG_WORK false //causes insane amount of debug output -#define BM1368_DEBUG_JOBS false //causes insane amount of debug output +#define BM1366_DEBUG_JOBS false //causes insane amount of debug output static const uint64_t BM1366_CORE_COUNT = 112; static const uint64_t BM1366_SMALL_CORE_COUNT = 894; diff --git a/components/asic/include/bm1370.h b/components/asic/include/bm1370.h index 55f195488..5ba0620a0 100644 --- a/components/asic/include/bm1370.h +++ b/components/asic/include/bm1370.h @@ -14,6 +14,7 @@ #define BM1370_SERIALTX_DEBUG true #define BM1370_SERIALRX_DEBUG false #define BM1370_DEBUG_WORK false //causes insane amount of debug output +#define BM1370_DEBUG_JOBS false //causes insane amount of debug output static const uint64_t BM1370_CORE_COUNT = 128; static const uint64_t BM1370_SMALL_CORE_COUNT = 2040; diff --git a/components/asic/include/bm1397.h b/components/asic/include/bm1397.h index d7708aead..be2f6061d 100644 --- a/components/asic/include/bm1397.h +++ b/components/asic/include/bm1397.h @@ -13,6 +13,7 @@ #define BM1937_SERIALTX_DEBUG false #define BM1937_SERIALRX_DEBUG false #define BM1397_DEBUG_WORK false //causes insane amount of debug output +#define BM1397_DEBUG_JOBS false //causes insane amount of debug output static const uint64_t BM1397_CORE_COUNT = 168; static const uint64_t BM1397_SMALL_CORE_COUNT = 672; diff --git a/main/EMC2101.c b/main/EMC2101.c index 82860b7f3..fa4a45bcb 100644 --- a/main/EMC2101.c +++ b/main/EMC2101.c @@ -69,6 +69,11 @@ float EMC2101_get_external_temp(void) } float result = (float) reading / 8.0; + + // Greater than 200C is probably an erroneous reading... + if (result > 200){ + return EMC2101_get_internal_temp(); + } return result; } diff --git a/main/global_state.h b/main/global_state.h index 280e665b2..90d6859e2 100644 --- a/main/global_state.h +++ b/main/global_state.h @@ -106,7 +106,7 @@ typedef struct uint32_t version_mask; int sock; - + bool ASIC_initalized; } GlobalState; #endif /* GLOBAL_STATE_H_ */ diff --git a/main/main.c b/main/main.c index 6e021c84a..d45e491b9 100644 --- a/main/main.c +++ b/main/main.c @@ -17,7 +17,13 @@ #include "stratum_task.h" #include "user_input_task.h" -static GlobalState GLOBAL_STATE = {.extranonce_str = NULL, .extranonce_2_len = 0, .abandon_work = 0, .version_mask = 0}; +static GlobalState GLOBAL_STATE = { + .extranonce_str = NULL, + .extranonce_2_len = 0, + .abandon_work = 0, + .version_mask = 0, + .ASIC_initalized = false +}; static const char * TAG = "bitaxe"; static const double NONCE_SPACE = 4294967296.0; // 2^32 @@ -195,6 +201,8 @@ void app_main(void) SERIAL_set_baud((*GLOBAL_STATE.ASIC_functions.set_max_baud_fn)()); SERIAL_clear_buffer(); + GLOBAL_STATE.ASIC_initalized = true; + xTaskCreate(stratum_task, "stratum admin", 8192, (void *) &GLOBAL_STATE, 5, NULL); xTaskCreate(create_jobs_task, "stratum miner", 8192, (void *) &GLOBAL_STATE, 10, NULL); xTaskCreate(ASIC_task, "asic", 8192, (void *) &GLOBAL_STATE, 10, NULL); diff --git a/main/self_test/self_test.c b/main/self_test/self_test.c index 406834e33..54e4c7be7 100644 --- a/main/self_test/self_test.c +++ b/main/self_test/self_test.c @@ -11,6 +11,7 @@ #include "vcore.h" #include "utils.h" #include "string.h" +#include "TPS546.h" static const char * TAG = "self_test"; @@ -51,11 +52,23 @@ static bool fan_sense_pass(GlobalState * GLOBAL_STATE) return false; } -static bool power_consumption_pass() +static bool INA260_power_consumption_pass(int target_power, int margin) { float power = INA260_read_power() / 1000; ESP_LOGI(TAG, "Power: %f", power); - if (power > 9 && power < 15) { + if (power > target_power -margin && power < target_power +margin) { + return true; + } + return false; +} + +static bool TPS546_power_consumption_pass(int target_power, int margin) +{ + float voltage = TPS546_get_vout(); + float current = TPS546_get_iout(); + float power = voltage * current; + ESP_LOGI(TAG, "Power: %f, Voltage: %f, Current %f", power, voltage, current); + if (power > target_power -margin && power < target_power +margin) { return true; } return false; @@ -143,10 +156,24 @@ void self_test(void * pvParameters) if(!DS4432U_test()){ ESP_LOGE(TAG, "DS4432 test failed!"); display_msg("DS4432U:FAIL", GLOBAL_STATE); + return; + } + }else{ + int result = TPS546_init(); + if(result != 0){ + ESP_LOGE(TAG, "TPS546 test failed!"); + display_msg("TPS546:FAIL", GLOBAL_STATE); + return; } } break; case DEVICE_GAMMA: + int result = TPS546_init(); + if(result != 0){ + ESP_LOGE(TAG, "TPS546 test failed!"); + display_msg("TPS546:FAIL", GLOBAL_STATE); + return; + } break; default: } @@ -240,12 +267,27 @@ void self_test(void * pvParameters) case DEVICE_MAX: case DEVICE_ULTRA: case DEVICE_SUPRA: - if (INA260_installed() && !power_consumption_pass()) { - ESP_LOGE(TAG, "INA260 test failed!"); - display_msg("MONITOR: FAIL", GLOBAL_STATE); - return; + if(GLOBAL_STATE->board_version != 402){ + if (!INA260_power_consumption_pass(12, 3)) { + ESP_LOGE(TAG, "INA260 Power Draw Failed"); + display_msg("POWER: FAIL", GLOBAL_STATE); + return; + } + } else { + if (!TPS546_power_consumption_pass(8, 2)) { + ESP_LOGE(TAG, "TPS546 Power Draw Failed, target %f", 8.0); + display_msg("POWER: FAIL", GLOBAL_STATE); + return; + } } break; + case DEVICE_GAMMA: + if (!TPS546_power_consumption_pass(15, 2)) { + ESP_LOGE(TAG, "TPS546 Power Draw Failed, target %f", 15.0); + display_msg("POWER: FAIL", GLOBAL_STATE); + return; + } + break; default: } diff --git a/main/tasks/power_management_task.c b/main/tasks/power_management_task.c index f72766f05..0cb545812 100644 --- a/main/tasks/power_management_task.c +++ b/main/tasks/power_management_task.c @@ -157,7 +157,7 @@ void POWER_MANAGEMENT_task(void * pvParameters) switch (GLOBAL_STATE->device_model) { case DEVICE_MAX: - power_management->chip_temp_avg = EMC2101_get_external_temp(); + power_management->chip_temp_avg = GLOBAL_STATE->ASIC_initalized ? EMC2101_get_external_temp() : -1; if ((power_management->chip_temp_avg > THROTTLE_TEMP) && (power_management->frequency_value > 50 || power_management->voltage > 1000)) { @@ -178,7 +178,7 @@ void POWER_MANAGEMENT_task(void * pvParameters) case DEVICE_SUPRA: if (GLOBAL_STATE->board_version == 402) { - power_management->chip_temp_avg = EMC2101_get_external_temp(); + power_management->chip_temp_avg = GLOBAL_STATE->ASIC_initalized ? EMC2101_get_external_temp() : -1; power_management->vr_temp = (float)TPS546_get_temperature(); } else { power_management->chip_temp_avg = EMC2101_get_internal_temp() + 5; @@ -212,7 +212,7 @@ void POWER_MANAGEMENT_task(void * pvParameters) break; case DEVICE_GAMMA: - power_management->chip_temp_avg = EMC2101_get_external_temp(); + power_management->chip_temp_avg = GLOBAL_STATE->ASIC_initalized ? EMC2101_get_external_temp() : -1; power_management->vr_temp = (float)TPS546_get_temperature(); // EMC2101 will give bad readings if the ASIC is turned off