Skip to content

Commit

Permalink
WIP: Fix job interval timing (#249)
Browse files Browse the repository at this point in the history
* add extended nonce debugging

* switch to semaphore for job timing

* clean up nonce debugging. hardcode 1366 job interval to 2s and show when new jobs are sent

* set nonce range to 0x0F0000 set job interval to 20s. better stratum debugging,

* testing out longer intervals on 1368

* looking at exact serial commands sent

* fix log value digit count

* add core from nonce debug

* set BM1366 initial chip difficulty to 32

* add version rolling debug

* improve debug printing again

* fix diff

* fix core_id extraction

* printf fix

* debug cleanup

* setup BM1368 for testing. reg10 to S21 default, job interval @ 0.5s

* set init diff on 1366 and 1368 back to 256

---------

Co-authored-by: Georges Palauqui <[email protected]>
  • Loading branch information
skot and Georges760 authored Jul 7, 2024
1 parent 248df91 commit 04c8b80
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 84 deletions.
96 changes: 39 additions & 57 deletions components/bm1397/bm1366.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ static void _send_BM1366(uint8_t header, uint8_t * data, uint8_t data_len, bool
}

// send serial data
SERIAL_send(buf, total_length, debug);
SERIAL_send(buf, total_length, BM1366_DEBUG_CHIP_SERIAL);

free(buf);
}
Expand All @@ -105,7 +105,7 @@ static void _send_simple(uint8_t * data, uint8_t total_length)
{
unsigned char * buf = malloc(total_length);
memcpy(buf, data, total_length);
SERIAL_send(buf, total_length, false);
SERIAL_send(buf, total_length, BM1366_DEBUG_CHIP_SERIAL);

free(buf);
}
Expand Down Expand Up @@ -402,38 +402,6 @@ static void do_frequency_ramp_up()
static uint8_t _send_init(uint64_t frequency, uint16_t asic_count)
{

// //send serial data
// vTaskDelay(SLEEP_TIME / portTICK_PERIOD_MS);

// unsigned char init[6] = { 0x00, 0xA4, 0x90, 0x00, 0xFF, 0xFF};

// _send_BM1366((TYPE_CMD | GROUP_ALL | CMD_WRITE), init, 6, true);

// unsigned char init18[7] = { 0x55, 0xAA, 0x52, 0x05, 0x00, 0x00, 0x0A };
// _send_simple(init18, 7);

// unsigned char init2[6] = { 0x00, 0xA8, 0x00, 0x07, 0x00, 0x00};
// _send_BM1366((TYPE_CMD | GROUP_ALL | CMD_WRITE), init2, 6, true);

// unsigned char init3[6] = { 0x00, 0x18, 0xFF, 0x0F, 0xC1, 0x00 };
// _send_BM1366((TYPE_CMD | GROUP_ALL | CMD_WRITE), init3, 6, true);

// _send_chain_inactive();
// // unsigned char init4[7] = { 0x55, 0xAA, 0x53, 0x05, 0x00, 0x00, 0x03 };
// // _send_simple(init4, 7);

// unsigned char init5[7] = { 0x55, 0xAA, 0x40, 0x05, 0x00, 0x00, 0x1C };
// _send_simple(init5, 7);

// unsigned char init6[6] = { 0x00, 0x3C, 0x80, 0x00, 0x85, 0x40 };
// _send_BM1366((TYPE_CMD | GROUP_ALL | CMD_WRITE), init6, 6, true);

// unsigned char init7[6] = { 0x00, 0x3C, 0x80, 0x00, 0x80, 0x20};
// _send_BM1366((TYPE_CMD | GROUP_ALL | CMD_WRITE), init7, 6, true);

// unsigned char init17[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0xA4, 0x90, 0x00, 0xFF, 0xFF, 0x1C};
// _send_simple(init17, 11);

unsigned char init0[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0xA4, 0x90, 0x00, 0xFF, 0xFF, 0x1C};
_send_simple(init0, 11);

Expand Down Expand Up @@ -481,8 +449,9 @@ static uint8_t _send_init(uint64_t frequency, uint16_t asic_count)
unsigned char init136[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x3C, 0x80, 0x00, 0x80, 0x20, 0x19};
_send_simple(init136, 11);

unsigned char init137[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x14, 0x00, 0x00, 0x00, 0xFF, 0x08};
_send_simple(init137, 11);
// unsigned char init137[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x14, 0x00, 0x00, 0x00, 0xFF, 0x08};
// _send_simple(init137, 11);
BM1366_set_job_difficulty_mask(BM1366_INITIAL_DIFFICULTY);

unsigned char init138[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x54, 0x00, 0x00, 0x00, 0x03, 0x1D};
_send_simple(init138, 11);
Expand Down Expand Up @@ -513,8 +482,13 @@ static uint8_t _send_init(uint64_t frequency, uint16_t asic_count)

BM1366_send_hash_frequency(frequency);

unsigned char init794[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x10, 0x00, 0x00, 0x15, 0x1C, 0x02};
_send_simple(init794, 11);
//register 10 is still a bit of a mystery. discussion: https://github.com/skot/ESP-Miner/pull/167

// unsigned char set_10_hash_counting[6] = {0x00, 0x10, 0x00, 0x00, 0x11, 0x5A}; //S19k Pro Default
// unsigned char set_10_hash_counting[6] = {0x00, 0x10, 0x00, 0x00, 0x14, 0x46}; //S19XP-Luxos Default
unsigned char set_10_hash_counting[6] = {0x00, 0x10, 0x00, 0x00, 0x15, 0x1C}; //S19XP-Stock Default
// unsigned char set_10_hash_counting[6] = {0x00, 0x10, 0x00, 0x0F, 0x00, 0x00}; //supposedly the "full" 32bit nonce range
_send_BM1366((TYPE_CMD | GROUP_ALL | CMD_WRITE), set_10_hash_counting, 6, false);

unsigned char init795[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0xA4, 0x90, 0x00, 0xFF, 0xFF, 0x1C};
_send_simple(init795, 11);
Expand Down Expand Up @@ -573,21 +547,20 @@ int BM1366_set_default_baud(void)
int BM1366_set_max_baud(void)
{

/// return 115749;

// divider of 0 for 3,125,000
ESP_LOGI(TAG, "Setting max baud of 1000000 ");

unsigned char init8[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x28, 0x11, 0x30, 0x02, 0x00, 0x03};
_send_simple(init8, 11);
return 1000000;

// // divider of 0 for 3,125,000
// ESP_LOGI(TAG, "Setting max baud of 1000000 ");

// unsigned char init8[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x28, 0x11, 0x30, 0x02, 0x00, 0x03};
// _send_simple(init8, 11);
// return 1000000;
}

void BM1366_set_job_difficulty_mask(int difficulty)
{

return;

// Default mask of 256 diff
unsigned char job_difficulty_mask[9] = {0x00, TICKET_MASK, 0b00000000, 0b00000000, 0b00000000, 0b11111111};

Expand Down Expand Up @@ -640,7 +613,9 @@ void BM1366_send_work(void * pvParameters, bm_job * next_bm_job)

pthread_mutex_lock(&GLOBAL_STATE->valid_jobs_lock);
GLOBAL_STATE->valid_jobs[job.job_id] = 1;
// ESP_LOGI(TAG, "Added Job: %i", job.job_id);

//debug sent jobs - this can get crazy if the interval is short
//ESP_LOGI(TAG, "Send Job: %02X", job.job_id);
pthread_mutex_unlock(&GLOBAL_STATE->valid_jobs_lock);

_send_BM1366((TYPE_JOB | GROUP_SINGLE | CMD_WRITE), &job, sizeof(BM1366_job), false);
Expand Down Expand Up @@ -673,6 +648,14 @@ static uint16_t reverse_uint16(uint16_t num)
return (num >> 8) | (num << 8);
}

static uint32_t reverse_uint32(uint32_t val)
{
return ((val >> 24) & 0xff) | // Move byte 3 to byte 0
((val << 8) & 0xff0000) | // Move byte 1 to byte 2
((val >> 8) & 0xff00) | // Move byte 2 to byte 1
((val << 24) & 0xff000000); // Move byte 0 to byte 3
}

task_result * BM1366_proccess_work(void * pvParameters)
{

Expand All @@ -682,23 +665,22 @@ task_result * BM1366_proccess_work(void * pvParameters)
return NULL;
}

uint8_t job_id = asic_result->job_id;
uint8_t rx_job_id = job_id & 0xf8;
ESP_LOGI(TAG, "Job ID: %02X, RX: %02X", job_id, rx_job_id);
uint8_t job_id = asic_result->job_id & 0xf8;
uint8_t core_id = (uint8_t)((reverse_uint32(asic_result->nonce) >> 25) & 0x7f); // BM1366 has 112 cores, so it should be coded on 7 bits
uint8_t small_core_id = asic_result->job_id & 0x07; // BM1366 has 8 small cores, so it should be coded on 3 bits
uint32_t version_bits = (reverse_uint16(asic_result->version) << 13); // shift the 16 bit value left 13
ESP_LOGI(TAG, "Job ID: %02X, Core: %d/%d, Ver: %08" PRIX32, job_id, core_id, small_core_id, version_bits);

GlobalState * GLOBAL_STATE = (GlobalState *) pvParameters;

if (GLOBAL_STATE->valid_jobs[rx_job_id] == 0) {
ESP_LOGE(TAG, "Invalid job nonce found, id=%d", rx_job_id);
if (GLOBAL_STATE->valid_jobs[job_id] == 0) {
ESP_LOGE(TAG, "Invalid job found, 0x%02X", job_id);
return NULL;
}

uint32_t rolled_version = GLOBAL_STATE->ASIC_TASK_MODULE.active_jobs[rx_job_id]->version;

// // // shift the 16 bit value left 13
rolled_version = (reverse_uint16(asic_result->version) << 13) | rolled_version;
uint32_t rolled_version = GLOBAL_STATE->ASIC_TASK_MODULE.active_jobs[job_id]->version | version_bits;

result.job_id = rx_job_id;
result.job_id = job_id;
result.nonce = asic_result->nonce;
result.rolled_version = rolled_version;

Expand Down
45 changes: 34 additions & 11 deletions components/bm1397/bm1368.c
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,15 @@ static uint8_t _send_init(uint64_t frequency, uint16_t asic_count)

BM1368_send_hash_frequency(frequency);

//register 10 is still a bit of a mystery. discussion: https://github.com/skot/ESP-Miner/pull/167

// unsigned char set_10_hash_counting[6] = {0x00, 0x10, 0x00, 0x00, 0x11, 0x5A}; //S19k Pro Default
// unsigned char set_10_hash_counting[6] = {0x00, 0x10, 0x00, 0x00, 0x14, 0x46}; //S19XP-Luxos Default
// unsigned char set_10_hash_counting[6] = {0x00, 0x10, 0x00, 0x00, 0x15, 0x1C}; //S19XP-Stock Default
unsigned char set_10_hash_counting[6] = {0x00, 0x10, 0x00, 0x00, 0x15, 0xA4}; //S21-Stock Default
// unsigned char set_10_hash_counting[6] = {0x00, 0x10, 0x00, 0x0F, 0x00, 0x00}; //supposedly the "full" 32bit nonce range
_send_BM1368((TYPE_CMD | GROUP_ALL | CMD_WRITE), set_10_hash_counting, 6, false);

return chip_counter;
}

Expand Down Expand Up @@ -482,7 +491,7 @@ void BM1368_send_work(void * pvParameters, bm_job * next_bm_job)

pthread_mutex_lock(&GLOBAL_STATE->valid_jobs_lock);
GLOBAL_STATE->valid_jobs[job.job_id] = 1;
// ESP_LOGI(TAG, "Added Job: %i", job.job_id);
//ESP_LOGI(TAG, "Send Job: %02X", job.job_id);
pthread_mutex_unlock(&GLOBAL_STATE->valid_jobs_lock);

_send_BM1368((TYPE_JOB | GROUP_SINGLE | CMD_WRITE), &job, sizeof(BM1368_job), false);
Expand Down Expand Up @@ -515,6 +524,14 @@ static uint16_t reverse_uint16(uint16_t num)
return (num >> 8) | (num << 8);
}

