ESPHome 2025.9.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 {
5namespace mmc5603 {
6
7static const char *const TAG = "mmc5603";
8static const uint8_t MMC5603_ADDRESS = 0x30;
9static const uint8_t MMC56X3_PRODUCT_ID = 0x39;
10
11static const uint8_t MMC56X3_DEFAULT_ADDRESS = 0x30;
12static const uint8_t MMC56X3_CHIP_ID = 0x10;
13
14static const uint8_t MMC56X3_ADDR_XOUT0 = 0x00;
15static const uint8_t MMC56X3_ADDR_XOUT1 = 0x01;
16static const uint8_t MMC56X3_ADDR_XOUT2 = 0x06;
17
18static const uint8_t MMC56X3_ADDR_YOUT0 = 0x02;
19static const uint8_t MMC56X3_ADDR_YOUT1 = 0x03;
20static const uint8_t MMC56X3_ADDR_YOUT2 = 0x07;
21
22static const uint8_t MMC56X3_ADDR_ZOUT0 = 0x04;
23static const uint8_t MMC56X3_ADDR_ZOUT1 = 0x05;
24static const uint8_t MMC56X3_ADDR_ZOUT2 = 0x08;
25
26static const uint8_t MMC56X3_OUT_TEMP = 0x09;
27static const uint8_t MMC56X3_STATUS_REG = 0x18;
28static const uint8_t MMC56X3_CTRL0_REG = 0x1B;
29static const uint8_t MMC56X3_CTRL1_REG = 0x1C;
30static const uint8_t MMC56X3_CTRL2_REG = 0x1D;
31static const uint8_t MMC5603_ODR_REG = 0x1A;
32
34 uint8_t id = 0;
35 if (!this->read_byte(MMC56X3_PRODUCT_ID, &id)) {
37 this->mark_failed();
38 return;
39 }
40
41 if (id != 0 && id != MMC56X3_CHIP_ID) { // ID is not reported correctly by all chips, 0 on some chips
42 ESP_LOGCONFIG(TAG, "Chip Wrong");
44 this->mark_failed();
45 return;
46 }
47
48 if (!this->write_byte(MMC56X3_CTRL1_REG, 0x80)) { // turn on set bit
49 ESP_LOGCONFIG(TAG, "Control 1 Failed for set bit");
51 this->mark_failed();
52 return;
53 }
54
55 if (!this->write_byte(MMC56X3_CTRL0_REG, 0x08)) { // turn on set bit
56 ESP_LOGCONFIG(TAG, "Control 0 Failed for set bit");
58 this->mark_failed();
59 return;
60 }
61
62 if (!this->write_byte(MMC56X3_CTRL0_REG, 0x10)) {
64 this->mark_failed();
65 return;
66 }
67
68 uint8_t ctrl_2 = 0;
69
70 ctrl_2 &= ~0x10; // turn off cmm_en bit
71 if (!this->write_byte(MMC56X3_CTRL2_REG, ctrl_2)) {
73 this->mark_failed();
74 return;
75 }
76}
78 ESP_LOGCONFIG(TAG, "MMC5603:");
79 LOG_I2C_DEVICE(this);
80 if (this->error_code_ == COMMUNICATION_FAILED) {
81 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
82 } else if (this->error_code_ == ID_REGISTERS) {
83 ESP_LOGE(TAG, "The ID registers don't match - Is this really an MMC5603?");
84 }
85 LOG_UPDATE_INTERVAL(this);
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
96 if (!this->write_byte(MMC56X3_CTRL0_REG, 0x01)) {
97 this->status_set_warning();
98 return;
99 }
100 uint8_t status = 0;
101 if (!this->read_byte(MMC56X3_STATUS_REG, &status)) {
102 this->status_set_warning();
103 return;
104 }
105
106 uint8_t buffer[9] = {0};
107
108 if (!this->read_byte(MMC56X3_ADDR_XOUT0, &buffer[0]) || !this->read_byte(MMC56X3_ADDR_XOUT1, &buffer[1]) ||
109 !this->read_byte(MMC56X3_ADDR_XOUT2, &buffer[2])) {
110 this->status_set_warning();
111 return;
112 }
113
114 if (!this->read_byte(MMC56X3_ADDR_YOUT0, &buffer[3]) || !this->read_byte(MMC56X3_ADDR_YOUT1, &buffer[4]) ||
115 !this->read_byte(MMC56X3_ADDR_YOUT2, &buffer[5])) {
116 this->status_set_warning();
117 return;
118 }
119
120 if (!this->read_byte(MMC56X3_ADDR_ZOUT0, &buffer[6]) || !this->read_byte(MMC56X3_ADDR_ZOUT1, &buffer[7]) ||
121 !this->read_byte(MMC56X3_ADDR_ZOUT2, &buffer[8])) {
122 this->status_set_warning();
123 return;
124 }
125
126 int32_t raw_x = 0;
127 raw_x |= buffer[0] << 12;
128 raw_x |= buffer[1] << 4;
129 raw_x |= buffer[2] << 0;
130
131 const float x = 0.0625 * (raw_x - 524288);
132
133 int32_t raw_y = 0;
134 raw_y |= buffer[3] << 12;
135 raw_y |= buffer[4] << 4;
136 raw_y |= buffer[5] << 0;
137
138 const float y = 0.0625 * (raw_y - 524288);
139
140 int32_t raw_z = 0;
141 raw_z |= buffer[6] << 12;
142 raw_z |= buffer[7] << 4;
143 raw_z |= buffer[8] << 0;
144
145 const float z = 0.0625 * (raw_z - 524288);
146
147 const float heading = atan2f(0.0f - x, y) * 180.0f / M_PI;
148 ESP_LOGD(TAG, "Got x=%0.02fµT y=%0.02fµT z=%0.02fµT heading=%0.01f°", x, y, z, heading);
149
150 if (this->x_sensor_ != nullptr)
151 this->x_sensor_->publish_state(x);
152 if (this->y_sensor_ != nullptr)
153 this->y_sensor_->publish_state(y);
154 if (this->z_sensor_ != nullptr)
155 this->z_sensor_->publish_state(z);
156 if (this->heading_sensor_ != nullptr)
157 this->heading_sensor_->publish_state(heading);
158}
159
160} // namespace mmc5603
161} // namespace esphome
uint8_t status
Definition bl0942.h:8
virtual void mark_failed()
Mark this component as failed.
void status_set_warning(const char *message=nullptr)
bool write_byte(uint8_t a_register, uint8_t data, bool stop=true)
Definition i2c.h:266
bool read_byte(uint8_t a_register, uint8_t *data, bool stop=true)
Definition i2c.h:239
enum esphome::mmc5603::MMC5603Component::ErrorCode error_code_
sensor::Sensor * heading_sensor_
Definition mmc5603.h:34
float get_setup_priority() const override
Definition mmc5603.cpp:93
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:45
bool z
Definition msa3xx.h:1
const float DATA
For components that import data from directly connected sensors like DHT.
Definition component.cpp:50
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6