From 676f6d0602bff308c7c3092a786e4657ca10f4a2 Mon Sep 17 00:00:00 2001 From: Ben Date: Fri, 2 Jun 2023 22:05:18 -0400 Subject: [PATCH] Computing job wait time --- components/bm1397/bm1397.c | 2 +- components/bm1397/include/bm1397.h | 9 +++ components/bm1397/serial.c | 2 +- main/miner.c | 91 ++++++++++++++++-------------- 4 files changed, 60 insertions(+), 44 deletions(-) diff --git a/components/bm1397/bm1397.c b/components/bm1397/bm1397.c index 8af3d6a3..3350def5 100644 --- a/components/bm1397/bm1397.c +++ b/components/bm1397/bm1397.c @@ -16,7 +16,7 @@ #define SLEEP_TIME 20 #define FREQ_MULT 25.0 -#define BM1397_FREQUENCY CONFIG_BM1397_FREQUENCY + static const char *TAG = "bm1397"; diff --git a/components/bm1397/include/bm1397.h b/components/bm1397/include/bm1397.h index 959e3172..95b1a89d 100644 --- a/components/bm1397/include/bm1397.h +++ b/components/bm1397/include/bm1397.h @@ -23,6 +23,15 @@ #define RESPONSE_JOB 0x80 #define CRC5_MASK 0x1F +static const u_int64_t BM1397_FREQUENCY = CONFIG_BM1397_FREQUENCY; +static const u_int64_t BM1397_CORE_COUNT = 672; +static const u_int64_t BM1397_HASHRATE_S = BM1397_FREQUENCY * BM1397_CORE_COUNT * 1000000; +//2^32 +static const u_int64_t NONCE_SPACE = 4294967296; +static const double BM1397_FULLSCAN_MS = ((double)NONCE_SPACE / (double)BM1397_HASHRATE_S) * 1000; + + + typedef enum { JOB_PACKET = 0, CMD_PACKET = 1, diff --git a/components/bm1397/serial.c b/components/bm1397/serial.c index e4fccf95..25a52c67 100644 --- a/components/bm1397/serial.c +++ b/components/bm1397/serial.c @@ -55,7 +55,7 @@ int send_serial(uint8_t *data, int len, bool debug) { /// @param buf number of ms to wait before timing out /// @return number of bytes read, or -1 on error int16_t serial_rx(uint8_t * buf, uint16_t size, uint16_t timeout_ms) { - int16_t bytes_read = uart_read_bytes(UART_NUM_1, buf, size, timeout_ms / portTICK_RATE_MS); + int16_t bytes_read = uart_read_bytes(UART_NUM_1, buf, size, timeout_ms / portTICK_PERIOD_MS); // if (bytes_read > 0) { // printf("rx: "); // prettyHex((unsigned char*) buf, bytes_read); diff --git a/main/miner.c b/main/miner.c index 0db18bc0..090a51c2 100755 --- a/main/miner.c +++ b/main/miner.c @@ -25,6 +25,10 @@ #include "serial.h" #include "bm1397.h" #include +#include "EMC2101.h" +#include "INA260.h" +#include +#include #define PORT CONFIG_STRATUM_PORT @@ -34,9 +38,6 @@ #define STRATUM_DIFFICULTY CONFIG_STRATUM_DIFFICULTY -#define DEFAULT_JOB_TIMEOUT 20 //ms - - static const char *TAG = "miner"; TaskHandle_t sysTaskHandle = NULL; @@ -114,55 +115,61 @@ static void AsicTask(void * pvParameters) send_work(&job); //send the job to the ASIC //wait for a response - int received = serial_rx(buf, 9, DEFAULT_JOB_TIMEOUT); //TODO: this timeout should be 2^32/hashrate + int received = serial_rx(buf, 9, BM1397_FULLSCAN_MS); //TODO: this timeout should be 2^32/hashrate if (received < 0) { ESP_LOGI(TAG, "Error in serial RX"); continue; + } else if(received == 0){ + // Didn't find a solution, restart and try again + continue; } - if (received == 0) { + + if(received != 9 || buf[0] != 0xAA || buf[1] != 0x55){ + ESP_LOGI(TAG, "Serial RX invalid %i", received); + ESP_LOG_BUFFER_HEX(TAG, buf, received); continue; } uint8_t nonce_found = 0; uint32_t first_nonce = 0; - for (int i = 0; i <= received - 9; i++) + + struct nonce_response nonce; + memcpy((void *) &nonce, buf, sizeof(struct nonce_response)); + + if (valid_jobs[nonce.job_id] == 0) { + ESP_LOGI(TAG, "Invalid job nonce found"); + } + + //print_hex((uint8_t *) &nonce.nonce, 4, 4, "nonce: "); + if (nonce_found == 0) { + first_nonce = nonce.nonce; + nonce_found = 1; + } else if (nonce.nonce == first_nonce) { + // stop if we've already seen this nonce + break; + } + + if (nonce.nonce == prev_nonce) { + continue; + } else { + prev_nonce = nonce.nonce; + } + + // check the nonce difficulty + double nonce_diff = test_nonce_value(active_jobs[nonce.job_id], nonce.nonce); + + uint16_t fan_speed = EMC2101_get_fan_speed(); + float chip_temp = EMC2101_get_chip_temp(); + float power = INA260_read_power() / 1000; + + ESP_LOGI(TAG, "Nonce difficulty %.2f of %d. Stats: Fan: %d RPM, Temp: %.1f C, Pwr: %.1f W", nonce_diff, active_jobs[nonce.job_id]->pool_diff, fan_speed, chip_temp, power); + + if (nonce_diff > active_jobs[nonce.job_id]->pool_diff) { - if (buf[i] == 0xAA && buf[i + 1] == 0x55) { - struct nonce_response nonce; - memcpy((void *) &nonce, buf + i, sizeof(struct nonce_response)); - - if (valid_jobs[nonce.job_id] == 0) { - ESP_LOGI(TAG, "Invalid job nonce found"); - } - - //print_hex((uint8_t *) &nonce.nonce, 4, 4, "nonce: "); - if (nonce_found == 0) { - first_nonce = nonce.nonce; - nonce_found = 1; - } else if (nonce.nonce == first_nonce) { - // stop if we've already seen this nonce - break; - } - - if (nonce.nonce == prev_nonce) { - continue; - } else { - prev_nonce = nonce.nonce; - } - - // check the nonce difficulty - double nonce_diff = test_nonce_value(active_jobs[nonce.job_id], nonce.nonce); - - ESP_LOGI(TAG, "Nonce difficulty %.2f of %d", nonce_diff, active_jobs[nonce.job_id]->pool_diff); - - if (nonce_diff > active_jobs[nonce.job_id]->pool_diff) - { - //print_hex((uint8_t *)&job, sizeof(struct job_packet), sizeof(struct job_packet), "job: "); - submit_share(sock, STRATUM_USER, active_jobs[nonce.job_id]->jobid, active_jobs[nonce.job_id]->ntime, - active_jobs[nonce.job_id]->extranonce2, nonce.nonce); - } - } + //print_hex((uint8_t *)&job, sizeof(struct job_packet), sizeof(struct job_packet), "job: "); + submit_share(sock, STRATUM_USER, active_jobs[nonce.job_id]->jobid, active_jobs[nonce.job_id]->ntime, + active_jobs[nonce.job_id]->extranonce2, nonce.nonce); } } } @@ -290,7 +297,7 @@ static void admin_task(void * pvParameters) while (1) { char * line = receive_jsonrpc_line(sock); - ESP_LOGI(TAG, "%s", line); //debug incoming stratum messages + //ESP_LOGI(TAG, "%s", line); //debug incoming stratum messages stratum_method method = parse_stratum_method(line); if (method == MINING_NOTIFY) {