|
1 | 1 | /* |
2 | 2 | * SPDX-FileCopyrightText: Copyright (c) 2024 Carl Zeiss Meditec AG |
| 3 | + * SPDX-FileCopyrightText: Copyright (c) 2024 Jilay Sandeep Pandya |
3 | 4 | * SPDX-License-Identifier: Apache-2.0 |
4 | 5 | */ |
5 | 6 |
|
|
10 | 11 | #include <zephyr/drivers/stepper.h> |
11 | 12 | #include <zephyr/drivers/stepper/stepper_trinamic.h> |
12 | 13 |
|
13 | | -#include "adi_tmc_reg.h" |
14 | 14 | #include "adi_tmc_spi.h" |
| 15 | +#include "adi_tmc5xxx_common.h" |
15 | 16 |
|
16 | 17 | #include <zephyr/logging/log.h> |
17 | 18 |
|
@@ -93,17 +94,6 @@ static int tmc5041_read(const struct device *dev, const uint8_t reg_addr, uint32 |
93 | 94 | return 0; |
94 | 95 | } |
95 | 96 |
|
96 | | -static void calculate_velocity_from_hz_to_fclk(const struct device *dev, const uint32_t velocity_hz, |
97 | | - uint32_t *const velocity_fclk) |
98 | | -{ |
99 | | - const struct tmc5041_config *config = dev->config; |
100 | | - |
101 | | - *velocity_fclk = |
102 | | - ((uint64_t)(velocity_hz) << TMC5041_CLOCK_FREQ_SHIFT) / config->clock_frequency; |
103 | | - LOG_DBG("Stepper motor controller %s velocity: %d Hz, velocity_fclk: %d", dev->name, |
104 | | - velocity_hz, *velocity_fclk); |
105 | | -} |
106 | | - |
107 | 97 | static int tmc5041_stepper_set_event_callback(const struct device *dev, |
108 | 98 | stepper_event_callback_t callback, void *user_data) |
109 | 99 | { |
@@ -356,10 +346,13 @@ static int tmc5041_stepper_move(const struct device *dev, const int32_t steps) |
356 | 346 | static int tmc5041_stepper_set_max_velocity(const struct device *dev, uint32_t velocity) |
357 | 347 | { |
358 | 348 | const struct tmc5041_stepper_config *config = dev->config; |
| 349 | + const struct tmc5041_config *tmc5041_config = config->controller->config; |
| 350 | + const uint32_t clock_frequency = tmc5041_config->clock_frequency; |
359 | 351 | uint32_t velocity_fclk; |
360 | 352 | int err; |
361 | 353 |
|
362 | | - calculate_velocity_from_hz_to_fclk(config->controller, velocity, &velocity_fclk); |
| 354 | + velocity_fclk = tmc5xxx_calculate_velocity_from_hz_to_fclk(velocity, clock_frequency); |
| 355 | + |
363 | 356 | err = tmc5041_write(config->controller, TMC5041_VMAX(config->index), velocity_fclk); |
364 | 357 | if (err != 0) { |
365 | 358 | LOG_ERR("%s: Failed to set max velocity", dev->name); |
@@ -482,11 +475,13 @@ static int tmc5041_stepper_enable_constant_velocity_mode(const struct device *de |
482 | 475 | { |
483 | 476 | LOG_DBG("Stepper motor controller %s enable constant velocity mode", dev->name); |
484 | 477 | const struct tmc5041_stepper_config *config = dev->config; |
| 478 | + const struct tmc5041_config *tmc5041_config = config->controller->config; |
485 | 479 | struct tmc5041_stepper_data *data = dev->data; |
| 480 | + const uint32_t clock_frequency = tmc5041_config->clock_frequency; |
486 | 481 | uint32_t velocity_fclk; |
487 | 482 | int err; |
488 | 483 |
|
489 | | - calculate_velocity_from_hz_to_fclk(config->controller, velocity, &velocity_fclk); |
| 484 | + velocity_fclk = tmc5xxx_calculate_velocity_from_hz_to_fclk(velocity, clock_frequency); |
490 | 485 |
|
491 | 486 | if (config->is_sg_enabled) { |
492 | 487 | err = stallguard_enable(dev, false); |
|
0 commit comments