ESP-Miner/main/system.c

634 lines
20 KiB
C
Raw Permalink Normal View History

2023-09-18 21:05:45 -04:00
#include "system.h"
2023-09-18 21:05:45 -04:00
#include "esp_log.h"
#include "i2c_master.h"
#include "EMC2101.h"
#include "INA260.h"
#include "TMP1075.h"
#include "adc.h"
#include "connect.h"
#include "led_controller.h"
#include "nvs_config.h"
#include "oled.h"
#include "vcore.h"
2023-09-18 21:05:45 -04:00
#include "driver/gpio.h"
2023-09-18 21:57:21 -04:00
#include "esp_app_desc.h"
2023-09-18 21:05:45 -04:00
#include "esp_netif.h"
#include "esp_timer.h"
#include "esp_wifi.h"
#include "lwip/inet.h"
#include <inttypes.h>
#include <math.h>
#include <stdint.h>
2023-09-18 21:05:45 -04:00
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
2023-09-18 21:05:45 -04:00
2024-06-05 23:19:09 +02:00
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "driver/gpio.h"
static const char * TAG = "SystemModule";
static void _suffix_string(uint64_t, char *, size_t, int);
static esp_netif_t * netif;
static esp_netif_ip_info_t ip_info;
2024-06-05 23:19:09 +02:00
QueueHandle_t user_input_queue;
static void _init_system(GlobalState * GLOBAL_STATE)
2023-08-26 12:28:17 -04:00
{
SystemModule * module = &GLOBAL_STATE->SYSTEM_MODULE;
2023-06-06 09:06:30 -04:00
module->duration_start = 0;
2023-06-06 14:09:51 -04:00
module->historical_hashrate_rolling_index = 0;
module->historical_hashrate_init = 0;
module->current_hashrate = 0;
module->screen_page = 0;
module->shares_accepted = 0;
module->shares_rejected = 0;
module->best_nonce_diff = nvs_config_get_u64(NVS_CONFIG_BEST_DIFF, 0);
module->best_session_nonce_diff = 0;
module->start_time = esp_timer_get_time();
module->lastClockSync = 0;
module->FOUND_BLOCK = false;
module->startup_done = false;
// set the pool url
module->pool_url = nvs_config_get_string(NVS_CONFIG_STRATUM_URL, CONFIG_STRATUM_URL);
//set the pool port
module->pool_port = nvs_config_get_u16(NVS_CONFIG_STRATUM_PORT, CONFIG_STRATUM_PORT);
2023-06-03 17:02:10 -04:00
// set the best diff string
_suffix_string(module->best_nonce_diff, module->best_diff_string, DIFF_STRING_SIZE, 0);
_suffix_string(module->best_session_nonce_diff, module->best_session_diff_string, DIFF_STRING_SIZE, 0);
2023-08-26 12:28:17 -04:00
// set the ssid string to blank
memset(module->ssid, 0, sizeof(module->ssid));
2023-08-26 12:28:17 -04:00
// set the wifi_status to blank
memset(module->wifi_status, 0, 20);
2023-08-26 12:28:17 -04:00
// test the LEDs
// ESP_LOGI(TAG, "Init LEDs!");
// ledc_init();
// led_set();
2023-08-26 12:28:17 -04:00
// Init I2C
ESP_ERROR_CHECK(i2c_master_init());
ESP_LOGI(TAG, "I2C initialized successfully");
2024-06-06 17:34:39 +02:00
// Initialize the core voltage regulator
VCORE_init(GLOBAL_STATE);
VCORE_set_voltage(nvs_config_get_u16(NVS_CONFIG_ASIC_VOLTAGE, CONFIG_ASIC_VOLTAGE) / 1000.0, GLOBAL_STATE);
2024-06-07 14:36:15 +02:00
switch (GLOBAL_STATE->device_model) {
2024-06-07 14:36:15 +02:00
case DEVICE_MAX:
case DEVICE_ULTRA:
case DEVICE_SUPRA:
EMC2101_init(nvs_config_get_u16(NVS_CONFIG_INVERT_FAN_POLARITY, 1));
break;
default:
}
2023-10-31 19:40:22 -04:00
2023-08-16 16:55:10 +02:00
vTaskDelay(500 / portTICK_PERIOD_MS);
switch (GLOBAL_STATE->device_model) {
case DEVICE_MAX:
case DEVICE_ULTRA:
case DEVICE_SUPRA:
// oled
if (!OLED_init()) {
ESP_LOGI(TAG, "OLED init failed!");
} else {
ESP_LOGI(TAG, "OLED init success!");
// clear the oled screen
OLED_fill(0);
}
break;
default:
}
netif = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
}
2024-08-01 22:58:19 +02:00
static void _show_overheat_screen(GlobalState * GLOBAL_STATE)
{
switch (GLOBAL_STATE->device_model) {
case DEVICE_MAX:
case DEVICE_ULTRA:
case DEVICE_SUPRA:
if (OLED_status()) {
OLED_clearLine(0);
OLED_clearLine(1);
OLED_clearLine(2);
OLED_clearLine(3);
OLED_writeString(0, 0, "DEVICE OVERHEATED");
OLED_writeString(0, 1, "Please check");
OLED_writeString(0, 2, "webUI for more");
OLED_writeString(0, 3, "information");
}
break;
default:
break;
}
}
static void _update_hashrate(GlobalState * GLOBAL_STATE)
2023-08-26 12:28:17 -04:00
{
SystemModule * module = &GLOBAL_STATE->SYSTEM_MODULE;
2023-06-06 14:09:51 -04:00
if (module->screen_page != 0) {
2023-08-26 12:28:17 -04:00
return;
}
2023-06-06 08:19:46 -04:00
switch (GLOBAL_STATE->device_model) {
case DEVICE_MAX:
case DEVICE_ULTRA:
case DEVICE_SUPRA:
float efficiency = GLOBAL_STATE->POWER_MANAGEMENT_MODULE.power / (module->current_hashrate / 1000.0);
OLED_clearLine(0);
memset(module->oled_buf, 0, 20);
snprintf(module->oled_buf, 20, "Gh%s: %.1f J/Th: %.1f", module->historical_hashrate_init < HISTORY_LENGTH ? "*" : "",
module->current_hashrate, efficiency);
OLED_writeString(0, 0, module->oled_buf);
break;
default:
}
2023-06-06 08:19:46 -04:00
}
static void _update_shares(GlobalState * GLOBAL_STATE)
2023-08-26 12:28:17 -04:00
{
SystemModule * module = &GLOBAL_STATE->SYSTEM_MODULE;
if (module->screen_page != 0) {
2023-08-26 12:28:17 -04:00
return;
2023-06-06 08:19:46 -04:00
}
switch (GLOBAL_STATE->device_model) {
case DEVICE_MAX:
case DEVICE_ULTRA:
case DEVICE_SUPRA:
OLED_clearLine(1);
memset(module->oled_buf, 0, 20);
snprintf(module->oled_buf, 20, "A/R: %llu/%llu", module->shares_accepted, module->shares_rejected);
OLED_writeString(0, 1, module->oled_buf);
break;
default:
}
2023-06-06 08:19:46 -04:00
}
static void _update_best_diff(GlobalState * GLOBAL_STATE)
2023-08-26 12:28:17 -04:00
{
SystemModule * module = &GLOBAL_STATE->SYSTEM_MODULE;
if (module->screen_page != 0) {
2023-08-26 12:28:17 -04:00
return;
}
switch (GLOBAL_STATE->device_model) {
case DEVICE_MAX:
case DEVICE_ULTRA:
case DEVICE_SUPRA:
OLED_clearLine(3);
memset(module->oled_buf, 0, 20);
snprintf(module->oled_buf, 20, module->FOUND_BLOCK ? "!!! BLOCK FOUND !!!" : "BD: %s", module->best_diff_string);
OLED_writeString(0, 3, module->oled_buf);
break;
default:
}
}
static void _clear_display(GlobalState * GLOBAL_STATE)
2023-08-26 12:28:17 -04:00
{
switch (GLOBAL_STATE->device_model) {
case DEVICE_MAX:
case DEVICE_ULTRA:
case DEVICE_SUPRA:
OLED_clearLine(0);
OLED_clearLine(1);
OLED_clearLine(2);
OLED_clearLine(3);
break;
default:
}
2023-06-05 14:08:25 -04:00
}
static void _update_system_info(GlobalState * GLOBAL_STATE)
2023-08-26 12:28:17 -04:00
{
SystemModule * module = &GLOBAL_STATE->SYSTEM_MODULE;
PowerManagementModule * power_management = &GLOBAL_STATE->POWER_MANAGEMENT_MODULE;
switch (GLOBAL_STATE->device_model) {
case DEVICE_MAX:
case DEVICE_ULTRA:
case DEVICE_SUPRA:
if (OLED_status()) {
2023-06-05 14:08:25 -04:00
memset(module->oled_buf, 0, 20);
Fixed fan speed web update #141 (#222) * Fixed fan speed web update #141 These changes fix fan rpm/percent requested and update both on the web * fix readme * refactor self_test to be modular for new hardware * Supra 402 (#221) * port TCH Supra 402 branch * refactor TMP1075 (unused?) driver using i2c_master module * pulled in @Bitmaker-hub stratum_task.c DNS changes from PR #185 * removing serial debug --------- Co-authored-by: Skot <skot@bitnet.cx> * adjust share accpeted/rejected functions to take higher level GLOBAL_STATE to fix share accounting. * Code clean resulting from looking into #218 (#220) * Code clean resulting from looking into #218 * Fixed asic count Set canary value for invalid device's asic_count --------- Co-authored-by: tommy <tommy@tommywatson.com> * fix another pointer error * Changes efficiency metric display in AxeOS (#231) Fixes https://github.com/skot/ESP-Miner/issues/230 * try to explain nonce space duration from paramters (#228) * try to explain nonce space duration from paramters * Fix Nonce Space duration for BM1397 (no version-rolling) * fixed issue with version mask on 1397. added easy serial debugging on 1397 * cleanup jobID debugs --------- Co-authored-by: Skot <skot@bitnet.cx> * Update bm1397.c to increase the max frequency to 650Mhz (#209) * Update bm1397.c to increase the max frequency to 650Mhz The original version was setting everything above 500Mhz to 500Mhz, the update increases the limit to 650Mhz. No changes to the web interface - drop-down still shows up to 575Mhz * Update edit.component.ts to include higher freqeuncy for BM1397 * Updated BM1397 frequencies to above 500Mhz * Update bm1397.c * Update bm1397.c * UN-Update readme.md * Update bm1397.c * Update bm1397.c * Update bm1397.c * fix: add recovery page (#232) Adds a recovery web interface to enable users to recover from a failed www.bin update. Partial fix for Issue #213. * refactor: unify merge_bin scripts (#189) Combines the functionality of merge_bin_update.sh and merge_bin_with_config.sh into merge_bin.sh. Also adds more verbose usage printing. * fix: check www.bin size before updating (#216) Adds a basic sanity check for www.bin uploading. Returns 400 if upload is attempted on a file larger than the available partition space. --------- Co-authored-by: tommy <tommy@tommywatson.com> Co-authored-by: Georges Palauqui <g.palauqui@gptechinno.com> Co-authored-by: Skot <skot@bitnet.cx> Co-authored-by: Nathan Day <87125117+dadofsambonzuki@users.noreply.github.com> Co-authored-by: yanir99 <32940160+yanir99@users.noreply.github.com> Co-authored-by: tdb3 <106488469+tdb3@users.noreply.github.com>
2024-06-20 16:23:53 -05:00
snprintf(module->oled_buf, 20, " Fan: %d RPM", power_management->fan_rpm);
OLED_writeString(0, 0, module->oled_buf);
2023-06-03 17:02:10 -04:00
memset(module->oled_buf, 0, 20);
snprintf(module->oled_buf, 20, "Temp: %.1f C", power_management->chip_temp_avg);
OLED_writeString(0, 1, module->oled_buf);
memset(module->oled_buf, 0, 20);
snprintf(module->oled_buf, 20, " Pwr: %.3f W", power_management->power);
OLED_writeString(0, 2, module->oled_buf);
memset(module->oled_buf, 0, 20);
snprintf(module->oled_buf, 20, " %i mV: %i mA", (int) power_management->voltage, (int) power_management->current);
OLED_writeString(0, 3, module->oled_buf);
}
break;
default:
}
}
2024-06-07 14:36:15 +02:00
static void _update_esp32_info(GlobalState * GLOBAL_STATE)
2023-08-26 12:28:17 -04:00
{
2024-06-07 14:36:15 +02:00
SystemModule * module = &GLOBAL_STATE->SYSTEM_MODULE;
uint32_t free_heap_size = esp_get_free_heap_size();
2024-06-07 14:36:15 +02:00
uint16_t vcore = VCORE_get_voltage_mv(GLOBAL_STATE);
switch (GLOBAL_STATE->device_model) {
case DEVICE_MAX:
case DEVICE_ULTRA:
case DEVICE_SUPRA:
if (OLED_status()) {
memset(module->oled_buf, 0, 20);
snprintf(module->oled_buf, 20, "FH: %lu bytes", free_heap_size);
OLED_writeString(0, 0, module->oled_buf);
memset(module->oled_buf, 0, 20);
snprintf(module->oled_buf, 20, "vCore: %u mV", vcore);
OLED_writeString(0, 1, module->oled_buf);
esp_netif_get_ip_info(netif, &ip_info);
char ip_address_str[IP4ADDR_STRLEN_MAX];
esp_ip4addr_ntoa(&ip_info.ip, ip_address_str, IP4ADDR_STRLEN_MAX);
memset(module->oled_buf, 0, 20);
snprintf(module->oled_buf, 20, "IP: %s", ip_address_str);
OLED_writeString(0, 2, module->oled_buf);
2023-09-18 21:57:21 -04:00
OLED_writeString(0, 3, esp_app_get_description()->version);
}
break;
default:
}
}
static void _init_connection(GlobalState * GLOBAL_STATE)
2023-08-26 12:28:17 -04:00
{
SystemModule * module = &GLOBAL_STATE->SYSTEM_MODULE;
switch (GLOBAL_STATE->device_model) {
case DEVICE_MAX:
case DEVICE_ULTRA:
case DEVICE_SUPRA:
if (OLED_status()) {
memset(module->oled_buf, 0, 20);
snprintf(module->oled_buf, 20, "Connecting to SSID:");
OLED_writeString(0, 0, module->oled_buf);
}
break;
default:
}
}
static void _update_connection(GlobalState * GLOBAL_STATE)
2023-08-26 12:28:17 -04:00
{
SystemModule * module = &GLOBAL_STATE->SYSTEM_MODULE;
switch (GLOBAL_STATE->device_model) {
case DEVICE_MAX:
case DEVICE_ULTRA:
case DEVICE_SUPRA:
if (OLED_status()) {
OLED_clearLine(2);
strncpy(module->oled_buf, module->ssid, sizeof(module->oled_buf));
module->oled_buf[sizeof(module->oled_buf) - 1] = 0;
OLED_writeString(0, 1, module->oled_buf);
memset(module->oled_buf, 0, 20);
snprintf(module->oled_buf, 20, "Configuration SSID:");
OLED_writeString(0, 2, module->oled_buf);
char ap_ssid[13];
generate_ssid(ap_ssid);
memset(module->oled_buf, 0, 20);
snprintf(module->oled_buf, 20, ap_ssid);
OLED_writeString(0, 3, module->oled_buf);
}
break;
default:
}
}
static void _update_system_performance(GlobalState * GLOBAL_STATE)
2023-08-26 12:28:17 -04:00
{
SystemModule * module = &GLOBAL_STATE->SYSTEM_MODULE;
2023-06-03 17:02:10 -04:00
// Calculate the uptime in seconds
double uptime_in_seconds = (esp_timer_get_time() - module->start_time) / 1000000;
2023-06-03 17:02:10 -04:00
int uptime_in_days = uptime_in_seconds / (3600 * 24);
int remaining_seconds = (int) uptime_in_seconds % (3600 * 24);
2023-06-04 13:16:34 -04:00
int uptime_in_hours = remaining_seconds / 3600;
remaining_seconds %= 3600;
int uptime_in_minutes = remaining_seconds / 60;
switch (GLOBAL_STATE->device_model) {
case DEVICE_MAX:
case DEVICE_ULTRA:
case DEVICE_SUPRA:
if (OLED_status()) {
_update_hashrate(GLOBAL_STATE);
_update_shares(GLOBAL_STATE);
_update_best_diff(GLOBAL_STATE);
2023-06-04 13:16:34 -04:00
memset(module->oled_buf, 0, 20);
snprintf(module->oled_buf, 20, "UT: %dd %ih %im", uptime_in_days, uptime_in_hours, uptime_in_minutes);
OLED_writeString(0, 2, module->oled_buf);
}
break;
default:
2023-06-03 17:02:10 -04:00
}
}
static void show_ap_information(const char * error, GlobalState * GLOBAL_STATE)
{
switch (GLOBAL_STATE->device_model) {
case DEVICE_MAX:
case DEVICE_ULTRA:
case DEVICE_SUPRA:
if (OLED_status()) {
_clear_display(GLOBAL_STATE);
if (error != NULL) {
OLED_writeString(0, 0, error);
}
OLED_writeString(0, 1, "Configuration SSID:");
char ap_ssid[13];
generate_ssid(ap_ssid);
OLED_writeString(0, 2, ap_ssid);
}
break;
default:
}
}
2023-08-26 12:28:17 -04:00
static double _calculate_network_difficulty(uint32_t nBits)
{
uint32_t mantissa = nBits & 0x007fffff; // Extract the mantissa from nBits
uint8_t exponent = (nBits >> 24) & 0xff; // Extract the exponent from nBits
double target = (double) mantissa * pow(256, (exponent - 3)); // Calculate the target value
2023-08-26 12:28:17 -04:00
double difficulty = (pow(2, 208) * 65535) / target; // Calculate the difficulty
2023-06-09 08:36:50 -04:00
return difficulty;
}
static void _check_for_best_diff(GlobalState * GLOBAL_STATE, double diff, uint8_t job_id)
2023-08-26 12:28:17 -04:00
{
SystemModule * module = &GLOBAL_STATE->SYSTEM_MODULE;
if ((uint64_t) diff > module->best_session_nonce_diff) {
module->best_session_nonce_diff = (uint64_t) diff;
_suffix_string((uint64_t) diff, module->best_session_diff_string, DIFF_STRING_SIZE, 0);
}
if ((uint64_t) diff <= module->best_nonce_diff) {
2023-06-09 08:36:50 -04:00
return;
}
module->best_nonce_diff = (uint64_t) diff;
2024-02-25 12:36:43 -05:00
nvs_config_set_u64(NVS_CONFIG_BEST_DIFF, module->best_nonce_diff);
2023-08-26 12:28:17 -04:00
// make the best_nonce_diff into a string
_suffix_string((uint64_t) diff, module->best_diff_string, DIFF_STRING_SIZE, 0);
2024-02-25 12:36:43 -05:00
double network_diff = _calculate_network_difficulty(GLOBAL_STATE->ASIC_TASK_MODULE.active_jobs[job_id]->target);
if (diff > network_diff) {
module->FOUND_BLOCK = true;
ESP_LOGI(TAG, "FOUND BLOCK!!!!!!!!!!!!!!!!!!!!!! %f > %f", diff, network_diff);
}
ESP_LOGI(TAG, "Network diff: %f", network_diff);
2023-06-09 08:36:50 -04:00
}
/* Convert a uint64_t value into a truncated string for displaying with its
* associated suitable for Mega, Giga etc. Buf array needs to be long enough */
static void _suffix_string(uint64_t val, char * buf, size_t bufsiz, int sigdigits)
2023-08-26 12:28:17 -04:00
{
const double dkilo = 1000.0;
const uint64_t kilo = 1000ull;
const uint64_t mega = 1000000ull;
const uint64_t giga = 1000000000ull;
const uint64_t tera = 1000000000000ull;
const uint64_t peta = 1000000000000000ull;
const uint64_t exa = 1000000000000000000ull;
char suffix[2] = "";
bool decimal = true;
double dval;
if (val >= exa) {
2023-08-26 12:28:17 -04:00
val /= peta;
dval = (double) val / dkilo;
2023-08-26 12:28:17 -04:00
strcpy(suffix, "E");
} else if (val >= peta) {
2023-08-26 12:28:17 -04:00
val /= tera;
dval = (double) val / dkilo;
2023-08-26 12:28:17 -04:00
strcpy(suffix, "P");
} else if (val >= tera) {
2023-08-26 12:28:17 -04:00
val /= giga;
dval = (double) val / dkilo;
2023-08-26 12:28:17 -04:00
strcpy(suffix, "T");
} else if (val >= giga) {
2023-08-26 12:28:17 -04:00
val /= mega;
dval = (double) val / dkilo;
2023-08-26 12:28:17 -04:00
strcpy(suffix, "G");
} else if (val >= mega) {
2023-08-26 12:28:17 -04:00
val /= kilo;
dval = (double) val / dkilo;
2023-08-26 12:28:17 -04:00
strcpy(suffix, "M");
} else if (val >= kilo) {
dval = (double) val / dkilo;
2023-08-26 12:28:17 -04:00
strcpy(suffix, "k");
} else {
2023-08-26 12:28:17 -04:00
dval = val;
decimal = false;
}
2023-06-09 08:36:50 -04:00
if (!sigdigits) {
2023-08-26 12:28:17 -04:00
if (decimal)
snprintf(buf, bufsiz, "%.3g%s", dval, suffix);
else
snprintf(buf, bufsiz, "%d%s", (unsigned int) dval, suffix);
} else {
2023-08-26 12:28:17 -04:00
/* Always show sigdigits + 1, padded on right with zeroes
* followed by suffix */
int ndigits = sigdigits - 1 - (dval > 0.0 ? floor(log10(dval)) : 0);
snprintf(buf, bufsiz, "%*.*f%s", sigdigits + 1, ndigits, dval, suffix);
}
}
2023-06-04 13:16:34 -04:00
void SYSTEM_task(void * pvParameters)
2023-08-26 12:28:17 -04:00
{
GlobalState * GLOBAL_STATE = (GlobalState *) pvParameters;
SystemModule * module = &GLOBAL_STATE->SYSTEM_MODULE;
2023-06-13 09:26:43 -04:00
_init_system(GLOBAL_STATE);
2024-06-05 23:19:09 +02:00
user_input_queue = xQueueCreate(10, sizeof(char[10])); // Create a queue to handle user input events
_clear_display(GLOBAL_STATE);
_init_connection(GLOBAL_STATE);
2024-06-05 23:19:09 +02:00
char input_event[10];
ESP_LOGI(TAG, "SYSTEM_task started");
while (GLOBAL_STATE->ASIC_functions.init_fn == NULL) {
show_ap_information("ASIC MODEL INVALID", GLOBAL_STATE);
vTaskDelay(5000 / portTICK_PERIOD_MS);
}
2023-08-26 12:28:17 -04:00
// show the connection screen
while (!module->startup_done) {
_update_connection(GLOBAL_STATE);
2024-05-24 20:00:10 -04:00
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
while (1) {
2024-08-01 22:58:19 +02:00
// Check for overheat mode
uint16_t overheat_mode = nvs_config_get_u16(NVS_CONFIG_OVERHEAT_MODE, 0);
if (overheat_mode == 1) {
_show_overheat_screen(GLOBAL_STATE);
vTaskDelay(5000 / portTICK_PERIOD_MS); // Update every 5 seconds
continue; // Skip the normal screen cycle
}
2024-06-05 23:19:09 +02:00
// Automatically cycle through screens
for (int screen = 0; screen < 3; screen++) {
_clear_display(GLOBAL_STATE);
2024-06-05 23:19:09 +02:00
module->screen_page = screen;
switch (module->screen_page) {
case 0:
_update_system_performance(GLOBAL_STATE);
break;
case 1:
_update_system_info(GLOBAL_STATE);
break;
case 2:
2024-06-07 14:36:15 +02:00
_update_esp32_info(GLOBAL_STATE);
2024-06-05 23:19:09 +02:00
break;
}
// Wait for 10 seconds or until a button press
for (int i = 0; i < 10; i++) {
if (xQueueReceive(user_input_queue, &input_event, pdMS_TO_TICKS(1000))) {
if (strcmp(input_event, "SHORT") == 0) {
ESP_LOGI(TAG, "Short button press detected, switching to next screen");
screen = (screen + 1) % 3; // Move to next screen
break;
} else if (strcmp(input_event, "LONG") == 0) {
ESP_LOGI(TAG, "Long button press detected, toggling WiFi SoftAP");
toggle_wifi_softap(); // Toggle AP
}
}
}
}
}
2023-06-06 08:19:46 -04:00
}
void SYSTEM_notify_accepted_share(GlobalState * GLOBAL_STATE)
2023-08-26 12:28:17 -04:00
{
SystemModule * module = &GLOBAL_STATE->SYSTEM_MODULE;
2023-06-06 14:09:51 -04:00
module->shares_accepted++;
_update_shares(GLOBAL_STATE);
2023-06-06 08:19:46 -04:00
}
void SYSTEM_notify_rejected_share(GlobalState * GLOBAL_STATE)
2023-08-26 12:28:17 -04:00
{
SystemModule * module = &GLOBAL_STATE->SYSTEM_MODULE;
2023-06-06 14:09:51 -04:00
module->shares_rejected++;
_update_shares(GLOBAL_STATE);
2023-06-06 08:19:46 -04:00
}
void SYSTEM_notify_mining_started(GlobalState * GLOBAL_STATE)
2023-08-26 12:28:17 -04:00
{
SystemModule * module = &GLOBAL_STATE->SYSTEM_MODULE;
2023-06-06 09:06:30 -04:00
module->duration_start = esp_timer_get_time();
2023-06-06 08:19:46 -04:00
}
void SYSTEM_notify_new_ntime(GlobalState * GLOBAL_STATE, uint32_t ntime)
2023-08-26 12:28:17 -04:00
{
SystemModule * module = &GLOBAL_STATE->SYSTEM_MODULE;
// 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(GlobalState * GLOBAL_STATE, double found_diff, uint8_t job_id)
2023-08-26 12:28:17 -04:00
{
SystemModule * module = &GLOBAL_STATE->SYSTEM_MODULE;
2023-06-06 08:19:46 -04:00
// Calculate the time difference in seconds with sub-second precision
// hashrate = (nonce_difficulty * 2^32) / time_to_find
module->historical_hashrate[module->historical_hashrate_rolling_index] = GLOBAL_STATE->initial_ASIC_difficulty;
2023-06-06 14:09:51 -04:00
module->historical_hashrate_time_stamps[module->historical_hashrate_rolling_index] = esp_timer_get_time();
2023-06-06 08:19:46 -04:00
2023-06-06 14:09:51 -04:00
module->historical_hashrate_rolling_index = (module->historical_hashrate_rolling_index + 1) % HISTORY_LENGTH;
2023-06-06 08:19:46 -04:00
// ESP_LOGI(TAG, "nonce_diff %.1f, ttf %.1f, res %.1f", nonce_diff, duration,
// historical_hashrate[historical_hashrate_rolling_index]);
if (module->historical_hashrate_init < HISTORY_LENGTH) {
2023-06-06 14:09:51 -04:00
module->historical_hashrate_init++;
} else {
module->duration_start =
module->historical_hashrate_time_stamps[(module->historical_hashrate_rolling_index + 1) % HISTORY_LENGTH];
2023-06-06 08:19:46 -04:00
}
double sum = 0;
for (int i = 0; i < module->historical_hashrate_init; i++) {
2023-06-06 14:09:51 -04:00
sum += module->historical_hashrate[i];
2023-06-06 08:19:46 -04:00
}
double duration = (double) (esp_timer_get_time() - module->duration_start) / 1000000;
2023-06-06 08:19:46 -04:00
double rolling_rate = (sum * 4294967296) / (duration * 1000000000);
if (module->historical_hashrate_init < HISTORY_LENGTH) {
2023-06-06 14:09:51 -04:00
module->current_hashrate = rolling_rate;
} else {
2023-06-06 08:19:46 -04:00
// More smoothing
2023-08-26 12:28:17 -04:00
module->current_hashrate = ((module->current_hashrate * 9) + rolling_rate) / 10;
2023-06-06 08:19:46 -04:00
}
_update_hashrate(GLOBAL_STATE);
2023-06-06 08:19:46 -04:00
// logArrayContents(historical_hashrate, HISTORY_LENGTH);
// logArrayContents(historical_hashrate_time_stamps, HISTORY_LENGTH);
2023-06-09 08:36:50 -04:00
_check_for_best_diff(GLOBAL_STATE, found_diff, job_id);
2023-06-06 08:19:46 -04:00
}