Skip to content

Commit

Permalink
add calculated frequency ramp up function. uncomment set_max_baudrate
Browse files Browse the repository at this point in the history
  • Loading branch information
skot committed Nov 30, 2024
1 parent 4e15ebe commit bf53a56
Showing 1 changed file with 36 additions and 229 deletions.
265 changes: 36 additions & 229 deletions components/asic/bm1366.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ typedef struct __attribute__((__packed__))
uint8_t crc;
} asic_result;

static float current_frequency = 56.25;

static const char * TAG = "bm1366Module";

static uint8_t asic_response_buffer[SERIAL_BUF_SIZE];
Expand Down Expand Up @@ -178,8 +180,7 @@ void BM1366_send_hash_frequency(float target_freq)
puts("Finding dividers failed, using default value (200Mhz)");
} else {
newf = 25.0 * (float) (fb_divider) / (float) (ref_divider * post_divider1 * post_divider2);
printf("final refdiv: %d, fbdiv: %d, postdiv1: %d, postdiv2: %d, min diff value: %f\n", ref_divider, fb_divider,
post_divider1, post_divider2, min_difference);
//printf("final refdiv: %d, fbdiv: %d, postdiv1: %d, postdiv2: %d, min diff value: %f\n", ref_divider, fb_divider, post_divider1, post_divider2, min_difference);

freqbuf[3] = fb_divider;
freqbuf[4] = ref_divider;
Expand All @@ -195,221 +196,36 @@ void BM1366_send_hash_frequency(float target_freq)
ESP_LOGI(TAG, "Setting Frequency to %.2fMHz (%.2f)", target_freq, newf);
}

static void do_frequency_ramp_up()
{
// 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 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);
static void do_frequency_ramp_up(float target_frequency) {
float step = 6.25;
float current = current_frequency;
float target = target_frequency;

unsigned char init762[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x40, 0xBC, 0x02, 0x31, 0x10};
_send_simple(init762, 11);
float direction = (target > current) ? step : -step;

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);
if (fmod(current, step) != 0) {
float next_dividable;
if (direction > 0) {
next_dividable = ceil(current / step) * step;
} else {
next_dividable = floor(current / step) * step;
}
current = next_dividable;
BM1366_send_hash_frequency(current);
vTaskDelay(100 / portTICK_PERIOD_MS);
}

unsigned char init793[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x08, 0x50, 0xC2, 0x02, 0x40, 0x1C};
_send_simple(init793, 11);
while ((direction > 0 && current < target) || (direction < 0 && current > target)) {
float next_step = fmin(fabs(direction), fabs(target - current));
current += direction > 0 ? next_step : -next_step;
BM1366_send_hash_frequency(current);
vTaskDelay(100 / portTICK_PERIOD_MS);
}
BM1366_send_hash_frequency(target);
return;
}


static uint8_t _send_init(uint64_t frequency, uint16_t asic_count)
{

Expand Down Expand Up @@ -438,16 +254,14 @@ static uint8_t _send_init(uint64_t frequency, uint16_t asic_count)
unsigned char init5[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x18, 0xFF, 0x0F, 0xC1, 0x00, 0x00};
_send_simple(init5, 11);

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

// split the chip address space evenly
uint8_t address_interval = (uint8_t) (256 / chip_counter);
for (uint8_t i = 0; i < chip_counter; i++) {
//{ 0x55, 0xAA, 0x40, 0x05, 0x00, 0x00, 0x1C };
_set_chip_address(i * address_interval);
// unsigned char init7[7] = { 0x55, 0xAA, 0x40, 0x05, 0x00, 0x00, 0x1C };
// _send_simple(init7, 7);
}

unsigned char init135[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x3C, 0x80, 0x00, 0x85, 0x40, 0x0C};
Expand All @@ -456,8 +270,7 @@ static uint8_t _send_init(uint64_t frequency, uint16_t asic_count)
unsigned char init136[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x3C, 0x80, 0x00, 0x80, 0x20, 0x19};
_send_simple(init136, 11);

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

unsigned char init138[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x54, 0x00, 0x00, 0x00, 0x03, 0x1D};
Expand All @@ -469,6 +282,7 @@ static uint8_t _send_init(uint64_t frequency, uint16_t asic_count)
unsigned char init171[11] = {0x55, 0xAA, 0x41, 0x09, 0x00, 0x2C, 0x00, 0x7C, 0x00, 0x03, 0x03};
_send_simple(init171, 11);

//S19XP Dump sends baudrate change here.. we wait until later.
// unsigned char init173[11] = {0x55, 0xAA, 0x51, 0x09, 0x00, 0x28, 0x11, 0x30, 0x02, 0x00, 0x03};
// _send_simple(init173, 11);

Expand All @@ -485,9 +299,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, BM1366_SERIALTX_DEBUG);
}

do_frequency_ramp_up();

BM1366_send_hash_frequency(frequency);
do_frequency_ramp_up((float)frequency);

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

Expand Down Expand Up @@ -553,16 +365,11 @@ int BM1366_set_default_baud(void)

int BM1366_set_max_baud(void)
{
ESP_LOGI(TAG, "Setting max baud of 1000000");


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

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

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

void BM1366_set_job_difficulty_mask(int difficulty)
Expand Down

0 comments on commit bf53a56

Please sign in to comment.