From c2d0decaf4c57cda0e4cb32255fc32a87eee3989 Mon Sep 17 00:00:00 2001 From: Robin van der Gracht Date: Sat, 21 Oct 2023 09:25:18 +0200 Subject: [PATCH 01/25] global_state: Remove unused bm1397Module The asic frequency is stored in POWER_MANAGEMENT_MODULE->frequency_value. Signed-off-by: Robin van der Gracht --- main/global_state.h | 1 - 1 file changed, 1 deletion(-) 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; From 9fe7e3ebebfef979221d0ff8b9d6198b74f0c0da Mon Sep 17 00:00:00 2001 From: Robin van der Gracht Date: Tue, 31 Oct 2023 16:48:05 +0100 Subject: [PATCH 02/25] Fix typo in filename config.csv.example Signed-off-by: Robin van der Gracht --- config.cvs.example => config.csv.example | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename config.cvs.example => config.csv.example (100%) diff --git a/config.cvs.example b/config.csv.example similarity index 100% rename from config.cvs.example rename to config.csv.example From b95b1bef1d86c6988dcca8a1223c6e4303358bcd Mon Sep 17 00:00:00 2001 From: Robin van der Gracht Date: Sat, 21 Oct 2023 09:25:19 +0200 Subject: [PATCH 03/25] Rename bm1397 component to asic A more generic name since multiple types are supported now. Signed-off-by: Robin van der Gracht --- components/{bm1397 => asic}/CMakeLists.txt | 0 components/{bm1397 => asic}/bm1366.c | 0 components/{bm1397 => asic}/bm1368.c | 0 components/{bm1397 => asic}/bm1397.c | 0 components/{bm1397 => asic}/crc.c | 0 components/{bm1397 => asic}/include/bm1366.h | 0 components/{bm1397 => asic}/include/bm1368.h | 0 components/{bm1397 => asic}/include/bm1397.h | 0 components/{bm1397 => asic}/include/common.h | 0 components/{bm1397 => asic}/include/crc.h | 0 components/{bm1397 => asic}/include/serial.h | 0 components/{bm1397 => asic}/serial.c | 0 components/{bm1397 => asic}/test/CMakeLists.txt | 0 components/{bm1397 => asic}/test/test_job_command.c | 0 main/CMakeLists.txt | 2 +- main/Kconfig.projbuild | 2 +- 16 files changed, 2 insertions(+), 2 deletions(-) rename components/{bm1397 => asic}/CMakeLists.txt (100%) rename components/{bm1397 => asic}/bm1366.c (100%) rename components/{bm1397 => asic}/bm1368.c (100%) rename components/{bm1397 => asic}/bm1397.c (100%) rename components/{bm1397 => asic}/crc.c (100%) rename components/{bm1397 => asic}/include/bm1366.h (100%) rename components/{bm1397 => asic}/include/bm1368.h (100%) rename components/{bm1397 => asic}/include/bm1397.h (100%) rename components/{bm1397 => asic}/include/common.h (100%) rename components/{bm1397 => asic}/include/crc.h (100%) rename components/{bm1397 => asic}/include/serial.h (100%) rename components/{bm1397 => asic}/serial.c (100%) rename components/{bm1397 => asic}/test/CMakeLists.txt (100%) rename components/{bm1397 => asic}/test/test_job_command.c (100%) diff --git a/components/bm1397/CMakeLists.txt b/components/asic/CMakeLists.txt similarity index 100% rename from components/bm1397/CMakeLists.txt rename to components/asic/CMakeLists.txt diff --git a/components/bm1397/bm1366.c b/components/asic/bm1366.c similarity index 100% rename from components/bm1397/bm1366.c rename to components/asic/bm1366.c diff --git a/components/bm1397/bm1368.c b/components/asic/bm1368.c similarity index 100% rename from components/bm1397/bm1368.c rename to components/asic/bm1368.c diff --git a/components/bm1397/bm1397.c b/components/asic/bm1397.c similarity index 100% rename from components/bm1397/bm1397.c rename to components/asic/bm1397.c 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 100% rename from components/bm1397/include/bm1366.h rename to components/asic/include/bm1366.h 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/bm1397/include/common.h b/components/asic/include/common.h similarity index 100% rename from components/bm1397/include/common.h rename to components/asic/include/common.h 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 100% rename from components/bm1397/include/serial.h rename to components/asic/include/serial.h diff --git a/components/bm1397/serial.c b/components/asic/serial.c similarity index 100% rename from components/bm1397/serial.c rename to components/asic/serial.c 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 100% rename from components/bm1397/test/test_job_command.c rename to components/asic/test/test_job_command.c 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/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" From 46b30c3be218580ddd292de4d5aa8f8363775b0f Mon Sep 17 00:00:00 2001 From: Robin van der Gracht Date: Sat, 21 Oct 2023 09:25:21 +0200 Subject: [PATCH 04/25] asic: Move SERIAL_debug_rx to test_job_command It's only used there and its debug output is to stderr. Signed-off-by: Robin van der Gracht --- components/asic/include/serial.h | 1 - components/asic/serial.c | 15 --------------- components/asic/test/test_job_command.c | 17 ++++++++++++++++- 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/components/asic/include/serial.h b/components/asic/include/serial.h index ae165c13..82b7d3d3 100644 --- a/components/asic/include/serial.h +++ b/components/asic/include/serial.h @@ -5,7 +5,6 @@ int SERIAL_send(uint8_t *, int, bool); void SERIAL_init(void); -void SERIAL_debug_rx(void); 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/asic/serial.c b/components/asic/serial.c index c1c55c15..cf139788 100644 --- a/components/asic/serial.c +++ b/components/asic/serial.c @@ -79,21 +79,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/asic/test/test_job_command.c b/components/asic/test/test_job_command.c index efdd5d4b..66c3127f 100644 --- a/components/asic/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] = { From 111e6e7e24693171378af6fb5f658b30f3e2d9a3 Mon Sep 17 00:00:00 2001 From: Robin van der Gracht Date: Sun, 22 Oct 2023 09:26:17 +0200 Subject: [PATCH 05/25] asic: bm1366: Improve performance of hash frequency divider calculation We should avoid using floating point arithmetic. Floating point calculations are always slower than integer calculations, especially on platforms without an FPU. Signed-off-by: Robin van der Gracht --- components/asic/bm1366.c | 72 ++++++++++++++------------------ components/asic/include/bm1366.h | 2 +- 2 files changed, 32 insertions(+), 42 deletions(-) diff --git a/components/asic/bm1366.c b/components/asic/bm1366.c index 74835672..00337687 100644 --- a/components/asic/bm1366.c +++ b/components/asic/bm1366.c @@ -126,62 +126,52 @@ 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; // 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; - } - } + for (rd = 2; rd > 0 && !fbdiv; rd--) { + for (pd1 = 7; pd1 > 0 && !fbdiv; 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 < 144000 || k_fd > 235000) + continue; // Out of acceptable range + fbdiv = k_fd / 1000; + refdiv = rd; + postdiv1 = pd1; + postdiv2 = pd2; + break; } } } - 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 ((fbdiv / refdiv) >= 96) + 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() @@ -511,7 +501,7 @@ static uint8_t _send_init(uint64_t frequency, uint16_t asic_count) do_frequency_ramp_up(); - BM1366_send_hash_frequency(frequency); + BM1366_send_hash_frequency(frequency * 1000); unsigned char init794[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x10, 0x00, 0x00, 0x15, 0x1C, 0x02}; _send_simple(init794, 11); diff --git a/components/asic/include/bm1366.h b/components/asic/include/bm1366.h index 9fa6d1d7..478da7e5 100644 --- a/components/asic/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 From c5f0509e0efeeb407c5951fb6249a0d338798b57 Mon Sep 17 00:00:00 2001 From: Robin van der Gracht Date: Sun, 22 Oct 2023 09:26:17 +0200 Subject: [PATCH 06/25] asic: bm1366: Allow equal post dividers The first set frequency command send in do_frequency_ramp_up() uses equal post dividers so apparently that's allowed. Signed-off-by: Robin van der Gracht --- components/asic/bm1366.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/components/asic/bm1366.c b/components/asic/bm1366.c index 00337687..a7704c10 100644 --- a/components/asic/bm1366.c +++ b/components/asic/bm1366.c @@ -135,11 +135,11 @@ void BM1366_send_hash_frequency(unsigned int khz) // 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 (rd = 2; rd > 0 && !fbdiv; rd--) { for (pd1 = 7; pd1 > 0 && !fbdiv; pd1--) { - for (pd2 = 1; pd2 < pd1; pd2++) { + for (pd2 = 1; pd2 <= pd1; pd2++) { k_fd = khz * rd * pd1 * pd2 / 25; if (k_fd % 1000) continue; // Not a round value From 3e6922910e69d019d2810d966ac87d4f8dfb1c49 Mon Sep 17 00:00:00 2001 From: Robin van der Gracht Date: Sun, 22 Oct 2023 09:26:18 +0200 Subject: [PATCH 07/25] asic: bm1366: Use the lowest possible pll frequency Keep on looping to find alternative divider settings which still result in the requested target hash frequency. The one that results in the lowest pll frequency (25 * fbdiv / refdiv) is used. This is more in line with the values used in the raw commands in do_frequency_ramp_up(). Since the resulting values ware a bit lower than the ones used by the raw commands in do_frequency_ramp_up(), the allowed minimal value for fbdiv had to be increased from 144 to 160. After doing so, using this function gives the exact same results as the raw commands in do_frequency_ramp_up(). Signed-off-by: Robin van der Gracht --- components/asic/bm1366.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/components/asic/bm1366.c b/components/asic/bm1366.c index a7704c10..862b588d 100644 --- a/components/asic/bm1366.c +++ b/components/asic/bm1366.c @@ -132,24 +132,34 @@ void BM1366_send_hash_frequency(unsigned int khz) 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 <= postdivider 2 // fbdiv is 144 to 235 + + // 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 && !fbdiv; pd1--) { + 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 < 144000 || k_fd > 235000) + 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; - break; } } } @@ -161,7 +171,7 @@ void BM1366_send_hash_frequency(unsigned int khz) printf("final refdiv: %d, fbdiv: %d, postdiv1: %d, postdiv2: %d, min diff value: %u\n", refdiv, fbdiv, postdiv1, postdiv2, k_pll_div_lowest / 1000); - if ((fbdiv / refdiv) >= 96) + if (k_pll_div_lowest >= 96000) cmd[2] = 0x50; cmd[3] = fbdiv; From 71b5040bb6262f1e355e8e6958f498614bddc9e6 Mon Sep 17 00:00:00 2001 From: Robin van der Gracht Date: Sun, 22 Oct 2023 09:26:19 +0200 Subject: [PATCH 08/25] asic: bm1366: Replace raw commands in do_frequency_ramp_up() Signed-off-by: Robin van der Gracht --- components/asic/bm1366.c | 219 ++------------------------------------- 1 file changed, 8 insertions(+), 211 deletions(-) diff --git a/components/asic/bm1366.c b/components/asic/bm1366.c index 862b588d..a163c024 100644 --- a/components/asic/bm1366.c +++ b/components/asic/bm1366.c @@ -184,219 +184,16 @@ void BM1366_send_hash_frequency(unsigned int khz) } -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 int khz, start_khz = 56250, step_khz = 6250; - unsigned char init725[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xAF, 0x02, 0x64, 0x08}; - _send_simple(init725, 11); + for (khz = start_khz; khz < end_khz; khz += step_khz) + BM1366_send_hash_frequency(khz); - 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 char init758[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xAC, 0x02, 0x31, 0x09}; - _send_simple(init758, 11); - - 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 - step_khz) != end_khz) + BM1366_send_hash_frequency(end_khz); } static uint8_t _send_init(uint64_t frequency, uint16_t asic_count) @@ -509,7 +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(); + do_frequency_ramp_up(485000); BM1366_send_hash_frequency(frequency * 1000); From 678c7ae84bf35d1de23c7be9e84875e30c7b4dca Mon Sep 17 00:00:00 2001 From: Robin van der Gracht Date: Sun, 22 Oct 2023 09:38:55 +0200 Subject: [PATCH 09/25] asic: bm1366: Don't ramp-up frequency beyond the target frequency Signed-off-by: Robin van der Gracht --- components/asic/bm1366.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/components/asic/bm1366.c b/components/asic/bm1366.c index a163c024..1ba72a88 100644 --- a/components/asic/bm1366.c +++ b/components/asic/bm1366.c @@ -186,13 +186,13 @@ void BM1366_send_hash_frequency(unsigned int khz) static void do_frequency_ramp_up(unsigned int end_khz) { - unsigned int khz, start_khz = 56250, step_khz = 6250; + unsigned int khz = 0, start_khz = 56250, step_khz = 6250; - for (khz = start_khz; khz < end_khz; khz += step_khz) + for (khz = start_khz; khz <= end_khz; khz += step_khz) BM1366_send_hash_frequency(khz); // In case end_khz is not dividable by step_khz, set explicitly - if ((khz - step_khz) != end_khz) + if (khz != end_khz) BM1366_send_hash_frequency(end_khz); } @@ -306,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(485000); - - BM1366_send_hash_frequency(frequency * 1000); + 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); From 529958c3e991f0dae5037cc53b6be23b55dd686d Mon Sep 17 00:00:00 2001 From: Robin van der Gracht Date: Wed, 1 Nov 2023 13:37:34 +0100 Subject: [PATCH 10/25] Convert power, voltage and current values to integers We should avoid using floating point arithmetic. Signed-off-by: Robin van der Gracht --- main/INA260.c | 6 +++--- main/INA260.h | 6 +++--- .../src/app/components/home/home.component.ts | 3 +-- .../src/app/components/logs/logs.component.ts | 2 +- main/http_server/http_server.c | 6 +++--- main/system.c | 7 ++++--- main/tasks/power_management_task.c | 20 +++++++++---------- main/tasks/power_management_task.h | 6 +++--- 8 files changed, 27 insertions(+), 29 deletions(-) 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/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/system.c b/main/system.c index 960d42c9..f6210e33 100644 --- a/main/system.c +++ b/main/system.c @@ -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; 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; From f3099d1b64c87d03995e4976e80dc9dd372b22a0 Mon Sep 17 00:00:00 2001 From: Robin van der Gracht Date: Sat, 4 Nov 2023 10:16:05 +0100 Subject: [PATCH 11/25] asic: Fix incompatible pointer type warning Signed-off-by: Robin van der Gracht --- components/asic/bm1366.c | 2 +- components/asic/bm1397.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/components/asic/bm1366.c b/components/asic/bm1366.c index 1ba72a88..98f33a01 100644 --- a/components/asic/bm1366.c +++ b/components/asic/bm1366.c @@ -438,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/asic/bm1397.c b/components/asic/bm1397.c index dbe42657..07099482 100644 --- a/components/asic/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) From f1735bb45640135f120b2ac121d2f692e2e5fdeb Mon Sep 17 00:00:00 2001 From: Robin van der Gracht Date: Sat, 20 Jan 2024 23:09:38 +0100 Subject: [PATCH 12/25] asic: bm1366: Use sizeof to determine size of asic_response_buffer This way we only have the buffer size defined in one location. Signed-off-by: Robin van der Gracht --- components/asic/bm1366.c | 2 +- components/asic/bm1368.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/components/asic/bm1366.c b/components/asic/bm1366.c index 98f33a01..80faaf3f 100644 --- a/components/asic/bm1366.c +++ b/components/asic/bm1366.c @@ -344,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); diff --git a/components/asic/bm1368.c b/components/asic/bm1368.c index 98369858..228ad5a0 100644 --- a/components/asic/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); From 9e39d516f77444e0c76934fefc96751b19b426a8 Mon Sep 17 00:00:00 2001 From: Robin van der Gracht Date: Tue, 31 Oct 2023 14:46:46 +0100 Subject: [PATCH 13/25] Migrate to the new oneshot API for ADC conversions. Cannot compile adc.c since the legacy API is no longer available. #warning "legacy adc driver is deprecated" please migrate to use esp_adc/adc_oneshot.h and esp_adc/adc_continuous.h for oneshot mode and continuous mode drivers respectively" Signed-off-by: Robin van der Gracht --- main/adc.c | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/main/adc.c b/main/adc.c index 9222a52f..6786f20f 100644 --- a/main/adc.c +++ b/main/adc.c @@ -1,19 +1,38 @@ -#include "driver/adc.h" -#include "esp_adc_cal.h" +#include "esp_err.h" +#include "esp_adc/adc_oneshot.h" +#include "esp_adc/adc_cali.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; // Sets up the ADC to read Vcore. Run this before ADC_get_vcore() void ADC_init(void) { - 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_oneshot_chan_cfg_t config = { + .bitwidth = ADC_BITWIDTH_DEFAULT, + .atten = ADC_ATTEN_DB_11, + }; + adc_cali_curve_fitting_config_t cali_config = { + .unit_id = ADC_UNIT_1, + .atten = ADC_ATTEN_DB_11, + .bitwidth = ADC_BITWIDTH_DEFAULT, + }; + ESP_ERROR_CHECK(adc_oneshot_new_unit(&init_config1, &adc1_handle)); + ESP_ERROR_CHECK(adc_oneshot_config_channel(adc1_handle, ADC_CHANNEL_1, &config)); + ESP_ERROR_CHECK(adc_cali_create_scheme_curve_fitting(&cali_config, &adc1_cali_handle)); } // returns the ADC voltage in mV +// Avoid calling from multiple threads without locks uint16_t ADC_get_vcore(void) { - 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, ADC_CHANNEL_1, &adc_raw_voltage)); + ESP_ERROR_CHECK(adc_cali_raw_to_voltage(adc1_cali_handle, adc_raw_voltage, &adc_voltage)); + return adc_voltage; +} From 3dc2925b7cb0a31a83145eea33466486818c2202 Mon Sep 17 00:00:00 2001 From: Robin van der Gracht Date: Tue, 2 Jul 2024 11:01:48 +0200 Subject: [PATCH 14/25] adc: Make API more flexible This change allows changing attenuation and perform measurements of alternative channels. Signed-off-by: Robin van der Gracht --- main/adc.c | 28 +++++++++++++++++----------- main/adc.h | 9 ++++++--- main/vcore.c | 5 +++-- 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/main/adc.c b/main/adc.c index 6786f20f..523fb7c9 100644 --- a/main/adc.c +++ b/main/adc.c @@ -1,38 +1,44 @@ #include "esp_err.h" -#include "esp_adc/adc_oneshot.h" #include "esp_adc/adc_cali.h" +#include "adc.h" + // static const char *TAG = "adc.c"; 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) { adc_oneshot_unit_init_cfg_t init_config1 = { .unit_id = ADC_UNIT_1, .ulp_mode = ADC_ULP_MODE_DISABLE, }; - adc_oneshot_chan_cfg_t config = { - .bitwidth = ADC_BITWIDTH_DEFAULT, - .atten = ADC_ATTEN_DB_11, - }; adc_cali_curve_fitting_config_t cali_config = { .unit_id = ADC_UNIT_1, - .atten = ADC_ATTEN_DB_11, + .atten = attenuation, .bitwidth = ADC_BITWIDTH_DEFAULT, }; ESP_ERROR_CHECK(adc_oneshot_new_unit(&init_config1, &adc1_handle)); - ESP_ERROR_CHECK(adc_oneshot_config_channel(adc1_handle, ADC_CHANNEL_1, &config)); 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 // Avoid calling from multiple threads without locks -uint16_t ADC_get_vcore(void) +int ADC_ch_get(adc_channel_t channel) { int adc_raw_voltage, adc_voltage; - ESP_ERROR_CHECK(adc_oneshot_read(adc1_handle, ADC_CHANNEL_1, &adc_raw_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/vcore.c b/main/vcore.c index 7c48354c..8cd829e6 100644 --- a/main/vcore.c +++ b/main/vcore.c @@ -25,7 +25,8 @@ 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); } /** @@ -79,5 +80,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; } From 7eebd497821be01721f0bcd6626d017ecd4c3e47 Mon Sep 17 00:00:00 2001 From: Robin van der Gracht Date: Wed, 1 Nov 2023 14:04:04 +0100 Subject: [PATCH 15/25] connect: Make AP ssid configurable through nvs Signed-off-by: Robin van der Gracht --- components/connect/connect.c | 22 ++++++++++------------ components/connect/include/connect.h | 2 +- config.csv.example | 1 + main/nvs_config.h | 1 + main/system.c | 4 ++-- 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/components/connect/connect.c b/components/connect/connect.c index a8f3d70d..b23f2eb8 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" @@ -74,28 +74,26 @@ static void event_handler(void * arg, esp_event_base_t event_base, int32_t event } } -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.csv.example b/config.csv.example index 6afb4714..639d9245 100644 --- a/config.csv.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/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/system.c b/main/system.c index f6210e33..7d9d3556 100644 --- a/main/system.c +++ b/main/system.c @@ -303,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); @@ -356,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; From a512fdb32b6761347923f752e717a0f6b7d561f9 Mon Sep 17 00:00:00 2001 From: Robin van der Gracht Date: Tue, 2 Jul 2024 11:01:49 +0200 Subject: [PATCH 16/25] Allow supplying serial pin number to serial init call This gives more flexibility and will allow using this code for boards with other pin configuration. Signed-off-by: Robin van der Gracht --- components/asic/include/serial.h | 2 +- components/asic/serial.c | 6 ++---- main/main.c | 2 +- main/self_test/self_test.c | 2 +- 4 files changed, 5 insertions(+), 7 deletions(-) diff --git a/components/asic/include/serial.h b/components/asic/include/serial.h index 82b7d3d3..6cf5b205 100644 --- a/components/asic/include/serial.h +++ b/components/asic/include/serial.h @@ -4,7 +4,7 @@ #define CHUNK_SIZE 1024 int SERIAL_send(uint8_t *, int, bool); -void SERIAL_init(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/asic/serial.c b/components/asic/serial.c index cf139788..1f9f946f 100644 --- a/components/asic/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 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/self_test/self_test.c b/main/self_test/self_test.c index e14e536c..3972295d 100644 --- a/main/self_test/self_test.c +++ b/main/self_test/self_test.c @@ -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); From 94bd3ca376bc5f2efdf20d19caa8ced90d49930c Mon Sep 17 00:00:00 2001 From: Robin van der Gracht Date: Fri, 28 Jun 2024 14:03:30 +0200 Subject: [PATCH 17/25] i2c_master: Allow specifying sda and scl pin numbers to init function Signed-off-by: Robin van der Gracht --- main/i2c_master.c | 8 +++----- main/i2c_master.h | 2 +- main/self_test/self_test.c | 2 +- main/system.c | 2 +- 4 files changed, 6 insertions(+), 8 deletions(-) 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/self_test/self_test.c b/main/self_test/self_test.c index 3972295d..a7362613 100644 --- a/main/self_test/self_test.c +++ b/main/self_test/self_test.c @@ -96,7 +96,7 @@ 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); diff --git a/main/system.c b/main/system.c index 7d9d3556..223dd821 100644 --- a/main/system.c +++ b/main/system.c @@ -81,7 +81,7 @@ 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 From f181561df904de93c7e1f9a03e0def298ae6b462 Mon Sep 17 00:00:00 2001 From: Robin van der Gracht Date: Fri, 28 Jun 2024 14:53:35 +0200 Subject: [PATCH 18/25] led_controller: Use integers instead of GPIO_NUM_* macros The macros availability depends on the platform you're building for. If you build for a esp32c6 (which has much less pins), than the macro for GPIO_NUM_36 is not available and compilation fails. Signed-off-by: Robin van der Gracht --- main/led_controller.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) 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) { From 2f249434bb003369e1f197ab012ee8c5d39f9cc6 Mon Sep 17 00:00:00 2001 From: Robin van der Gracht Date: Sun, 30 Jun 2024 12:29:56 +0200 Subject: [PATCH 19/25] vcore: Simplify vcore range check This allows dropping 2 board specific defines. Signed-off-by: Robin van der Gracht --- main/vcore.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/main/vcore.c b/main/vcore.c index 8cd829e6..b511f307 100644 --- a/main/vcore.c +++ b/main/vcore.c @@ -16,8 +16,6 @@ #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 static const char *TAG = "vcore.c"; @@ -38,16 +36,14 @@ static uint8_t ds4432_tps40305_bitaxe_voltage_to_reg(float vout) 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; - } - // 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); + // make sure the requested voltage is in within range + if (reg > 127) + return 0; + // Set the MSB high if the requested voltage is BELOW nominal if (vout < BITAXE_VNOM) { From 575292b772faad527206676833b7bd100fc2bf72 Mon Sep 17 00:00:00 2001 From: Robin van der Gracht Date: Sun, 30 Jun 2024 14:09:43 +0200 Subject: [PATCH 20/25] vcore: Let ds4432_*_voltage_to_reg function take arguments This allows using this function for other setups with a ds4432 as well. Converted calculations to int for efficiency especially on systems without a fpu. Signed-off-by: Robin van der Gracht --- main/vcore.c | 52 +++++++++++++++++++++++++--------------------------- 1 file changed, 25 insertions(+), 27 deletions(-) diff --git a/main/vcore.c b/main/vcore.c index b511f307..8577633d 100644 --- a/main/vcore.c +++ b/main/vcore.c @@ -7,15 +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_IFS 98921 // nA +#define BITAXE_RTOP 4750 // Ohm +#define BITAXE_RBOT 3320 // Ohm +#define BITAXE_VNOM 1451 // mV static const char *TAG = "vcore.c"; @@ -27,28 +22,29 @@ void VCORE_init(GlobalState * global_state) { 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) +static uint8_t ds4432_voltage_to_reg(uint32_t vout_mv, uint32_t vnom_mv, + uint32_t ra_ohm, uint32_t rb_ohm, + int32_t ifs_na, uint32_t vfb_mv) { - float change; uint8_t reg; + // Calculate current flowing though bottom resistor (Rb) in nA + int32_t irb_na = (vfb_mv * 1000 * 1000) / rb_ohm; + // Calculate current required through top resistor (Ra) to achieve vout in nA + int32_t ira_na = ((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; - // 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); - - // make sure the requested voltage is in within range - if (reg > 127) + // make sure the requested voltage is in within range + if (dac_steps > 127) return 0; - // Set the MSB high if the requested voltage is BELOW nominal - if (vout < BITAXE_VNOM) - { + reg = dac_steps; + + // dac_steps is absolute. For sink S = 0; for source S = 1. + if (vout_mv < vnom_mv) reg |= 0x80; - } return reg; } @@ -63,8 +59,10 @@ bool VCORE_set_voltage(float core_voltage, GlobalState * global_state) ESP_LOGI(TAG, "Set ASIC voltage = %.3fV", core_voltage); TPS546_set_vout(core_voltage * (float)global_state->voltage_domain); } 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); + uint16_t vcore_mv = core_voltage * 1000 * global_state->voltage_domain; + uint8_t reg_setting = ds4432_voltage_to_reg( + 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; From 6e99f7aa68ad5e3db42146cc409ae2b90d1401eb Mon Sep 17 00:00:00 2001 From: Robin van der Gracht Date: Sun, 30 Jun 2024 14:59:30 +0200 Subject: [PATCH 21/25] vcore: Convert VCORE_set_voltage to int mv This now matches the return type of VCORE_get_voltage_mv(). Signed-off-by: Robin van der Gracht --- main/self_test/self_test.c | 2 +- main/system.c | 2 +- main/vcore.c | 11 ++++++----- main/vcore.h | 2 +- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/main/self_test/self_test.c b/main/self_test/self_test.c index a7362613..2885746c 100644 --- a/main/self_test/self_test.c +++ b/main/self_test/self_test.c @@ -100,7 +100,7 @@ void self_test(void * pvParameters) 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: diff --git a/main/system.c b/main/system.c index 223dd821..05547b1e 100644 --- a/main/system.c +++ b/main/system.c @@ -86,7 +86,7 @@ static void _init_system(GlobalState * GLOBAL_STATE) // 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: diff --git a/main/vcore.c b/main/vcore.c index 8577633d..fff54c01 100644 --- a/main/vcore.c +++ b/main/vcore.c @@ -49,19 +49,20 @@ static uint8_t ds4432_voltage_to_reg(uint32_t vout_mv, uint32_t vnom_mv, return reg; } -bool VCORE_set_voltage(float core_voltage, GlobalState * global_state) +bool VCORE_set_voltage(uint16_t vcore_mv, GlobalState * global_state) { + uint32_t sum_vcore_mv = vcore_mv * global_state->voltage_domain; + 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 { - uint16_t vcore_mv = core_voltage * 1000 * global_state->voltage_domain; uint8_t reg_setting = ds4432_voltage_to_reg( - vcore_mv, BITAXE_VNOM, BITAXE_RTOP, BITAXE_RBOT, BITAXE_IFS, 600); + 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! } 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_ */ From 35e2083ff2cdddd5bd50f68f4b49c48a3ec03c00 Mon Sep 17 00:00:00 2001 From: Robin van der Gracht Date: Tue, 2 Jul 2024 11:01:50 +0200 Subject: [PATCH 22/25] asic: Fix compilation warnings of defined but not used common functions Every file that included common.h but didn't use the _largest_power_of_two or _reverse_bits functions gave warnings about them being defined but not used since they ware declared in the header file. Signed-off-by: Robin van der Gracht --- components/asic/CMakeLists.txt | 1 + components/asic/common.c | 28 ++++++++++++++++++++++++++++ components/asic/include/common.h | 27 ++------------------------- 3 files changed, 31 insertions(+), 25 deletions(-) create mode 100644 components/asic/common.c diff --git a/components/asic/CMakeLists.txt b/components/asic/CMakeLists.txt index 77065556..5cf75973 100644 --- a/components/asic/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/asic/common.c b/components/asic/common.c new file mode 100644 index 00000000..f098632e --- /dev/null +++ b/components/asic/common.c @@ -0,0 +1,28 @@ + +#include "common.h" + +unsigned char _reverse_bits(unsigned char num) +{ + unsigned char reversed = 0; + int i; + + for (i = 0; i < 8; i++) { + reversed <<= 1; // Left shift the reversed variable by 1 + reversed |= num & 1; // Use bitwise OR to set the rightmost bit of reversed to the current bit of num + num >>= 1; // Right shift num by 1 to get the next bit + } + + return reversed; +} + +int _largest_power_of_two(int num) +{ + int power = 0; + + while (num > 1) { + num = num >> 1; + power++; + } + + return 1 << power; +} diff --git a/components/asic/include/common.h b/components/asic/include/common.h index 4434ae9b..31d6eedd 100644 --- a/components/asic/include/common.h +++ b/components/asic/include/common.h @@ -10,30 +10,7 @@ typedef struct __attribute__((__packed__)) uint32_t rolled_version; } task_result; -static unsigned char _reverse_bits(unsigned char num) -{ - unsigned char reversed = 0; - int i; - - for (i = 0; i < 8; i++) { - reversed <<= 1; // Left shift the reversed variable by 1 - reversed |= num & 1; // Use bitwise OR to set the rightmost bit of reversed to the current bit of num - num >>= 1; // Right shift num by 1 to get the next bit - } - - return reversed; -} - -static int _largest_power_of_two(int num) -{ - int power = 0; - - while (num > 1) { - num = num >> 1; - power++; - } - - return 1 << power; -} +unsigned char _reverse_bits(unsigned char num); +int _largest_power_of_two(int num); #endif \ No newline at end of file From 88a884c331f8631e376b5e7fba0fd70d52bd45da Mon Sep 17 00:00:00 2001 From: Robin van der Gracht Date: Thu, 2 Nov 2023 10:52:51 +0100 Subject: [PATCH 23/25] connect: Don't use board specific name in generic code Signed-off-by: Robin van der Gracht --- components/connect/connect.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/connect/connect.c b/components/connect/connect.c index b23f2eb8..07f2c2a3 100644 --- a/components/connect/connect.c +++ b/components/connect/connect.c @@ -68,7 +68,7 @@ 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); } From dd2f094d1df3ace9e058b9d6187ef9fcc05ba8bf Mon Sep 17 00:00:00 2001 From: Robin van der Gracht Date: Tue, 2 Jul 2024 11:01:50 +0200 Subject: [PATCH 24/25] Move ds4432_voltage_to_reg to DS4432 driver The function is now generic and can be moved to the driver. Signed-off-by: Robin van der Gracht --- main/DS4432U.c | 27 +++++++++++++++++++++++++++ main/DS4432U.h | 4 +++- main/vcore.c | 29 +---------------------------- 3 files changed, 31 insertions(+), 29 deletions(-) diff --git a/main/DS4432U.c b/main/DS4432U.c index f6f962e0..bc60f7b7 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, + int32_t ifs_na, uint32_t vfb_mv) +{ + uint8_t reg; + // Calculate current flowing though bottom resistor (Rb) in nA + int32_t irb_na = (vfb_mv * 1000 * 1000) / rb_ohm; + // Calculate current required through top resistor (Ra) to achieve vout in nA + int32_t ira_na = ((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..6afcba7b 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, + int32_t ifs_na, uint32_t vfb_mv); #endif /* DS4432U_H_ */ diff --git a/main/vcore.c b/main/vcore.c index fff54c01..97620ef3 100644 --- a/main/vcore.c +++ b/main/vcore.c @@ -22,33 +22,6 @@ void VCORE_init(GlobalState * global_state) { ADC_ch_init(ADC_CHANNEL_1); } -static uint8_t ds4432_voltage_to_reg(uint32_t vout_mv, uint32_t vnom_mv, - uint32_t ra_ohm, uint32_t rb_ohm, - int32_t ifs_na, uint32_t vfb_mv) -{ - uint8_t reg; - // Calculate current flowing though bottom resistor (Rb) in nA - int32_t irb_na = (vfb_mv * 1000 * 1000) / rb_ohm; - // Calculate current required through top resistor (Ra) to achieve vout in nA - int32_t ira_na = ((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; -} - bool VCORE_set_voltage(uint16_t vcore_mv, GlobalState * global_state) { uint32_t sum_vcore_mv = vcore_mv * global_state->voltage_domain; @@ -61,7 +34,7 @@ bool VCORE_set_voltage(uint16_t vcore_mv, GlobalState * global_state) ESP_LOGI(TAG, "Set ASIC voltage = %umV", vcore_mv); TPS546_set_vout((float)sum_vcore_mv / 1000.0); } else { - uint8_t reg_setting = ds4432_voltage_to_reg( + 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! From 1c5fa831bc81188d06d8baba8ec1bd9fb40caca4 Mon Sep 17 00:00:00 2001 From: Robin van der Gracht Date: Fri, 5 Jul 2024 19:21:31 +0200 Subject: [PATCH 25/25] DS4432: Fix integer overflow for vcore values of 4900 mv and higher Signed-off-by: Robin van der Gracht --- main/DS4432U.c | 6 +++--- main/DS4432U.h | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/main/DS4432U.c b/main/DS4432U.c index bc60f7b7..a42f6b0b 100644 --- a/main/DS4432U.c +++ b/main/DS4432U.c @@ -49,13 +49,13 @@ bool DS4432U_test(void) uint8_t DS4432U_voltage_to_reg(uint32_t vout_mv, uint32_t vnom_mv, uint32_t ra_ohm, uint32_t rb_ohm, - int32_t ifs_na, uint32_t vfb_mv) + uint32_t ifs_na, uint32_t vfb_mv) { uint8_t reg; // Calculate current flowing though bottom resistor (Rb) in nA - int32_t irb_na = (vfb_mv * 1000 * 1000) / rb_ohm; + 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 = ((vout_mv - vfb_mv) * 1000 * 1000) / ra_ohm; + 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) diff --git a/main/DS4432U.h b/main/DS4432U.h index 6afcba7b..b9460160 100644 --- a/main/DS4432U.h +++ b/main/DS4432U.h @@ -11,5 +11,5 @@ 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, - int32_t ifs_na, uint32_t vfb_mv); + uint32_t ifs_na, uint32_t vfb_mv); #endif /* DS4432U_H_ */