Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TCH - 2.1.9 All-In-One PR #276

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ cmake_minimum_required(VERSION 3.5)

include($ENV{IDF_PATH}/tools/cmake/project.cmake)

set(PROJECT_VER "2.1.9-TCH-All-In-One")

project(esp-miner)

2 changes: 1 addition & 1 deletion components/stratum/stratum_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ int STRATUM_V1_subscribe(int socket, char * model)
char subscribe_msg[BUFFER_SIZE];
const esp_app_desc_t *app_desc = esp_ota_get_app_description();
const char *version = app_desc->version;
sprintf(subscribe_msg, "{\"id\": %d, \"method\": \"mining.subscribe\", \"params\": [\"bitaxe/%s (%s)\"]}\n", send_uid++, model, version);
sprintf(subscribe_msg, "{\"id\": %d, \"method\": \"mining.subscribe\", \"params\": [\"bitaxe/%s/%s\"]}\n", send_uid++, model, version);
debug_stratum_tx(subscribe_msg);
write(socket, subscribe_msg, strlen(subscribe_msg));

Expand Down
1 change: 1 addition & 0 deletions main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ SRCS
"adc.c"
"DS4432U.c"
"EMC2101.c"
"EMC2302.c"
"fonts.c"
"i2c_master.c"
"INA260.c"
Expand Down
89 changes: 89 additions & 0 deletions main/EMC2302.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include "driver/i2c.h"
#include "esp_log.h"
#include <stdio.h>

#include "EMC2302.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_NUM \
0 /*!< I2C master i2c port number, the number of i2c peripheral interfaces available will depend on the chip */
#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 */
#define I2C_MASTER_TIMEOUT_MS 1000

static const char *TAG = "EMC2302.c";

/**
* @brief Read a sequence of I2C bytes
*/
static esp_err_t register_read(uint8_t reg_addr, uint8_t * data, size_t len)
{
return i2c_master_write_read_device(I2C_MASTER_NUM, EMC2302_I2CADDR_DEFAULT, &reg_addr, 1, data, len,
I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);
}

/**
* @brief Write a byte to a I2C register
*/
static esp_err_t register_write_byte(uint8_t reg_addr, uint8_t data)
{
int ret;
uint8_t write_buf[2] = {reg_addr, data};

ret = i2c_master_write_to_device(I2C_MASTER_NUM, EMC2302_I2CADDR_DEFAULT, write_buf, sizeof(write_buf),
I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS);

return ret;
}

// run this first. sets up the PWM polarity register
void EMC2302_init(bool invertPolarity)
{
if (invertPolarity) {
ESP_ERROR_CHECK(register_write_byte(EMC2302_PWM_POLARITY, 0b00011111));
}
}

// Sets the fan speed to a given percent
void EMC2302_set_fan_speed(uint8_t devicenum, float percent)
{
uint8_t speed;
uint8_t FAN_SETTING_REG = EMC2302_FAN1_SETTING + (devicenum * 0x10);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't do arithmetic with constants, this is bound to cause problems. Use ifs or switch and handle out of bound devicenum.


speed = (uint8_t) (255.0 * (1.0f-percent));
ESP_ERROR_CHECK(register_write_byte(FAN_SETTING_REG, speed));
}

// Gets the fan speed
uint16_t EMC2302_get_fan_speed(uint8_t devicenum)
{
uint8_t tach_lsb, tach_msb;
uint16_t RPM;
uint8_t TACH_LSB_REG = EMC2302_TACH1_LSB + (devicenum * 0x10);
uint8_t TACH_MSB_REG = EMC2302_TACH1_MSB + (devicenum * 0x10);

ESP_ERROR_CHECK(register_read(TACH_LSB_REG, &tach_lsb, 1));
ESP_ERROR_CHECK(register_read(TACH_MSB_REG, &tach_msb, 1));

//ESP_LOGI(TAG, "Raw Fan Speed[%d] = %02X %02X", devicenum, tach_msb, tach_lsb);
RPM = (tach_msb << 5) + ((tach_lsb >> 3) & 0x1F);
RPM = EMC2302_FAN_RPM_NUMERATOR / RPM;
//ESP_LOGI(TAG, "Fan Speed[%d] = %d RPM", devicenum, RPM);

return RPM;
}

float EMC2302_get_external_temp(void)
{
// We don't have temperature on this chip, so fake it
return 0;
}

uint8_t EMC2302_get_internal_temp(void)
{
// We don't have temperature on this chip, so fake it
return 0;
}

