more cleanup of mb1397 and system

This commit is contained in:
Ben
2023-06-06 14:09:51 -04:00
committed by johnny9
parent c148aec820
commit 3d9d1692f2
4 changed files with 108 additions and 98 deletions

View File

@ -16,6 +16,15 @@
#define SLEEP_TIME 20 #define SLEEP_TIME 20
#define FREQ_MULT 25.0 #define FREQ_MULT 25.0
#define CLOCK_ORDER_CONTROL_0 0x80
#define CLOCK_ORDER_CONTROL_1 0x84
#define ORDERED_CLOCK_ENABLE 0x20
#define CORE_REGISTER_CONTROL 0x3C
#define PLL3_PARAMETER 0x68
#define FAST_UART_CONFIGURATION 0x28
#define TICKET_MASK 0x14
#define MISC_CONTROL 0x18
static const char *TAG = "bm1397Module"; static const char *TAG = "bm1397Module";
/// @brief /// @brief
@ -175,27 +184,24 @@ static void _send_init(void) {
_set_chip_address(0x00); _set_chip_address(0x00);
unsigned char init[6] = {0x00, 0x80, 0x00, 0x00, 0x00, 0x00}; //init1 - clock_order_control0 unsigned char init[6] = {0x00, CLOCK_ORDER_CONTROL_0, 0x00, 0x00, 0x00, 0x00}; //init1 - clock_order_control0
_send_BM1397((TYPE_CMD | GROUP_ALL | CMD_WRITE), init, 6, false); _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 unsigned char init2[6] = {0x00, CLOCK_ORDER_CONTROL_1, 0x00, 0x00, 0x00, 0x00}; //init2 - clock_order_control1
_send_BM1397((TYPE_CMD | GROUP_ALL | CMD_WRITE), init2, 6, false); _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 unsigned char init3[9] = {0x00, ORDERED_CLOCK_ENABLE, 0x00, 0x00, 0x00, 0x01}; //init3 - ordered_clock_enable
_send_BM1397((TYPE_CMD | GROUP_ALL | CMD_WRITE), init3, 6, false); _send_BM1397((TYPE_CMD | GROUP_ALL | CMD_WRITE), init3, 6, false);
unsigned char init4[9] = {0x00, 0x3C, 0x80, 0x00, 0x80, 0x74}; //init4 - init_4_? unsigned char init4[9] = {0x00, CORE_REGISTER_CONTROL, 0x80, 0x00, 0x80, 0x74}; //init4 - init_4_?
_send_BM1397((TYPE_CMD | GROUP_ALL | CMD_WRITE), init4, 6, false); _send_BM1397((TYPE_CMD | GROUP_ALL | CMD_WRITE), init4, 6, false);
BM1397_set_job_difficulty_mask(256); BM1397_set_job_difficulty_mask(256);
unsigned char init5[9] = {0x00, 0x68, 0xC0, 0x70, 0x01, 0x11}; //init5 - pll3_parameter unsigned char init5[9] = {0x00, PLL3_PARAMETER, 0xC0, 0x70, 0x01, 0x11}; //init5 - pll3_parameter
_send_BM1397((TYPE_CMD | GROUP_ALL | CMD_WRITE), init5, 6, false); _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 unsigned char init6[9] = {0x00, FAST_UART_CONFIGURATION, 0x06, 0x00, 0x00, 0x0F}; //init6 - fast_uart_configuration
_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); _send_BM1397((TYPE_CMD | GROUP_ALL | CMD_WRITE), init6, 6, false);
BM1397_set_default_baud(); BM1397_set_default_baud();
@ -253,20 +259,20 @@ void BM1397_init(void) {
// The denominator is 5 bits found in the misc_control (bits 9-13) // The denominator is 5 bits found in the misc_control (bits 9-13)
void BM1397_set_default_baud(void){ void BM1397_set_default_baud(void){
//default divider of 26 (11010) for 115,749 //default divider of 26 (11010) for 115,749
unsigned char baudrate[9] = {0x00, 0x18, 0x00, 0x00, 0b01111010, 0b00110001}; //baudrate - misc_control unsigned char baudrate[9] = {0x00, MISC_CONTROL, 0x00, 0x00, 0b01111010, 0b00110001}; //baudrate - misc_control
_send_BM1397((TYPE_CMD | GROUP_ALL | CMD_WRITE), baudrate, 6, false); _send_BM1397((TYPE_CMD | GROUP_ALL | CMD_WRITE), baudrate, 6, false);
} }
void BM1397_set_max_baud(void){ void BM1397_set_max_baud(void){
// divider of 0 for 3,125,000 // divider of 0 for 3,125,000
unsigned char baudrate[9] = { 0x00, 0x18, 0x00, 0x00, 0b01100000, 0b00110001 };; //baudrate - misc_control unsigned char baudrate[9] = { 0x00, MISC_CONTROL, 0x00, 0x00, 0b01100000, 0b00110001 };; //baudrate - misc_control
_send_BM1397((TYPE_CMD | GROUP_ALL | CMD_WRITE), baudrate, 6, false); _send_BM1397((TYPE_CMD | GROUP_ALL | CMD_WRITE), baudrate, 6, false);
} }
void BM1397_set_job_difficulty_mask(int difficulty){ void BM1397_set_job_difficulty_mask(int difficulty){
// Default mask of 256 diff // Default mask of 256 diff
unsigned char job_difficulty_mask[9] = {0x00, 0x14, 0b00000000, 0b00000000, 0b00000000, 0b11111111}; unsigned char job_difficulty_mask[9] = {0x00, TICKET_MASK, 0b00000000, 0b00000000, 0b00000000, 0b11111111};
// The mask must be a power of 2 so there are no holes // The mask must be a power of 2 so there are no holes
// Correct: {0b00000000, 0b00000000, 0b11111111, 0b11111111} // Correct: {0b00000000, 0b00000000, 0b11111111, 0b11111111}

View File

@ -338,10 +338,10 @@ static void stratum_task(void * pvParameters)
int16_t parsed_id; int16_t parsed_id;
if (parse_stratum_result_message(line, &parsed_id)) { if (parse_stratum_result_message(line, &parsed_id)) {
ESP_LOGI(TAG, "message id %d result accepted", parsed_id); ESP_LOGI(TAG, "message id %d result accepted", parsed_id);
SYSTEM_notify_accepted_share(); SYSTEM_notify_accepted_share(&SYSTEM_MODULE);
} else { } else {
ESP_LOGI(TAG, "message id %d result rejected", parsed_id); ESP_LOGI(TAG, "message id %d result rejected", parsed_id);
SYSTEM_notify_rejected_share(); SYSTEM_notify_rejected_share(&SYSTEM_MODULE);
} }
free(line); free(line);
} else { } else {

View File

@ -18,29 +18,19 @@
static const char *TAG = "SystemModule"; static const char *TAG = "SystemModule";
#define BM1397_VOLTAGE CONFIG_BM1397_VOLTAGE #define BM1397_VOLTAGE CONFIG_BM1397_VOLTAGE
#define HISTORY_LENGTH 100
#define HISTORY_WINDOW_SIZE 5
static char oled_buf[20];
static int screen_page = 0;
static uint16_t shares_accepted = 0;
static uint16_t shares_rejected = 0;
static time_t start_time;
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};
static int historical_hashrate_init = 0;
static double current_hashrate = 0;
static void _init_system(SystemModule* module) { static void _init_system(SystemModule* module) {
module->duration_start = 0; module->duration_start = 0;
module->historical_hashrate_rolling_index = 0;
module->historical_hashrate_init = 0;
module->current_hashrate = 0;
module->screen_page = 0;
module->shares_accepted = 0;
module->shares_rejected = 0;
start_time = time(NULL); module->start_time = time(NULL);
//test the LEDs //test the LEDs
// ESP_LOGI(TAG, "Init LEDs!"); // ESP_LOGI(TAG, "Init LEDs!");
@ -77,30 +67,31 @@ static void _init_system(SystemModule* module) {
} }
static void _update_hashrate(void){ static void _update_hashrate(SystemModule* module){
if(screen_page != 0){
if(module->screen_page != 0){
return; return;
} }
float power = INA260_read_power() / 1000; float power = INA260_read_power() / 1000;
float efficiency = power / (current_hashrate/1000.0); float efficiency = power / (module->current_hashrate/1000.0);
OLED_clearLine(0); OLED_clearLine(0);
memset(oled_buf, 0, 20); memset(module->oled_buf, 0, 20);
snprintf(oled_buf, 20, "Gh%s: %.1f W/Th: %.1f", historical_hashrate_init < HISTORY_LENGTH ? "*": "", current_hashrate, efficiency); snprintf(module->oled_buf, 20, "Gh%s: %.1f W/Th: %.1f", module->historical_hashrate_init < HISTORY_LENGTH ? "*": "", module->current_hashrate, efficiency);
OLED_writeString(0, 0, oled_buf); OLED_writeString(0, 0, module->oled_buf);
} }
static void _update_shares(void){ static void _update_shares(SystemModule* module){
if(screen_page != 0){ if(module->screen_page != 0){
return; return;
} }
OLED_clearLine(1); OLED_clearLine(1);
memset(oled_buf, 0, 20); memset(module->oled_buf, 0, 20);
snprintf(oled_buf, 20, "A/R: %u/%u", shares_accepted, shares_rejected); snprintf(module->oled_buf, 20, "A/R: %u/%u", module->shares_accepted, module->shares_rejected);
OLED_writeString(0, 1, oled_buf); OLED_writeString(0, 1, module->oled_buf);
} }
static void _clear_display(void){ static void _clear_display(void){
@ -111,8 +102,8 @@ static void _clear_display(void){
} }
static void _update_system_info(void) { static void _update_system_info(SystemModule* module) {
char oled_buf[21];
uint16_t fan_speed = EMC2101_get_fan_speed(); uint16_t fan_speed = EMC2101_get_fan_speed();
float chip_temp = EMC2101_get_chip_temp(); float chip_temp = EMC2101_get_chip_temp();
@ -122,27 +113,27 @@ static void _update_system_info(void) {
if (OLED_status()) { if (OLED_status()) {
memset(oled_buf, 0, 20); memset(module->oled_buf, 0, 20);
snprintf(oled_buf, 20, " Fan: %d RPM", fan_speed); snprintf(module->oled_buf, 20, " Fan: %d RPM", fan_speed);
OLED_writeString(0, 0, oled_buf); OLED_writeString(0, 0, module->oled_buf);
memset(oled_buf, 0, 20); memset(module->oled_buf, 0, 20);
snprintf(oled_buf, 20, "Temp: %.1f C", chip_temp); snprintf(module->oled_buf, 20, "Temp: %.1f C", chip_temp);
OLED_writeString(0, 1, oled_buf); OLED_writeString(0, 1, module->oled_buf);
memset(oled_buf, 0, 20); memset(module->oled_buf, 0, 20);
snprintf(oled_buf, 20, " Pwr: %.3f W", power); snprintf(module->oled_buf, 20, " Pwr: %.3f W", power);
OLED_writeString(0, 2, oled_buf); OLED_writeString(0, 2, module->oled_buf);
memset(oled_buf, 0, 20); memset(module->oled_buf, 0, 20);
snprintf(oled_buf, 20, " %i mV: %i mA",(int)voltage, (int)current); snprintf(module->oled_buf, 20, " %i mV: %i mA",(int)voltage, (int)current);
OLED_writeString(0, 3, oled_buf); OLED_writeString(0, 3, module->oled_buf);
} }
} }
static void _update_esp32_info(void) { static void _update_esp32_info(SystemModule* module) {
char oled_buf[20];
uint32_t free_heap_size = esp_get_free_heap_size(); uint32_t free_heap_size = esp_get_free_heap_size();
@ -150,13 +141,13 @@ static void _update_esp32_info(void) {
if (OLED_status()) { if (OLED_status()) {
memset(oled_buf, 0, 20); memset(module->oled_buf, 0, 20);
snprintf(oled_buf, 20, "FH: %u bytes", free_heap_size); snprintf(module->oled_buf, 20, "FH: %u bytes", free_heap_size);
OLED_writeString(0, 0, oled_buf); OLED_writeString(0, 0, module->oled_buf);
memset(oled_buf, 0, 20); memset(module->oled_buf, 0, 20);
snprintf(oled_buf, 20, "vCore: %u mV", vcore); snprintf(module->oled_buf, 20, "vCore: %u mV", vcore);
OLED_writeString(0, 1, oled_buf); OLED_writeString(0, 1, module->oled_buf);
// memset(oled_buf, 0, 20); // memset(oled_buf, 0, 20);
// snprintf(oled_buf, 20, "Pwr: %.2f W", power); // snprintf(oled_buf, 20, "Pwr: %.2f W", power);
@ -165,10 +156,11 @@ static void _update_esp32_info(void) {
} }
static void _update_system_performance(){ static void _update_system_performance(SystemModule* module){
// Calculate the uptime in seconds // Calculate the uptime in seconds
double uptime_in_seconds = difftime(time(NULL), start_time); double uptime_in_seconds = difftime(time(NULL), module->start_time);
int uptime_in_days = uptime_in_seconds / (3600 * 24); int uptime_in_days = uptime_in_seconds / (3600 * 24);
int remaining_seconds = (int)uptime_in_seconds % (3600 * 24); int remaining_seconds = (int)uptime_in_seconds % (3600 * 24);
int uptime_in_hours = remaining_seconds / 3600; int uptime_in_hours = remaining_seconds / 3600;
@ -178,12 +170,12 @@ static void _update_system_performance(){
if (OLED_status()) { if (OLED_status()) {
_update_hashrate(); _update_hashrate(module);
_update_shares(); _update_shares(module);
memset(oled_buf, 0, 20); memset(module->oled_buf, 0, 20);
snprintf(oled_buf, 20, "UT: %dd %ih %im", uptime_in_days, uptime_in_hours, uptime_in_minutes); snprintf(module->oled_buf, 20, "UT: %dd %ih %im", uptime_in_days, uptime_in_hours, uptime_in_minutes);
OLED_writeString(0, 2, oled_buf); OLED_writeString(0, 2, module->oled_buf);
} }
} }
@ -196,18 +188,18 @@ void SYSTEM_task(SystemModule* module) {
while(1){ while(1){
_clear_display(); _clear_display();
screen_page = 0; module->screen_page = 0;
_update_system_performance(); _update_system_performance(module);
vTaskDelay(40000 / portTICK_RATE_MS); vTaskDelay(40000 / portTICK_RATE_MS);
_clear_display(); _clear_display();
screen_page = 1; module->screen_page = 1;
_update_system_info(); _update_system_info(module);
vTaskDelay(10000 / portTICK_RATE_MS); vTaskDelay(10000 / portTICK_RATE_MS);
_clear_display(); _clear_display();
screen_page = 2; module->screen_page = 2;
_update_esp32_info(); _update_esp32_info(module);
vTaskDelay(10000 / portTICK_RATE_MS); vTaskDelay(10000 / portTICK_RATE_MS);
} }
@ -215,13 +207,13 @@ void SYSTEM_task(SystemModule* module) {
void SYSTEM_notify_accepted_share(void){ void SYSTEM_notify_accepted_share(SystemModule* module){
shares_accepted++; module->shares_accepted++;
_update_shares(); _update_shares(module);
} }
void SYSTEM_notify_rejected_share(void){ void SYSTEM_notify_rejected_share(SystemModule* module){
shares_rejected++; module->shares_rejected++;
_update_shares(); _update_shares(module);
} }
@ -239,35 +231,35 @@ void SYSTEM_notify_found_nonce(SystemModule* module, double nonce_diff){
// hashrate = (nonce_difficulty * 2^32) / time_to_find // hashrate = (nonce_difficulty * 2^32) / time_to_find
historical_hashrate[historical_hashrate_rolling_index] = nonce_diff; module->historical_hashrate[module->historical_hashrate_rolling_index] = nonce_diff;
historical_hashrate_time_stamps[historical_hashrate_rolling_index] = esp_timer_get_time(); module->historical_hashrate_time_stamps[module->historical_hashrate_rolling_index] = esp_timer_get_time();
historical_hashrate_rolling_index = (historical_hashrate_rolling_index + 1) % HISTORY_LENGTH; module->historical_hashrate_rolling_index = (module->historical_hashrate_rolling_index + 1) % HISTORY_LENGTH;
//ESP_LOGI(TAG, "nonce_diff %.1f, ttf %.1f, res %.1f", nonce_diff, duration, historical_hashrate[historical_hashrate_rolling_index]); //ESP_LOGI(TAG, "nonce_diff %.1f, ttf %.1f, res %.1f", nonce_diff, duration, historical_hashrate[historical_hashrate_rolling_index]);
if(historical_hashrate_init < HISTORY_LENGTH){ if(module->historical_hashrate_init < HISTORY_LENGTH){
historical_hashrate_init++; module->historical_hashrate_init++;
}else{ }else{
module->duration_start = historical_hashrate_time_stamps[(historical_hashrate_rolling_index + 1) % HISTORY_LENGTH]; module->duration_start = module->historical_hashrate_time_stamps[(module->historical_hashrate_rolling_index + 1) % HISTORY_LENGTH];
} }
double sum = 0; double sum = 0;
for (int i = 0; i < historical_hashrate_init; i++) { for (int i = 0; i < module->historical_hashrate_init; i++) {
sum += historical_hashrate[i]; sum += module->historical_hashrate[i];
} }
double duration = (double)(esp_timer_get_time() - module->duration_start) / 1000000; double duration = (double)(esp_timer_get_time() - module->duration_start) / 1000000;
double rolling_rate = (sum * 4294967296) / (duration * 1000000000); double rolling_rate = (sum * 4294967296) / (duration * 1000000000);
if(historical_hashrate_init < HISTORY_LENGTH){ if(module->historical_hashrate_init < HISTORY_LENGTH){
current_hashrate = rolling_rate; module->current_hashrate = rolling_rate;
}else{ }else{
// More smoothing // More smoothing
current_hashrate = ((current_hashrate * 9) + rolling_rate)/10; module->current_hashrate = ((module->current_hashrate * 9) + rolling_rate)/10;
} }
_update_hashrate(); _update_hashrate(module);
// logArrayContents(historical_hashrate, HISTORY_LENGTH); // logArrayContents(historical_hashrate, HISTORY_LENGTH);
// logArrayContents(historical_hashrate_time_stamps, HISTORY_LENGTH); // logArrayContents(historical_hashrate_time_stamps, HISTORY_LENGTH);

View File

@ -1,15 +1,27 @@
#ifndef SYSTEM_H_ #ifndef SYSTEM_H_
#define SYSTEM_H_ #define SYSTEM_H_
#define HISTORY_LENGTH 100
#define HISTORY_WINDOW_SIZE 5
typedef struct { typedef struct {
double duration_start; double duration_start;
int historical_hashrate_rolling_index;
double historical_hashrate_time_stamps[HISTORY_LENGTH];
double historical_hashrate[HISTORY_LENGTH];
int historical_hashrate_init;
double current_hashrate;
time_t start_time;
uint16_t shares_accepted;
uint16_t shares_rejected;
int screen_page;
char oled_buf[20]
} SystemModule; } SystemModule;
void SYSTEM_task(SystemModule* module); void SYSTEM_task(SystemModule* module);
void SYSTEM_notify_accepted_share(void); void SYSTEM_notify_accepted_share(SystemModule* module);
void SYSTEM_notify_rejected_share(void); void SYSTEM_notify_rejected_share(SystemModule* module);
void SYSTEM_notify_found_nonce(SystemModule* module, double nonce_diff); void SYSTEM_notify_found_nonce(SystemModule* module, double nonce_diff);
void SYSTEM_notify_mining_started(SystemModule* module); void SYSTEM_notify_mining_started(SystemModule* module);