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