ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
emc2101.cpp
Go to the documentation of this file.
1// Implementation based on:
2// - Adafruit_EMC2101: https://github.com/adafruit/Adafruit_EMC2101
3// - Official Datasheet: https://ww1.microchip.com/downloads/en/DeviceDoc/2101.pdf
4
5#include "esphome/core/log.h"
7#include "emc2101.h"
8
9namespace esphome::emc2101 {
10
11ESPHOME_LOG_TAG(TAG, "EMC2101");
12
13static const uint8_t EMC2101_CHIP_ID = 0x16; // EMC2101 default device id from part id
14static const uint8_t EMC2101_ALT_CHIP_ID = 0x28; // EMC2101 alternate device id from part id
15
16// EMC2101 registers from the datasheet. We only define what we use.
17static const uint8_t EMC2101_REGISTER_INTERNAL_TEMP = 0x00; // The internal temperature register
18static const uint8_t EMC2101_REGISTER_EXTERNAL_TEMP_MSB = 0x01; // high byte for the external temperature reading
19static const uint8_t EMC2101_REGISTER_DAC_CONV_RATE = 0x04; // DAC convesion rate config
20static const uint8_t EMC2101_REGISTER_EXTERNAL_TEMP_LSB = 0x10; // low byte for the external temperature reading
21static const uint8_t EMC2101_REGISTER_CONFIG = 0x03; // configuration register
22static const uint8_t EMC2101_REGISTER_TACH_LSB = 0x46; // Tach RPM data low byte
23static const uint8_t EMC2101_REGISTER_TACH_MSB = 0x47; // Tach RPM data high byte
24static const uint8_t EMC2101_REGISTER_FAN_CONFIG = 0x4A; // General fan config register
25static const uint8_t EMC2101_REGISTER_FAN_SETTING = 0x4C; // Fan speed for non-LUT settings
26static const uint8_t EMC2101_REGISTER_PWM_FREQ = 0x4D; // PWM frequency setting
27static const uint8_t EMC2101_REGISTER_PWM_DIV = 0x4E; // PWM frequency divisor
28static const uint8_t EMC2101_REGISTER_WHOAMI = 0xFD; // Chip ID register
29
30// EMC2101 configuration bits from the datasheet. We only define what we use.
31
32// Determines the funcionallity of the ALERT/TACH pin.
33// 0 (default): The ALERT/TECH pin will function as an open drain, active low interrupt.
34// 1: The ALERT/TECH pin will function as a high impedance TACH input. This may require an
35// external pull-up resistor to set the proper signaling levels.
36static const uint8_t EMC2101_ALT_TCH_BIT = 1 << 2;
37
38// Determines the FAN output mode.
39// 0 (default): PWM output enabled at FAN pin.
40// 1: DAC output enabled at FAN ping.
41static const uint8_t EMC2101_DAC_BIT = 1 << 4;
42
43// Overrides the CLK_SEL bit and uses the Frequency Divide Register to determine
44// the base PWM frequency. It is recommended that this bit be set for maximum PWM resolution.
45// 0 (default): The base clock frequency for the PWM is determined by the CLK_SEL bit.
46// 1 (recommended): The base clock that is used to determine the PWM frequency is set by the
47// Frequency Divide Register
48static const uint8_t EMC2101_CLK_OVR_BIT = 1 << 2;
49
50// Sets the polarity of the Fan output driver.
51// 0 (default): The polarity of the Fan output driver is non-inverted. A '00h' setting will
52// correspond to a 0% duty cycle or a minimum DAC output voltage.
53// 1: The polarity of the Fan output driver is inverted. A '00h' setting will correspond to a
54// 100% duty cycle or a maximum DAC output voltage.
55static const uint8_t EMC2101_POLARITY_BIT = 1 << 4;
56
58
60 // make sure we're talking to the right chip
61 uint8_t chip_id = reg(EMC2101_REGISTER_WHOAMI).get();
62 if ((chip_id != EMC2101_CHIP_ID) && (chip_id != EMC2101_ALT_CHIP_ID)) {
63 ESP_LOGE(TAG, "Wrong chip ID %02X", chip_id);
64 this->mark_failed(LOG_STR("Wrong chip ID"));
65 return;
66 }
67
68 // Configure EMC2101
69 i2c::I2CRegister config = reg(EMC2101_REGISTER_CONFIG);
70 config |= EMC2101_ALT_TCH_BIT;
71 if (this->dac_mode_) {
72 config |= EMC2101_DAC_BIT;
73 }
74 if (this->inverted_) {
75 reg(EMC2101_REGISTER_FAN_CONFIG) |= EMC2101_POLARITY_BIT;
76 }
77
78 if (this->dac_mode_) { // DAC mode configurations
79 // set DAC conversion rate
80 reg(EMC2101_REGISTER_DAC_CONV_RATE) = this->dac_conversion_rate_;
81 } else { // PWM mode configurations
82 // set PWM divider
83 reg(EMC2101_REGISTER_FAN_CONFIG) |= EMC2101_CLK_OVR_BIT;
84 reg(EMC2101_REGISTER_PWM_DIV) = this->pwm_divider_;
85
86 // set PWM resolution
87 reg(EMC2101_REGISTER_PWM_FREQ) = this->pwm_resolution_;
88 }
89}
90
92 ESP_LOGCONFIG(TAG, "Emc2101 component:");
93 LOG_I2C_DEVICE(this);
94 ESP_LOGCONFIG(TAG, " Mode: %s", this->dac_mode_ ? LOG_STR_LITERAL("DAC") : LOG_STR_LITERAL("PWM"));
95 if (this->dac_mode_) {
96 ESP_LOGCONFIG(TAG, " DAC Conversion Rate: %X", this->dac_conversion_rate_);
97 } else {
98 ESP_LOGCONFIG(TAG,
99 " PWM Resolution: %02X\n"
100 " PWM Divider: %02X",
101 this->pwm_resolution_, this->pwm_divider_);
102 }
103 ESP_LOGCONFIG(TAG, " Inverted: %s", YESNO(this->inverted_));
104}
105
107 uint8_t duty_cycle = remap(value, 0.0f, 1.0f, (uint8_t) 0, this->max_output_value_);
108 ESP_LOGD(TAG, "Setting duty fan setting to %02X", duty_cycle);
109 if (!this->write_byte(EMC2101_REGISTER_FAN_SETTING, duty_cycle)) {
110 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
111 this->status_set_warning();
112 return;
113 }
114}
115
117 uint8_t duty_cycle;
118 if (!this->read_byte(EMC2101_REGISTER_FAN_SETTING, &duty_cycle)) {
119 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
120 this->status_set_warning();
121 return NAN;
122 }
123 return remap(duty_cycle, (uint8_t) 0, this->max_output_value_, 0.0f, 1.0f);
124}
125
127 uint8_t temperature;
128 if (!this->read_byte(EMC2101_REGISTER_INTERNAL_TEMP, &temperature)) {
129 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
130 this->status_set_warning();
131 return NAN;
132 }
133 return temperature;
134}
135
137 // Read **MSB** first to match 'Data Read Interlock' behavior from 6.1 of datasheet
138 uint8_t lsb, msb;
139 if (!this->read_byte(EMC2101_REGISTER_EXTERNAL_TEMP_MSB, &msb) ||
140 !this->read_byte(EMC2101_REGISTER_EXTERNAL_TEMP_LSB, &lsb)) {
141 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
142 this->status_set_warning();
143 return NAN;
144 }
145
146 // join msb and lsb (5 least significant bits are not used); msb is signed, so read as int16_t
147 int16_t raw = static_cast<int16_t>(encode_uint16(msb, lsb)) >> 5;
148 return raw * 0.125f;
149}
150
152 // Read **LSB** first to match 'Data Read Interlock' behavior from 6.1 of datasheet
153 uint8_t lsb, msb;
154 if (!this->read_byte(EMC2101_REGISTER_TACH_LSB, &lsb) || !this->read_byte(EMC2101_REGISTER_TACH_MSB, &msb)) {
155 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
156 this->status_set_warning();
157 return NAN;
158 }
159
160 // calculate RPMs
161 uint16_t tach = encode_uint16(msb, lsb);
162 return tach == 0xFFFF ? 0.0f : 5400000.0f / tach;
163}
164
165} // namespace esphome::emc2101
uint8_t raw[35]
Definition bl0939.h:0
void mark_failed()
Mark this component as failed.
void setup() override
Used by ESPHome framework.
Definition emc2101.cpp:59
float get_duty_cycle()
Gets the Fan output duty cycle.
Definition emc2101.cpp:116
void dump_config() override
Used by ESPHome framework.
Definition emc2101.cpp:91
float get_setup_priority() const override
Used by ESPHome framework.
Definition emc2101.cpp:57
float get_speed()
Gets the tachometer speed sensor reading.
Definition emc2101.cpp:151
float get_internal_temperature()
Gets the internal temperature sensor reading.
Definition emc2101.cpp:126
float get_external_temperature()
Gets the external temperature sensor reading.
Definition emc2101.cpp:136
Emc2101DACConversionRate dac_conversion_rate_
Definition emc2101.h:110
void set_duty_cycle(float value)
Sets the Fan output duty cycle.
Definition emc2101.cpp:106
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:312
bool read_byte(uint8_t a_register, uint8_t *data)
Definition i2c.h:272
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
Definition i2c.h:164
This class is used to create I2CRegister objects that act as proxies to read/write internal registers...
Definition i2c.h:44
uint8_t get() const
returns the register value
Definition i2c.cpp:143
ESPHOME_LOG_TAG(TAG, "EMC2101")
constexpr float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.h:43
uint32_t value
Definition hal.cpp:41
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Definition helpers.h:1043
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).
Definition helpers.h:932
uint16_t temperature
Definition sun_gtil2.cpp:12