11 static const char *
const TAG =
"EMC2101";
13 static const uint8_t EMC2101_CHIP_ID = 0x16;
14 static const uint8_t EMC2101_ALT_CHIP_ID = 0x28;
17 static const uint8_t EMC2101_REGISTER_INTERNAL_TEMP = 0x00;
18 static const uint8_t EMC2101_REGISTER_EXTERNAL_TEMP_MSB = 0x01;
19 static const uint8_t EMC2101_REGISTER_DAC_CONV_RATE = 0x04;
20 static const uint8_t EMC2101_REGISTER_EXTERNAL_TEMP_LSB = 0x10;
21 static const uint8_t EMC2101_REGISTER_CONFIG = 0x03;
22 static const uint8_t EMC2101_REGISTER_TACH_LSB = 0x46;
23 static const uint8_t EMC2101_REGISTER_TACH_MSB = 0x47;
24 static const uint8_t EMC2101_REGISTER_FAN_CONFIG = 0x4A;
25 static const uint8_t EMC2101_REGISTER_FAN_SETTING = 0x4C;
26 static const uint8_t EMC2101_REGISTER_PWM_FREQ = 0x4D;
27 static const uint8_t EMC2101_REGISTER_PWM_DIV = 0x4E;
28 static const uint8_t EMC2101_REGISTER_WHOAMI = 0xFD;
36 static const uint8_t EMC2101_ALT_TCH_BIT = 1 << 2;
41 static const uint8_t EMC2101_DAC_BIT = 1 << 4;
48 static const uint8_t EMC2101_CLK_OVR_BIT = 1 << 2;
55 static const uint8_t EMC2101_POLARITY_BIT = 1 << 4;
60 ESP_LOGCONFIG(TAG,
"Setting up Emc2101 sensor...");
63 uint8_t chip_id =
reg(EMC2101_REGISTER_WHOAMI).
get();
64 if ((chip_id != EMC2101_CHIP_ID) && (chip_id != EMC2101_ALT_CHIP_ID)) {
65 ESP_LOGE(TAG,
"Wrong chip ID %02X", chip_id);
72 config |= EMC2101_ALT_TCH_BIT;
74 config |= EMC2101_DAC_BIT;
77 config |= EMC2101_POLARITY_BIT;
85 reg(EMC2101_REGISTER_FAN_CONFIG) |= EMC2101_CLK_OVR_BIT;
94 ESP_LOGCONFIG(TAG,
"Emc2101 component:");
97 ESP_LOGE(TAG,
"Communication with EMC2101 failed!");
99 ESP_LOGCONFIG(TAG,
" Mode: %s", this->
dac_mode_ ?
"DAC" :
"PWM");
104 ESP_LOGCONFIG(TAG,
" PWM Divider: %02X", this->
pwm_divider_);
106 ESP_LOGCONFIG(TAG,
" Inverted: %s", YESNO(this->
inverted_));
111 ESP_LOGD(TAG,
"Setting duty fan setting to %02X", duty_cycle);
112 if (!this->
write_byte(EMC2101_REGISTER_FAN_SETTING, duty_cycle)) {
113 ESP_LOGE(TAG,
"Communication with EMC2101 failed!");
121 if (!this->
read_byte(EMC2101_REGISTER_FAN_SETTING, &duty_cycle)) {
122 ESP_LOGE(TAG,
"Communication with EMC2101 failed!");
131 if (!this->
read_byte(EMC2101_REGISTER_INTERNAL_TEMP, &temperature)) {
132 ESP_LOGE(TAG,
"Communication with EMC2101 failed!");
142 if (!this->
read_byte(EMC2101_REGISTER_EXTERNAL_TEMP_MSB, &msb) ||
143 !this->
read_byte(EMC2101_REGISTER_EXTERNAL_TEMP_LSB, &lsb)) {
144 ESP_LOGE(TAG,
"Communication with EMC2101 failed!");
150 uint16_t
raw = (msb << 8 | lsb) >> 5;
157 if (!this->
read_byte(EMC2101_REGISTER_TACH_LSB, &lsb) || !this->
read_byte(EMC2101_REGISTER_TACH_MSB, &msb)) {
158 ESP_LOGE(TAG,
"Communication with EMC2101 failed!");
164 uint16_t tach = msb << 8 | lsb;
165 return tach == 0xFFFF ? 0.0f : 5400000.0f / tach;
bool read_byte(uint8_t a_register, uint8_t *data, bool stop=true)
void status_set_warning(const char *message="unspecified")
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
uint8_t get() const
returns the register value
uint8_t max_output_value_
void dump_config() override
Used by ESPHome framework.
Emc2101DACConversionRate dac_conversion_rate_
float get_internal_temperature()
Gets the internal temperature sensor reading.
void setup() override
Used by ESPHome framework.
float get_duty_cycle()
Gets the Fan output duty cycle.
float get_speed()
Gets the tachometer speed sensor reading.
T remap(U value, U min, U max, T min_out, T max_out)
Remap value from the range (min, max) to (min_out, max_out).
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
This class is used to create I2CRegister objects that act as proxies to read/write internal registers...
float get_external_temperature()
Gets the external temperature sensor reading.
bool write_byte(uint8_t a_register, uint8_t data, bool stop=true)
virtual void mark_failed()
Mark this component as failed.
void set_duty_cycle(float value)
Sets the Fan output duty cycle.
Implementation of SPI Controller mode.
float get_setup_priority() const override
Used by ESPHome framework.