ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
mmc5983.cpp
Go to the documentation of this file.
1// See https://github.com/sparkfun/SparkFun_MMC5983MA_Magnetometer_Arduino_Library/tree/main
2// for datasheets and an Arduino implementation.
3
4#include "mmc5983.h"
5#include "esphome/core/log.h"
6
7namespace esphome::mmc5983 {
8
9static const char *const TAG = "mmc5983";
10
11namespace {
12constexpr uint8_t IC0_ADDR = 0x09;
13constexpr uint8_t IC1_ADDR = 0x0a;
14constexpr uint8_t IC2_ADDR = 0x0b;
15constexpr uint8_t IC3_ADDR = 0x0c;
16constexpr uint8_t PRODUCT_ID_ADDR = 0x2f;
17
18float convert_data_to_millitesla(uint8_t data_17_10, uint8_t data_9_2, uint8_t data_1_0) {
19 int32_t counts = (data_17_10 << 10) | (data_9_2 << 2) | data_1_0;
20 counts -= 131072; // "Null Field Output" from datasheet.
21
22 // Sensitivity is 16384 counts/gauss, which is 163840 counts/mT.
23 return counts / 163840.0f;
24}
25} // namespace
26
28 // Schedule a SET/RESET. This will recalibrate the sensor.
29 // We are supposed to be able to set this once, and have it automatically continue every reading, but
30 // this does not appear to work in continuous mode, even with En_prd_set turned on in Internal Control 2.
31 // Bit 5 = Auto_SR_en (automatic SET/RESET enable).
32 const uint8_t ic0_value = 0b10000;
33 i2c::ErrorCode err = this->write_register(IC0_ADDR, &ic0_value, 1);
34 if (err != i2c::ErrorCode::ERROR_OK) {
35 ESP_LOGW(TAG, "Writing Internal Control 0 failed with i2c error %d", err);
36 this->status_set_warning();
37 }
38
39 // Read out the data, 7 bytes starting from 0x00.
40 uint8_t data[7];
41 err = this->read_register(0x00, data, sizeof(data));
42 if (err != i2c::ErrorCode::ERROR_OK) {
43 ESP_LOGW(TAG, "Reading data failed with i2c error %d", err);
44 this->status_set_warning();
45 return;
46 }
47
48 // Unpack the data and publish to sensors.
49 // Data is in this format:
50 // data[0]: Xout[17:10]
51 // data[1]: Xout[9:2]
52 // data[2]: Yout[17:10]
53 // data[3]: Yout[9:2]
54 // data[4]: Zout[17:10]
55 // data[5]: Zout[9:2]
56 // data[6]: { Xout[1], Xout[0], Yout[1], Yout[0], Zout[1], Zout[0], 0, 0 }
57 if (this->x_sensor_) {
58 this->x_sensor_->publish_state(convert_data_to_millitesla(data[0], data[1], (data[6] & 0b11000000) >> 6));
59 }
60 if (this->y_sensor_) {
61 this->y_sensor_->publish_state(convert_data_to_millitesla(data[2], data[3], (data[6] & 0b00110000) >> 4));
62 }
63 if (this->z_sensor_) {
64 this->z_sensor_->publish_state(convert_data_to_millitesla(data[4], data[5], (data[6] & 0b00001100) >> 2));
65 }
66}
67
69 // Verify product id.
70 const uint8_t mmc5983_product_id = 0x30;
71 uint8_t id;
72 i2c::ErrorCode err = this->read_register(PRODUCT_ID_ADDR, &id, 1);
73 if (err != i2c::ErrorCode::ERROR_OK) {
74 ESP_LOGE(TAG, "Reading product id failed with i2c error %d", err);
75 this->mark_failed();
76 return;
77 }
78 if (id != mmc5983_product_id) {
79 ESP_LOGE(TAG, "Product id 0x%02x does not match expected value 0x%02x", id, mmc5983_product_id);
80 this->mark_failed();
81 return;
82 }
83
84 // Initialize Internal Control registers to 0.
85 // Internal Control 0.
86 const uint8_t zero = 0;
87 err = this->write_register(IC0_ADDR, &zero, 1);
88 if (err != i2c::ErrorCode::ERROR_OK) {
89 ESP_LOGE(TAG, "Initializing Internal Control 0 failed with i2c error %d", err);
90 this->mark_failed();
91 return;
92 }
93 // Internal Control 1.
94 err = this->write_register(IC1_ADDR, &zero, 1);
95 if (err != i2c::ErrorCode::ERROR_OK) {
96 ESP_LOGE(TAG, "Initializing Internal Control 1 failed with i2c error %d", err);
97 this->mark_failed();
98 return;
99 }
100 // Internal Control 2.
101 err = this->write_register(IC2_ADDR, &zero, 1);
102 if (err != i2c::ErrorCode::ERROR_OK) {
103 ESP_LOGE(TAG, "Initializing Internal Control 2 failed with i2c error %d", err);
104 this->mark_failed();
105 return;
106 }
107 // Internal Control 3.
108 err = this->write_register(IC3_ADDR, &zero, 1);
109 if (err != i2c::ErrorCode::ERROR_OK) {
110 ESP_LOGE(TAG, "Initializing Internal Control 3 failed with i2c error %d", err);
111 this->mark_failed();
112 return;
113 }
114
115 // Enable continuous mode at 100 Hz, using Internal Control 2.
116 // Bit 3 = Cmm_en (continuous mode enable).
117 // Bit [2:0] = Cm_freq. 0b101 = 100 Hz, the fastest reading speed at Bandwidth=100 Hz.
118 const uint8_t ic2_value = 0b00001101;
119 err = this->write_register(IC2_ADDR, &ic2_value, 1);
120 if (err != i2c::ErrorCode::ERROR_OK) {
121 ESP_LOGE(TAG, "Writing Internal Control 2 failed with i2c error %d", err);
122 this->mark_failed();
123 return;
124 }
125}
126
128 ESP_LOGD(TAG, "MMC5983:");
129 LOG_I2C_DEVICE(this);
130 LOG_SENSOR(" ", "X", this->x_sensor_);
131 LOG_SENSOR(" ", "Y", this->y_sensor_);
132 LOG_SENSOR(" ", "Z", this->z_sensor_);
133}
134
135} // namespace esphome::mmc5983
void mark_failed()
Mark this component as failed.
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:34
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:25
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
uint16_t id
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
Definition i2c_bus.h:12
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:14