static uint32_t reverse_uint32(uint32_t val)
{
return ((val >> 24) & 0xff) | // Move byte 3 to byte 0
((val << 8) & 0xff0000) | // Move byte 1 to byte 2
((val >> 8) & 0xff00) | // Move byte 2 to byte 1
((val << 24) & 0xff000000); // Move byte 0 to byte 3
}

task_result * BM1368_proccess_work(void * pvParameters)
{

Expand All @@ -524,23 +541,29 @@ task_result * BM1368_proccess_work(void * pvParameters)
return NULL;
}

uint8_t job_id = asic_result->job_id;
uint8_t rx_job_id = ((int8_t)job_id & 0xf0) >> 1;
ESP_LOGI(TAG, "Job ID: %02X, RX: %02X", job_id, rx_job_id);
// uint8_t job_id = asic_result->job_id;
// uint8_t rx_job_id = ((int8_t)job_id & 0xf0) >> 1;
// ESP_LOGI(TAG, "Job ID: %02X, RX: %02X", job_id, rx_job_id);

// uint8_t job_id = asic_result->job_id & 0xf8;
// ESP_LOGI(TAG, "Job ID: %02X, Core: %01X", job_id, asic_result->job_id & 0x07);

uint8_t job_id = (asic_result->job_id & 0xf0) >> 1;
uint8_t core_id = (uint8_t)((reverse_uint32(asic_result->nonce) >> 25) & 0x7f); // BM1368 has 80 cores, so it should be coded on 7 bits
uint8_t small_core_id = asic_result->job_id & 0x0f; // BM1368 has 16 small cores, so it should be coded on 4 bits
uint32_t version_bits = (reverse_uint16(asic_result->version) << 13); // shift the 16 bit value left 13
ESP_LOGI(TAG, "Job ID: %02X, Core: %d/%d, Ver: %08" PRIX32, job_id, core_id, small_core_id, version_bits);