83 changes: 83 additions & 0 deletions main/EMC2302.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#ifndef EMC2302_H_
#define EMC2302_H_


#define EMC2302_I2CADDR_DEFAULT 0x2F ///< EMC2302-2 default i2c address
#define EMC2302_1_I2CADDR_DEFAULT 0x2E ///< EMC2302-2 default i2c address
#define EMC2302_WHOAMI 0xFD ///< Chip ID register
#define EMC2302_MANUFACTURER_ID 0xFE ///< Manufacturer ID
#define EMC2302_REVISION 0xFF ///< Chip revision

#define EMC2302_CONFIG 0x20 ///< configuration register
#define EMC2302_STATUS 0x24 ///< status register
#define EMC2302_STALL_STATUS 0x25 ///< fan stall status
#define EMC2302_SPIN_STATUS 0x26 ///< fan spin status
#define EMC2302_DRIVE_FAIL_STATUS 0x27 ///< drive fail status
#define EMC2302_INT_ENABLE 0x29 ///< interrupt enable register
#define EMC2302_PWM_POLARITY 0x2A ///< PWM polarity config
#define EMC2302_PWM_OUTPUT 0x2B ///< PWM output config
#define EMC2302_PWM_BASEF45 0x2C ///<
#define EMC2302_PWM_BASEF123 ///<

#define EMC2302_FAN1_SETTING 0x30 ///< Fan 1 setting
#define EMC2302_PWM1_DIVIDE 0x31 ///< PWM 1 divider
#define EMC2302_FAN1_CONFIG1 0x32 ///< Fan 1 configuration 1 register
#define EMC2302_FAN1_CONFIG2 0x33 ///< Fan 1 configuration 2 register
#define EMC2302_GAIN1 0x35 ///< Gain 1 register
#define EMC2302_FAN1_SPINUP_CONFIG 0x36 ///< Fan 1 spinup configuration register
#define EMC2302_FAN1_MAX_STEP 0x37 ///< Fan 1 max step register
#define EMC2302_FAN1_MIN_DRIVE 0x38 ///< Fan 1 minimum drive register
#define EMC2302_FAN1_TACH_COUNT 0x39 ///< Fan 1 valid TACH count
#define EMC2302_FAN1_DRV_FAIL_LOW 0x3A ///< Fan 1 drive fail band low byte
#define EMC2302_FAN1_DRV_FAIL_HIGH 0x3B ///< Fan 1 drive fail band high byte
#define EMC2302_TACH1_TARGET_LSB 0x3C ///< Tach 1 target low byte
#define EMC2302_TACH1_TARGET_MSB 0x3D ///< Tach 1 target high byte
#define EMC2302_TACH1_LSB 0x3E ///< Tach 1 reading low byte
#define EMC2302_TACH1_MSB 0x3F ///< Tach 1 reading high byte

#define EMC2302_FAN2_SETTING 0x40 ///< Fan 2 setting
#define EMC2302_PWM2_DIVIDE 0x41 ///< PWM 2 divider
#define EMC2302_FAN2_CONFIG1 0x42 ///< Fan 2 configuration 1 register
#define EMC2302_FAN2_CONFIG2 0x43 ///< Fan 2 configuration 2 register
#define EMC2302_GAIN2 0x45 ///< Gain 2 register
#define EMC2302_FAN2_SPINUP_CONFIG 0x46 ///< Fan 2 spinup configuration register
#define EMC2302_FAN2_MAX_STEP 0x47 ///< Fan 2 max step register
#define EMC2302_FAN2_MIN_DRIVE 0x48 ///< Fan 2 minimum drive register
#define EMC2302_FAN2_TACH_COUNT 0x49 ///< Fan 2 valid TACH count
#define EMC2302_FAN2_DRV_FAIL_LOW 0x4A ///< Fan 2 drive fail band low byte
#define EMC2302_FAN2_DRV_FAIL_HIGH 0x4B ///< Fan 2 drive fail band high byte
#define EMC2302_TACH2_TARGET_LSB 0x4C ///< Tach 2 target low byte
#define EMC2302_TACH2_TARGET_MSB 0x4D ///< Tach 2 target high byte
#define EMC2302_TACH2_LSB 0x4E ///< Tach 2 reading low byte
#define EMC2302_TACH2_MSB 0x4F ///< Tach 2 reading high byte

#define EMC2302_FAN_RPM_NUMERATOR 3932160 ///< Conversion unit to convert LSBs to fan RPM
#define _TEMP_LSB 0.125 ///< single bit value for internal temperature readings

