ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
as5600.cpp
Go to the documentation of this file.
1#include "as5600.h"
2#include "esphome/core/log.h"
3
4namespace esphome {
5namespace as5600 {
6
7static const char *const TAG = "as5600";
8
9// Configuration registers
10static const uint8_t REGISTER_ZMCO = 0x00; // 8 bytes / R
11static const uint8_t REGISTER_ZPOS = 0x01; // 16 bytes / RW
12static const uint8_t REGISTER_MPOS = 0x03; // 16 bytes / RW
13static const uint8_t REGISTER_MANG = 0x05; // 16 bytes / RW
14static const uint8_t REGISTER_CONF = 0x07; // 16 bytes / RW
15
16// Output registers
17static const uint8_t REGISTER_ANGLE_RAW = 0x0C; // 16 bytes / R
18static const uint8_t REGISTER_ANGLE = 0x0E; // 16 bytes / R
19
20// Status registers
21static const uint8_t REGISTER_STATUS = 0x0B; // 8 bytes / R
22static const uint8_t REGISTER_AGC = 0x1A; // 8 bytes / R
23static const uint8_t REGISTER_MAGNITUDE = 0x1B; // 16 bytes / R
24
26 if (!this->read_byte(REGISTER_STATUS).has_value()) {
27 this->mark_failed();
28 return;
29 }
30
31 // configuration direction pin, if given
32 // the dir pin on the chip should be low for clockwise
33 // and high for counterclockwise. If the pin is left floating
34 // the reported positions will be erratic.
35 if (this->dir_pin_ != nullptr) {
37 this->dir_pin_->digital_write(this->direction_ == 1);
38 }
39
40 // build config register
41 // take the value, shift it left, and add mask to it to ensure we
42 // are only changing the bits appropriate for that setting in the
43 // off chance we somehow have bad value in there and it makes for
44 // a nice visual for the bit positions.
45 uint16_t config = 0;
46 // clang-format off
47 config |= (this->watchdog_ << 13) & 0b0010000000000000;
48 config |= (this->fast_filter_ << 10) & 0b0001110000000000;
49 config |= (this->slow_filter_ << 8) & 0b0000001100000000;
50 config |= (this->pwm_frequency_ << 6) & 0b0000000011000000;
51 config |= (this->output_mode_ << 4) & 0b0000000000110000;
52 config |= (this->hysteresis_ << 2) & 0b0000000000001100;
53 config |= (this->power_mode_ << 0) & 0b0000000000000011;
54 // clang-format on
55
56 // write config to config register
57 if (!this->write_byte_16(REGISTER_CONF, config)) {
58 this->mark_failed();
59 return;
60 }
61
62 // configure the start position
63 this->write_byte_16(REGISTER_ZPOS, this->start_position_);
64
65 // configure either end position or max angle
66 if (this->end_mode_ == END_MODE_POSITION) {
67 this->write_byte_16(REGISTER_MPOS, this->end_position_);
68 } else {
69 this->write_byte_16(REGISTER_MANG, this->end_position_);
70 }
71
72 // calculate the raw max from end position or start + range
73 this->raw_max_ = this->end_mode_ == END_MODE_POSITION ? this->end_position_ & 4095
74 : (this->start_position_ + this->end_position_) & 4095;
75
76 // calculate allowed range of motion by taking the start from the end
77 // but only if the end is greater than the start. If the start is greater
78 // than the end position, then that means we take the start all the way to
79 // reset point (i.e. 0 deg raw) and then we that with the end position
80 uint16_t range = this->raw_max_ > this->start_position_ ? this->raw_max_ - this->start_position_
81 : (4095 - this->start_position_) + this->raw_max_;
82
83 // range scale is ratio of actual allowed range to the full range
84 this->range_scale_ = range / 4095.0f;
85}
86
88 ESP_LOGCONFIG(TAG, "AS5600:");
89 LOG_I2C_DEVICE(this);
90
91 if (this->is_failed()) {
92 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
93 return;
94 }
95
96 ESP_LOGCONFIG(TAG,
97 " Watchdog: %d\n"
98 " Fast Filter: %d\n"
99 " Slow Filter: %d\n"
100 " Hysteresis: %d\n"
101 " Start Position: %d",
102 this->watchdog_, this->fast_filter_, this->slow_filter_, this->hysteresis_, this->start_position_);
103 if (this->end_mode_ == END_MODE_POSITION) {
104 ESP_LOGCONFIG(TAG, " End Position: %d", this->end_position_);
105 } else {
106 ESP_LOGCONFIG(TAG, " Range: %d", this->end_position_);
107 }
108}
109
110bool AS5600Component::in_range(uint16_t raw_position) {
111 return this->raw_max_ > this->start_position_
112 ? raw_position >= this->start_position_ && raw_position <= this->raw_max_
113 : raw_position >= this->start_position_ || raw_position <= this->raw_max_;
114}
115
117 uint8_t status = this->reg(REGISTER_STATUS).get() >> 3 & 0b000111;
118 return static_cast<AS5600MagnetStatus>(status);
119}
120
122 uint16_t pos = 0;
123 if (!this->read_byte_16(REGISTER_ANGLE, &pos)) {
124 return {};
125 }
126 return pos;
127}
128
130 uint16_t pos = 0;
131 if (!this->read_byte_16(REGISTER_ANGLE_RAW, &pos)) {
132 return {};
133 }
134 return pos;
135}
136
137} // namespace as5600
138} // namespace esphome
uint8_t status
Definition bl0942.h:8
virtual void mark_failed()
Mark this component as failed.
bool is_failed() const
virtual void pin_mode(gpio::Flags flags)=0
virtual void digital_write(bool value)=0
optional< uint16_t > read_position()
Definition as5600.cpp:121
AS5600MagnetStatus read_magnet_status()
Definition as5600.cpp:116
optional< uint16_t > read_raw_position()
Definition as5600.cpp:129
bool in_range(uint16_t raw_position)
Definition as5600.cpp:110
void setup() override
Set up the internal sensor array.
Definition as5600.cpp:25
EndPositionMode end_mode_
Definition as5600.h:99
InternalGPIOPin * dir_pin_
Definition as5600.h:87
bool read_byte_16(uint8_t a_register, uint16_t *data)
Definition i2c.h:250
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
Definition i2c.h:153
bool read_byte(uint8_t a_register, uint8_t *data, bool stop=true)
Definition i2c.h:239
bool write_byte_16(uint8_t a_register, uint16_t data)
Definition i2c.h:270
uint8_t get() const
returns the register value
Definition i2c.cpp:75
Range range
Definition msa3xx.h:0
@ END_MODE_POSITION
Definition as5600.h:24
@ FLAG_OUTPUT
Definition gpio.h:19
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7