Skip to content

Commit

Permalink
create driver for temperature sensors, support for multiple fans
Browse files Browse the repository at this point in the history
  • Loading branch information
macphyter committed Nov 13, 2023
1 parent 8672112 commit acd2eda
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 40 deletions.
1 change: 1 addition & 0 deletions main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ SRCS
"EMC2302.c"
"fonts.c"
"INA219.c"
"TMP1075.c"
"led_controller.c"
"main.c"
"nvs_config.c"
Expand Down
40 changes: 13 additions & 27 deletions main/EMC2302.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,56 +48,42 @@ void EMC2302_init(bool invertPolarity)
}

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

speed = (uint8_t) (63.0 * percent);
ESP_ERROR_CHECK(register_write_byte(EMC2302_FAN1_SETTING, speed));
ESP_ERROR_CHECK(register_write_byte(EMC2302_FAN2_SETTING, speed));
ESP_ERROR_CHECK(register_write_byte(FAN_SETTING_REG, speed));
}

// Gets the fan speed
uint16_t EMC2302_get_fan_speed(void)
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(EMC2302_TACH1_LSB, &tach_lsb, 1));
ESP_ERROR_CHECK(register_read(EMC2302_TACH1_MSB, &tach_msb, 1));
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 = %02X %02X", tach_msb, tach_lsb);
ESP_LOGI(TAG, "Raw Fan Speed[%d] = %02X %02X", devicenum, tach_msb, tach_lsb);
RPM = (tach_msb << 5) + ((tach_lsb >> 3) & 0x1F);
ESP_LOGI(TAG, "Fan Speed[%d] = %d RPM", devicenum, RPM);

RPM = tach_lsb | (tach_msb << 8);

ESP_LOGI(TAG, "Fan Speed = %d RPM", RPM);
return RPM;
}

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

//uint8_t temp_msb, temp_lsb;
//uint16_t reading;

//ESP_ERROR_CHECK(register_read(EMC2101_EXTERNAL_TEMP_MSB, &temp_msb, 1));
//ESP_ERROR_CHECK(register_read(EMC2101_EXTERNAL_TEMP_LSB, &temp_lsb, 1));

//reading = temp_lsb | (temp_msb << 8);
//reading >>= 5;

//return (float) reading / 8.0;
return 0;
}

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

//uint8_t temp;
//ESP_ERROR_CHECK(register_read(EMC2101_INTERNAL_TEMP, &temp, 1));
//return temp;
return 0;
}

5 changes: 2 additions & 3 deletions main/EMC2302.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,8 @@ typedef enum
} emc2101_rate_t;

void EMC2302_init(bool);
void EMC2302_set_fan_speed(float);
// void EMC2302_read(void);
uint16_t EMC2302_get_fan_speed(void);
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);
Expand Down
6 changes: 3 additions & 3 deletions main/INA219.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ float INA219_read_voltage(void)
uint8_t data[2];

ESP_ERROR_CHECK(register_read(INA219_REG_BUSVOLTAGE, data, 2));
ESP_LOGI(TAG, "Raw Voltage = %02X %02X", data[0], data[1]);
//ESP_LOGI(TAG, "Raw Voltage = %02X %02X", data[0], data[1]);
ESP_LOGI(TAG, "mVolts: %d", (((data[0] << 8) | data[1]) >> 3) * 4);

return (uint16_t)(((data[0] << 8) | data[1]) >> 3) * 4;
Expand All @@ -59,7 +59,7 @@ float INA219_read_current(void)
uint8_t data[2];

ESP_ERROR_CHECK(register_read(INA219_REG_CURRENT, data, 2));
ESP_LOGI(TAG, "Raw Current = %02X %02X", data[0], data[1]);
//ESP_LOGI(TAG, "Raw Current = %02X %02X", data[0], data[1]);
ESP_LOGI(TAG, "mAmps: %d", (int16_t)(data[0] << 8) | data[1]);

return (int16_t)((data[0] << 8) | data[0]);
Expand All @@ -70,7 +70,7 @@ float INA219_read_power(void)
uint8_t data[2];

ESP_ERROR_CHECK(register_read(INA219_REG_POWER, data, 2));
ESP_LOGI(TAG, "Raw Power = %02X %02X", data[0], data[1]);
//ESP_LOGI(TAG, "Raw Power = %02X %02X", data[0], data[1]);
ESP_LOGI(TAG, "mWatts: %d", ((data[0] << 8) | data[1]) * 20);

return ((data[0] << 8) | data[1]) * 20;
Expand Down
62 changes: 62 additions & 0 deletions main/TMP1075.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <stdio.h>
#include "esp_log.h"
#include "driver/i2c.h"

#include "TMP1075.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 = "TMP1075.c";

bool TMP1075_installed(int);
uint8_t TMP1075_read_temperature(int);

