diff --git a/components/bm1397/bm1366.c b/components/bm1397/bm1366.c index 68d648d5b..3ebd38e79 100644 --- a/components/bm1397/bm1366.c +++ b/components/bm1397/bm1366.c @@ -1,18 +1,19 @@ -#include -#include -#include -#include -#include +#include "bm1366.h" + +#include "crc.h" +#include "global_state.h" +#include "serial.h" +#include "utils.h" +#include "esp_log.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" -#include "esp_log.h" -#include "serial.h" -#include "bm1366.h" -#include "utils.h" -#include "crc.h" -#include "global_state.h" +#include +#include +#include +#include +#include #define BM1366_RST_PIN GPIO_NUM_1 @@ -54,7 +55,7 @@ typedef struct __attribute__((__packed__)) uint8_t crc; } asic_result; -static const char *TAG = "bm1366Module"; +static const char * TAG = "bm1366Module"; static uint8_t asic_response_buffer[CHUNK_SIZE]; static task_result result; @@ -64,13 +65,13 @@ static task_result result; /// @param header /// @param data /// @param len -static void _send_BM1366(uint8_t header, uint8_t *data, uint8_t data_len, bool debug) +static void _send_BM1366(uint8_t header, uint8_t * data, uint8_t data_len, bool debug) { packet_type_t packet_type = (header & TYPE_JOB) ? JOB_PACKET : CMD_PACKET; uint8_t total_length = (packet_type == JOB_PACKET) ? (data_len + 6) : (data_len + 5); // allocate memory for buffer - unsigned char *buf = malloc(total_length); + unsigned char * buf = malloc(total_length); // add the preamble buf[0] = 0x55; @@ -86,14 +87,11 @@ static void _send_BM1366(uint8_t header, uint8_t *data, uint8_t data_len, bool d memcpy(buf + 4, data, data_len); // add the correct crc type - if (packet_type == JOB_PACKET) - { + if (packet_type == JOB_PACKET) { uint16_t crc16_total = crc16_false(buf + 2, data_len + 2); buf[4 + data_len] = (crc16_total >> 8) & 0xFF; buf[5 + data_len] = crc16_total & 0xFF; - } - else - { + } else { buf[4 + data_len] = crc5(buf + 2, data_len + 2); } @@ -103,9 +101,9 @@ static void _send_BM1366(uint8_t header, uint8_t *data, uint8_t data_len, bool d free(buf); } -static void _send_simple(uint8_t *data, uint8_t total_length) +static void _send_simple(uint8_t * data, uint8_t total_length) { - unsigned char *buf = malloc(total_length); + unsigned char * buf = malloc(total_length); memcpy(buf, data, total_length); SERIAL_send(buf, total_length, false); @@ -131,11 +129,8 @@ static void _set_chip_address(uint8_t chipAddr) // borrowed from cgminer driver-gekko.c calc_gsf_freq() void BM1366_send_hash_frequency(float frequency) { - - unsigned char prefreq1[9] = {0x00, 0x70, 0x0F, 0x0F, 0x0F, 0x00}; // prefreq - pll0_divider - // default 200Mhz if it fails - unsigned char freqbuf[9] = {0x00, 0x08, 0x40, 0xA0, 0x02, 0x25}; // freqbuf - pll0_parameter + unsigned char freqbuf[9] = {0x00, 0x08, 0x40, 0xA0, 0x02, 0x40}; // freqbuf - pll0_parameter float deffreq = 200.0; @@ -146,34 +141,25 @@ void BM1366_send_hash_frequency(float frequency) // bound the frequency setting // You can go as low as 13 but it doesn't really scale or // produce any nonces - if (frequency < 50) - { + if (frequency < 50) { f1 = 50; - } - else if (frequency > 500) - { + } else if (frequency > 500) { f1 = 500; - } - else - { + } else { f1 = frequency; } fb = 2; - fc1 = 1; - fc2 = 5; // initial multiplier of 10 - if (f1 >= 500) - { + fc1 = 5; + fc2 = 1; // initial multiplier of 10 + + if (f1 >= 500) { // halve down to '250-400' - fb = 1; - } - else if (f1 <= 150) - { + fc1 = 6; + } else if (f1 <= 150) { // triple up to '300-450' fc1 = 3; - } - else if (f1 <= 250) - { + } else if (f1 <= 250) { // double up to '300-500' fc1 = 2; } @@ -187,37 +173,28 @@ void BM1366_send_hash_frequency(float frequency) fa = basef / FREQ_MULT; // code failure ... basef isn't 400 to 6000 - if (fa < famin || fa > famax) - { + if (fa < famin || fa > famax) { newf = deffreq; - } - else - { - freqbuf[3] = (int)fa; - freqbuf[4] = (int)fb; + } else { + freqbuf[3] = (int) fa; + freqbuf[4] = (int) fb; // fc1, fc2 'should' already be 1..15 - freqbuf[5] = (((int)fc1 & 0xf) << 4) + ((int)fc2 & 0xf); + freqbuf[5] = ((int) (fc1 - 1) & 0xf) << 4; + freqbuf[5] += ((int) (fc2 - 1) & 0xf); - newf = basef / ((float)fb * (float)fc1 * (float)fc2); - } + if (fa * fb >= 2400) { + freqbuf[2] = 0x50; + } - for (i = 0; i < 2; i++) - { - vTaskDelay(10 / portTICK_PERIOD_MS); - _send_BM1366((TYPE_CMD | GROUP_ALL | CMD_WRITE), prefreq1, 6, false); - } - for (i = 0; i < 2; i++) - { - vTaskDelay(10 / portTICK_PERIOD_MS); - _send_BM1366((TYPE_CMD | GROUP_ALL | CMD_WRITE), freqbuf, 6, false); + newf = basef / (fb * fc1 * fc2); } - vTaskDelay(10 / portTICK_PERIOD_MS); + _send_BM1366((TYPE_CMD | GROUP_ALL | CMD_WRITE), freqbuf, 6, false); ESP_LOGI(TAG, "Setting Frequency to %.2fMHz (%.2f)", frequency, newf); } -static void _send_init(u_int64_t frequency) +static void _send_init(uint64_t frequency) { // //send serial data @@ -553,7 +530,7 @@ static void _send_read_address(void) _send_BM1366((TYPE_CMD | GROUP_ALL | CMD_READ), read_address, 2, false); } -void BM1366_init(u_int64_t frequency) +void BM1366_init(uint64_t frequency) { ESP_LOGI(TAG, "Initializing BM1366"); @@ -605,13 +582,13 @@ void BM1366_set_job_difficulty_mask(int difficulty) // The mask must be a power of 2 so there are no holes // Correct: {0b00000000, 0b00000000, 0b11111111, 0b11111111} // Incorrect: {0b00000000, 0b00000000, 0b11100111, 0b11111111} - difficulty = _largest_power_of_two(difficulty) - 1; // (difficulty - 1) if it is a pow 2 then step down to second largest for more hashrate sampling + // (difficulty - 1) if it is a pow 2 then step down to second largest for more hashrate sampling + difficulty = _largest_power_of_two(difficulty) - 1; // convert difficulty into char array // Ex: 256 = {0b00000000, 0b00000000, 0b00000000, 0b11111111}, {0x00, 0x00, 0x00, 0xff} // Ex: 512 = {0b00000000, 0b00000000, 0b00000001, 0b11111111}, {0x00, 0x00, 0x01, 0xff} - for (int i = 0; i < 4; i++) - { + for (int i = 0; i < 4; i++) { char value = (difficulty >> (8 * i)) & 0xFF; // The char is read in backwards to the register so we need to reverse them // So a mask of 512 looks like 0b00000000 00000000 00000001 1111111 @@ -627,10 +604,10 @@ void BM1366_set_job_difficulty_mask(int difficulty) static uint8_t id = 0; -void BM1366_send_work(void *pvParameters, bm_job *next_bm_job) +void BM1366_send_work(void * pvParameters, bm_job * next_bm_job) { - GlobalState *GLOBAL_STATE = (GlobalState *)pvParameters; + GlobalState * GLOBAL_STATE = (GlobalState *) pvParameters; BM1366_job job; id = (id + 8) % 128; @@ -643,8 +620,7 @@ void BM1366_send_work(void *pvParameters, bm_job *next_bm_job) memcpy(job.prev_block_hash, next_bm_job->prev_block_hash_be, 32); memcpy(&job.version, &next_bm_job->version, 4); - if (GLOBAL_STATE->ASIC_TASK_MODULE.active_jobs[job.job_id] != NULL) - { + if (GLOBAL_STATE->ASIC_TASK_MODULE.active_jobs[job.job_id] != NULL) { free_bm_job(GLOBAL_STATE->ASIC_TASK_MODULE.active_jobs[job.job_id]); } @@ -658,30 +634,26 @@ void BM1366_send_work(void *pvParameters, bm_job *next_bm_job) _send_BM1366((TYPE_JOB | GROUP_SINGLE | CMD_WRITE), &job, sizeof(BM1366_job), false); } -asic_result *BM1366_receive_work(void) +asic_result * BM1366_receive_work(void) { // wait for a response, wait time is pretty arbitrary int received = SERIAL_rx(asic_response_buffer, 11, 60000); - if (received < 0) - { + if (received < 0) { ESP_LOGI(TAG, "Error in serial RX"); return NULL; - } - else if (received == 0) - { + } else if (received == 0) { // Didn't find a solution, restart and try again return NULL; } - if (received != 11 || asic_response_buffer[0] != 0xAA || asic_response_buffer[1] != 0x55) - { + if (received != 11 || asic_response_buffer[0] != 0xAA || asic_response_buffer[1] != 0x55) { ESP_LOGI(TAG, "Serial RX invalid %i", received); ESP_LOG_BUFFER_HEX(TAG, asic_response_buffer, received); return NULL; } - return (asic_result *)asic_response_buffer; + return (asic_result *) asic_response_buffer; } uint16_t reverse_uint16(uint16_t num) @@ -689,20 +661,19 @@ uint16_t reverse_uint16(uint16_t num) return (num >> 8) | (num << 8); } -task_result *BM1366_proccess_work(void *pvParameters) +task_result * BM1366_proccess_work(void * pvParameters) { - asic_result *asic_result = BM1366_receive_work(); + asic_result * asic_result = BM1366_receive_work(); - if (asic_result == NULL) - { + if (asic_result == NULL) { return NULL; } uint8_t job_id = asic_result->job_id; uint8_t rx_job_id = job_id & 0xf8; - GlobalState *GLOBAL_STATE = (GlobalState *)pvParameters; + GlobalState * GLOBAL_STATE = (GlobalState *) pvParameters; uint32_t rolled_version = GLOBAL_STATE->ASIC_TASK_MODULE.active_jobs[rx_job_id]->version;