diff --git a/components/bm1397/bm1397.c b/components/bm1397/bm1397.c index a0d74676..2b33fb8b 100644 --- a/components/bm1397/bm1397.c +++ b/components/bm1397/bm1397.c @@ -16,41 +16,14 @@ #define SLEEP_TIME 20 #define FREQ_MULT 25.0 - - -static const char *TAG = "bm1397"; - -static void send_hash_frequency(float frequency); - -//reset the BM1397 via the RTS line -void reset_BM1397(void) { - gpio_set_level(BM1397_RST_PIN, 0); - - //delay for 100ms - vTaskDelay(100 / portTICK_RATE_MS); - - //set the gpio pin high - gpio_set_level(BM1397_RST_PIN, 1); - - //delay for 100ms - vTaskDelay(100 / portTICK_RATE_MS); - -} - -void init_BM1397(void) { - ESP_LOGI(TAG, "Initializing BM1397"); - - gpio_pad_select_gpio(BM1397_RST_PIN); - gpio_set_direction(BM1397_RST_PIN, GPIO_MODE_OUTPUT); - -} +static const char *TAG = "bm1397Module"; /// @brief /// @param ftdi /// @param header /// @param data /// @param len -void send_BM1397(uint8_t header, uint8_t * data, uint8_t data_len, bool debug) { +static void _send_BM1397(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); @@ -85,103 +58,21 @@ void send_BM1397(uint8_t header, uint8_t * data, uint8_t data_len, bool debug) { free(buf); } -void send_read_address(void) { +static void _send_chain_inactive(void) { unsigned char read_address[2] = {0x00, 0x00}; //send serial data - send_BM1397((TYPE_CMD | GROUP_ALL | CMD_READ), read_address, 2, false); + _send_BM1397((TYPE_CMD | GROUP_ALL | CMD_INACTIVE), read_address, 2, false); } -void send_chain_inactive(void) { - - unsigned char read_address[2] = {0x00, 0x00}; - //send serial data - send_BM1397((TYPE_CMD | GROUP_ALL | CMD_INACTIVE), read_address, 2, false); -} - -void set_chip_address(uint8_t chipAddr) { +static void _set_chip_address(uint8_t chipAddr) { unsigned char read_address[2] = {chipAddr, 0x00}; //send serial data - send_BM1397((TYPE_CMD | GROUP_SINGLE | CMD_SETADDRESS), read_address, 2, false); + _send_BM1397((TYPE_CMD | GROUP_SINGLE | CMD_SETADDRESS), read_address, 2, false); } -void send_init(void) { - - //send serial data - vTaskDelay(SLEEP_TIME / portTICK_RATE_MS); - send_chain_inactive(); - - set_chip_address(0x00); - - unsigned char init[6] = {0x00, 0x80, 0x00, 0x00, 0x00, 0x00}; //init1 - clock_order_control0 - send_BM1397((TYPE_CMD | GROUP_ALL | CMD_WRITE), init, 6, false); - - unsigned char init2[6] = {0x00, 0x84, 0x00, 0x00, 0x00, 0x00}; //init2 - clock_order_control1 - send_BM1397((TYPE_CMD | GROUP_ALL | CMD_WRITE), init2, 6, false); - - unsigned char init3[9] = {0x00, 0x20, 0x00, 0x00, 0x00, 0x01}; //init3 - ordered_clock_enable - send_BM1397((TYPE_CMD | GROUP_ALL | CMD_WRITE), init3, 6, false); - - unsigned char init4[9] = {0x00, 0x3C, 0x80, 0x00, 0x80, 0x74}; //init4 - init_4_? - send_BM1397((TYPE_CMD | GROUP_ALL | CMD_WRITE), init4, 6, false); - - set_job_difficulty_mask(256); - - unsigned char init5[9] = {0x00, 0x68, 0xC0, 0x70, 0x01, 0x11}; //init5 - pll3_parameter - send_BM1397((TYPE_CMD | GROUP_ALL | CMD_WRITE), init5, 6, false); - - unsigned char init5_2[9] = {0x00, 0x68, 0xC0, 0x70, 0x01, 0x11}; //init5_2 - pll3_parameter - send_BM1397((TYPE_CMD | GROUP_ALL | CMD_WRITE), init5_2, 6, false); - - unsigned char init6[9] = {0x00, 0x28, 0x06, 0x00, 0x00, 0x0F}; //init6 - fast_uart_configuration - send_BM1397((TYPE_CMD | GROUP_ALL | CMD_WRITE), init6, 6, false); - - set_default_baud(); - - send_hash_frequency(BM1397_FREQUENCY); -} - -// Baud formula = 25M/((denominator+1)*8) -// The denominator is 5 bits found in the misc_control (bits 9-13) -void set_default_baud(void){ - //default divider of 26 (11010) for 115,749 - unsigned char baudrate[9] = {0x00, 0x18, 0x00, 0x00, 0b01111010, 0b00110001}; //baudrate - misc_control - send_BM1397((TYPE_CMD | GROUP_ALL | CMD_WRITE), baudrate, 6, false); -} - -void set_bm1397_max_baud(void){ - // divider of 0 for 3,125,000 - unsigned char baudrate[9] = { 0x00, 0x18, 0x00, 0x00, 0b01100000, 0b00110001 };; //baudrate - misc_control - send_BM1397((TYPE_CMD | GROUP_ALL | CMD_WRITE), baudrate, 6, false); -} - -void set_job_difficulty_mask(int difficulty){ - - // Default mask of 256 diff - unsigned char job_difficulty_mask[9] = {0x00, 0x14, 0b00000000, 0b00000000, 0b00000000, 0b11111111}; - - // The mask must be a power of 2 so there are no holes - // Correct: {0b00000000, 0b00000000, 0b11111111, 0b11111111} - // Incorrect: {0b00000000, 0b00000000, 0b11100111, 0b11111111} - difficulty = largestPowerOfTwo(difficulty) -1; // (difficulty - 1) if it is a pow 2 then step down to second largest for more hashrate sampling - - // convert difficulty into char array - // Ex: 256 = {0b00000000, 0b00000000, 0b00000000, 0b11111111}, {0x00, 0x00, 0x00, 0xff} - // Ex: 512 = {0b00000000, 0b00000000, 0b00000001, 0b11111111}, {0x00, 0x00, 0x01, 0xff} - for (int i = 0; i < 4; i++) { - char value = (difficulty >> (8 * i)) & 0xFF; - //The char is read in backwards to the register so we need to reverse them - //So a mask of 512 looks like 0b00000000 00000000 00000001 1111111 - //and not 0b00000000 00000000 10000000 1111111 - - job_difficulty_mask[5 - i] = reverseBits(value); - } - - send_BM1397((TYPE_CMD | GROUP_ALL | CMD_WRITE), job_difficulty_mask, 6, false); -} - -unsigned char reverseBits(unsigned char num) { +static unsigned char _reverse_bits(unsigned char num) { unsigned char reversed = 0; int i; @@ -194,7 +85,7 @@ unsigned char reverseBits(unsigned char num) { return reversed; } -int largestPowerOfTwo(int num) { +static int _largest_power_of_two(int num) { int power = 0; while (num > 1) { @@ -205,14 +96,8 @@ int largestPowerOfTwo(int num) { return 1 << power; } - -void send_work(struct job_packet *job) { - send_BM1397((TYPE_JOB | GROUP_SINGLE | CMD_WRITE), (uint8_t*)job, sizeof(struct job_packet), false); -} - - // borrowed from cgminer driver-gekko.c calc_gsf_freq() -static void send_hash_frequency(float frequency) { +static void _send_hash_frequency(float frequency) { unsigned char prefreq1[9] = {0x00, 0x70, 0x0F, 0x0F, 0x0F, 0x00}; //prefreq - pll0_divider @@ -269,11 +154,11 @@ static void send_hash_frequency(float frequency) { for (i = 0; i < 2; i++) { vTaskDelay(10 / portTICK_RATE_MS); - send_BM1397((TYPE_CMD | GROUP_ALL | CMD_WRITE), prefreq1, 6, false); + _send_BM1397((TYPE_CMD | GROUP_ALL | CMD_WRITE), prefreq1, 6, false); } for (i = 0; i < 2; i++) { vTaskDelay(10 / portTICK_RATE_MS); - send_BM1397((TYPE_CMD | GROUP_ALL | CMD_WRITE), freqbuf, 6, false); + _send_BM1397((TYPE_CMD | GROUP_ALL | CMD_WRITE), freqbuf, 6, false); } vTaskDelay(10 / portTICK_RATE_MS); @@ -281,3 +166,133 @@ static void send_hash_frequency(float frequency) { ESP_LOGI(TAG, "Setting Frequency to %.2fMHz (%.2f)", frequency, newf); } + +static void _send_init(void) { + + //send serial data + vTaskDelay(SLEEP_TIME / portTICK_RATE_MS); + _send_chain_inactive(); + + _set_chip_address(0x00); + + unsigned char init[6] = {0x00, 0x80, 0x00, 0x00, 0x00, 0x00}; //init1 - clock_order_control0 + _send_BM1397((TYPE_CMD | GROUP_ALL | CMD_WRITE), init, 6, false); + + unsigned char init2[6] = {0x00, 0x84, 0x00, 0x00, 0x00, 0x00}; //init2 - clock_order_control1 + _send_BM1397((TYPE_CMD | GROUP_ALL | CMD_WRITE), init2, 6, false); + + unsigned char init3[9] = {0x00, 0x20, 0x00, 0x00, 0x00, 0x01}; //init3 - ordered_clock_enable + _send_BM1397((TYPE_CMD | GROUP_ALL | CMD_WRITE), init3, 6, false); + + unsigned char init4[9] = {0x00, 0x3C, 0x80, 0x00, 0x80, 0x74}; //init4 - init_4_? + _send_BM1397((TYPE_CMD | GROUP_ALL | CMD_WRITE), init4, 6, false); + + BM1397_set_job_difficulty_mask(256); + + unsigned char init5[9] = {0x00, 0x68, 0xC0, 0x70, 0x01, 0x11}; //init5 - pll3_parameter + _send_BM1397((TYPE_CMD | GROUP_ALL | CMD_WRITE), init5, 6, false); + + unsigned char init5_2[9] = {0x00, 0x68, 0xC0, 0x70, 0x01, 0x11}; //init5_2 - pll3_parameter + _send_BM1397((TYPE_CMD | GROUP_ALL | CMD_WRITE), init5_2, 6, false); + + unsigned char init6[9] = {0x00, 0x28, 0x06, 0x00, 0x00, 0x0F}; //init6 - fast_uart_configuration + _send_BM1397((TYPE_CMD | GROUP_ALL | CMD_WRITE), init6, 6, false); + + BM1397_set_default_baud(); + + _send_hash_frequency(BM1397_FREQUENCY); +} + + +//reset the BM1397 via the RTS line +static void _reset(void) { + gpio_set_level(BM1397_RST_PIN, 0); + + //delay for 100ms + vTaskDelay(100 / portTICK_RATE_MS); + + //set the gpio pin high + gpio_set_level(BM1397_RST_PIN, 1); + + //delay for 100ms + vTaskDelay(100 / portTICK_RATE_MS); + +} + + +static void _send_read_address(void) { + + unsigned char read_address[2] = {0x00, 0x00}; + //send serial data + _send_BM1397((TYPE_CMD | GROUP_ALL | CMD_READ), read_address, 2, false); +} + + +void BM1397_init(void) { + ESP_LOGI(TAG, "Initializing BM1397"); + + gpio_pad_select_gpio(BM1397_RST_PIN); + gpio_set_direction(BM1397_RST_PIN, GPIO_MODE_OUTPUT); + + //reset the bm1397 + _reset(); + + //send the init command + _send_read_address(); + + _send_init(); + + +} + + + + + +// Baud formula = 25M/((denominator+1)*8) +// The denominator is 5 bits found in the misc_control (bits 9-13) +void BM1397_set_default_baud(void){ + //default divider of 26 (11010) for 115,749 + unsigned char baudrate[9] = {0x00, 0x18, 0x00, 0x00, 0b01111010, 0b00110001}; //baudrate - misc_control + _send_BM1397((TYPE_CMD | GROUP_ALL | CMD_WRITE), baudrate, 6, false); +} + +void BM1397_set_max_baud(void){ + // divider of 0 for 3,125,000 + unsigned char baudrate[9] = { 0x00, 0x18, 0x00, 0x00, 0b01100000, 0b00110001 };; //baudrate - misc_control + _send_BM1397((TYPE_CMD | GROUP_ALL | CMD_WRITE), baudrate, 6, false); +} + +void BM1397_set_job_difficulty_mask(int difficulty){ + + // Default mask of 256 diff + unsigned char job_difficulty_mask[9] = {0x00, 0x14, 0b00000000, 0b00000000, 0b00000000, 0b11111111}; + + // The mask must be a power of 2 so there are no holes + // Correct: {0b00000000, 0b00000000, 0b11111111, 0b11111111} + // Incorrect: {0b00000000, 0b00000000, 0b11100111, 0b11111111} + difficulty = _largest_power_of_two(difficulty) -1; // (difficulty - 1) if it is a pow 2 then step down to second largest for more hashrate sampling + + // convert difficulty into char array + // Ex: 256 = {0b00000000, 0b00000000, 0b00000000, 0b11111111}, {0x00, 0x00, 0x00, 0xff} + // Ex: 512 = {0b00000000, 0b00000000, 0b00000001, 0b11111111}, {0x00, 0x00, 0x01, 0xff} + for (int i = 0; i < 4; i++) { + char value = (difficulty >> (8 * i)) & 0xFF; + //The char is read in backwards to the register so we need to reverse them + //So a mask of 512 looks like 0b00000000 00000000 00000001 1111111 + //and not 0b00000000 00000000 10000000 1111111 + + job_difficulty_mask[5 - i] = _reverse_bits(value); + } + + _send_BM1397((TYPE_CMD | GROUP_ALL | CMD_WRITE), job_difficulty_mask, 6, false); +} + + + + +void BM1397_send_work(struct job_packet *job) { + _send_BM1397((TYPE_JOB | GROUP_SINGLE | CMD_WRITE), (uint8_t*)job, sizeof(struct job_packet), false); +} + + diff --git a/components/bm1397/include/bm1397.h b/components/bm1397/include/bm1397.h index 31454fbd..a5a89a17 100644 --- a/components/bm1397/include/bm1397.h +++ b/components/bm1397/include/bm1397.h @@ -30,7 +30,9 @@ static const u_int64_t BM1397_HASHRATE_S = BM1397_FREQUENCY * BM1397_CORE_COUNT static const u_int64_t NONCE_SPACE = 4294967296; static const double BM1397_FULLSCAN_MS = ((double)NONCE_SPACE / (double)BM1397_HASHRATE_S) * 1000; +typedef struct { +} bm1397Module; typedef enum { JOB_PACKET = 0, @@ -60,16 +62,12 @@ struct __attribute__((__packed__)) nonce_response { uint8_t crc; }; +void BM1397_init(void); -void send_read_address(void); -void send_init(void); -void send_work(struct job_packet *job); -void reset_BM1397(void); -void init_BM1397(void); -void set_job_difficulty_mask(int); -unsigned char reverseBits(unsigned char num); -int largestPowerOfTwo(int num); -void set_bm1397_max_baud(void); -void set_default_baud(void); +void BM1397_send_init(void); +void BM1397_send_work(struct job_packet *job); +void BM1397_set_job_difficulty_mask(int); +void BM1397_set_max_baud(void); +void BM1397_set_default_baud(void); #endif /* BM1397_H_ */ \ No newline at end of file diff --git a/components/bm1397/serial.c b/components/bm1397/serial.c index 0c0286e4..8da6bb67 100644 --- a/components/bm1397/serial.c +++ b/components/bm1397/serial.c @@ -43,7 +43,7 @@ void init_serial(void) { void set_max_baud(void){ ESP_LOGI("SERIAL", "SETTING CHIP MAX BAUD"); - set_bm1397_max_baud(); + BM1397_set_max_baud(); ESP_LOGI("SERIAL", "SETTING UART MAX BAUD"); uart_set_baudrate(UART_NUM_1, 3125000); } diff --git a/components/bm1397/test/test_job_command.c b/components/bm1397/test/test_job_command.c index 08d359d7..b4d0dfd1 100644 --- a/components/bm1397/test/test_job_command.c +++ b/components/bm1397/test/test_job_command.c @@ -14,19 +14,11 @@ TEST_CASE("Check known working midstate + job command", "[bm1397]") init_serial(); uart_initialized = 1; - init_BM1397(); - - // reset the bm1397 - reset_BM1397(); - - // send the init command - send_read_address(); + BM1397_init(); // read back response debug_serial_rx(); - // send the init commands - send_init(); } uint8_t work1[50] = { @@ -44,7 +36,7 @@ TEST_CASE("Check known working midstate + job command", "[bm1397]") uint8_t buf[1024]; memset(buf, 0, 1024); - send_work(&test_job); + BM1397_send_work(&test_job); uint16_t received = serial_rx(buf, 9, 20); TEST_ASSERT_GREATER_OR_EQUAL_UINT16(sizeof(struct nonce_response), received); diff --git a/main/miner.c b/main/miner.c index 10c4562b..40bd239a 100755 --- a/main/miner.c +++ b/main/miner.c @@ -56,25 +56,14 @@ bm_job ** active_jobs; uint8_t * valid_jobs; pthread_mutex_t valid_jobs_lock; +static SystemModule SYSTEM_MODULE; +static bm1397Module BM1397_MODULE; + static void ASIC_task(void * pvParameters) { init_serial(); - init_BM1397(); - - ESP_LOGI(TAG, "Job wait time:%f", BM1397_FULLSCAN_MS); - - //reset the bm1397 - reset_BM1397(); - - //send the init command - send_read_address(); - - //read back response - debug_serial_rx(); - - //send the init commands - send_init(); + BM1397_init(); uint8_t buf[CHUNK_SIZE]; memset(buf, 0, 1024); @@ -92,7 +81,7 @@ static void ASIC_task(void * pvParameters) set_max_baud(); - SYSTEM_notify_mining_started(); + SYSTEM_notify_mining_started(&SYSTEM_MODULE); ESP_LOGI(TAG, "Mining!"); while (1) { bm_job * next_bm_job = (bm_job *) queue_dequeue(&ASIC_jobs_queue); @@ -118,7 +107,7 @@ static void ASIC_task(void * pvParameters) pthread_mutex_unlock(&valid_jobs_lock); clear_serial_buffer(); - send_work(&job); //send the job to the ASIC + BM1397_send_work(&job); //send the job to the ASIC //wait for a response int received = serial_rx(buf, 9, BM1397_FULLSCAN_MS); @@ -169,7 +158,7 @@ static void ASIC_task(void * pvParameters) if (nonce_diff > active_jobs[nonce.job_id]->pool_diff) { - SYSTEM_notify_found_nonce(active_jobs[nonce.job_id]->pool_diff); + SYSTEM_notify_found_nonce(&SYSTEM_MODULE, active_jobs[nonce.job_id]->pool_diff); submit_share(sock, STRATUM_USER, active_jobs[nonce.job_id]->jobid, active_jobs[nonce.job_id]->ntime, active_jobs[nonce.job_id]->extranonce2, nonce.nonce); @@ -334,7 +323,7 @@ static void stratum_task(void * pvParameters) stratum_difficulty = new_difficulty; difficulty_changed = true; ESP_LOGI(TAG, "Set stratum difficulty: %d", stratum_difficulty); - set_job_difficulty_mask(stratum_difficulty); + BM1397_set_job_difficulty_mask(stratum_difficulty); } free(line); @@ -379,7 +368,7 @@ void app_main(void) ESP_ERROR_CHECK(esp_netif_init()); ESP_ERROR_CHECK(esp_event_loop_create_default()); - xTaskCreate(SYSTEM_task, "SYSTEM_task", 4096, NULL, 10, &sysTaskHandle); + xTaskCreate(SYSTEM_task, "SYSTEM_task", 4096, &SYSTEM_MODULE, 10, &sysTaskHandle); /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. * Read "Establishing Wi-Fi or Ethernet Connection" section in @@ -394,3 +383,5 @@ void app_main(void) xTaskCreate(create_jobs_task, "stratum miner", 8192, NULL, 10, NULL); xTaskCreate(ASIC_task, "asic", 8192, NULL, 10, &serialTaskHandle); } + + diff --git a/main/system.c b/main/system.c index 130f4834..00008662 100644 --- a/main/system.c +++ b/main/system.c @@ -15,7 +15,7 @@ #include "system.h" -static const char *TAG = "system"; +static const char *TAG = "SystemModule"; #define BM1397_VOLTAGE CONFIG_BM1397_VOLTAGE #define HISTORY_LENGTH 100 @@ -28,7 +28,7 @@ static uint16_t shares_accepted = 0; static uint16_t shares_rejected = 0; static time_t start_time; -static double duration_start = 0; + static int historical_hashrate_rolling_index = 0; static double historical_hashrate_time_stamps[HISTORY_LENGTH] = {0.0}; static double historical_hashrate[HISTORY_LENGTH] = {0.0}; @@ -36,7 +36,9 @@ static int historical_hashrate_init = 0; static double current_hashrate = 0; -static void _init_system(void) { +static void _init_system(SystemModule* module) { + + module->duration_start = 0; start_time = time(NULL); @@ -186,9 +188,11 @@ static void _update_system_performance(){ } -void SYSTEM_task(void *arg) { +void SYSTEM_task(SystemModule* module) { - _init_system(); + + + _init_system(module); while(1){ _clear_display(); @@ -221,11 +225,11 @@ void SYSTEM_notify_rejected_share(void){ } -void SYSTEM_notify_mining_started(void){ - duration_start = esp_timer_get_time(); +void SYSTEM_notify_mining_started(SystemModule* module){ + module->duration_start = esp_timer_get_time(); } -void SYSTEM_notify_found_nonce(double nonce_diff){ +void SYSTEM_notify_found_nonce(SystemModule* module, double nonce_diff){ @@ -246,14 +250,14 @@ void SYSTEM_notify_found_nonce(double nonce_diff){ if(historical_hashrate_init < HISTORY_LENGTH){ historical_hashrate_init++; }else{ - duration_start = historical_hashrate_time_stamps[(historical_hashrate_rolling_index + 1) % HISTORY_LENGTH]; + module->duration_start = historical_hashrate_time_stamps[(historical_hashrate_rolling_index + 1) % HISTORY_LENGTH]; } double sum = 0; for (int i = 0; i < historical_hashrate_init; i++) { sum += historical_hashrate[i]; } - double duration = (double)(esp_timer_get_time() - duration_start) / 1000000; + double duration = (double)(esp_timer_get_time() - module->duration_start) / 1000000; double rolling_rate = (sum * 4294967296) / (duration * 1000000000); if(historical_hashrate_init < HISTORY_LENGTH){ diff --git a/main/system.h b/main/system.h index d6e05c79..5d3ae303 100644 --- a/main/system.h +++ b/main/system.h @@ -2,15 +2,15 @@ #define SYSTEM_H_ typedef struct { - + double duration_start; } SystemModule; -void SYSTEM_task(void *arg); +void SYSTEM_task(SystemModule* module); void SYSTEM_notify_accepted_share(void); void SYSTEM_notify_rejected_share(void); -void SYSTEM_notify_found_nonce(double nonce_diff); -void SYSTEM_notify_mining_started(void); +void SYSTEM_notify_found_nonce(SystemModule* module, double nonce_diff); +void SYSTEM_notify_mining_started(SystemModule* module); -#endif /* SYSTEM_H_ */ \ No newline at end of file +#endif /* SYSTEM_H_ */