14static constexpr uint8_t SYNC_BYTE = 0x05;
15static constexpr uint8_t MASTER_ADDRESS = 0xFF;
16static constexpr uint8_t WRITE_BIT = 0x80;
17static constexpr size_t READ_REQUEST_SIZE = 4;
18static constexpr size_t DATAGRAM_SIZE = 8;
19static constexpr uint8_t DEFAULT_TOFF = 3;
20static constexpr float SENSE_RESISTOR_OFFSET = 0.02f;
21static constexpr float INTERNAL_RSENSE_OHM = 0.17f;
24static constexpr float APPROACH_TIME_S = 0.02f;
26static constexpr int32_t VACTUAL_CHANGE_DIVISOR = 16;
27static constexpr uint32_t STATUS_POLL_INTERVAL_MS = 1000;
30static constexpr uint8_t UNREACHABLE_POLL_SKIP = 9;
31static constexpr uint32_t GSTAT_FLAGS = 0x07;
32static constexpr uint32_t DRV_STATUS_FLAGS = 0x0FFF;
36 "Overtemperature prewarning",
"Overtemperature shutdown",
"Short to ground on phase A",
37 "Short to ground on phase B",
"Low side short on phase A",
"Low side short on phase B",
38 "Open load on phase A",
"Open load on phase B",
"Temperature above 120C",
"Temperature above 143C",
39 "Temperature above 150C",
"Temperature above 157C",
"Unknown");
42static uint8_t datagram_crc(
const uint8_t *data,
size_t len) {
44 for (
size_t i = 0; i <
len; i++) {
46 crc =
crc8(&reflected, 1, crc, 0x07,
true);
52static int restored_index(uint8_t reg) {
53 for (
size_t i = 0; i < RESTORED_REGISTERS.size(); i++) {
54 if (RESTORED_REGISTERS[i] == reg)
55 return static_cast<int>(i);
68 if (!version.has_value()) {
69 ESP_LOGE(TAG,
"No response from driver at address %u", this->
address_);
75 ESP_LOGW(TAG,
"Unexpected IC version 0x%02X", this->
version_);
117 for (
size_t i = 0; i < RESTORED_REGISTERS.size(); i++) {
136 if (toff.has_value() && *toff != 0)
154 if (!gstat.has_value())
160 if (!
value.has_value())
165 if ((*gstat & GSTAT_FLAGS) != 0)
168 uint16_t
status = (*gstat & GSTAT_FLAGS) | ((drv_status & DRV_STATUS_FLAGS) << 3);
171 ESP_LOGW(TAG,
"Restoring settings after driver reset");
180 if ((changed & (1u << event)) == 0)
182 bool active = (
status & (1u << event)) != 0;
185 ESP_LOGW(TAG,
"%s", LOG_STR_ARG(DriverEventStrings::get_log_str(event)));
187 ESP_LOGD(TAG,
"%s: %s", LOG_STR_ARG(DriverEventStrings::get_log_str(event)),
188 active ? LOG_STR_LITERAL(
"active") : LOG_STR_LITERAL(
"cleared"));
198 " IC version: 0x%02X",
202 uint8_t irun = this->
read_field(IRUN).value_or(0);
203 uint8_t ihold = this->
read_field(IHOLD).value_or(0);
207 " Run current scale: %u\n"
208 " Hold current scale: %u\n"
209 " Current scaled by VREF",
213 " Run current: %.2f A\n"
214 " Hold current: %.2f A\n"
215 " Max current: %.2f A",
219 LOG_PIN(
" ENN Pin: ", this->
enn_pin_);
221 LOG_PIN(
" DIR Pin: ", this->
dir_pin_);
234 ESP_LOGW(TAG,
"DIAG rose, but the driver status could not be read");
235 }
else if ((this->
status_ & GSTAT_FLAGS) == 0) {
270 int8_t
direction = (remaining > 0) - (remaining < 0);
281 if (vactual != 0 && (vactual > 0) == (last > 0) && std::abs(vactual - last) < std::abs(last) / VACTUAL_CHANGE_DIVISOR)
336 for (uint8_t mres = 0; mres <= 8; mres++) {
337 if ((256u >> mres) == microsteps) {
342 ESP_LOGW(TAG,
"Invalid microsteps %u", microsteps);
347 if (!mres.has_value() || *mres > 8)
349 return 256u >> *mres;
364 ((std::isnan(this->
rsense_) ? INTERNAL_RSENSE_OHM : this->
rsense_) + SENSE_RESISTOR_OFFSET);
368 return (std::min<uint8_t>(scale, 31) + 1) / 32.0f * this->
full_scale_current_() / std::numbers::sqrt2_v<float>;
373 ESP_LOGW(TAG,
"Current in A is not supported with analog current scale, use irun/ihold");
376 float scale = 32.0f * std::numbers::sqrt2_v<float> * current / this->
full_scale_current_() - 1.0f;
378 ESP_LOGW(TAG,
"%.2f A is above the %.2f A limit, using the limit", current, this->
scale_to_current(31));
381 return static_cast<uint8_t
>(std::max(0.0f, std::round(scale)));
405 uint8_t datagram[DATAGRAM_SIZE] = {SYNC_BYTE,
407 static_cast<uint8_t
>(reg | WRITE_BIT),
408 static_cast<uint8_t
>(
value >> 24),
409 static_cast<uint8_t
>(
value >> 16),
410 static_cast<uint8_t
>(
value >> 8),
411 static_cast<uint8_t
>(
value),
413 datagram[7] = datagram_crc(datagram, 7);
419 uint8_t echo[DATAGRAM_SIZE];
421 ESP_LOGV(TAG,
"No echo writing register 0x%02X", reg);
427 }
else if (
int index = restored_index(reg); index >= 0) {
438 uint8_t request[READ_REQUEST_SIZE] = {SYNC_BYTE, this->
address_, reg, 0};
439 request[3] = datagram_crc(request, 3);
444 uint8_t reply[DATAGRAM_SIZE];
446 ESP_LOGV(TAG,
"No reply reading register 0x%02X", reg);
450 if (!this->
status_comm_result(reply[0] == SYNC_BYTE && reply[1] == MASTER_ADDRESS && reply[2] == reg &&
451 reply[7] == datagram_crc(reply, 7))) {
452 ESP_LOGV(TAG,
"Invalid reply reading register 0x%02X", reg);
455 return encode_uint32(reply[3], reply[4], reply[5], reply[6]);
462 optional<uint32_t> data;
463 int index = restored_index(field.
reg);
471 if (!data.has_value())
479 if (!data.has_value())
void mark_failed()
Mark this component as failed.
void status_set_warning()
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
void set_interval(const char *name, uint32_t interval, SchedulerCallback &&f)
Set an interval function with a const char* name.
virtual void digital_write(bool value)=0
void set(bool active)
Run the loop continuously while active is true. Repeating the current value does nothing.
Helper class to disable interrupts.
void attach_interrupt(void(*func)(T *), T *arg, gpio::InterruptType type) const
bool has_reached_target()
void calculate_speed_(uint32_t now)
int32_t speed_to_vactual_(float speed) const
uint8_t status_poll_skip_
float initial_run_current_
void apply_config_()
Write the settings from the configuration to the readable registers.
void on_shutdown() override
std::array< uint32_t, RESTORED_REGISTERS.size()> restored_values_
bool write_register(uint8_t reg, uint32_t value)
static uint32_t extract_field(uint32_t data, const RegisterField &field)
int32_t get_vactual_() const
void set_microsteps(uint16_t microsteps)
void set_toff(uint8_t toff)
Chopper off time (1-15). TOFF=0 is used to disable the outputs, so this is applied on the next enable...
optional< uint8_t > current_to_scale_(float current)
float initial_hold_current_
virtual uint32_t * shadow_register(uint8_t reg)
Return the cached value of a write-only register, or nullptr when the register is readable.
virtual void restore_registers()
Write the cached write-only registers and the last written configuration registers to the driver,...
uint32_t clock_frequency_
void set_enabled(bool enabled)
Enable or disable the motor outputs, using the ENN pin when configured and TOFF otherwise.
uint16_t get_microsteps()
optional< uint32_t > read_register(uint8_t reg)
uint16_t initial_microsteps_
HighFrequencyLoopRequester high_freq_
optional< uint32_t > read_field(const RegisterField &field)
IndexPulseStore index_store_
InternalGPIOPin * diag_pin_
void set_hold_current(float current)
float full_scale_current_()
LazyCallbackManager< void(DriverEvent, bool)> status_callback_
virtual uint8_t expected_version() const =0
IC version reported in IOIN by the supported chip.
float scale_to_current(uint8_t scale)
Convert a current scale (0-31) to A RMS, without analog current scale.
void dump_config() override
InternalGPIOPin * index_pin_
uint8_t restored_written_
bool analog_current_scale_
bool update_status_()
Read GSTAT and DRV_STATUS and report changes. Returns false when the status could not be read.
optional< uint8_t > ottrim_
void set_run_current(float current)
Set the motor run or hold current in A RMS.
bool write_field(const RegisterField &field, uint32_t value)
virtual void on_diag_without_error()
Called when DIAG rises without a driver error.
optional< std::array< uint8_t, N > > read_array()
void discard_available()
Read and drop every byte waiting in the receive buffer.
void write_array(const uint8_t *data, size_t len)
ESPHOME_LOG_TAG(TAG, "tmc22xx")
PROGMEM_STRING_TABLE(DriverEventStrings, "Driver reset", "Driver error", "Charge pump undervoltage", "Overtemperature prewarning", "Overtemperature shutdown", "Short to ground on phase A", "Short to ground on phase B", "Low side short on phase A", "Low side short on phase B", "Open load on phase A", "Open load on phase B", "Temperature above 120C", "Temperature above 143C", "Temperature above 150C", "Temperature above 157C", "Unknown")
DriverEvent
Driver conditions reported to on_status, in the order of their status bits: GSTAT bits 0-2,...
@ DRIVER_EVENT_OPEN_LOAD_A
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
uint32_t IRAM_ATTR HOT micros()
constexpr uint32_t encode_uint32(uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4)
Encode a 32-bit value given four bytes in most to least significant byte order.
uint8_t crc8(const uint8_t *data, uint8_t len, uint8_t crc, uint8_t poly, bool msb_first)
Calculate a CRC-8 checksum of data with size len.
void setup_output_pin(GPIOPin *pin, bool level)
Set up pin and drive it to level. Does nothing when pin is null, so an optional pin needs no check.
uint8_t reverse_bits(uint8_t x)
Reverse the order of 8 bits.
Set by a rising edge on DIAG, which signals a driver error or, on chips that support it,...
static void gpio_intr(DiagPinStore *arg)
Counts the step pulses the driver reports on INDEX while it runs from VACTUAL.
static void gpio_intr(IndexPulseStore *arg)
volatile int8_t direction
A bit field inside one register.
constexpr uint32_t mask() const