GlobalState * GLOBAL_STATE = (GlobalState *) pvParameters;

if (GLOBAL_STATE->valid_jobs[rx_job_id] == 0) {
ESP_LOGE(TAG, "Invalid job nonce found, 0x%02X", rx_job_id);
if (GLOBAL_STATE->valid_jobs[job_id] == 0) {
ESP_LOGE(TAG, "Invalid job nonce found, 0x%02X", job_id);
return NULL;
}

uint32_t rolled_version = GLOBAL_STATE->ASIC_TASK_MODULE.active_jobs[rx_job_id]->version;

// // // shift the 16 bit value left 13
rolled_version = (reverse_uint16(asic_result->version) << 13) | rolled_version;
uint32_t rolled_version = GLOBAL_STATE->ASIC_TASK_MODULE.active_jobs[job_id]->version | version_bits;

result.job_id = rx_job_id;
result.job_id = job_id;
result.nonce = asic_result->nonce;
result.rolled_version = rolled_version;

Expand Down
2 changes: 2 additions & 0 deletions components/bm1397/include/bm1366.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#define CRC5_MASK 0x1F
#define BM1366_INITIAL_DIFFICULTY 256

#define BM1366_DEBUG_CHIP_SERIAL false

static const uint64_t BM1366_CORE_COUNT = 112;
static const uint64_t BM1366_SMALL_CORE_COUNT = 894;

Expand Down
12 changes: 11 additions & 1 deletion components/stratum/stratum_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -356,13 +356,23 @@ void STRATUM_V1_configure_version_rolling(int socket, uint32_t * version_mask)
"{\"id\": %d, \"method\": \"mining.configure\", \"params\": [[\"version-rolling\"], {\"version-rolling.mask\": "
"\"ffffffff\"}]}\n",
send_uid++);
ESP_LOGI(TAG, "tx: %s", configure_msg);
debug_stratum_tx(configure_msg);
write(socket, configure_msg, strlen(configure_msg));

