diff --git a/components/bm1397/CMakeLists.txt b/components/asic/CMakeLists.txt similarity index 97% rename from components/bm1397/CMakeLists.txt rename to components/asic/CMakeLists.txt index 77065556..5cf75973 100644 --- a/components/bm1397/CMakeLists.txt +++ b/components/asic/CMakeLists.txt @@ -5,6 +5,7 @@ SRCS "bm1397.c" "serial.c" "crc.c" + "common.c" INCLUDE_DIRS "include" diff --git a/components/bm1397/bm1366.c b/components/asic/bm1366.c similarity index 54% rename from components/bm1397/bm1366.c rename to components/asic/bm1366.c index 74835672..80faaf3f 100644 --- a/components/bm1397/bm1366.c +++ b/components/asic/bm1366.c @@ -126,277 +126,74 @@ static void _set_chip_address(uint8_t chipAddr) _send_BM1366((TYPE_CMD | GROUP_SINGLE | CMD_SETADDRESS), read_address, 2, false); } -void BM1366_send_hash_frequency(float target_freq) +void BM1366_send_hash_frequency(unsigned int khz) { - // 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; + unsigned int newf = 200000; + uint8_t cmd[] = {0x00, 0x08, 0x40, 0xA0, 0x02, 0x41}; // 200MHz fallback + int rd, k_fd, pd1, pd2; + uint8_t refdiv, postdiv1, postdiv2, fbdiv = 0; + unsigned int k_pll_div, k_pll_div_lowest = 0; // refdiver is 2 or 1 // postdivider 2 is 1 to 7 - // postdivider 1 is 1 to 7 and less than postdivider 2 + // postdivider 1 is 1 to 7 and <= 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; - } - } + + // Find a suitable setting with the lowest pll frequency + // If a valid setting is found with rd=2, we're not going to find a better + // one with rd=1. + for (rd = 2; rd > 0 && !fbdiv; rd--) { + for (pd1 = 7; pd1 > 0; pd1--) { + for (pd2 = 1; pd2 <= pd1; pd2++) { + k_fd = khz * rd * pd1 * pd2 / 25; + if (k_fd % 1000) + continue; // Not a round value + if (k_fd < 160000) + continue; // Out of acceptable range + if (k_fd > 235000) + break; // Increasing pd2 will only give a higher fbdiv + k_pll_div = k_fd / rd; + if (k_pll_div_lowest && k_pll_div >= k_pll_div_lowest) + continue; // Not a better setting + k_pll_div_lowest = k_pll_div; + fbdiv = k_fd / 1000; + refdiv = rd; + postdiv1 = pd1; + postdiv2 = pd2; } } } - if (fb_divider == 0) { - puts("Finding dividers failed, using default value (200Mhz)"); + if (fbdiv == 0) { + puts("Finding dividers failed, using default value (200000 Khz)"); } else { - newf = 25.0 / (float) (ref_divider * fb_divider) / (float) (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); + newf = (25000 * fbdiv) / (refdiv * postdiv1 * postdiv2); + printf("final refdiv: %d, fbdiv: %d, postdiv1: %d, postdiv2: %d, min diff value: %u\n", + refdiv, fbdiv, postdiv1, postdiv2, k_pll_div_lowest / 1000); - freqbuf[3] = fb_divider; - freqbuf[4] = ref_divider; - freqbuf[5] = (((post_divider1 - 1) & 0xf) << 4) + ((post_divider2 - 1) & 0xf); + if (k_pll_div_lowest >= 96000) + cmd[2] = 0x50; - if (fb_divider * 25 / (float) ref_divider >= 2400) { - freqbuf[2] = 0x50; - } + cmd[3] = fbdiv; + cmd[4] = refdiv; + cmd[5] = (postdiv1 - 1) << 4 | (postdiv2 - 1); + ESP_LOGI(TAG, "Setting Frequency to %ukHz (%u)", khz, newf); } - _send_BM1366((TYPE_CMD | GROUP_ALL | CMD_WRITE), freqbuf, 6, false); + _send_BM1366((TYPE_CMD | GROUP_ALL | CMD_WRITE), cmd, 6, false); - ESP_LOGI(TAG, "Setting Frequency to %.2fMHz (%.2f)", target_freq, newf); } -static void do_frequency_ramp_up() +static void do_frequency_ramp_up(unsigned int end_khz) { - // TODO: figure out how to replicate this ramp up. - // bm1366 doesn't get going until after this sequence - unsigned char init724[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xA2, 0x02, 0x55, 0x0F}; - _send_simple(init724, 11); - - unsigned char init725[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xAF, 0x02, 0x64, 0x08}; - _send_simple(init725, 11); - - unsigned char init726[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xA5, 0x02, 0x54, 0x08}; - _send_simple(init726, 11); - - unsigned char init727[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xA8, 0x02, 0x63, 0x11}; - _send_simple(init727, 11); - - unsigned char init728[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xB6, 0x02, 0x63, 0x0C}; - _send_simple(init728, 11); - - unsigned char init729[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xA8, 0x02, 0x53, 0x1A}; - _send_simple(init729, 11); - - unsigned char init730[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xB4, 0x02, 0x53, 0x12}; - _send_simple(init730, 11); - - unsigned char init731[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xA8, 0x02, 0x62, 0x14}; - _send_simple(init731, 11); - - unsigned char init732[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xAA, 0x02, 0x43, 0x15}; - _send_simple(init732, 11); - - unsigned char init733[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xA2, 0x02, 0x52, 0x14}; - _send_simple(init733, 11); - - unsigned char init734[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xAB, 0x02, 0x52, 0x12}; - _send_simple(init734, 11); - - unsigned char init735[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xB4, 0x02, 0x52, 0x17}; - _send_simple(init735, 11); - - unsigned char init736[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xBD, 0x02, 0x52, 0x11}; - _send_simple(init736, 11); - - unsigned char init737[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xA5, 0x02, 0x42, 0x0C}; - _send_simple(init737, 11); - - unsigned char init738[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xA1, 0x02, 0x61, 0x1D}; - _send_simple(init738, 11); - - unsigned char init739[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xA8, 0x02, 0x61, 0x1B}; - _send_simple(init739, 11); - - unsigned char init740[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xAF, 0x02, 0x61, 0x19}; - _send_simple(init740, 11); - - unsigned char init741[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xB6, 0x02, 0x61, 0x06}; - _send_simple(init741, 11); - - unsigned char init742[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xA2, 0x02, 0x51, 0x1B}; - _send_simple(init742, 11); - - unsigned char init743[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xA8, 0x02, 0x51, 0x10}; - _send_simple(init743, 11); - - unsigned char init744[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xAE, 0x02, 0x51, 0x0A}; - _send_simple(init744, 11); - - unsigned char init745[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xB4, 0x02, 0x51, 0x18}; - _send_simple(init745, 11); - - unsigned char init746[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xBA, 0x02, 0x51, 0x1C}; - _send_simple(init746, 11); - - unsigned char init747[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xA0, 0x02, 0x41, 0x14}; - _send_simple(init747, 11); - - unsigned char init748[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xA5, 0x02, 0x41, 0x03}; - _send_simple(init748, 11); - - unsigned char init749[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xAA, 0x02, 0x41, 0x1F}; - _send_simple(init749, 11); - - unsigned char init750[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xAF, 0x02, 0x41, 0x08}; - _send_simple(init750, 11); - - unsigned char init751[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xB4, 0x02, 0x41, 0x02}; - _send_simple(init751, 11); - - unsigned char init752[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xB9, 0x02, 0x41, 0x0B}; - _send_simple(init752, 11); - - unsigned char init753[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xBE, 0x02, 0x41, 0x09}; - _send_simple(init753, 11); - - unsigned char init754[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x50, 0xC3, 0x02, 0x41, 0x01}; - _send_simple(init754, 11); - - unsigned char init755[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xA0, 0x02, 0x31, 0x18}; - _send_simple(init755, 11); - - unsigned char init756[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xA4, 0x02, 0x31, 0x17}; - _send_simple(init756, 11); - - unsigned char init757[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xA8, 0x02, 0x31, 0x06}; - _send_simple(init757, 11); + unsigned int khz = 0, start_khz = 56250, step_khz = 6250; - unsigned char init758[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xAC, 0x02, 0x31, 0x09}; - _send_simple(init758, 11); + for (khz = start_khz; khz <= end_khz; khz += step_khz) + BM1366_send_hash_frequency(khz); - unsigned char init759[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xB0, 0x02, 0x31, 0x01}; - _send_simple(init759, 11); - - unsigned char init760[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xB4, 0x02, 0x31, 0x0E}; - _send_simple(init760, 11); - - unsigned char init761[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xA1, 0x02, 0x60, 0x18}; - _send_simple(init761, 11); - - unsigned char init762[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xBC, 0x02, 0x31, 0x10}; - _send_simple(init762, 11); - - unsigned char init763[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xA8, 0x02, 0x60, 0x1E}; - _send_simple(init763, 11); - - unsigned char init764[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x50, 0xC4, 0x02, 0x31, 0x0F}; - _send_simple(init764, 11); - - unsigned char init765[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xAF, 0x02, 0x60, 0x1C}; - _send_simple(init765, 11); - - unsigned char init766[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x50, 0xCC, 0x02, 0x31, 0x11}; - _send_simple(init766, 11); - - unsigned char init767[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xB6, 0x02, 0x60, 0x03}; - _send_simple(init767, 11); - - unsigned char init768[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x50, 0xD4, 0x02, 0x31, 0x16}; - _send_simple(init768, 11); - - unsigned char init769[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xA2, 0x02, 0x50, 0x1E}; - _send_simple(init769, 11); - - unsigned char init770[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xA5, 0x02, 0x50, 0x1C}; - _send_simple(init770, 11); - - unsigned char init771[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xA8, 0x02, 0x50, 0x15}; - _send_simple(init771, 11); - - unsigned char init772[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xAB, 0x02, 0x50, 0x18}; - _send_simple(init772, 11); - - unsigned char init773[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xAE, 0x02, 0x50, 0x0F}; - _send_simple(init773, 11); - - unsigned char init774[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xB1, 0x02, 0x50, 0x0A}; - _send_simple(init774, 11); - - unsigned char init775[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xB4, 0x02, 0x50, 0x1D}; - _send_simple(init775, 11); - - unsigned char init776[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xB7, 0x02, 0x50, 0x10}; - _send_simple(init776, 11); - - unsigned char init777[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xBA, 0x02, 0x50, 0x19}; - _send_simple(init777, 11); - - unsigned char init778[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xBD, 0x02, 0x50, 0x1B}; - _send_simple(init778, 11); - - unsigned char init779[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xA0, 0x02, 0x40, 0x11}; - _send_simple(init779, 11); - - unsigned char init780[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x50, 0xC3, 0x02, 0x50, 0x1E}; - _send_simple(init780, 11); - - unsigned char init781[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xA5, 0x02, 0x40, 0x06}; - _send_simple(init781, 11); - - unsigned char init782[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x50, 0xC9, 0x02, 0x50, 0x15}; - _send_simple(init782, 11); - - unsigned char init783[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xAA, 0x02, 0x40, 0x1A}; - _send_simple(init783, 11); - - unsigned char init784[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x50, 0xCF, 0x02, 0x50, 0x0F}; - _send_simple(init784, 11); - - unsigned char init785[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xAF, 0x02, 0x40, 0x0D}; - _send_simple(init785, 11); - - unsigned char init786[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x50, 0xD5, 0x02, 0x50, 0x1D}; - _send_simple(init786, 11); - - unsigned char init787[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xB4, 0x02, 0x40, 0x07}; - _send_simple(init787, 11); - - unsigned char init788[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x50, 0xDB, 0x02, 0x50, 0x19}; - _send_simple(init788, 11); - - unsigned char init789[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xB9, 0x02, 0x40, 0x0E}; - _send_simple(init789, 11); - - unsigned char init790[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x50, 0xE1, 0x02, 0x50, 0x1C}; - _send_simple(init790, 11); - - unsigned char init791[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xBE, 0x02, 0x40, 0x0C}; - _send_simple(init791, 11); - - unsigned char init792[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x50, 0xE7, 0x02, 0x50, 0x06}; - _send_simple(init792, 11); - - unsigned char init793[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x50, 0xC2, 0x02, 0x40, 0x1C}; - _send_simple(init793, 11); + // In case end_khz is not dividable by step_khz, set explicitly + if (khz != end_khz) + BM1366_send_hash_frequency(end_khz); } static uint8_t _send_init(uint64_t frequency, uint16_t asic_count) @@ -509,9 +306,7 @@ static uint8_t _send_init(uint64_t frequency, uint16_t asic_count) _send_BM1366((TYPE_CMD | GROUP_SINGLE | CMD_WRITE), set_3c_register_third, 6, false); } - do_frequency_ramp_up(); - - BM1366_send_hash_frequency(frequency); + do_frequency_ramp_up(frequency * 1000); unsigned char init794[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x10, 0x00, 0x00, 0x15, 0x1C, 0x02}; _send_simple(init794, 11); @@ -549,7 +344,7 @@ uint8_t BM1366_init(uint64_t frequency, uint16_t asic_count) { ESP_LOGI(TAG, "Initializing BM1366"); - memset(asic_response_buffer, 0, 1024); + memset(asic_response_buffer, 0, sizeof(asic_response_buffer)); esp_rom_gpio_pad_select_gpio(BM1366_RST_PIN); gpio_set_direction(BM1366_RST_PIN, GPIO_MODE_OUTPUT); @@ -643,7 +438,7 @@ void BM1366_send_work(void * pvParameters, bm_job * next_bm_job) // ESP_LOGI(TAG, "Added Job: %i", job.job_id); pthread_mutex_unlock(&GLOBAL_STATE->valid_jobs_lock); - _send_BM1366((TYPE_JOB | GROUP_SINGLE | CMD_WRITE), &job, sizeof(BM1366_job), false); + _send_BM1366((TYPE_JOB | GROUP_SINGLE | CMD_WRITE), (uint8_t *)&job, sizeof(BM1366_job), false); } asic_result * BM1366_receive_work(void) diff --git a/components/bm1397/bm1368.c b/components/asic/bm1368.c similarity index 99% rename from components/bm1397/bm1368.c rename to components/asic/bm1368.c index 98369858..228ad5a0 100644 --- a/components/bm1397/bm1368.c +++ b/components/asic/bm1368.c @@ -393,7 +393,7 @@ uint8_t BM1368_init(uint64_t frequency, uint16_t asic_count) { ESP_LOGI(TAG, "Initializing BM1368"); - memset(asic_response_buffer, 0, 1024); + memset(asic_response_buffer, 0, sizeof(asic_response_buffer)); esp_rom_gpio_pad_select_gpio(BM1368_RST_PIN); gpio_set_direction(BM1368_RST_PIN, GPIO_MODE_OUTPUT); diff --git a/components/bm1397/bm1397.c b/components/asic/bm1397.c similarity index 99% rename from components/bm1397/bm1397.c rename to components/asic/bm1397.c index dbe42657..07099482 100644 --- a/components/bm1397/bm1397.c +++ b/components/asic/bm1397.c @@ -387,7 +387,7 @@ void BM1397_send_work(void *pvParameters, bm_job *next_bm_job) // ESP_LOGI(TAG, "Added Job: %i", job.job_id); pthread_mutex_unlock(&GLOBAL_STATE->valid_jobs_lock); - _send_BM1397((TYPE_JOB | GROUP_SINGLE | CMD_WRITE), &job, sizeof(job_packet), BM1397_DEBUG_WORK); + _send_BM1397((TYPE_JOB | GROUP_SINGLE | CMD_WRITE), (uint8_t *)&job, sizeof(job_packet), BM1397_DEBUG_WORK); } asic_result *BM1397_receive_work(void) diff --git a/components/bm1397/include/common.h b/components/asic/common.c similarity index 62% rename from components/bm1397/include/common.h rename to components/asic/common.c index 4434ae9b..f098632e 100644 --- a/components/bm1397/include/common.h +++ b/components/asic/common.c @@ -1,16 +1,7 @@ -#ifndef COMMON_H_ -#define COMMON_H_ -#include +#include "common.h" -typedef struct __attribute__((__packed__)) -{ - uint8_t job_id; - uint32_t nonce; - uint32_t rolled_version; -} task_result; - -static unsigned char _reverse_bits(unsigned char num) +unsigned char _reverse_bits(unsigned char num) { unsigned char reversed = 0; int i; @@ -24,7 +15,7 @@ static unsigned char _reverse_bits(unsigned char num) return reversed; } -static int _largest_power_of_two(int num) +int _largest_power_of_two(int num) { int power = 0; @@ -35,5 +26,3 @@ static int _largest_power_of_two(int num) return 1 << power; } - -#endif \ No newline at end of file diff --git a/components/bm1397/crc.c b/components/asic/crc.c similarity index 100% rename from components/bm1397/crc.c rename to components/asic/crc.c diff --git a/components/bm1397/include/bm1366.h b/components/asic/include/bm1366.h similarity index 94% rename from components/bm1397/include/bm1366.h rename to components/asic/include/bm1366.h index 9fa6d1d7..478da7e5 100644 --- a/components/bm1397/include/bm1366.h +++ b/components/asic/include/bm1366.h @@ -35,7 +35,7 @@ void BM1366_send_work(void * GLOBAL_STATE, bm_job * next_bm_job); void BM1366_set_job_difficulty_mask(int); int BM1366_set_max_baud(void); int BM1366_set_default_baud(void); -void BM1366_send_hash_frequency(float frequency); +void BM1366_send_hash_frequency(unsigned int khz); task_result * BM1366_proccess_work(void * GLOBAL_STATE); #endif /* BM1366_H_ */ \ No newline at end of file diff --git a/components/bm1397/include/bm1368.h b/components/asic/include/bm1368.h similarity index 100% rename from components/bm1397/include/bm1368.h rename to components/asic/include/bm1368.h diff --git a/components/bm1397/include/bm1397.h b/components/asic/include/bm1397.h similarity index 100% rename from components/bm1397/include/bm1397.h rename to components/asic/include/bm1397.h diff --git a/components/asic/include/common.h b/components/asic/include/common.h new file mode 100644 index 00000000..31d6eedd --- /dev/null +++ b/components/asic/include/common.h @@ -0,0 +1,16 @@ +#ifndef COMMON_H_ +#define COMMON_H_ + +#include + +typedef struct __attribute__((__packed__)) +{ + uint8_t job_id; + uint32_t nonce; + uint32_t rolled_version; +} task_result; + +unsigned char _reverse_bits(unsigned char num); +int _largest_power_of_two(int num); + +#endif \ No newline at end of file diff --git a/components/bm1397/include/crc.h b/components/asic/include/crc.h similarity index 100% rename from components/bm1397/include/crc.h rename to components/asic/include/crc.h diff --git a/components/bm1397/include/serial.h b/components/asic/include/serial.h similarity index 82% rename from components/bm1397/include/serial.h rename to components/asic/include/serial.h index ae165c13..6cf5b205 100644 --- a/components/bm1397/include/serial.h +++ b/components/asic/include/serial.h @@ -4,8 +4,7 @@ #define CHUNK_SIZE 1024 int SERIAL_send(uint8_t *, int, bool); -void SERIAL_init(void); -void SERIAL_debug_rx(void); +void SERIAL_init(uint8_t pin_txd, uint8_t pin_rxd); int16_t SERIAL_rx(uint8_t *, uint16_t, uint16_t); void SERIAL_clear_buffer(void); void SERIAL_set_baud(int baud); diff --git a/components/bm1397/serial.c b/components/asic/serial.c similarity index 83% rename from components/bm1397/serial.c rename to components/asic/serial.c index c1c55c15..1f9f946f 100644 --- a/components/bm1397/serial.c +++ b/components/asic/serial.c @@ -13,13 +13,11 @@ #include "serial.h" #include "utils.h" -#define ECHO_TEST_TXD (17) -#define ECHO_TEST_RXD (18) #define BUF_SIZE (1024) static const char *TAG = "serial"; -void SERIAL_init(void) +void SERIAL_init(uint8_t pin_txd, uint8_t pin_rxd) { ESP_LOGI(TAG, "Initializing serial"); // Configure UART1 parameters @@ -34,7 +32,7 @@ void SERIAL_init(void) // Configure UART1 parameters uart_param_config(UART_NUM_1, &uart_config); // Set UART1 pins(TX: IO17, RX: I018) - uart_set_pin(UART_NUM_1, ECHO_TEST_TXD, ECHO_TEST_RXD, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE); + uart_set_pin(UART_NUM_1, pin_txd, pin_rxd, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE); // Install UART driver (we don't need an event queue here) // tx buffer 0 so the tx time doesn't overlap with the job wait time @@ -79,21 +77,6 @@ int16_t SERIAL_rx(uint8_t *buf, uint16_t size, uint16_t timeout_ms) return bytes_read; } -void SERIAL_debug_rx(void) -{ - int ret; - uint8_t buf[CHUNK_SIZE]; - - ret = SERIAL_rx(buf, 100, 20); - if (ret < 0) - { - fprintf(stderr, "unable to read data\n"); - return; - } - - memset(buf, 0, 1024); -} - void SERIAL_clear_buffer(void) { uart_flush(UART_NUM_1); diff --git a/components/bm1397/test/CMakeLists.txt b/components/asic/test/CMakeLists.txt similarity index 100% rename from components/bm1397/test/CMakeLists.txt rename to components/asic/test/CMakeLists.txt diff --git a/components/bm1397/test/test_job_command.c b/components/asic/test/test_job_command.c similarity index 92% rename from components/bm1397/test/test_job_command.c rename to components/asic/test/test_job_command.c index efdd5d4b..66c3127f 100644 --- a/components/bm1397/test/test_job_command.c +++ b/components/asic/test/test_job_command.c @@ -7,6 +7,21 @@ static uint8_t uart_initialized = 0; +static void debug_rx(void) +{ + int ret; + uint8_t buf[CHUNK_SIZE]; + + ret = SERIAL_rx(buf, 100, 20); + if (ret < 0) + { + fprintf(stderr, "unable to read data\n"); + return; + } + + memset(buf, 0, 1024); +} + TEST_CASE("Check known working midstate + job command", "[bm1397]") { if (!uart_initialized) @@ -17,7 +32,7 @@ TEST_CASE("Check known working midstate + job command", "[bm1397]") BM1397_init(); // read back response - SERIAL_debug_rx(); + debug_rx(); } uint8_t work1[146] = { diff --git a/components/connect/connect.c b/components/connect/connect.c index a8f3d70d..07f2c2a3 100644 --- a/components/connect/connect.c +++ b/components/connect/connect.c @@ -9,7 +9,7 @@ #include "lwip/err.h" #include "lwip/lwip_napt.h" #include "lwip/sys.h" -#include "nvs_flash.h" +#include "nvs_config.h" #include #include "connect.h" @@ -68,34 +68,32 @@ static void event_handler(void * arg, esp_event_base_t event_base, int32_t event } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { ip_event_got_ip_t * event = (ip_event_got_ip_t *) event_data; - ESP_LOGI(TAG, "Bitaxe ip:" IPSTR, IP2STR(&event->ip_info.ip)); + ESP_LOGI(TAG, "IP:" IPSTR, IP2STR(&event->ip_info.ip)); s_retry_num = 0; xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); } } -void generate_ssid(char * ssid) +void generate_ssid(char *ssid, size_t max_size) { uint8_t mac[6]; + char *base_ssid; esp_wifi_get_mac(ESP_IF_WIFI_AP, mac); + + base_ssid = nvs_config_get_string(NVS_CONFIG_WIFI_AP_SSID, "esp-miner"); + // Format the last 4 bytes of the MAC address as a hexadecimal string - snprintf(ssid, 32, "Bitaxe_%02X%02X", mac[4], mac[5]); + snprintf(ssid, max_size, "%s_%02X%02X", base_ssid, mac[4], mac[5]); } esp_netif_t * wifi_init_softap(void) { esp_netif_t * esp_netif_ap = esp_netif_create_default_wifi_ap(); - - // Define a buffer for the SSID - char ssid_with_mac[13]; // "Bitaxe" + 4 bytes from MAC address - - // Generate the SSID - generate_ssid(ssid_with_mac); - wifi_config_t wifi_ap_config; + memset(&wifi_ap_config, 0, sizeof(wifi_ap_config)); // Clear the structure - strncpy((char *) wifi_ap_config.ap.ssid, ssid_with_mac, sizeof(wifi_ap_config.ap.ssid)); - wifi_ap_config.ap.ssid_len = strlen(ssid_with_mac); + generate_ssid((char *) wifi_ap_config.ap.ssid, sizeof(wifi_ap_config.ap.ssid)); + wifi_ap_config.ap.ssid_len = strlen((char *) wifi_ap_config.ap.ssid); wifi_ap_config.ap.channel = 1; wifi_ap_config.ap.max_connection = 30; wifi_ap_config.ap.authmode = WIFI_AUTH_OPEN; diff --git a/components/connect/include/connect.h b/components/connect/include/connect.h index bd0b9756..c5e732b8 100644 --- a/components/connect/include/connect.h +++ b/components/connect/include/connect.h @@ -34,4 +34,4 @@ void wifi_softap_on(void); void wifi_softap_off(void); void wifi_init(const char * wifi_ssid, const char * wifi_pass, const char * hostname); EventBits_t wifi_connect(void); -void generate_ssid(char * ssid); +void generate_ssid(char *ssid, size_t max_size); diff --git a/config.cvs.example b/config.csv.example similarity index 94% rename from config.cvs.example rename to config.csv.example index 6afb4714..639d9245 100644 --- a/config.cvs.example +++ b/config.csv.example @@ -3,6 +3,7 @@ main,namespace,, hostname,data,string,bitaxe wifissid,data,string,myssid wifipass,data,string,mypass +wifiapssid,data,string,Bitaxe stratumurl,data,string,public-pool.io stratumport,data,u16,21496 stratumuser,data,string,bc1qnp980s5fpp8l94p5cvttmtdqy8rvrq74qly2yrfmzkdsntqzlc5qkc4rkq.bitaxe diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index dcf0026e..ad639a59 100755 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -28,7 +28,7 @@ INCLUDE_DIRS "." "tasks" "http_server" - "../components/bm1397/include" + "../components/asic/include" "../components/connect/include" "../components/dns_server/include" "../components/stratum/include" diff --git a/main/DS4432U.c b/main/DS4432U.c index f6f962e0..a42f6b0b 100644 --- a/main/DS4432U.c +++ b/main/DS4432U.c @@ -46,3 +46,30 @@ bool DS4432U_test(void) ESP_LOGI(TAG, "DS4432U+ OUT0 = 0x%02X", data); return register_result == ESP_OK; } + +uint8_t DS4432U_voltage_to_reg(uint32_t vout_mv, uint32_t vnom_mv, + uint32_t ra_ohm, uint32_t rb_ohm, + uint32_t ifs_na, uint32_t vfb_mv) +{ + uint8_t reg; + // Calculate current flowing though bottom resistor (Rb) in nA + int32_t irb_na = ((uint64_t)vfb_mv * 1000 * 1000) / rb_ohm; + // Calculate current required through top resistor (Ra) to achieve vout in nA + int32_t ira_na = ((uint64_t)(vout_mv - vfb_mv) * 1000 * 1000) / ra_ohm; + // Calculate the delta current the DAC needs to sink/source in nA + uint32_t dac_na = abs(irb_na - ira_na); + // Calculate required DAC steps to get dac_na (rounded) + uint32_t dac_steps = ((dac_na * 127) + (ifs_na / 2)) / ifs_na; + + // make sure the requested voltage is in within range + if (dac_steps > 127) + return 0; + + reg = dac_steps; + + // dac_steps is absolute. For sink S = 0; for source S = 1. + if (vout_mv < vnom_mv) + reg |= 0x80; + + return reg; +} diff --git a/main/DS4432U.h b/main/DS4432U.h index 2075d877..b9460160 100644 --- a/main/DS4432U.h +++ b/main/DS4432U.h @@ -9,5 +9,7 @@ bool DS4432U_test(void); esp_err_t DS4432U_set_current_code(uint8_t output, uint8_t code); esp_err_t DS4432U_get_current_code(uint8_t output, uint8_t *code); - +uint8_t DS4432U_voltage_to_reg(uint32_t vout_mv, uint32_t vnom_mv, + uint32_t ra_ohm, uint32_t rb_ohm, + uint32_t ifs_na, uint32_t vfb_mv); #endif /* DS4432U_H_ */ diff --git a/main/INA260.c b/main/INA260.c index d65727e9..02c8ef03 100644 --- a/main/INA260.c +++ b/main/INA260.c @@ -11,7 +11,7 @@ bool INA260_installed(void) return i2c_master_register_read(INA260_I2CADDR_DEFAULT, INA260_REG_BUSVOLTAGE, data, 2) == ESP_OK; } -float INA260_read_current(void) +uint16_t INA260_read_current(void) { uint8_t data[2]; @@ -21,7 +21,7 @@ float INA260_read_current(void) return (uint16_t)(data[1] | (data[0] << 8)) * 1.25; } -float INA260_read_voltage(void) +uint16_t INA260_read_voltage(void) { uint8_t data[2]; @@ -31,7 +31,7 @@ float INA260_read_voltage(void) return (uint16_t)(data[1] | (data[0] << 8)) * 1.25; } -float INA260_read_power(void) +uint16_t INA260_read_power(void) { uint8_t data[2]; diff --git a/main/INA260.h b/main/INA260.h index b2a42826..75664b86 100644 --- a/main/INA260.h +++ b/main/INA260.h @@ -105,8 +105,8 @@ typedef enum _alert_latch cleared **/ } INA260_AlertLatch; bool INA260_installed(void); -float INA260_read_current(void); -float INA260_read_voltage(void); -float INA260_read_power(void); +uint16_t INA260_read_current(void); +uint16_t INA260_read_voltage(void); +uint16_t INA260_read_power(void); #endif /* INA260_H_ */ diff --git a/main/Kconfig.projbuild b/main/Kconfig.projbuild index fbcfcc0f..3e20d062 100755 --- a/main/Kconfig.projbuild +++ b/main/Kconfig.projbuild @@ -11,7 +11,7 @@ menu "Bitaxe Configuration" range 50 800 default 250 help - The BM1397 hash frequency + The asic hash frequency endmenu menu "Stratum Configuration" diff --git a/main/adc.c b/main/adc.c index 9222a52f..523fb7c9 100644 --- a/main/adc.c +++ b/main/adc.c @@ -1,19 +1,44 @@ -#include "driver/adc.h" -#include "esp_adc_cal.h" +#include "esp_err.h" +#include "esp_adc/adc_cali.h" + +#include "adc.h" // static const char *TAG = "adc.c"; -static esp_adc_cal_characteristics_t adc1_chars; +static adc_oneshot_unit_handle_t adc1_handle; +static adc_cali_handle_t adc1_cali_handle; +static adc_atten_t adc_attenuation; -// Sets up the ADC to read Vcore. Run this before ADC_get_vcore() -void ADC_init(void) +void ADC_init(adc_atten_t attenuation) { - adc1_config_channel_atten(ADC1_CHANNEL_1, ADC_ATTEN_DB_11); - esp_adc_cal_characterize(ADC_UNIT_1, ADC_ATTEN_DB_11, ADC_WIDTH_BIT_DEFAULT, 0, &adc1_chars); + adc_oneshot_unit_init_cfg_t init_config1 = { + .unit_id = ADC_UNIT_1, + .ulp_mode = ADC_ULP_MODE_DISABLE, + }; + adc_cali_curve_fitting_config_t cali_config = { + .unit_id = ADC_UNIT_1, + .atten = attenuation, + .bitwidth = ADC_BITWIDTH_DEFAULT, + }; + ESP_ERROR_CHECK(adc_oneshot_new_unit(&init_config1, &adc1_handle)); + ESP_ERROR_CHECK(adc_cali_create_scheme_curve_fitting(&cali_config, &adc1_cali_handle)); + adc_attenuation = attenuation; +} + +void ADC_ch_init(adc_channel_t channel) +{ + adc_oneshot_chan_cfg_t config = { + .bitwidth = ADC_BITWIDTH_DEFAULT, + .atten = adc_attenuation, + }; + ESP_ERROR_CHECK(adc_oneshot_config_channel(adc1_handle, channel, &config)); } // returns the ADC voltage in mV -uint16_t ADC_get_vcore(void) +// Avoid calling from multiple threads without locks +int ADC_ch_get(adc_channel_t channel) { - adc1_config_width(ADC_WIDTH_BIT_DEFAULT); - return esp_adc_cal_raw_to_voltage(adc1_get_raw(ADC1_CHANNEL_1), &adc1_chars); -} \ No newline at end of file + int adc_raw_voltage, adc_voltage; + ESP_ERROR_CHECK(adc_oneshot_read(adc1_handle, channel, &adc_raw_voltage)); + ESP_ERROR_CHECK(adc_cali_raw_to_voltage(adc1_cali_handle, adc_raw_voltage, &adc_voltage)); + return adc_voltage; +} diff --git a/main/adc.h b/main/adc.h index c3423a33..6c1deb84 100644 --- a/main/adc.h +++ b/main/adc.h @@ -1,7 +1,10 @@ #ifndef ADC_H_ #define ADC_H_ -void ADC_init(void); -uint16_t ADC_get_vcore(void); +#include "esp_adc/adc_oneshot.h" -#endif /* ADC_H_ */ \ No newline at end of file +void ADC_init(adc_atten_t attenuation); +void ADC_ch_init(adc_channel_t channel); +int ADC_ch_get(adc_channel_t channel); + +#endif /* ADC_H_ */ diff --git a/main/global_state.h b/main/global_state.h index b25778fb..e8d4c5f7 100644 --- a/main/global_state.h +++ b/main/global_state.h @@ -86,7 +86,6 @@ typedef struct work_queue stratum_queue; work_queue ASIC_jobs_queue; - bm1397Module BM1397_MODULE; SystemModule SYSTEM_MODULE; AsicTaskModule ASIC_TASK_MODULE; PowerManagementModule POWER_MANAGEMENT_MODULE; diff --git a/main/http_server/axe-os/src/app/components/home/home.component.ts b/main/http_server/axe-os/src/app/components/home/home.component.ts index f5216f74..84a224a7 100644 --- a/main/http_server/axe-os/src/app/components/home/home.component.ts +++ b/main/http_server/axe-os/src/app/components/home/home.component.ts @@ -118,7 +118,7 @@ export class HomeComponent { }), map(info => { - info.power = parseFloat(info.power.toFixed(1)) + info.power = parseFloat((info.power / 1000).toFixed(1)); info.voltage = parseFloat((info.voltage / 1000).toFixed(1)); info.current = parseFloat((info.current / 1000).toFixed(1)); info.coreVoltageActual = parseFloat((info.coreVoltageActual / 1000).toFixed(2)); @@ -158,4 +158,3 @@ export class HomeComponent { } - diff --git a/main/http_server/axe-os/src/app/components/logs/logs.component.ts b/main/http_server/axe-os/src/app/components/logs/logs.component.ts index ca004f43..5451a877 100644 --- a/main/http_server/axe-os/src/app/components/logs/logs.component.ts +++ b/main/http_server/axe-os/src/app/components/logs/logs.component.ts @@ -34,7 +34,7 @@ export class LogsComponent implements OnDestroy, AfterViewChecked { return this.systemService.getInfo() }), map(info => { - info.power = parseFloat(info.power.toFixed(1)) + info.power = parseFloat((info.power / 1000).toFixed(1)) info.voltage = parseFloat((info.voltage / 1000).toFixed(1)); info.current = parseFloat((info.current / 1000).toFixed(1)); info.coreVoltageActual = parseFloat((info.coreVoltageActual / 1000).toFixed(2)); diff --git a/main/http_server/http_server.c b/main/http_server/http_server.c index d1a59470..da058514 100644 --- a/main/http_server/http_server.c +++ b/main/http_server/http_server.c @@ -365,9 +365,9 @@ static esp_err_t GET_system_info(httpd_req_t * req) char * board_version = nvs_config_get_string(NVS_CONFIG_BOARD_VERSION, "unknown"); cJSON * root = cJSON_CreateObject(); - cJSON_AddNumberToObject(root, "power", GLOBAL_STATE->POWER_MANAGEMENT_MODULE.power); - cJSON_AddNumberToObject(root, "voltage", GLOBAL_STATE->POWER_MANAGEMENT_MODULE.voltage); - cJSON_AddNumberToObject(root, "current", GLOBAL_STATE->POWER_MANAGEMENT_MODULE.current); + cJSON_AddNumberToObject(root, "power", GLOBAL_STATE->POWER_MANAGEMENT_MODULE.power_mw); + cJSON_AddNumberToObject(root, "voltage", GLOBAL_STATE->POWER_MANAGEMENT_MODULE.voltage_mv); + cJSON_AddNumberToObject(root, "current", GLOBAL_STATE->POWER_MANAGEMENT_MODULE.current_ma); cJSON_AddNumberToObject(root, "temp", GLOBAL_STATE->POWER_MANAGEMENT_MODULE.chip_temp_avg); cJSON_AddNumberToObject(root, "vrTemp", GLOBAL_STATE->POWER_MANAGEMENT_MODULE.vr_temp); cJSON_AddNumberToObject(root, "hashRate", GLOBAL_STATE->SYSTEM_MODULE.current_hashrate); diff --git a/main/i2c_master.c b/main/i2c_master.c index 00abd849..0142fff5 100644 --- a/main/i2c_master.c +++ b/main/i2c_master.c @@ -1,7 +1,5 @@ #include "i2c_master.h" -#define I2C_MASTER_SCL_IO 48 /*!< GPIO number used for I2C master clock */ -#define I2C_MASTER_SDA_IO 47 /*!< GPIO number used for I2C master data */ #define I2C_MASTER_FREQ_HZ 400000 /*!< I2C master clock frequency */ #define I2C_MASTER_TX_BUF_DISABLE 0 /*!< I2C master doesn't need buffer */ #define I2C_MASTER_RX_BUF_DISABLE 0 /*!< I2C master doesn't need buffer */ @@ -9,14 +7,14 @@ /** * @brief i2c master initialization */ -esp_err_t i2c_master_init(void) +esp_err_t i2c_master_init(uint8_t pin_sda, uint8_t pin_scl) { int i2c_master_port = I2C_MASTER_NUM; i2c_config_t conf = { .mode = I2C_MODE_MASTER, - .sda_io_num = I2C_MASTER_SDA_IO, - .scl_io_num = I2C_MASTER_SCL_IO, + .sda_io_num = pin_sda, + .scl_io_num = pin_scl, .sda_pullup_en = GPIO_PULLUP_ENABLE, .scl_pullup_en = GPIO_PULLUP_ENABLE, .master.clk_speed = I2C_MASTER_FREQ_HZ, diff --git a/main/i2c_master.h b/main/i2c_master.h index a928ddfb..5728223e 100644 --- a/main/i2c_master.h +++ b/main/i2c_master.h @@ -6,7 +6,7 @@ #define I2C_MASTER_NUM 0 /*!< I2C master i2c port number, the number of i2c peripheral interfaces available will depend on the chip */ #define I2C_MASTER_TIMEOUT_MS 1000 -esp_err_t i2c_master_init(void); +esp_err_t i2c_master_init(uint8_t pin_sda, uint8_t pin_scl); esp_err_t i2c_master_delete(void); esp_err_t i2c_master_register_read(uint8_t device_address, uint8_t reg_addr, uint8_t * data, size_t len); esp_err_t i2c_master_register_write_byte(uint8_t device_address, uint8_t reg_addr, uint8_t data); diff --git a/main/led_controller.c b/main/led_controller.c index 6ceb4591..09fc5198 100644 --- a/main/led_controller.c +++ b/main/led_controller.c @@ -4,12 +4,12 @@ #include "driver/gpio.h" // LEDS -#define LEDX_R GPIO_NUM_35 -#define LEDX_G GPIO_NUM_36 -#define LEDX_B GPIO_NUM_37 -#define LEDZ_R GPIO_NUM_5 -#define LEDZ_G GPIO_NUM_6 -#define LEDZ_B GPIO_NUM_7 +#define LEDX_R 35 +#define LEDX_G 36 +#define LEDX_B 37 +#define LEDZ_R 5 +#define LEDZ_G 6 +#define LEDZ_B 7 void initLEDs(void) { diff --git a/main/main.c b/main/main.c index 0bb9fead..ea93c30c 100644 --- a/main/main.c +++ b/main/main.c @@ -171,7 +171,7 @@ void app_main(void) queue_init(&GLOBAL_STATE.stratum_queue); queue_init(&GLOBAL_STATE.ASIC_jobs_queue); - SERIAL_init(); + SERIAL_init(GPIO_NUM_17, GPIO_NUM_18); (*GLOBAL_STATE.ASIC_functions.init_fn)(GLOBAL_STATE.POWER_MANAGEMENT_MODULE.frequency_value, GLOBAL_STATE.asic_count); SERIAL_set_baud((*GLOBAL_STATE.ASIC_functions.set_max_baud_fn)()); SERIAL_clear_buffer(); diff --git a/main/nvs_config.h b/main/nvs_config.h index 84dd4fe2..6665575e 100644 --- a/main/nvs_config.h +++ b/main/nvs_config.h @@ -7,6 +7,7 @@ #define NVS_CONFIG_WIFI_SSID "wifissid" #define NVS_CONFIG_WIFI_PASS "wifipass" +#define NVS_CONFIG_WIFI_AP_SSID "wifiapssid" #define NVS_CONFIG_HOSTNAME "hostname" #define NVS_CONFIG_STRATUM_URL "stratumurl" #define NVS_CONFIG_STRATUM_PORT "stratumport" diff --git a/main/self_test/self_test.c b/main/self_test/self_test.c index e14e536c..2885746c 100644 --- a/main/self_test/self_test.c +++ b/main/self_test/self_test.c @@ -96,11 +96,11 @@ void self_test(void * pvParameters) } // Init I2C - ESP_ERROR_CHECK(i2c_master_init()); + ESP_ERROR_CHECK(i2c_master_init(47, 48)); ESP_LOGI(TAG, "I2C initialized successfully"); VCORE_init(GLOBAL_STATE); - VCORE_set_voltage(nvs_config_get_u16(NVS_CONFIG_ASIC_VOLTAGE, CONFIG_ASIC_VOLTAGE) / 1000.0, GLOBAL_STATE); + VCORE_set_voltage(nvs_config_get_u16(NVS_CONFIG_ASIC_VOLTAGE, CONFIG_ASIC_VOLTAGE), GLOBAL_STATE); switch (GLOBAL_STATE->device_model) { case DEVICE_MAX: @@ -145,7 +145,7 @@ void self_test(void * pvParameters) } - SERIAL_init(); + SERIAL_init(GPIO_NUM_17, GPIO_NUM_18); uint8_t chips_detected = (GLOBAL_STATE->ASIC_functions.init_fn)(GLOBAL_STATE->POWER_MANAGEMENT_MODULE.frequency_value, GLOBAL_STATE->asic_count); ESP_LOGI(TAG, "%u chips detected, %u expected", chips_detected, GLOBAL_STATE->asic_count); diff --git a/main/system.c b/main/system.c index 960d42c9..05547b1e 100644 --- a/main/system.c +++ b/main/system.c @@ -81,12 +81,12 @@ static void _init_system(GlobalState * GLOBAL_STATE) // led_set(); // Init I2C - ESP_ERROR_CHECK(i2c_master_init()); + ESP_ERROR_CHECK(i2c_master_init(47, 48)); ESP_LOGI(TAG, "I2C initialized successfully"); // 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); + VCORE_set_voltage(nvs_config_get_u16(NVS_CONFIG_ASIC_VOLTAGE, CONFIG_ASIC_VOLTAGE), GLOBAL_STATE); switch (GLOBAL_STATE->device_model) { case DEVICE_MAX: @@ -130,7 +130,7 @@ static void _update_hashrate(GlobalState * GLOBAL_STATE) case DEVICE_MAX: case DEVICE_ULTRA: case DEVICE_SUPRA: - float efficiency = GLOBAL_STATE->POWER_MANAGEMENT_MODULE.power / (module->current_hashrate / 1000.0); + float efficiency = GLOBAL_STATE->POWER_MANAGEMENT_MODULE.power_mw / module->current_hashrate; 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 ? "*" : "", @@ -217,11 +217,12 @@ static void _update_system_info(GlobalState * GLOBAL_STATE) OLED_writeString(0, 1, module->oled_buf); memset(module->oled_buf, 0, 20); - snprintf(module->oled_buf, 20, " Pwr: %.3f W", power_management->power); + snprintf(module->oled_buf, 20, " Pwr: %d mW", power_management->power_mw); 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); + snprintf(module->oled_buf, 20, " %i mV: %i mA", + power_management->voltage_mv, power_management->current_ma); OLED_writeString(0, 3, module->oled_buf); } break; @@ -302,7 +303,7 @@ static void _update_connection(GlobalState * GLOBAL_STATE) OLED_writeString(0, 2, module->oled_buf); char ap_ssid[13]; - generate_ssid(ap_ssid); + generate_ssid(ap_ssid, sizeof(ap_ssid)); memset(module->oled_buf, 0, 20); snprintf(module->oled_buf, 20, ap_ssid); OLED_writeString(0, 3, module->oled_buf); @@ -355,7 +356,7 @@ static void show_ap_information(const char * error, GlobalState * GLOBAL_STATE) } OLED_writeString(0, 1, "Configuration SSID:"); char ap_ssid[13]; - generate_ssid(ap_ssid); + generate_ssid(ap_ssid, sizeof(ap_ssid)); OLED_writeString(0, 2, ap_ssid); } break; diff --git a/main/tasks/power_management_task.c b/main/tasks/power_management_task.c index 4f1c54b6..4f509fac 100644 --- a/main/tasks/power_management_task.c +++ b/main/tasks/power_management_task.c @@ -121,14 +121,14 @@ void POWER_MANAGEMENT_task(void * pvParameters) case DEVICE_ULTRA: case DEVICE_SUPRA: if (GLOBAL_STATE->board_version == 402) { - power_management->voltage = TPS546_get_vin() * 1000; - power_management->current = TPS546_get_iout() * 1000; + power_management->voltage_mv = TPS546_get_vin(); + power_management->current_ma = TPS546_get_iout(); // calculate regulator power (in milliwatts) - power_management->power = (TPS546_get_vout() * power_management->current) / 1000; + power_management->power_mw = (TPS546_get_vout() * power_management->current_ma); } else if (INA260_installed() == true) { - power_management->voltage = INA260_read_voltage(); - power_management->current = INA260_read_current(); - power_management->power = INA260_read_power() / 1000; + power_management->voltage_mv = INA260_read_voltage(); + power_management->current_ma = INA260_read_current(); + power_management->power_mw = INA260_read_power(); } power_management->fan_rpm = EMC2101_get_fan_speed(); @@ -146,7 +146,7 @@ void POWER_MANAGEMENT_task(void * pvParameters) power_management->chip_temp_avg = EMC2101_get_external_temp(); if ((power_management->chip_temp_avg > THROTTLE_TEMP) && - (power_management->frequency_value > 50 || power_management->voltage > 1000)) { + (power_management->frequency_value > 50 || power_management->voltage_mv > 1000)) { ESP_LOGE(TAG, "OVERHEAT ASIC %fC", power_management->chip_temp_avg ); EMC2101_set_fan_speed(1); @@ -178,12 +178,12 @@ void POWER_MANAGEMENT_task(void * pvParameters) } // EMC2101 will give bad readings if the ASIC is turned off - if(power_management->voltage < TPS546_INIT_VOUT_MIN){ + if(power_management->voltage_mv < TPS546_INIT_VOUT_MIN){ break; } if ((power_management->vr_temp > TPS546_THROTTLE_TEMP || power_management->chip_temp_avg > THROTTLE_TEMP) && - (power_management->frequency_value > 50 || power_management->voltage > 1000)) { + (power_management->frequency_value > 50 || power_management->voltage_mv > 1000)) { ESP_LOGE(TAG, "OVERHEAT VR: %fC ASIC %fC", power_management->vr_temp, power_management->chip_temp_avg ); EMC2101_set_fan_speed(1); @@ -201,7 +201,6 @@ void POWER_MANAGEMENT_task(void * pvParameters) } break; - default: } } @@ -237,4 +236,3 @@ void POWER_MANAGEMENT_task(void * pvParameters) vTaskDelay(POLL_RATE / portTICK_PERIOD_MS); } } - diff --git a/main/tasks/power_management_task.h b/main/tasks/power_management_task.h index d198d112..27546444 100644 --- a/main/tasks/power_management_task.h +++ b/main/tasks/power_management_task.h @@ -8,11 +8,11 @@ typedef struct float chip_temp[6]; float chip_temp_avg; float vr_temp; - float voltage; + int voltage_mv; float frequency_multiplier; float frequency_value; - float power; - float current; + int power_mw; + int current_ma; bool HAS_POWER_EN; bool HAS_PLUG_SENSE; } PowerManagementModule; diff --git a/main/vcore.c b/main/vcore.c index 7c48354c..97620ef3 100644 --- a/main/vcore.c +++ b/main/vcore.c @@ -7,17 +7,10 @@ #include "DS4432U.h" #include "TPS546.h" -#define TPS40305_VFB 0.6 - -// DS4432U Transfer function constants for Bitaxe board -// #define BITAXE_RFS 80000.0 // R16 -// #define BITAXE_IFS ((DS4432_VRFS * 127.0) / (BITAXE_RFS * 16)) -#define BITAXE_IFS 0.000098921 // (Vrfs / Rfs) x (127/16) -> Vrfs = 0.997, Rfs = 80000 -#define BITAXE_RA 4750.0 // R14 -#define BITAXE_RB 3320.0 // R15 -#define BITAXE_VNOM 1.451 // this is with the current DAC set to 0. Should be pretty close to (VFB*(RA+RB))/RB -#define BITAXE_VMAX 2.39 -#define BITAXE_VMIN 0.046 +#define BITAXE_IFS 98921 // nA +#define BITAXE_RTOP 4750 // Ohm +#define BITAXE_RBOT 3320 // Ohm +#define BITAXE_VNOM 1451 // mV static const char *TAG = "vcore.c"; @@ -25,49 +18,25 @@ void VCORE_init(GlobalState * global_state) { if (global_state->board_version == 402) { TPS546_init(); } - ADC_init(); + ADC_init(ADC_ATTEN_DB_11); + ADC_ch_init(ADC_CHANNEL_1); } -/** - * @brief ds4432_tps40305_bitaxe_voltage_to_reg takes a voltage and returns a register setting for the DS4432U to get that voltage on the TPS40305 - * careful with this one!! - */ -static uint8_t ds4432_tps40305_bitaxe_voltage_to_reg(float vout) +bool VCORE_set_voltage(uint16_t vcore_mv, GlobalState * global_state) { - float change; - uint8_t reg; - - // make sure the requested voltage is in within range of BITAXE_VMIN and BITAXE_VMAX - if (vout > BITAXE_VMAX || vout < BITAXE_VMIN) - { - return 0; - } + uint32_t sum_vcore_mv = vcore_mv * global_state->voltage_domain; - // this is the transfer function. comes from the DS4432U+ datasheet - change = fabs((((TPS40305_VFB / BITAXE_RB) - ((vout - TPS40305_VFB) / BITAXE_RA)) / BITAXE_IFS) * 127); - reg = (uint8_t)ceil(change); - - // Set the MSB high if the requested voltage is BELOW nominal - if (vout < BITAXE_VNOM) - { - reg |= 0x80; - } - - return reg; -} - -bool VCORE_set_voltage(float core_voltage, GlobalState * global_state) -{ switch (global_state->device_model) { case DEVICE_MAX: case DEVICE_ULTRA: case DEVICE_SUPRA: if (global_state->board_version == 402) { - ESP_LOGI(TAG, "Set ASIC voltage = %.3fV", core_voltage); - TPS546_set_vout(core_voltage * (float)global_state->voltage_domain); + ESP_LOGI(TAG, "Set ASIC voltage = %umV", vcore_mv); + TPS546_set_vout((float)sum_vcore_mv / 1000.0); } else { - uint8_t reg_setting = ds4432_tps40305_bitaxe_voltage_to_reg(core_voltage * (float)global_state->voltage_domain); - ESP_LOGI(TAG, "Set ASIC voltage = %.3fV [0x%02X]", core_voltage, reg_setting); + uint8_t reg_setting = DS4432U_voltage_to_reg( + sum_vcore_mv, BITAXE_VNOM, BITAXE_RTOP, BITAXE_RBOT, BITAXE_IFS, 600); + ESP_LOGI(TAG, "Set ASIC voltage = %umV [0x%02X]", vcore_mv, reg_setting); DS4432U_set_current_code(0, reg_setting); /// eek! } break; @@ -79,5 +48,5 @@ bool VCORE_set_voltage(float core_voltage, GlobalState * global_state) } uint16_t VCORE_get_voltage_mv(GlobalState * global_state) { - return ADC_get_vcore() / global_state->voltage_domain; + return ADC_ch_get(ADC_CHANNEL_1) / global_state->voltage_domain; } diff --git a/main/vcore.h b/main/vcore.h index 0ee7e779..49d73615 100644 --- a/main/vcore.h +++ b/main/vcore.h @@ -4,7 +4,7 @@ #include "global_state.h" void VCORE_init(GlobalState * global_state); -bool VCORE_set_voltage(float core_voltage, GlobalState * global_state); +bool VCORE_set_voltage(uint16_t vcore_mv, GlobalState * global_state); uint16_t VCORE_get_voltage_mv(GlobalState * global_state); #endif /* VCORE_H_ */