ESPHome 2026.11.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
9ESPHOME_LOG_TAG(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;
17constexpr uint8_t MMC5983_PRODUCT_ID = 0x30;
18
19float convert_data_to_millitesla(uint8_t data_17_10, uint8_t data_9_2, uint8_t data_1_0) {
20 int32_t counts = (data_17_10 << 10) | (data_9_2 << 2) | data_1_0;
21 counts -= 131072; // "Null Field Output" from datasheet.
22
23 // Sensitivity is 16384 counts/gauss, which is 163840 counts/mT.
24 return counts / 163840.0f;
25}
26} // namespace
27
29 // Schedule a SET/RESET. This will recalibrate the sensor.
30 // We are supposed to be able to set this once, and have it automatically continue every reading, but
31 // this does not appear to work in continuous mode, even with En_prd_set turned on in Internal Control 2.
32 // Bit 5 = Auto_SR_en (automatic SET/RESET enable).
33 const uint8_t ic0_value = 0b10000;
34 i2c::ErrorCode err = this->write_register(IC0_ADDR, &ic0_value, 1);
35 if (err != i2c::ErrorCode::ERROR_OK) {
36 ESP_LOGW(TAG, "Writing Internal Control 0 failed with i2c error %d", err);
37 this->status_set_warning();
38 }
39
40 // Read out the data, 7 bytes starting from 0x00.
41 uint8_t data[7];
42 err = this->read_register(0x00, data, sizeof(data));
43 if (err != i2c::ErrorCode::ERROR_OK) {
44 ESP_LOGW(TAG, "Reading data failed with i2c error %d", err);
45 this->status_set_warning();
46 return;
47 }
48
49 // Unpack the data and publish to sensors.
50 // Data is in this format:
51 // data[0]: Xout[17:10]
52 // data[1]: Xout[9:2]
53 // data[2]: Yout[17:10]
54 // data[3]: Yout[9:2]
55 // data[4]: Zout[17:10]
56 // data[5]: Zout[9:2]
57 // data[6]: { Xout[1], Xout[0], Yout[1], Yout[0], Zout[1], Zout[0], 0, 0 }
58 if (this->x_sensor_) {
59 this->x_sensor_->publish_state(convert_data_to_millitesla(data[0], data[1], (data[6] & 0b11000000) >> 6));
60 }
61 if (this->y_sensor_) {
62 this->y_sensor_->publish_state(convert_data_to_millitesla(data[2], data[3], (data[6] & 0b00110000) >> 4));
63 }
64 if (this->z_sensor_) {
65 this->z_sensor_->publish_state(convert_data_to_millitesla(data[4], data[5], (data[6] & 0b00001100) >> 2));
66 }
67}
68
70 if (!this->check_chip_id(this, PRODUCT_ID_ADDR, MMC5983_PRODUCT_ID))
71 return;
72
73 // Initialize Internal Control registers to 0.
74 // Internal Control 0.
75 const uint8_t zero = 0;
76 i2c::ErrorCode err = this->write_register(IC0_ADDR, &zero, 1);
77 if (err != i2c::ErrorCode::ERROR_OK) {
78 ESP_LOGE(TAG, "Initializing Internal Control 0 failed with i2c error %d", err);
79 this->mark_failed();
80 return;
81 }
82 // Internal Control 1.
83 err = this->write_register(IC1_ADDR, &zero, 1);
84 if (err != i2c::ErrorCode::ERROR_OK) {
85 ESP_LOGE(TAG, "Initializing Internal Control 1 failed with i2c error %d", err);
86 this->mark_failed();
87 return;
88 }
89 // Internal Control 2.
90 err = this->write_register(IC2_ADDR, &zero, 1);
91 if (err != i2c::ErrorCode::ERROR_OK) {
92 ESP_LOGE(TAG, "Initializing Internal Control 2 failed with i2c error %d", err);
93 this->mark_failed();
94 return;
95 }
96 // Internal Control 3.
97 err = this->write_register(IC3_ADDR, &zero, 1);
98 if (err != i2c::ErrorCode::ERROR_OK) {
99 ESP_LOGE(TAG, "Initializing Internal Control 3 failed with i2c error %d", err);
100 this->mark_failed();
101 return;
102 }
103
104 // Enable continuous mode at 100 Hz, using Internal Control 2.
105 // Bit 3 = Cmm_en (continuous mode enable).
106 // Bit [2:0] = Cm_freq. 0b101 = 100 Hz, the fastest reading speed at Bandwidth=100 Hz.
107 const uint8_t ic2_value = 0b00001101;
108 err = this->write_register(IC2_ADDR, &ic2_value, 1);
109 if (err != i2c::ErrorCode::ERROR_OK) {
110 ESP_LOGE(TAG, "Writing Internal Control 2 failed with i2c error %d", err);
111 this->mark_failed();
112 return;
113 }
114}
115
117 ESP_LOGD(TAG, "MMC5983:");
118 LOG_I2C_DEVICE(this);
119 LOG_SENSOR(" ", "X", this->x_sensor_);
120 LOG_SENSOR(" ", "Y", this->y_sensor_);
121 LOG_SENSOR(" ", "Z", this->z_sensor_);
122}
123
124} // 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:37
bool check_chip_id(Component *component, uint8_t a_register, uint8_t expected, uint8_t mask=0xFF)
Read the 8-bit ID at a_register and accept it when it matches expected in the bits set in mask.
Definition i2c.h:232
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:28
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
Definition i2c_bus.h:13
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:15
ESPHOME_LOG_TAG(TAG, "mmc5983")