return;
}

static void debug_stratum_tx(const char * msg)
{
//remove the trailing newline
char * newline = strchr(msg, '\n');
if (newline != NULL) {
*newline = '\0';
}
ESP_LOGI(TAG, "tx: %s", msg);

//put it back!
if (newline != NULL) {
*newline = '\n';
}
}
16 changes: 9 additions & 7 deletions main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@

static GlobalState GLOBAL_STATE = {.extranonce_str = NULL, .extranonce_2_len = 0, .abandon_work = 0, .version_mask = 0};

static const char * TAG = "miner";
static const char * TAG = "bitaxe";
static const double NONCE_SPACE = 4294967296.0; // 2^32

void app_main(void)
{
ESP_LOGI(TAG, "Welcome to the bitaxe - hack the planet!");
ESP_ERROR_CHECK(nvs_flash_init());

GLOBAL_STATE.POWER_MANAGEMENT_MODULE.frequency_value = nvs_config_get_u16(NVS_CONFIG_ASIC_FREQ, CONFIG_ASIC_FREQUENCY);
Expand Down Expand Up @@ -59,31 +60,33 @@ void app_main(void)

GLOBAL_STATE.asic_model_str = nvs_config_get_string(NVS_CONFIG_ASIC_MODEL, "");
if (strcmp(GLOBAL_STATE.asic_model_str, "BM1366") == 0) {
ESP_LOGI(TAG, "ASIC: BM1366");
ESP_LOGI(TAG, "ASIC: %dx BM1366 (%" PRIu64 " cores)", GLOBAL_STATE.asic_count, BM1366_CORE_COUNT);
GLOBAL_STATE.asic_model = ASIC_BM1366;
AsicFunctions ASIC_functions = {.init_fn = BM1366_init,
.receive_result_fn = BM1366_proccess_work,
.set_max_baud_fn = BM1366_set_max_baud,
.set_difficulty_mask_fn = BM1366_set_job_difficulty_mask,
.send_work_fn = BM1366_send_work};
GLOBAL_STATE.asic_job_frequency_ms = (NONCE_SPACE / (double) (GLOBAL_STATE.POWER_MANAGEMENT_MODULE.frequency_value * BM1366_SMALL_CORE_COUNT * 1000)) / (double) GLOBAL_STATE.asic_count; // version-rolling so Small Cores have different Nonce Space
//GLOBAL_STATE.asic_job_frequency_ms = (NONCE_SPACE / (double) (GLOBAL_STATE.POWER_MANAGEMENT_MODULE.frequency_value * BM1366_CORE_COUNT * 1000)) / (double) GLOBAL_STATE.asic_count; // version-rolling so Small Cores have different Nonce Space
GLOBAL_STATE.asic_job_frequency_ms = 2000; //ms
GLOBAL_STATE.initial_ASIC_difficulty = BM1366_INITIAL_DIFFICULTY;

GLOBAL_STATE.ASIC_functions = ASIC_functions;
} else if (strcmp(GLOBAL_STATE.asic_model_str, "BM1368") == 0) {
ESP_LOGI(TAG, "ASIC: BM1368");
ESP_LOGI(TAG, "ASIC: %dx BM1368 (%" PRIu64 " cores)", GLOBAL_STATE.asic_count, BM1368_CORE_COUNT);
GLOBAL_STATE.asic_model = ASIC_BM1368;
AsicFunctions ASIC_functions = {.init_fn = BM1368_init,
.receive_result_fn = BM1368_proccess_work,
.set_max_baud_fn = BM1368_set_max_baud,
.set_difficulty_mask_fn = BM1368_set_job_difficulty_mask,
.send_work_fn = BM1368_send_work};
GLOBAL_STATE.asic_job_frequency_ms = (NONCE_SPACE / (double) (GLOBAL_STATE.POWER_MANAGEMENT_MODULE.frequency_value * BM1368_SMALL_CORE_COUNT * 1000)) / (double) GLOBAL_STATE.asic_count; // version-rolling so Small Cores have different Nonce Space
//GLOBAL_STATE.asic_job_frequency_ms = (NONCE_SPACE / (double) (GLOBAL_STATE.POWER_MANAGEMENT_MODULE.frequency_value * BM1368_CORE_COUNT * 1000)) / (double) GLOBAL_STATE.asic_count; // version-rolling so Small Cores have different Nonce Space
GLOBAL_STATE.asic_job_frequency_ms = 500; //ms
GLOBAL_STATE.initial_ASIC_difficulty = BM1368_INITIAL_DIFFICULTY;

GLOBAL_STATE.ASIC_functions = ASIC_functions;
} else if (strcmp(GLOBAL_STATE.asic_model_str, "BM1397") == 0) {
ESP_LOGI(TAG, "ASIC: BM1397");
ESP_LOGI(TAG, "ASIC: %dx BM1397 (%" PRIu64 " cores)", GLOBAL_STATE.asic_count, BM1397_SMALL_CORE_COUNT);
GLOBAL_STATE.asic_model = ASIC_BM1397;
AsicFunctions ASIC_functions = {.init_fn = BM1397_init,
.receive_result_fn = BM1397_proccess_work,
Expand Down Expand Up @@ -115,7 +118,6 @@ void app_main(void)

xTaskCreate(SYSTEM_task, "SYSTEM_task", 4096, (void *) &GLOBAL_STATE, 3, NULL);
xTaskCreate(POWER_MANAGEMENT_task, "power mangement", 8192, (void *) &GLOBAL_STATE, 10, NULL);
ESP_LOGI(TAG, "Welcome to the bitaxe!");

// pull the wifi credentials and hostname out of NVS
char * wifi_ssid = nvs_config_get_string(NVS_CONFIG_WIFI_SSID, WIFI_SSID);
Expand Down
5 changes: 3 additions & 2 deletions main/tasks/asic_result_task.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void ASIC_result_task(void *pvParameters)

if (GLOBAL_STATE->valid_jobs[job_id] == 0)
{
ESP_LOGI(TAG, "Invalid job nonce found, id=%d", job_id);
ESP_LOGI(TAG, "Invalid job nonce found, 0x%02X", job_id);
continue;
}

Expand All @@ -39,7 +39,8 @@ void ASIC_result_task(void *pvParameters)
asic_result->nonce,
asic_result->rolled_version);

ESP_LOGI(TAG, "Nonce difficulty %.2f of %ld.", nonce_diff, GLOBAL_STATE->ASIC_TASK_MODULE.active_jobs[job_id]->pool_diff);
//log the ASIC response
ESP_LOGI(TAG, "Ver: %08" PRIX32 " Nonce %08" PRIX32 " diff %.1f of %ld.", asic_result->rolled_version, asic_result->nonce, nonce_diff, GLOBAL_STATE->ASIC_TASK_MODULE.active_jobs[job_id]->pool_diff);

if (nonce_diff > GLOBAL_STATE->ASIC_TASK_MODULE.active_jobs[job_id]->pool_diff)
{
Expand Down
Loading

0 comments on commit 04c8b80

Please sign in to comment.