ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
mmc5603.cpp
Go to the documentation of this file.
1#include "mmc5603.h"
2#include "esphome/core/log.h"
3
4namespace esphome::mmc5603 {
5
6static const char *const TAG = "mmc5603";
7static const uint8_t MMC5603_ADDRESS = 0x30;
8static const uint8_t MMC56X3_PRODUCT_ID = 0x39;
9
10static const uint8_t MMC56X3_DEFAULT_ADDRESS = 0x30;
11static const uint8_t MMC56X3_CHIP_ID = 0x10;
12
13static const uint8_t MMC56X3_ADDR_XOUT0 = 0x00;
14static const uint8_t MMC56X3_ADDR_XOUT1 = 0x01;
15static const uint8_t MMC56X3_ADDR_XOUT2 = 0x06;
16
17static const uint8_t MMC56X3_ADDR_YOUT0 = 0x02;
18static const uint8_t MMC56X3_ADDR_YOUT1 = 0x03;
19static const uint8_t MMC56X3_ADDR_YOUT2 = 0x07;
20
21static const uint8_t MMC56X3_ADDR_ZOUT0 = 0x04;
22static const uint8_t MMC56X3_ADDR_ZOUT1 = 0x05;
23static const uint8_t MMC56X3_ADDR_ZOUT2 = 0x08;
24
25static const uint8_t MMC56X3_OUT_TEMP = 0x09;
26static const uint8_t MMC56X3_STATUS_REG = 0x18;
27static const uint8_t MMC56X3_CTRL0_REG = 0x1B;
28static const uint8_t MMC56X3_CTRL1_REG = 0x1C;
29static const uint8_t MMC56X3_CTRL2_REG = 0x1D;
30static const uint8_t MMC5603_ODR_REG = 0x1A;
31
33 uint8_t id = 0;
34 if (!this->read_byte(MMC56X3_PRODUCT_ID, &id)) {
35 this->error_code_ = COMMUNICATION_FAILED;
36 this->mark_failed();
37 return;
38 }
39
40 if (id != 0 && id != MMC56X3_CHIP_ID) { // ID is not reported correctly by all chips, 0 on some chips
41 ESP_LOGCONFIG(TAG, "Chip Wrong");
42 this->error_code_ = ID_REGISTERS;
43 this->mark_failed();
44 return;
45 }
46
47 if (!this->write_byte(MMC56X3_CTRL1_REG, 0x80)) { // turn on set bit
48 ESP_LOGCONFIG(TAG, "Control 1 Failed for set bit");
49 this->error_code_ = COMMUNICATION_FAILED;
50 this->mark_failed();
51 return;
52 }
53
54 if (!this->write_byte(MMC56X3_CTRL0_REG, 0x08)) { // turn on set bit
55 ESP_LOGCONFIG(TAG, "Control 0 Failed for set bit");
56 this->error_code_ = COMMUNICATION_FAILED;
57 this->mark_failed();
58 return;
59 }
60
61 if (!this->write_byte(MMC56X3_CTRL0_REG, 0x10)) {
62 this->error_code_ = COMMUNICATION_FAILED;
63 this->mark_failed();
64 return;
65 }
66
67 uint8_t ctrl_2 = 0;
68
69 ctrl_2 &= ~0x10; // turn off cmm_en bit
70 if (!this->write_byte(MMC56X3_CTRL2_REG, ctrl_2)) {
71 this->error_code_ = COMMUNICATION_FAILED;
72 this->mark_failed();
73 return;
74 }
75}
77 ESP_LOGCONFIG(TAG, "MMC5603:");
78 LOG_I2C_DEVICE(this);
79 if (this->error_code_ == COMMUNICATION_FAILED) {
80 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
81 } else if (this->error_code_ == ID_REGISTERS) {
82 ESP_LOGE(TAG, "The ID registers don't match - Is this really an MMC5603?");
83 }
84 LOG_UPDATE_INTERVAL(this);
85 ESP_LOGCONFIG(TAG, " Auto set/reset: %s", ONOFF(this->auto_set_reset_));
86
87 LOG_SENSOR(" ", "X Axis", this->x_sensor_);
88 LOG_SENSOR(" ", "Y Axis", this->y_sensor_);
89 LOG_SENSOR(" ", "Z Axis", this->z_sensor_);
90 LOG_SENSOR(" ", "Heading", this->heading_sensor_);
91}
92
94 uint8_t ctrl0 = (this->auto_set_reset_) ? 0x21 : 0x01;
95 if (!this->write_byte(MMC56X3_CTRL0_REG, ctrl0)) {
96 this->status_set_warning();
97 return;
98 }
99 uint8_t status = 0;
100 if (!this->read_byte(MMC56X3_STATUS_REG, &status)) {
101 this->status_set_warning();
102 return;
103 }
104
105 uint8_t buffer[9] = {0};
106
107 if (!this->read_byte(MMC56X3_ADDR_XOUT0, &buffer[0]) || !this->read_byte(MMC56X3_ADDR_XOUT1, &buffer[1]) ||
108 !this->read_byte(MMC56X3_ADDR_XOUT2, &buffer[2])) {
109 this->status_set_warning();
110 return;
111 }
112
113 if (!this->read_byte(MMC56X3_ADDR_YOUT0, &buffer[3]) || !this->read_byte(MMC56X3_ADDR_YOUT1, &buffer[4]) ||
114 !this->read_byte(MMC56X3_ADDR_YOUT2, &buffer[5])) {
115 this->status_set_warning();
116 return;
117 }
118
119 if (!this->read_byte(MMC56X3_ADDR_ZOUT0, &buffer[6]) || !this->read_byte(MMC56X3_ADDR_ZOUT1, &buffer[7]) ||
120 !this->read_byte(MMC56X3_ADDR_ZOUT2, &buffer[8])) {
121 this->status_set_warning();
122 return;
123 }
124
125 int32_t raw_x = 0;
126 raw_x |= buffer[0] << 12;
127 raw_x |= buffer[1] << 4;
128 raw_x |= buffer[2] & 0x0F;
129
130 const float x = 0.00625 * (raw_x - 524288);
131
132 int32_t raw_y = 0;
133 raw_y |= buffer[3] << 12;
134 raw_y |= buffer[4] << 4;
135 raw_y |= buffer[5] & 0x0F;
136
137 const float y = 0.00625 * (raw_y - 524288);
138
139 int32_t raw_z = 0;
140 raw_z |= buffer[6] << 12;
141 raw_z |= buffer[7] << 4;
142 raw_z |= buffer[8] & 0x0F;
143
144 const float z = 0.00625 * (raw_z - 524288);
145
146 const float heading = atan2f(0.0f - x, y) * 180.0f / M_PI;
147 ESP_LOGD(TAG, "Got x=%0.02fµT y=%0.02fµT z=%0.02fµT heading=%0.01f°", x, y, z, heading);
148
149 if (this->x_sensor_ != nullptr)
150 this->x_sensor_->publish_state(x);
151 if (this->y_sensor_ != nullptr)
152 this->y_sensor_->publish_state(y);
153 if (this->z_sensor_ != nullptr)
154 this->z_sensor_->publish_state(z);
155 if (this->heading_sensor_ != nullptr)
156 this->heading_sensor_->publish_state(heading);
157}
158
159} // namespace esphome::mmc5603
uint8_t status
Definition bl0942.h:8
void mark_failed()
Mark this component as failed.
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:265
bool read_byte(uint8_t a_register, uint8_t *data)
Definition i2c.h:240
sensor::Sensor * heading_sensor_
Definition mmc5603.h:33
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
bool z
Definition msa3xx.h:1
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6