ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
mt6701_i2c.cpp
Go to the documentation of this file.
1#include "mt6701_i2c.h"
2#include "esphome/core/hal.h"
3#include "esphome/core/log.h"
4
6
7ESPHOME_LOG_TAG(TAG, "mt6701_i2c");
8
9// Registers apply_config_() may write, each read-modify-written once
10static const uint8_t CONFIG_REGS[] PROGMEM = {
11 REG_ABZ_MUX_DIR, REG_RES_H, REG_ABZ_RES_L, REG_CONFIG_H, REG_ZERO_L,
12 REG_HYST_L, REG_OUT, REG_A_HIGH, REG_A_START_L, REG_A_STOP_L,
13};
14static constexpr uint8_t CONFIG_REG_COUNT = sizeof(CONFIG_REGS);
15
17 uint16_t count;
18 if (!this->read_count(count)) {
19 this->mark_failed(LOG_STR("not responding on I2C"));
20 return;
21 }
22 this->apply_config_();
23}
24
26 ESP_LOGCONFIG(TAG, "MT6701 (I2C):");
27 LOG_I2C_DEVICE(this);
28 if (this->direction_ != UNSET_U8) {
29 ESP_LOGCONFIG(TAG, " Direction bit: %u", this->direction_);
30 }
31 if (this->zero_offset_ != UNSET_U16) {
32 ESP_LOGCONFIG(TAG, " Zero offset: %u", this->zero_offset_);
33 }
34 if (this->hysteresis_ != UNSET_U8) {
35 ESP_LOGCONFIG(TAG, " Hysteresis code: %u", this->hysteresis_);
36 }
37 if (this->output_mode_ != UNSET_U8) {
38 ESP_LOGCONFIG(TAG, " Output mode: %s", this->output_mode_ != 0 ? LOG_STR_LITERAL("UVW") : LOG_STR_LITERAL("ABZ"));
39 }
41 ESP_LOGCONFIG(TAG, " ABZ pulses/rev: %u", this->abz_pulses_per_revolution_);
42 }
43 if (this->z_pulse_width_ != UNSET_U8) {
44 ESP_LOGCONFIG(TAG, " Z pulse width code: %u", this->z_pulse_width_);
45 }
46 if (this->uvw_pole_pairs_ != UNSET_U8) {
47 ESP_LOGCONFIG(TAG, " UVW pole pairs: %u", this->uvw_pole_pairs_);
48 }
49 if (this->out_pin_mode_ != UNSET_U8) {
50 ESP_LOGCONFIG(TAG, " Output pin mode: %s",
51 this->out_pin_mode_ != 0 ? LOG_STR_LITERAL("PWM") : LOG_STR_LITERAL("analog"));
52 }
53 if (this->pwm_frequency_ != UNSET_U8) {
54 ESP_LOGCONFIG(TAG, " PWM frequency code: %u", this->pwm_frequency_);
55 }
56 if (this->pwm_polarity_ != UNSET_U8) {
57 ESP_LOGCONFIG(TAG, " PWM polarity code: %u", this->pwm_polarity_);
58 }
59 if (this->analog_start_ != UNSET_U16) {
60 ESP_LOGCONFIG(TAG, " Analog start: %u", this->analog_start_);
61 }
62 if (this->analog_stop_ != UNSET_U16) {
63 ESP_LOGCONFIG(TAG, " Analog stop: %u", this->analog_stop_);
64 }
65}
66
68 // Single-byte reads, 0x03 first; datasheet 1.9 documents no burst read
69 uint8_t high;
70 uint8_t low;
71 if (this->read_register(REG_ANGLE_H, &high, 1) != i2c::ERROR_OK ||
72 this->read_register(REG_ANGLE_L, &low, 1) != i2c::ERROR_OK)
73 return false;
74 count = encode_uint16(high, low) >> 2;
75 return true;
76}
77
78bool MT6701I2CComponent::update_register_(uint8_t reg, uint8_t mask, uint8_t value) {
79 uint8_t current;
80 if (this->read_register(reg, &current, 1) != i2c::ERROR_OK)
81 return false;
82 uint8_t updated = (current & ~mask) | (value & mask);
83 if (updated == current)
84 return true;
85 return this->write_register(reg, &updated, 1) == i2c::ERROR_OK;
86}
87
89 // Collect the bits per register first; several options share one register
90 uint8_t masks[CONFIG_REG_COUNT] = {};
91 uint8_t values[CONFIG_REG_COUNT] = {};
92 auto add = [&masks, &values](uint8_t reg, uint8_t mask, uint8_t value) {
93 for (uint8_t i = 0; i < CONFIG_REG_COUNT; i++) {
94 if (progmem_read_byte(&CONFIG_REGS[i]) == reg) {
95 masks[i] |= mask;
96 values[i] |= value & mask;
97 return;
98 }
99 }
100 };
101
102 if (this->direction_ != UNSET_U8)
103 add(REG_ABZ_MUX_DIR, 0x02, this->direction_ << 1);
104 if (this->output_mode_ != UNSET_U8)
105 add(REG_ABZ_MUX_DIR, 0x40, (this->output_mode_ & 0x01) << 6);
106 if (this->zero_offset_ != UNSET_U16) {
107 add(REG_CONFIG_H, 0x0F, this->zero_offset_ >> 8);
108 add(REG_ZERO_L, 0xFF, this->zero_offset_ & 0xFF);
109 }
110 if (this->hysteresis_ != UNSET_U8) {
111 add(REG_CONFIG_H, 0x80, (this->hysteresis_ & 0x04) << 5); // HYST[2] -> bit7
112 add(REG_HYST_L, 0xC0, (this->hysteresis_ & 0x03) << 6); // HYST[1:0] -> bits7:6
113 }
115 // ABZ_RES holds pulses per revolution minus 1
116 uint16_t res = this->abz_pulses_per_revolution_ - 1;
117 add(REG_RES_H, 0x03, res >> 8);
118 add(REG_ABZ_RES_L, 0xFF, res & 0xFF);
119 }
120 if (this->z_pulse_width_ != UNSET_U8)
121 add(REG_CONFIG_H, 0x70, (this->z_pulse_width_ & 0x07) << 4);
122 if (this->uvw_pole_pairs_ != UNSET_U8) {
123 // UVW_RES holds pole pairs minus 1
124 add(REG_RES_H, 0xF0, (this->uvw_pole_pairs_ - 1) << 4);
125 }
126 if (this->out_pin_mode_ != UNSET_U8)
127 add(REG_OUT, 0x20, (this->out_pin_mode_ & 0x01) << 5);
128 if (this->pwm_frequency_ != UNSET_U8)
129 add(REG_OUT, 0x80, (this->pwm_frequency_ & 0x01) << 7);
130 if (this->pwm_polarity_ != UNSET_U8)
131 add(REG_OUT, 0x40, (this->pwm_polarity_ & 0x01) << 6);
132 if (this->analog_start_ != UNSET_U16) {
133 add(REG_A_HIGH, 0x0F, this->analog_start_ >> 8);
134 add(REG_A_START_L, 0xFF, this->analog_start_ & 0xFF);
135 }
136 if (this->analog_stop_ != UNSET_U16) {
137 add(REG_A_HIGH, 0xF0, (this->analog_stop_ >> 8) << 4);
138 add(REG_A_STOP_L, 0xFF, this->analog_stop_ & 0xFF);
139 }
140
141 bool ok = true;
142 for (uint8_t i = 0; i < CONFIG_REG_COUNT; i++) {
143 if (masks[i] != 0)
144 ok &= this->update_register_(progmem_read_byte(&CONFIG_REGS[i]), masks[i], values[i]);
145 }
146 if (!ok) {
147 // A half-configured chip must not be sampled or have its EEPROM saved
148 this->mark_failed(LOG_STR("configuration register write failed"));
149 }
150}
151
153 if (this->is_failed()) {
154 // A failed component gets no timeouts, so the bus would stay suspended
155 ESP_LOGW(TAG, "Not programming EEPROM: component is marked failed");
156 return;
157 }
158 if (this->suspend_sampling_) {
159 ESP_LOGW(TAG, "EEPROM programming already in progress");
160 return;
161 }
162 ESP_LOGW(TAG, "Programming MT6701 EEPROM. The EEPROM has a limited number of write "
163 "cycles, so avoid doing this repeatedly. Supply must be 4.5-5.5 V.");
164 this->suspend_sampling_ = true;
165
166 bool ok = this->write_byte(REG_EEPROM_KEY, EEPROM_KEY_VALUE) && this->write_byte(REG_EEPROM_CMD, EEPROM_CMD_VALUE);
167 if (!ok) {
168 ESP_LOGE(TAG, "Failed to start EEPROM programming");
169 this->suspend_sampling_ = false;
170 return;
171 }
172
173 // suspend_sampling_ keeps a second save out, so the timer needs no name
174 this->set_timeout(EEPROM_PROGRAM_DELAY_MS, [this]() {
175 this->suspend_sampling_ = false;
176 ESP_LOGI(TAG, "EEPROM programming finished. Power-cycle the MT6701 to verify the stored configuration");
177 });
178}
179
180} // namespace esphome::mt6701_i2c
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:287
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
Definition component.cpp:96
ErrorCode write_register(uint8_t a_register, const uint8_t *data, size_t len) const
writes an array of bytes to a specific register in the I²C device
Definition i2c.cpp:54
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:312
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
Definition i2c.h:164
ErrorCode read_register(uint8_t a_register, uint8_t *data, size_t len)
reads an array of bytes from a specific register in the I²C device
Definition i2c.cpp:45
void save_eeprom()
Write the registers to the EEPROM: pauses sampling for the programming time.
static constexpr uint16_t UNSET_U16
Definition mt6701_i2c.h:62
bool read_count(uint16_t &count) override
static constexpr uint8_t UNSET_U8
Definition mt6701_i2c.h:63
bool update_register_(uint8_t reg, uint8_t mask, uint8_t value)
unsigned count
Definition gif.cpp:51
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:15
ESPHOME_LOG_TAG(TAG, "mt6701_i2c")
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
uint8_t progmem_read_byte(const uint8_t *addr)
Definition hal.h:51