From 552795d61ee90e00af09de002ea888b20a9ccf71 Mon Sep 17 00:00:00 2001 From: Skot Croshere Date: Wed, 31 May 2023 00:19:23 -0400 Subject: [PATCH] tighten up the serial_rx loop for collecting nonces. tried to fix the ckpool diff issue -- still broken --- components/bm1397/bm1397.c | 4 ++-- components/bm1397/include/serial.h | 3 ++- components/bm1397/serial.c | 11 ++++++++--- components/stratum/include/mining.h | 1 + main/miner.c | 27 ++++++++++++++++++++------- 5 files changed, 33 insertions(+), 13 deletions(-) diff --git a/components/bm1397/bm1397.c b/components/bm1397/bm1397.c index 1a752f2a..8af3d6a3 100644 --- a/components/bm1397/bm1397.c +++ b/components/bm1397/bm1397.c @@ -207,11 +207,11 @@ static void send_hash_frequency(float frequency) { for (i = 0; i < 2; i++) { vTaskDelay(10 / portTICK_RATE_MS); - send_BM1397((TYPE_CMD | GROUP_ALL | CMD_WRITE), prefreq1, 6, true); + send_BM1397((TYPE_CMD | GROUP_ALL | CMD_WRITE), prefreq1, 6, false); } for (i = 0; i < 2; i++) { vTaskDelay(10 / portTICK_RATE_MS); - send_BM1397((TYPE_CMD | GROUP_ALL | CMD_WRITE), freqbuf, 6, true); + send_BM1397((TYPE_CMD | GROUP_ALL | CMD_WRITE), freqbuf, 6, false); } vTaskDelay(10 / portTICK_RATE_MS); diff --git a/components/bm1397/include/serial.h b/components/bm1397/include/serial.h index 7ba22edc..18e9c6f8 100644 --- a/components/bm1397/include/serial.h +++ b/components/bm1397/include/serial.h @@ -7,6 +7,7 @@ void SerialTask(void *arg); int send_serial(uint8_t *, int, bool); void init_serial(void); void debug_serial_rx(void); -int16_t serial_rx(uint8_t * buf); +int16_t serial_rx(uint8_t *, uint16_t, uint16_t); +void clear_serial_buffer(void); #endif /* SERIAL_H_ */ \ No newline at end of file diff --git a/components/bm1397/serial.c b/components/bm1397/serial.c index d84662ed..e4fccf95 100644 --- a/components/bm1397/serial.c +++ b/components/bm1397/serial.c @@ -52,9 +52,10 @@ int send_serial(uint8_t *data, int len, bool debug) { /// @brief waits for a serial response from the device /// @param buf buffer to read data into +/// @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) { - int16_t bytes_read = uart_read_bytes(UART_NUM_1, buf, BUF_SIZE, 20 / portTICK_RATE_MS); +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); // if (bytes_read > 0) { // printf("rx: "); // prettyHex((unsigned char*) buf, bytes_read); @@ -67,7 +68,7 @@ void debug_serial_rx(void) { int ret; uint8_t buf[CHUNK_SIZE]; - ret = serial_rx(buf); + ret = serial_rx(buf, 100, 20); if (ret < 0) { fprintf(stderr, "unable to read data\n"); return; @@ -77,6 +78,10 @@ void debug_serial_rx(void) { } +void clear_serial_buffer(void) { + uart_flush(UART_NUM_1); +} + void SerialTask(void *arg) { init_serial(); diff --git a/components/stratum/include/mining.h b/components/stratum/include/mining.h index 485185fb..97909e40 100644 --- a/components/stratum/include/mining.h +++ b/components/stratum/include/mining.h @@ -12,6 +12,7 @@ typedef struct { uint32_t starting_nonce; uint8_t midstate[32]; + uint32_t pool_diff; char * jobid; char * extranonce2; } bm_job; diff --git a/main/miner.c b/main/miner.c index cfc7281a..dc026308 100755 --- a/main/miner.c +++ b/main/miner.c @@ -34,6 +34,8 @@ #define STRATUM_DIFFICULTY CONFIG_STRATUM_DIFFICULTY +#define DEFAULT_JOB_TIMEOUT 20 //ms + static const char *TAG = "miner"; @@ -108,11 +110,19 @@ static void AsicTask(void * pvParameters) valid_jobs[id] = 1; pthread_mutex_unlock(&valid_jobs_lock); - send_work(&job); - int received = serial_rx(buf); - // if (received > 0) { - // ESP_LOGI(TAG, "Received %d bytes from bm1397", received); - // } + clear_serial_buffer(); + 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 related to the hash frequency + + if (received < 0) { + ESP_LOGI(TAG, "Error in serial RX"); + continue; + } + if (received == 0) { + continue; + } uint8_t nonce_found = 0; uint32_t first_nonce = 0; @@ -144,9 +154,9 @@ static void AsicTask(void * pvParameters) // 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, stratum_difficulty); + ESP_LOGI(TAG, "Nonce difficulty %.2f of %d", nonce_diff, active_jobs[nonce.job_id]->pool_diff); - if (nonce_diff > stratum_difficulty) + 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, @@ -180,6 +190,8 @@ static void mining_task(void * pvParameters) bm_job next_job = construct_bm_job(¶ms, merkle_root); + next_job.pool_diff = stratum_difficulty; //each job is tied to the _current_ difficulty + //ESP_LOGI(TAG, "bm_job: "); // print_hex((uint8_t *) &next_job.target, 4, 4, "nbits: "); // print_hex((uint8_t *) &next_job.ntime, 4, 4, "ntime: "); @@ -190,6 +202,7 @@ static void mining_task(void * pvParameters) memcpy(queued_next_job, &next_job, sizeof(bm_job)); queued_next_job->extranonce2 = strdup(extranonce_2_str); queued_next_job->jobid = strdup(params.job_id); + queue_enqueue(&g_bm_queue, queued_next_job); free(coinbase_tx);