tighten up the serial_rx loop for collecting nonces. tried to fix the ckpool diff issue -- still broken

This commit is contained in:
Skot Croshere 2023-05-31 00:19:23 -04:00 committed by johnny9
parent 8c2079b8db
commit 552795d61e
5 changed files with 33 additions and 13 deletions

View File

@ -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);

View File

@ -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_ */

View File

@ -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();

View File

@ -12,6 +12,7 @@ typedef struct {
uint32_t starting_nonce;
uint8_t midstate[32];
uint32_t pool_diff;
char * jobid;
char * extranonce2;
} bm_job;

View File

@ -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(&params, 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);