diff --git a/main/system.c b/main/system.c index 8b0985c6..9f030825 100644 --- a/main/system.c +++ b/main/system.c @@ -31,7 +31,8 @@ static void _init_system(SystemModule* module) { module->shares_accepted = 0; module->shares_rejected = 0; module->best_nonce_diff = 0; - module->start_time = time(NULL); + module->start_time = esp_timer_get_time(); + module->lastClockSync = 0; //test the LEDs // ESP_LOGI(TAG, "Init LEDs!"); @@ -171,7 +172,7 @@ static void _update_system_performance(SystemModule* module){ // Calculate the uptime in seconds - double uptime_in_seconds = difftime(time(NULL), module->start_time); + double uptime_in_seconds = (esp_timer_get_time() - module->start_time) / 1000000; int uptime_in_days = uptime_in_seconds / (3600 * 24); int remaining_seconds = (int)uptime_in_seconds % (3600 * 24); int uptime_in_hours = remaining_seconds / 3600; @@ -252,6 +253,19 @@ void SYSTEM_notify_mining_started(SystemModule* module){ module->duration_start = esp_timer_get_time(); } +void SYSTEM_notify_new_ntime(SystemModule* module, uint32_t ntime){ + // Hourly clock sync + if(module->lastClockSync + (60 * 60) > ntime){ + return; + } + ESP_LOGI(TAG, "Syncing clock"); + module->lastClockSync = ntime; + struct timeval tv; + tv.tv_sec = ntime; + tv.tv_usec = 0; + settimeofday(&tv, NULL); +} + void SYSTEM_notify_found_nonce(SystemModule* module, double pool_diff, double found_diff, uint32_t nbits){ diff --git a/main/system.h b/main/system.h index 939b613c..edc05def 100644 --- a/main/system.h +++ b/main/system.h @@ -10,12 +10,14 @@ typedef struct { double historical_hashrate[HISTORY_LENGTH]; int historical_hashrate_init; double current_hashrate; - time_t start_time; + int64_t start_time; uint16_t shares_accepted; uint16_t shares_rejected; int screen_page; char oled_buf[20]; uint32_t best_nonce_diff; + + uint32_t lastClockSync; } SystemModule; void SYSTEM_task(void *parameters); @@ -25,6 +27,6 @@ void SYSTEM_notify_accepted_share(SystemModule* module); void SYSTEM_notify_rejected_share(SystemModule* module); void SYSTEM_notify_found_nonce(SystemModule* module, double pool_diff, double found_diff, uint32_t nbits); void SYSTEM_notify_mining_started(SystemModule* module); - +void SYSTEM_notify_new_ntime(SystemModule* module, uint32_t ntime); #endif /* SYSTEM_H_ */ diff --git a/main/tasks/create_jobs_task.c b/main/tasks/create_jobs_task.c index c9846b8f..61b13679 100644 --- a/main/tasks/create_jobs_task.c +++ b/main/tasks/create_jobs_task.c @@ -30,6 +30,7 @@ void create_jobs_task(void * pvParameters) // Expire jobs after 5 minutes // This seems to be an issue with some pools (ckpool) + // ESP_LOGI(TAG, "job_time_sec %d", job_time_sec); if(job_time_sec > 60 * 5){ ESP_LOGI(TAG, "Job Expired"); break; diff --git a/main/tasks/stratum_task.c b/main/tasks/stratum_task.c index 3d44a566..eb9e7739 100644 --- a/main/tasks/stratum_task.c +++ b/main/tasks/stratum_task.c @@ -31,33 +31,6 @@ void dns_found_cb(const char * name, const ip_addr_t * ipaddr, void * callback_a bDNSFound = true; } -void obtain_time(void) -{ - // Initialize SNTP - sntp_setoperatingmode(SNTP_OPMODE_POLL); - sntp_setservername(0, "pool.ntp.org"); // Set NTP server - sntp_init(); - - // Wait for the time to be set - time_t now = 0; - struct tm timeinfo = {0}; - int retry = 0; - const int retry_count = 60; - while (timeinfo.tm_year < (2021 - 1900) && ++retry < retry_count) { - ESP_LOGI(TAG, "Waiting for system time to be set... (%d/%d)", retry, retry_count); - vTaskDelay(pdMS_TO_TICKS(1000)); - time(&now); - localtime_r(&now, &timeinfo); - } - - // Print the obtained time - if (retry < retry_count) { - ESP_LOGI(TAG, "System time is set."); - ESP_LOGI(TAG, "Current time: %s", asctime(&timeinfo)); - } else { - ESP_LOGW(TAG, "Could not set system time."); - } -} void stratum_task(void * pvParameters) { @@ -106,8 +79,6 @@ void stratum_task(void * pvParameters) break; } - obtain_time(); - STRATUM_V1_subscribe(GLOBAL_STATE->sock, &GLOBAL_STATE->extranonce_str, &GLOBAL_STATE->extranonce_2_len); STRATUM_V1_configure_version_rolling(GLOBAL_STATE->sock); @@ -128,7 +99,7 @@ void stratum_task(void * pvParameters) if (stratum_api_v1_message.method == MINING_NOTIFY) { - //ESP_LOGI(TAG, "Mining Notify"); + SYSTEM_notify_new_ntime(&GLOBAL_STATE->SYSTEM_MODULE, stratum_api_v1_message.mining_notification->ntime); if (stratum_api_v1_message.should_abandon_work && (GLOBAL_STATE->stratum_queue.count > 0 || GLOBAL_STATE->ASIC_jobs_queue.count > 0)) { ESP_LOGI(TAG, "abandoning work");