/**
* @brief
*
* Allowed values for `setDataRate`.
*/
typedef enum
{
EMC2302_RATE_1_16_HZ, ///< 1_16_HZ
EMC2302_RATE_1_8_HZ, ///< 1_8_HZ
EMC2302_RATE_1_4_HZ, ///< 1_4_HZ
EMC2302_RATE_1_2_HZ, ///< 1_2_HZ
EMC2302_RATE_1_HZ, ///< 1_HZ
EMC2302_RATE_2_HZ, ///< 2_HZ
EMC2302_RATE_4_HZ, ///< 4_HZ
EMC2302_RATE_8_HZ, ///< 8_HZ
EMC2302_RATE_16_HZ, ///< 16_HZ
EMC2302_RATE_32_HZ, ///< 32_HZ
} emc2302_rate_t;

void EMC2302_init(bool);
void EMC2302_set_fan_speed(uint8_t, float);
uint16_t EMC2302_get_fan_speed(uint8_t);

float EMC2302_get_external_temp(void);
uint8_t EMC2302_get_internal_temp(void);

#endif /* EMC2302_H_ */
43 changes: 24 additions & 19 deletions main/TPS546.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ static uint8_t MFR_REVISION[] = {0x00, 0x00, 0x01};

static uint8_t COMPENSATION_CONFIG[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF};

static TPS546_CONFIG _tps_config;

/**
* @brief SMBus read byte
*/
Expand Down Expand Up @@ -356,9 +358,12 @@ static uint16_t float_2_ulinear16(float value)
/*--- Public TPS546 functions ---*/

