ESPHome 2026.11.0b6
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 PUBLISH_STATE(this->x_sensor_, convert_data_to_millitesla(data[0], data[1], (data[6] & 0b11000000) >> 6));
59 PUBLISH_STATE(this->y_sensor_, convert_data_to_millitesla(data[2], data[3], (data[6] & 0b00110000) >> 4));
60 PUBLISH_STATE(this->z_sensor_, convert_data_to_millitesla(data[4], data[5], (data[6] & 0b00001100) >> 2));
61}
62
63void MMC5983Component::setup() {
64 if (!this->check_chip_id(this, PRODUCT_ID_ADDR, MMC5983_PRODUCT_ID))
65 return;
66
67 // Initialize Internal Control registers to 0.
68 // Internal Control 0.
69 const uint8_t zero = 0;
70 i2c::ErrorCode err = this->write_register(IC0_ADDR, &zero, 1);
71 if (err != i2c::ErrorCode::ERROR_OK) {
72 ESP_LOGE(TAG, "Initializing Internal Control 0 failed with i2c error %d", err);
73 this->mark_failed();
74 return;
75 }
76 // Internal Control 1.
77 err = this->write_register(IC1_ADDR, &zero, 1);
78 if (err != i2c::ErrorCode::ERROR_OK) {
79 ESP_LOGE(TAG, "Initializing Internal Control 1 failed with i2c error %d", err);
80 this->mark_failed();
81 return;
82 }
83 // Internal Control 2.
84 err = this->write_register(IC2_ADDR, &zero, 1);
85 if (err != i2c::ErrorCode::ERROR_OK) {
86 ESP_LOGE(TAG, "Initializing Internal Control 2 failed with i2c error %d", err);
87 this->mark_failed();
88 return;
89 }
90 // Internal Control 3.
91 err = this->write_register(IC3_ADDR, &zero, 1);
92 if (err != i2c::ErrorCode::ERROR_OK) {
93 ESP_LOGE(TAG, "Initializing Internal Control 3 failed with i2c error %d", err);
94 this->mark_failed();
95 return;
96 }
97
98 // Enable continuous mode at 100 Hz, using Internal Control 2.
99 // Bit 3 = Cmm_en (continuous mode enable).
100 // Bit [2:0] = Cm_freq. 0b101 = 100 Hz, the fastest reading speed at Bandwidth=100 Hz.
101 const uint8_t ic2_value = 0b00001101;
102 err = this->write_register(IC2_ADDR, &ic2_value, 1);
103 if (err != i2c::ErrorCode::ERROR_OK) {
104 ESP_LOGE(TAG, "Writing Internal Control 2 failed with i2c error %d", err);
105 this->mark_failed();
106 return;
107 }
108}
109
110void MMC5983Component::dump_config() {
111 ESP_LOGD(TAG, "MMC5983:");
112 LOG_I2C_DEVICE(this);
113 LOG_SENSOR(" ", "X", this->x_sensor_);
114 LOG_SENSOR(" ", "Y", this->y_sensor_);
115 LOG_SENSOR(" ", "Z", this->z_sensor_);
116}
117
118} // namespace esphome::mmc5983
void mark_failed()
Mark this component as failed.
virtual void update()=0
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:54
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:45
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")
std::span< const uint8_t > data