diff --git a/components/asic/bm1368.c b/components/asic/bm1368.c index 6ee5ce9b5..a1563a493 100644 --- a/components/asic/bm1368.c +++ b/components/asic/bm1368.c @@ -1,5 +1,4 @@ #include "bm1368.h" - #include "crc.h" #include "global_state.h" #include "serial.h" @@ -57,36 +56,24 @@ typedef struct __attribute__((__packed__)) static const char * TAG = "bm1368Module"; -static uint8_t asic_response_buffer[SERIAL_BUF_SIZE]; +static uint8_t asic_response_buffer[CHUNK_SIZE]; static task_result result; -/// @brief -/// @param ftdi -/// @param header -/// @param data -/// @param len +static float current_frequency = 56.25; + static void _send_BM1368(uint8_t header, uint8_t * data, uint8_t data_len, bool debug) { packet_type_t packet_type = (header & TYPE_JOB) ? JOB_PACKET : CMD_PACKET; uint8_t total_length = (packet_type == JOB_PACKET) ? (data_len + 6) : (data_len + 5); - // allocate memory for buffer unsigned char * buf = malloc(total_length); - // add the preamble buf[0] = 0x55; buf[1] = 0xAA; - - // add the header field buf[2] = header; - - // add the length field buf[3] = (packet_type == JOB_PACKET) ? (data_len + 4) : (data_len + 3); - - // add the data memcpy(buf + 4, data, data_len); - // add the correct crc type if (packet_type == JOB_PACKET) { uint16_t crc16_total = crc16_false(buf + 2, data_len + 2); buf[4 + data_len] = (crc16_total >> 8) & 0xFF; @@ -95,7 +82,6 @@ static void _send_BM1368(uint8_t header, uint8_t * data, uint8_t data_len, bool buf[4 + data_len] = crc5(buf + 2, data_len + 2); } - // send serial data SERIAL_send(buf, total_length, debug); free(buf); @@ -112,355 +98,227 @@ static void _send_simple(uint8_t * data, uint8_t total_length) static void _send_chain_inactive(void) { - unsigned char read_address[2] = {0x00, 0x00}; - // send serial data _send_BM1368((TYPE_CMD | GROUP_ALL | CMD_INACTIVE), read_address, 2, BM1368_SERIALTX_DEBUG); } static void _set_chip_address(uint8_t chipAddr) { - unsigned char read_address[2] = {chipAddr, 0x00}; - // send serial data _send_BM1368((TYPE_CMD | GROUP_SINGLE | CMD_SETADDRESS), read_address, 2, BM1368_SERIALTX_DEBUG); } -void BM1368_send_hash_frequency(float target_freq) +static void _reset(void) { - // default 200Mhz if it fails - unsigned char freqbuf[9] = {0x00, 0x08, 0x40, 0xA0, 0x02, 0x41}; // freqbuf - pll0_parameter - float newf = 200.0; - - uint8_t fb_divider = 0; - uint8_t post_divider1 = 0, post_divider2 = 0; - uint8_t ref_divider = 0; - float min_difference = 10; - - // refdiver is 2 or 1 - // postdivider 2 is 1 to 7 - // postdivider 1 is 1 to 7 and less than postdivider 2 - // fbdiv is 144 to 235 - for (uint8_t refdiv_loop = 2; refdiv_loop > 0 && fb_divider == 0; refdiv_loop--) { - for (uint8_t postdiv1_loop = 7; postdiv1_loop > 0 && fb_divider == 0; postdiv1_loop--) { - for (uint8_t postdiv2_loop = 1; postdiv2_loop < postdiv1_loop && fb_divider == 0; postdiv2_loop++) { - int temp_fb_divider = round(((float) (postdiv1_loop * postdiv2_loop * target_freq * refdiv_loop) / 25.0)); - - if (temp_fb_divider >= 144 && temp_fb_divider <= 235) { - float temp_freq = 25.0 * (float) temp_fb_divider / (float) (refdiv_loop * postdiv2_loop * postdiv1_loop); - float freq_diff = fabs(target_freq - temp_freq); - - if (freq_diff < min_difference) { - fb_divider = temp_fb_divider; - post_divider1 = postdiv1_loop; - post_divider2 = postdiv2_loop; - ref_divider = refdiv_loop; - min_difference = freq_diff; - break; - } + gpio_set_level(BM1368_RST_PIN, 0); + vTaskDelay(100 / portTICK_PERIOD_MS); + gpio_set_level(BM1368_RST_PIN, 1); + vTaskDelay(100 / portTICK_PERIOD_MS); +} + +bool BM1368_send_hash_frequency(float target_freq) { + float max_diff = 0.001; + uint8_t freqbuf[6] = {0x00, 0x08, 0x40, 0xA0, 0x02, 0x41}; + uint8_t postdiv_min = 255; + uint8_t postdiv2_min = 255; + float best_freq = 0; + uint8_t best_refdiv = 0, best_fbdiv = 0, best_postdiv1 = 0, best_postdiv2 = 0; + bool found = false; + + for (uint8_t refdiv = 2; refdiv > 0; refdiv--) { + for (uint8_t postdiv1 = 7; postdiv1 > 0; postdiv1--) { + for (uint8_t postdiv2 = 7; postdiv2 > 0; postdiv2--) { + uint16_t fb_divider = round(target_freq / 25.0 * (refdiv * postdiv2 * postdiv1)); + float newf = 25.0 * fb_divider / (refdiv * postdiv2 * postdiv1); + + if (fb_divider >= 144 && fb_divider <= 235 && + fabs(target_freq - newf) < max_diff && + postdiv1 >= postdiv2 && + postdiv1 * postdiv2 < postdiv_min && + postdiv2 <= postdiv2_min) { + + postdiv2_min = postdiv2; + postdiv_min = postdiv1 * postdiv2; + best_freq = newf; + best_refdiv = refdiv; + best_fbdiv = fb_divider; + best_postdiv1 = postdiv1; + best_postdiv2 = postdiv2; + found = true; } } } } - if (fb_divider == 0) { - puts("Finding dividers failed, using default value (200Mhz)"); - } else { - newf = 25.0 * (float) (fb_divider) / (float) (ref_divider * post_divider1 * post_divider2); - printf("final refdiv: %d, fbdiv: %d, postdiv1: %d, postdiv2: %d, min diff value: %f\n", ref_divider, fb_divider, - post_divider1, post_divider2, min_difference); - - freqbuf[3] = fb_divider; - freqbuf[4] = ref_divider; - freqbuf[5] = (((post_divider1 - 1) & 0xf) << 4) + ((post_divider2 - 1) & 0xf); - - if (fb_divider * 25 / (float) ref_divider >= 2400) { - freqbuf[2] = 0x50; - } + if (!found) { + ESP_LOGE(TAG, "Didn't find PLL settings for target frequency %.2f", target_freq); + return false; } - _send_BM1368((TYPE_CMD | GROUP_ALL | CMD_WRITE), freqbuf, 6, BM1368_SERIALTX_DEBUG); + freqbuf[2] = (best_fbdiv * 25 / best_refdiv >= 2400) ? 0x50 : 0x40; + freqbuf[3] = best_fbdiv; + freqbuf[4] = best_refdiv; + freqbuf[5] = (((best_postdiv1 - 1) & 0xf) << 4) | ((best_postdiv2 - 1) & 0xf); - ESP_LOGI(TAG, "Setting Frequency to %.2fMHz (%.2f)", target_freq, newf); -} + _send_BM1368(TYPE_CMD | GROUP_ALL | CMD_WRITE, freqbuf, sizeof(freqbuf), BM1368_SERIALTX_DEBUG); -static void do_frequency_ramp_up() { - - //PLLO settings taken from a S21 dump. - //todo: do this right. - uint8_t freq_list[65][4] = {{0x40, 0xA2, 0x02, 0x55}, - {0x40, 0xAF, 0x02, 0x64}, - {0x40, 0xA5, 0x02, 0x54}, - {0x40, 0xA8, 0x02, 0x63}, - {0x40, 0xB6, 0x02, 0x63}, - {0x40, 0xA8, 0x02, 0x53}, - {0x40, 0xB4, 0x02, 0x53}, - {0x40, 0xA8, 0x02, 0x62}, - {0x40, 0xAA, 0x02, 0x43}, - {0x40, 0xA2, 0x02, 0x52}, - {0x40, 0xAB, 0x02, 0x52}, - {0x40, 0xB4, 0x02, 0x52}, - {0x40, 0xBD, 0x02, 0x52}, - {0x40, 0xA5, 0x02, 0x42}, - {0x40, 0xA1, 0x02, 0x61}, - {0x40, 0xA8, 0x02, 0x61}, - {0x40, 0xAF, 0x02, 0x61}, - {0x40, 0xB6, 0x02, 0x61}, - {0x40, 0xA2, 0x02, 0x51}, - {0x40, 0xA8, 0x02, 0x51}, - {0x40, 0xAE, 0x02, 0x51}, - {0x40, 0xB4, 0x02, 0x51}, - {0x40, 0xBA, 0x02, 0x51}, - {0x40, 0xA0, 0x02, 0x41}, - {0x40, 0xA5, 0x02, 0x41}, - {0x40, 0xAA, 0x02, 0x41}, - {0x40, 0xAF, 0x02, 0x41}, - {0x40, 0xB4, 0x02, 0x41}, - {0x40, 0xB9, 0x02, 0x41}, - {0x40, 0xBE, 0x02, 0x41}, - {0x40, 0xA0, 0x02, 0x31}, - {0x40, 0xA4, 0x02, 0x31}, - {0x40, 0xA8, 0x02, 0x31}, - {0x40, 0xAC, 0x02, 0x31}, - {0x40, 0xB0, 0x02, 0x31}, - {0x40, 0xB4, 0x02, 0x31}, - {0x40, 0xA1, 0x02, 0x60}, - {0x40, 0xBC, 0x02, 0x31}, - {0x40, 0xA8, 0x02, 0x60}, - {0x40, 0xAF, 0x02, 0x60}, - {0x50, 0xCC, 0x02, 0x31}, - {0x40, 0xB6, 0x02, 0x60}, - {0x50, 0xD4, 0x02, 0x31}, - {0x40, 0xA2, 0x02, 0x50}, - {0x40, 0xA5, 0x02, 0x50}, - {0x40, 0xA8, 0x02, 0x50}, - {0x40, 0xAB, 0x02, 0x50}, - {0x40, 0xAE, 0x02, 0x50}, - {0x40, 0xB1, 0x02, 0x50}, - {0x40, 0xB4, 0x02, 0x50}, - {0x40, 0xB7, 0x02, 0x50}, - {0x40, 0xBA, 0x02, 0x50}, - {0x40, 0xBD, 0x02, 0x50}, - {0x40, 0xA0, 0x02, 0x40}, - {0x50, 0xC3, 0x02, 0x50}, - {0x40, 0xA5, 0x02, 0x40}, - {0x50, 0xC9, 0x02, 0x50}, - {0x40, 0xAA, 0x02, 0x40}, - {0x50, 0xCF, 0x02, 0x50}, - {0x40, 0xAF, 0x02, 0x40}, - {0x50, 0xD5, 0x02, 0x50}, - {0x40, 0xB4, 0x02, 0x40}, - {0x50, 0xDB, 0x02, 0x50}, - {0x40, 0xB9, 0x02, 0x40}, - {0x50, 0xE0, 0x02, 0x50}}; - - uint8_t freq_cmd[6] = {0x00, 0x08, 0x40, 0xB4, 0x02, 0x40}; - - for (int i = 0; i < 65; i++) { - freq_cmd[2] = freq_list[i][0]; - freq_cmd[3] = freq_list[i][1]; - freq_cmd[4] = freq_list[i][2]; - freq_cmd[5] = freq_list[i][3]; - _send_BM1368((TYPE_CMD | GROUP_ALL | CMD_WRITE), freq_cmd, 6, BM1368_SERIALTX_DEBUG); - vTaskDelay(100 / portTICK_PERIOD_MS); - } + ESP_LOGI(TAG, "Setting Frequency to %.2fMHz (%.2f)", target_freq, best_freq); + current_frequency = target_freq; + return true; } -static uint8_t _send_init(uint64_t frequency, uint16_t asic_count) -{ - - //enable and set version rolling mask to 0xFFFF - unsigned char init0[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0xA4, 0x90, 0x00, 0xFF, 0xFF, 0x1C}; - _send_simple(init0, 11); - - //enable and set version rolling mask to 0xFFFF (again) - unsigned char init1[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0xA4, 0x90, 0x00, 0xFF, 0xFF, 0x1C}; - _send_simple(init1, 11); - - //enable and set version rolling mask to 0xFFFF (again) - unsigned char init2[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0xA4, 0x90, 0x00, 0xFF, 0xFF, 0x1C}; - _send_simple(init2, 11); +bool do_frequency_transition(float target_frequency) { + float step = 6.25; + float current = current_frequency; + float target = target_frequency; - //read register 00 on all chips (should respond AA 55 13 68 00 00 00 00 00 00 0F) - unsigned char init3[7] = {0x55, 0xAA, 0x52, 0x05, 0x00, 0x00, 0x0A}; - _send_simple(init3, 7); + float direction = (target > current) ? step : -step; - int chip_counter = 0; - while (true) { - if (SERIAL_rx(asic_response_buffer, 11, 1000) > 0) { - chip_counter++; + if (fmod(current, step) != 0) { + float next_dividable; + if (direction > 0) { + next_dividable = ceil(current / step) * step; } else { - break; + next_dividable = floor(current / step) * step; } + current = next_dividable; + BM1368_send_hash_frequency(current); + vTaskDelay(100 / portTICK_PERIOD_MS); } - ESP_LOGI(TAG, "%i chip(s) detected on the chain, expected %i", chip_counter, asic_count); - //enable and set version rolling mask to 0xFFFF (again) - unsigned char init4[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0xA4, 0x90, 0x00, 0xFF, 0xFF, 0x1C}; - _send_simple(init4, 11); - - //Reg_A8 - unsigned char init5[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0xA8, 0x00, 0x07, 0x00, 0x00, 0x03}; - _send_simple(init5, 11); - - //Misc Control - unsigned char init6[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x18, 0xFF, 0x0F, 0xC1, 0x00, 0x00}; - _send_simple(init6, 11); - - //chain inactive - _send_chain_inactive(); - // unsigned char init7[7] = {0x55, 0xAA, 0x53, 0x05, 0x00, 0x00, 0x03}; - // _send_simple(init7, 7); - - // split the chip address space evenly - uint8_t address_interval = (uint8_t) (256 / chip_counter); - for (uint8_t i = 0; i < chip_counter; i++) { - _set_chip_address(i * address_interval); - // unsigned char init8[7] = {0x55, 0xAA, 0x40, 0x05, 0x00, 0x00, 0x1C}; - // _send_simple(init8, 7); + while ((direction > 0 && current < target) || (direction < 0 && current > target)) { + float next_step = fmin(fabs(direction), fabs(target - current)); + current += direction > 0 ? next_step : -next_step; + BM1368_send_hash_frequency(current); + vTaskDelay(100 / portTICK_PERIOD_MS); } + BM1368_send_hash_frequency(target); + return true; +} - //Core Register Control - unsigned char init9[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x3C, 0x80, 0x00, 0x8B, 0x00, 0x12}; - _send_simple(init9, 11); +bool BM1368_set_frequency(float target_freq) { + return do_frequency_transition(target_freq); +} - //Core Register Control - unsigned char init10[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x3C, 0x80, 0x00, 0x80, 0x18, 0x1F}; - _send_simple(init10, 11); +static int count_asic_chips(void) { + _send_BM1368(TYPE_CMD | GROUP_ALL | CMD_READ, (uint8_t[]){0x00, 0x00}, 2, false); - //set ticket mask - // unsigned char init11[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x14, 0x00, 0x00, 0x00, 0xFF, 0x08}; - // _send_simple(init11, 11); - BM1368_set_job_difficulty_mask(BM1368_INITIAL_DIFFICULTY); + int chip_counter = 0; + while (true) { + if (SERIAL_rx(asic_response_buffer, 11, 5000) <= 0) { + break; + } - //Analog Mux Control - unsigned char init12[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x54, 0x00, 0x00, 0x00, 0x03, 0x1D}; - _send_simple(init12, 11); - - //Set the IO Driver Strength on chip 00 - unsigned char init13[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x58, 0x02, 0x11, 0x11, 0x11, 0x06}; - _send_simple(init13, 11); - - for (uint8_t i = 0; i < chip_counter; i++) { - //Reg_A8 - unsigned char set_a8_register[6] = {i * address_interval, 0xA8, 0x00, 0x07, 0x01, 0xF0}; - _send_BM1368((TYPE_CMD | GROUP_SINGLE | CMD_WRITE), set_a8_register, 6, BM1368_SERIALTX_DEBUG); - //Misc Control - unsigned char set_18_register[6] = {i * address_interval, 0x18, 0xF0, 0x00, 0xC1, 0x00}; - _send_BM1368((TYPE_CMD | GROUP_SINGLE | CMD_WRITE), set_18_register, 6, BM1368_SERIALTX_DEBUG); - //Core Register Control - unsigned char set_3c_register_first[6] = {i * address_interval, 0x3C, 0x80, 0x00, 0x8B, 0x00}; - _send_BM1368((TYPE_CMD | GROUP_SINGLE | CMD_WRITE), set_3c_register_first, 6, BM1368_SERIALTX_DEBUG); - //Core Register Control - unsigned char set_3c_register_second[6] = {i * address_interval, 0x3C, 0x80, 0x00, 0x80, 0x18}; - _send_BM1368((TYPE_CMD | GROUP_SINGLE | CMD_WRITE), set_3c_register_second, 6, BM1368_SERIALTX_DEBUG); - //Core Register Control - unsigned char set_3c_register_third[6] = {i * address_interval, 0x3C, 0x80, 0x00, 0x82, 0xAA}; - _send_BM1368((TYPE_CMD | GROUP_SINGLE | CMD_WRITE), set_3c_register_third, 6, BM1368_SERIALTX_DEBUG); + if (memcmp(asic_response_buffer, "\xaa\x55\x13\x68\x00\x00", 6) == 0) { + chip_counter++; + } } - do_frequency_ramp_up(); - - 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, BM1368_SERIALTX_DEBUG); - + _send_chain_inactive(); return chip_counter; } -// reset the BM1368 via the RTS line -static void _reset(void) -{ - gpio_set_level(BM1368_RST_PIN, 0); - - // delay for 100ms - vTaskDelay(100 / portTICK_PERIOD_MS); - - // set the gpio pin high - gpio_set_level(BM1368_RST_PIN, 1); - - // delay for 100ms - vTaskDelay(100 / portTICK_PERIOD_MS); -} - -static void _send_read_address(void) -{ - - unsigned char read_address[2] = {0x00, 0x00}; - // send serial data - _send_BM1368((TYPE_CMD | GROUP_ALL | CMD_READ), read_address, 2, BM1368_SERIALTX_DEBUG); +static void do_frequency_ramp_up(float target_frequency) { + ESP_LOGI(TAG, "Ramping up frequency from %.2f MHz to %.2f MHz", current_frequency, target_frequency); + do_frequency_transition(target_frequency); } uint8_t BM1368_init(uint64_t frequency, uint16_t asic_count) { ESP_LOGI(TAG, "Initializing BM1368"); - memset(asic_response_buffer, 0, SERIAL_BUF_SIZE); + memset(asic_response_buffer, 0, CHUNK_SIZE); esp_rom_gpio_pad_select_gpio(BM1368_RST_PIN); gpio_set_direction(BM1368_RST_PIN, GPIO_MODE_OUTPUT); - // reset the bm1368 _reset(); - return _send_init(frequency, asic_count); + uint8_t init_cmd[] = {0x00, 0xA4, 0x90, 0x00, 0xFF, 0xFF}; + for (int i = 0; i < 4; i++) { + _send_BM1368(TYPE_CMD | GROUP_ALL | CMD_WRITE, init_cmd, 6, false); + } + + int chip_counter = count_asic_chips(); + + if (chip_counter != asic_count) { + ESP_LOGE(TAG, "Chip count mismatch. Expected: %d, Actual: %d", asic_count, chip_counter); + return 0; + } + + uint8_t init_cmds[][6] = { + {0x00, 0xA8, 0x00, 0x07, 0x00, 0x00}, + {0x00, 0x18, 0xFF, 0x0F, 0xC1, 0x00}, + {0x00, 0x3C, 0x80, 0x00, 0x8b, 0x00}, + {0x00, 0x3C, 0x80, 0x00, 0x80, 0x18}, + {0x00, 0x14, 0x00, 0x00, 0x00, 0xFF}, + {0x00, 0x54, 0x00, 0x00, 0x00, 0x03}, + {0x00, 0x58, 0x02, 0x11, 0x11, 0x11} + }; + + for (int i = 0; i < sizeof(init_cmds) / sizeof(init_cmds[0]); i++) { + _send_BM1368(TYPE_CMD | GROUP_ALL | CMD_WRITE, init_cmds[i], 6, false); + } + + uint8_t address_interval = (uint8_t) (256 / chip_counter); + for (int i = 0; i < chip_counter; i++) { + _set_chip_address(i * address_interval); + } + + for (int i = 0; i < chip_counter; i++) { + uint8_t chip_init_cmds[][6] = { + {i * address_interval, 0xA8, 0x00, 0x07, 0x01, 0xF0}, + {i * address_interval, 0x18, 0xF0, 0x00, 0xC1, 0x00}, + {i * address_interval, 0x3C, 0x80, 0x00, 0x8b, 0x00}, + {i * address_interval, 0x3C, 0x80, 0x00, 0x80, 0x18}, + {i * address_interval, 0x3C, 0x80, 0x00, 0x82, 0xAA} + }; + + for (int j = 0; j < sizeof(chip_init_cmds) / sizeof(chip_init_cmds[0]); j++) { + _send_BM1368(TYPE_CMD | GROUP_SINGLE | CMD_WRITE, chip_init_cmds[j], 6, false); + } + vTaskDelay(pdMS_TO_TICKS(500)); + } + + BM1368_set_job_difficulty_mask(BM1368_INITIAL_DIFFICULTY); + + do_frequency_ramp_up((float)frequency); + + _send_BM1368(TYPE_CMD | GROUP_ALL | CMD_WRITE, (uint8_t[]){0x00, 0x10, 0x00, 0x00, 0x15, 0xa4}, 6, false); + _send_BM1368(TYPE_CMD | GROUP_ALL | CMD_WRITE, (uint8_t[]){0x00, 0xA4, 0x90, 0x00, 0xFF, 0xFF}, 6, false); + + ESP_LOGI(TAG, "%i chip(s) detected on the chain, expected %i", chip_counter, asic_count); + return chip_counter; } -// Baud formula = 25M/((denominator+1)*8) -// The denominator is 5 bits found in the misc_control (bits 9-13) int BM1368_set_default_baud(void) { - // default divider of 26 (11010) for 115,749 - unsigned char baudrate[9] = {0x00, MISC_CONTROL, 0x00, 0x00, 0b01111010, 0b00110001}; // baudrate - misc_control + unsigned char baudrate[9] = {0x00, MISC_CONTROL, 0x00, 0x00, 0b01111010, 0b00110001}; _send_BM1368((TYPE_CMD | GROUP_ALL | CMD_WRITE), baudrate, 6, BM1368_SERIALTX_DEBUG); return 115749; } int BM1368_set_max_baud(void) { - - /// return 115749; - - // divider of 0 for 3,125,000 - ESP_LOGI(TAG, "Setting max baud of 1000000 "); + 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 BM1368_set_job_difficulty_mask(int difficulty) { - // Default mask of 256 diff unsigned char job_difficulty_mask[9] = {0x00, TICKET_MASK, 0b00000000, 0b00000000, 0b00000000, 0b11111111}; - // The mask must be a power of 2 so there are no holes - // Correct: {0b00000000, 0b00000000, 0b11111111, 0b11111111} - // Incorrect: {0b00000000, 0b00000000, 0b11100111, 0b11111111} - // (difficulty - 1) if it is a pow 2 then step down to second largest for more hashrate sampling difficulty = _largest_power_of_two(difficulty) - 1; - // convert difficulty into char array - // Ex: 256 = {0b00000000, 0b00000000, 0b00000000, 0b11111111}, {0x00, 0x00, 0x00, 0xff} - // Ex: 512 = {0b00000000, 0b00000000, 0b00000001, 0b11111111}, {0x00, 0x00, 0x01, 0xff} for (int i = 0; i < 4; i++) { char value = (difficulty >> (8 * i)) & 0xFF; - // The char is read in backwards to the register so we need to reverse them - // So a mask of 512 looks like 0b00000000 00000000 00000001 1111111 - // and not 0b00000000 00000000 10000000 1111111 - job_difficulty_mask[5 - i] = _reverse_bits(value); } - ESP_LOGI(TAG, "Setting ASIC difficulty mask to %d", difficulty); + ESP_LOGI(TAG, "Setting job ASIC mask to %d", difficulty); _send_BM1368((TYPE_CMD | GROUP_ALL | CMD_WRITE), job_difficulty_mask, 6, BM1368_SERIALTX_DEBUG); } @@ -469,7 +327,6 @@ static uint8_t id = 0; void BM1368_send_work(void * pvParameters, bm_job * next_bm_job) { - GlobalState * GLOBAL_STATE = (GlobalState *) pvParameters; BM1368_job job; @@ -502,14 +359,12 @@ void BM1368_send_work(void * pvParameters, bm_job * next_bm_job) asic_result * BM1368_receive_work(void) { - // wait for a response, wait time is pretty arbitrary int received = SERIAL_rx(asic_response_buffer, 11, 60000); if (received < 0) { ESP_LOGI(TAG, "Error in serial RX"); return NULL; } else if (received == 0) { - // Didn't find a solution, restart and try again return NULL; } @@ -530,38 +385,30 @@ static uint16_t reverse_uint16(uint16_t num) 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 + return ((val >> 24) & 0xff) | + ((val << 8) & 0xff0000) | + ((val >> 8) & 0xff00) | + ((val << 24) & 0xff000000); } task_result * BM1368_proccess_work(void * pvParameters) { - asic_result * asic_result = BM1368_receive_work(); if (asic_result == NULL) { 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 & 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 + uint8_t core_id = (uint8_t)((reverse_uint32(asic_result->nonce) >> 25) & 0x7f); + uint8_t small_core_id = asic_result->job_id & 0x0f; + uint32_t version_bits = (reverse_uint16(asic_result->version) << 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[job_id] == 0) { - ESP_LOGE(TAG, "Invalid job nonce found, 0x%02X", job_id); + ESP_LOGE(TAG, "Invalid job found, 0x%02X", job_id); return NULL; } @@ -572,4 +419,4 @@ task_result * BM1368_proccess_work(void * pvParameters) result.rolled_version = rolled_version; return &result; -} +} \ No newline at end of file diff --git a/components/asic/include/bm1368.h b/components/asic/include/bm1368.h index da1035cb7..d0fadc019 100644 --- a/components/asic/include/bm1368.h +++ b/components/asic/include/bm1368.h @@ -40,7 +40,8 @@ void BM1368_send_work(void * GLOBAL_STATE, bm_job * next_bm_job); void BM1368_set_job_difficulty_mask(int); int BM1368_set_max_baud(void); int BM1368_set_default_baud(void); -void BM1368_send_hash_frequency(float frequency); +bool BM1368_send_hash_frequency(float frequency); +bool do_frequency_transition(float target_frequency); task_result * BM1368_proccess_work(void * GLOBAL_STATE); -#endif /* BM1368_H_ */ +#endif /* BM1368_H_ */ \ No newline at end of file diff --git a/components/asic/include/serial.h b/components/asic/include/serial.h index 163d7afc3..83f56197f 100644 --- a/components/asic/include/serial.h +++ b/components/asic/include/serial.h @@ -2,6 +2,8 @@ #define SERIAL_H_ #define SERIAL_BUF_SIZE 16 +#define CHUNK_SIZE 1024 + int SERIAL_send(uint8_t *, int, bool); void SERIAL_init(void);