/**
* @brief Read a sequence of I2C bytes
*/
static esp_err_t register_read(uint8_t dev_addr, uint8_t reg_addr, uint8_t *data, size_t len)
{
return i2c_master_write_read_device(I2C_MASTER_NUM, dev_addr, &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_word(uint8_t dev_addr, uint8_t reg_addr, uint8_t *data) {
int ret;
uint8_t write_buf[3] = {reg_addr, data[0], data[1]};

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

return ret;
}

bool TMP1075_installed(int device_index)
{
uint8_t data[2];
esp_err_t result = ESP_OK;

// read the configuration register
ESP_LOGI(TAG, "Reading configuration register");
ESP_ERROR_CHECK(register_read(TMP1075_I2CADDR_DEFAULT + device_index, TMP1075_CONFIG_REG, data, 2));
ESP_LOGI(TAG, "Configuration[%d] = %02X %02X", device_index, data[0], data[1]);

return (result == ESP_OK?true:false);
}

uint8_t TMP1075_read_temperature(int device_index)
{
uint8_t data[2];

ESP_ERROR_CHECK(register_read(TMP1075_I2CADDR_DEFAULT + device_index, TMP1075_TEMP_REG, data, 2));
//ESP_LOGI(TAG, "Raw Temperature = %02X %02X", data[0], data[1]);
ESP_LOGI(TAG, "Temperature[%d] = %d", device_index, data[0]);
return data[0];
}

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

#define TMP1075_I2CADDR_DEFAULT 0x4A ///< TMP1075 i2c address
#define TMP1075_TEMP_REG 0x00 ///< Temperature register
#define TMP1075_CONFIG_REG 0x01 ///< Configuration register
#define TMP1075_LOW_LIMIT 0x02 ///< Low limit register
#define TMP1075_HIGH_LIMIT 0x03 ///< High limit register
#define TMP1075_DEVICE_ID 0x0F ///< Device ID register

bool TMP1075_installed(int);
uint8_t TMP1075_read_temperature(int);

#endif /* TMP1075_H_ */
3 changes: 2 additions & 1 deletion main/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ static void _init_system(GlobalState * global_state, SystemModule * module)
// Fan Tests
EMC2302_init(nvs_config_get_u16(NVS_CONFIG_INVERT_FAN_POLARITY, 1));

EMC2302_set_fan_speed((float) nvs_config_get_u16(NVS_CONFIG_FAN_SPEED, 100) / 100);
EMC2302_set_fan_speed(0, (float) nvs_config_get_u16(NVS_CONFIG_FAN_SPEED, 100) / 100);
EMC2302_set_fan_speed(1, (float) nvs_config_get_u16(NVS_CONFIG_FAN_SPEED, 100) / 100);

vTaskDelay(500 / portTICK_PERIOD_MS);

Expand Down
15 changes: 9 additions & 6 deletions main/tasks/power_management_task.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "DS4432U.h"
#include "EMC2302.h"
#include "INA219.h"
#include "TMP1075.h"
#include "bm1397.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
Expand Down Expand Up @@ -45,6 +46,7 @@ void POWER_MANAGEMENT_task(void * pvParameters)
int last_frequency_increase = 0;

bool read_power = INA219_installed();
bool read_temp = TMP1075_installed(0) && TMP1075_installed(1);

uint16_t frequency_target = nvs_config_get_u16(NVS_CONFIG_ASIC_FREQ, CONFIG_ASIC_FREQUENCY);

Expand All @@ -57,11 +59,13 @@ void POWER_MANAGEMENT_task(void * pvParameters)
power_management->power = INA219_read_power() / 1000;
power_management->current = INA219_read_current();
}
power_management->fan_speed = EMC2302_get_fan_speed();
power_management->fan_speed = EMC2302_get_fan_speed(0);
EMC2302_get_fan_speed(1);

if (strcmp(GLOBAL_STATE->asic_model, "BM1397") == 0) {
power_management->chip_temp = TMP1075_read_temperature(0);
TMP1075_read_temperature(1);

power_management->chip_temp = EMC2302_get_external_temp();
if (strcmp(GLOBAL_STATE->asic_model, "BM1397") == 0) {

// Voltage
// We'll throttle between 4.9v and 3.5v
Expand Down Expand Up @@ -117,7 +121,6 @@ void POWER_MANAGEMENT_task(void * pvParameters)
}
}
} else if (strcmp(GLOBAL_STATE->asic_model, "BM1366") == 0) {
power_management->chip_temp = EMC2302_get_internal_temp() + 5;

if (power_management->chip_temp > THROTTLE_TEMP &&
(power_management->frequency_value > 50 || power_management->voltage > 1000)) {
Expand All @@ -128,8 +131,8 @@ void POWER_MANAGEMENT_task(void * pvParameters)
}
}

// ESP_LOGI(TAG, "target %f, Freq %f, Volt %f, Power %f", target_frequency, power_management->frequency_value,
// power_management->voltage, power_management->power);
// ESP_LOGI(TAG, "target %f, Freq %f, Volt %f, Power %f", target_frequency, power_management->frequency_value,
// power_management->voltage, power_management->power);
vTaskDelay(POLL_RATE / portTICK_PERIOD_MS);
}
}

0 comments on commit acd2eda

Please sign in to comment.