#include #include "esp_log.h" #include "i2c_bitaxe.h" #include "EMC2101.h" static const char * TAG = "EMC2101"; // static const char *TAG = "EMC2101.c"; static i2c_master_dev_handle_t emc2101_dev_handle; /** * @brief Initialize the EMC2101 sensor. * * @return esp_err_t ESP_OK on success, or an error code on failure. */ esp_err_t EMC2101_init(bool invertPolarity) { if (i2c_bitaxe_add_device(EMC2101_I2CADDR_DEFAULT, &emc2101_dev_handle) != ESP_OK) { ESP_LOGE(TAG, "Failed to add device"); return ESP_FAIL; } // set the TACH input ESP_ERROR_CHECK(i2c_bitaxe_register_write_byte(emc2101_dev_handle, EMC2101_REG_CONFIG, 0x04)); if (invertPolarity) { ESP_ERROR_CHECK(i2c_bitaxe_register_write_byte(emc2101_dev_handle, EMC2101_FAN_CONFIG, 0b00100011)); } return ESP_OK; } // takes a fan speed percent void EMC2101_set_fan_speed(float percent) { uint8_t speed; speed = (uint8_t) (63.0 * percent); ESP_ERROR_CHECK(i2c_bitaxe_register_write_byte(emc2101_dev_handle, EMC2101_REG_FAN_SETTING, speed)); } // RPM = 5400000/reading uint16_t EMC2101_get_fan_speed(void) { uint8_t tach_lsb, tach_msb; uint16_t reading; uint16_t RPM; ESP_ERROR_CHECK(i2c_bitaxe_register_read(emc2101_dev_handle, EMC2101_TACH_LSB, &tach_lsb, 1)); ESP_ERROR_CHECK(i2c_bitaxe_register_read(emc2101_dev_handle, EMC2101_TACH_MSB, &tach_msb, 1)); // ESP_LOGI(TAG, "Raw Fan Speed = %02X %02X", tach_msb, tach_lsb); reading = tach_lsb | (tach_msb << 8); RPM = 5400000 / reading; // ESP_LOGI(TAG, "Fan Speed = %d RPM", RPM); if (RPM == 82) { return 0; } return RPM; } float EMC2101_get_external_temp(void) { uint8_t temp_msb, temp_lsb; uint16_t reading; ESP_ERROR_CHECK(i2c_bitaxe_register_read(emc2101_dev_handle, EMC2101_EXTERNAL_TEMP_MSB, &temp_msb, 1)); ESP_ERROR_CHECK(i2c_bitaxe_register_read(emc2101_dev_handle, EMC2101_EXTERNAL_TEMP_LSB, &temp_lsb, 1)); reading = temp_lsb | (temp_msb << 8); reading >>= 5; if (reading == EMC2101_TEMP_FAULT_OPEN_CIRCUIT) { ESP_LOGE(TAG, "EMC2101 TEMP_FAULT_OPEN_CIRCUIT: %04X", reading); } if (reading == EMC2101_TEMP_FAULT_SHORT) { ESP_LOGE(TAG, "EMC2101 TEMP_FAULT_SHORT: %04X", reading); } float result = (float) reading / 8.0; // Greater than 200C is probably an erroneous reading... if (result > 200){ return EMC2101_get_internal_temp(); } return result; } uint8_t EMC2101_get_internal_temp(void) { uint8_t temp; ESP_ERROR_CHECK(i2c_bitaxe_register_read(emc2101_dev_handle, EMC2101_INTERNAL_TEMP, &temp, 1)); return temp; }