// Set up the TPS546 regulator and turn it on
int TPS546_init(void)
int TPS546_init(TPS546_CONFIG config)
{
uint8_t data[6];
//_tps_config=config;
memcpy(&_tps_config, &config, sizeof(config));

uint8_t data[6];
uint8_t u8_value;
uint16_t u16_value;
float vin;
Expand Down Expand Up @@ -478,7 +483,7 @@ void TPS546_set_mfr_info(void)
/* Set all the relevant config registers for normal operation */
void TPS546_write_entire_config(void)
{
ESP_LOGI(TAG, "---Writing new config values to TPS546---");
ESP_LOGI(TAG, "---Writing new config values to %s---",_tps_config.PROFILE);
/* set up the ON_OFF_CONFIG */
ESP_LOGI(TAG, "Setting ON_OFF_CONFIG");
smb_write_byte(PMBUS_ON_OFF_CONFIG, TPS546_INIT_ON_OFF_CONFIG);
Expand All @@ -489,33 +494,33 @@ void TPS546_write_entire_config(void)

/* vin voltage */
ESP_LOGI(TAG, "Setting VIN");
smb_write_word(PMBUS_VIN_ON, float_2_slinear11(TPS546_INIT_VIN_ON));
smb_write_word(PMBUS_VIN_OFF, float_2_slinear11(TPS546_INIT_VIN_OFF));
smb_write_word(PMBUS_VIN_UV_WARN_LIMIT, float_2_slinear11(TPS546_INIT_VIN_UV_WARN_LIMIT));
smb_write_word(PMBUS_VIN_OV_FAULT_LIMIT, float_2_slinear11(TPS546_INIT_VIN_OV_FAULT_LIMIT));
smb_write_byte(PMBUS_VIN_OV_FAULT_RESPONSE, TPS546_INIT_VIN_OV_FAULT_RESPONSE);
smb_write_word(PMBUS_VIN_ON, float_2_slinear11(_tps_config.TPS546_INIT_VIN_ON));
smb_write_word(PMBUS_VIN_OFF, float_2_slinear11(_tps_config.TPS546_INIT_VIN_OFF));
smb_write_word(PMBUS_VIN_UV_WARN_LIMIT, float_2_slinear11(_tps_config.TPS546_INIT_VIN_UV_WARN_LIMIT));
smb_write_word(PMBUS_VIN_OV_FAULT_LIMIT, float_2_slinear11(_tps_config.TPS546_INIT_VIN_OV_FAULT_LIMIT));
smb_write_byte(PMBUS_VIN_OV_FAULT_RESPONSE, _tps_config.TPS546_INIT_VIN_OV_FAULT_RESPONSE);

/* vout voltage */
ESP_LOGI(TAG, "Setting VOUT SCALE");
smb_write_word(PMBUS_VOUT_SCALE_LOOP, float_2_slinear11(TPS546_INIT_SCALE_LOOP));
smb_write_word(PMBUS_VOUT_SCALE_LOOP, float_2_slinear11(_tps_config.TPS546_INIT_SCALE_LOOP));
ESP_LOGI(TAG, "VOUT_COMMAND");
smb_write_word(PMBUS_VOUT_COMMAND, float_2_ulinear16(TPS546_INIT_VOUT_COMMAND));
smb_write_word(PMBUS_VOUT_COMMAND, float_2_ulinear16(_tps_config.TPS546_INIT_VOUT_COMMAND));
ESP_LOGI(TAG, "VOUT_MAX");
smb_write_word(PMBUS_VOUT_MAX, float_2_ulinear16(TPS546_INIT_VOUT_MAX));
smb_write_word(PMBUS_VOUT_MAX, float_2_ulinear16(_tps_config.TPS546_INIT_VOUT_MAX));
ESP_LOGI(TAG, "VOUT_OV_FAULT_LIMIT");
smb_write_word(PMBUS_VOUT_OV_FAULT_LIMIT, float_2_ulinear16(TPS546_INIT_VOUT_OV_FAULT_LIMIT));
smb_write_word(PMBUS_VOUT_OV_FAULT_LIMIT, float_2_ulinear16(_tps_config.TPS546_INIT_VOUT_OV_FAULT_LIMIT));
ESP_LOGI(TAG, "VOUT_OV_WARN_LIMIT");
smb_write_word(PMBUS_VOUT_OV_WARN_LIMIT, float_2_ulinear16(TPS546_INIT_VOUT_OV_WARN_LIMIT));
smb_write_word(PMBUS_VOUT_OV_WARN_LIMIT, float_2_ulinear16(_tps_config.TPS546_INIT_VOUT_OV_WARN_LIMIT));
ESP_LOGI(TAG, "VOUT_MARGIN_HIGH");
smb_write_word(PMBUS_VOUT_MARGIN_HIGH, float_2_ulinear16(TPS546_INIT_VOUT_MARGIN_HIGH));
smb_write_word(PMBUS_VOUT_MARGIN_HIGH, float_2_ulinear16(_tps_config.TPS546_INIT_VOUT_MARGIN_HIGH));
ESP_LOGI(TAG, "VOUT_MARGIN_LOW");
smb_write_word(PMBUS_VOUT_MARGIN_LOW, float_2_ulinear16(TPS546_INIT_VOUT_MARGIN_LOW));
smb_write_word(PMBUS_VOUT_MARGIN_LOW, float_2_ulinear16(_tps_config.TPS546_INIT_VOUT_MARGIN_LOW));
ESP_LOGI(TAG, "VOUT_UV_WARN_LIMIT");
smb_write_word(PMBUS_VOUT_UV_WARN_LIMIT, float_2_ulinear16(TPS546_INIT_VOUT_UV_WARN_LIMIT));
smb_write_word(PMBUS_VOUT_UV_WARN_LIMIT, float_2_ulinear16(_tps_config.TPS546_INIT_VOUT_UV_WARN_LIMIT));
ESP_LOGI(TAG, "VOUT_UV_FAULT_LIMIT");
smb_write_word(PMBUS_VOUT_UV_FAULT_LIMIT, float_2_ulinear16(TPS546_INIT_VOUT_UV_FAULT_LIMIT));
smb_write_word(PMBUS_VOUT_UV_FAULT_LIMIT, float_2_ulinear16(_tps_config.TPS546_INIT_VOUT_UV_FAULT_LIMIT));
ESP_LOGI(TAG, "VOUT_MIN");
smb_write_word(PMBUS_VOUT_MIN, float_2_ulinear16(TPS546_INIT_VOUT_MIN));
smb_write_word(PMBUS_VOUT_MIN, float_2_ulinear16(_tps_config.TPS546_INIT_VOUT_MIN));

/* iout current */
ESP_LOGI(TAG, "Setting IOUT");
Expand Down Expand Up @@ -662,7 +667,7 @@ void TPS546_set_vout(float volts)
smb_write_byte(PMBUS_OPERATION, OPERATION_OFF);
} else {
/* make sure we're in range */
if ((volts < TPS546_INIT_VOUT_MIN) || (volts > TPS546_INIT_VOUT_MAX)) {
if ((volts < _tps_config.TPS546_INIT_VOUT_MIN) || (volts > _tps_config.TPS546_INIT_VOUT_MAX)) {
ESP_LOGI(TAG, "ERR- Voltage requested (%f V) is out of range", volts);
} else {
/* set the output voltage */
Expand Down
Loading