diff --git a/drivers/stepper/adi_tmc/adi_tmc5041_stepper_controller.c b/drivers/stepper/adi_tmc/adi_tmc5041_stepper_controller.c index bd9857b95cf1a..c5433c1a83591 100644 --- a/drivers/stepper/adi_tmc/adi_tmc5041_stepper_controller.c +++ b/drivers/stepper/adi_tmc/adi_tmc5041_stepper_controller.c @@ -1,5 +1,6 @@ /* * SPDX-FileCopyrightText: Copyright (c) 2024 Carl Zeiss Meditec AG + * SPDX-FileCopyrightText: Copyright (c) 2024 Jilay Sandeep Pandya * SPDX-License-Identifier: Apache-2.0 */ @@ -10,8 +11,8 @@ #include #include -#include "adi_tmc_reg.h" #include "adi_tmc_spi.h" +#include "adi_tmc5xxx_common.h" #include @@ -93,17 +94,6 @@ static int tmc5041_read(const struct device *dev, const uint8_t reg_addr, uint32 return 0; } -static void calculate_velocity_from_hz_to_fclk(const struct device *dev, const uint32_t velocity_hz, - uint32_t *const velocity_fclk) -{ - const struct tmc5041_config *config = dev->config; - - *velocity_fclk = - ((uint64_t)(velocity_hz) << TMC5041_CLOCK_FREQ_SHIFT) / config->clock_frequency; - LOG_DBG("Stepper motor controller %s velocity: %d Hz, velocity_fclk: %d", dev->name, - velocity_hz, *velocity_fclk); -} - static int tmc5041_stepper_set_event_callback(const struct device *dev, stepper_event_callback_t callback, void *user_data) { @@ -356,10 +346,13 @@ static int tmc5041_stepper_move(const struct device *dev, const int32_t steps) static int tmc5041_stepper_set_max_velocity(const struct device *dev, uint32_t velocity) { const struct tmc5041_stepper_config *config = dev->config; + const struct tmc5041_config *tmc5041_config = config->controller->config; + const uint32_t clock_frequency = tmc5041_config->clock_frequency; uint32_t velocity_fclk; int err; - calculate_velocity_from_hz_to_fclk(config->controller, velocity, &velocity_fclk); + velocity_fclk = tmc5xxx_calculate_velocity_from_hz_to_fclk(velocity, clock_frequency); + err = tmc5041_write(config->controller, TMC5041_VMAX(config->index), velocity_fclk); if (err != 0) { 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 { LOG_DBG("Stepper motor controller %s enable constant velocity mode", dev->name); const struct tmc5041_stepper_config *config = dev->config; + const struct tmc5041_config *tmc5041_config = config->controller->config; struct tmc5041_stepper_data *data = dev->data; + const uint32_t clock_frequency = tmc5041_config->clock_frequency; uint32_t velocity_fclk; int err; - calculate_velocity_from_hz_to_fclk(config->controller, velocity, &velocity_fclk); + velocity_fclk = tmc5xxx_calculate_velocity_from_hz_to_fclk(velocity, clock_frequency); if (config->is_sg_enabled) { err = stallguard_enable(dev, false); diff --git a/drivers/stepper/adi_tmc/adi_tmc5xxx_common.h b/drivers/stepper/adi_tmc/adi_tmc5xxx_common.h new file mode 100644 index 0000000000000..7eafe62326eb8 --- /dev/null +++ b/drivers/stepper/adi_tmc/adi_tmc5xxx_common.h @@ -0,0 +1,51 @@ +/** + * @file drivers/stepper/adi_tmc/adi_tmc5xxx_common.h + * + * @brief Common TMC5xxx stepper controller driver definitions + */ + +/** + * SPDX-FileCopyrightText: Copyright (c) 2024 Jilay Sandeep Pandya + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef ZEPHYR_DRIVERS_STEPPER_ADI_TMC_ADI_TMC5XXX_COMMON_H_ +#define ZEPHYR_DRIVERS_STEPPER_ADI_TMC_ADI_TMC5XXX_COMMON_H_ + +#include "adi_tmc_reg.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @name TMC5xxx module functions + * @anchor TMC5XXX_FUNCTIONS + * + * @{ + */ + +/** + * @brief Calculate the velocity in full clock cycles from the velocity in Hz + * + * @param velocity_hz Velocity in Hz + * @param clock_frequency Clock frequency in Hz + * + * @return Calculated velocity in full clock cycles + */ +static inline uint32_t tmc5xxx_calculate_velocity_from_hz_to_fclk(uint64_t velocity_hz, + uint32_t clock_frequency) +{ + __ASSERT_NO_MSG(clock_frequency); + return (velocity_hz << TMC5XXX_CLOCK_FREQ_SHIFT) / clock_frequency; +} + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif /* ZEPHYR_DRIVERS_STEPPER_ADI_TMC_ADI_TMC5XXX_COMMON_H_ */ diff --git a/drivers/stepper/adi_tmc/adi_tmc_reg.h b/drivers/stepper/adi_tmc/adi_tmc_reg.h index 1c88020c352a4..ebf0de57ed168 100644 --- a/drivers/stepper/adi_tmc/adi_tmc_reg.h +++ b/drivers/stepper/adi_tmc/adi_tmc_reg.h @@ -17,6 +17,13 @@ extern "C" { #endif +/** Common Registers for TMC5041 and TMC51XX */ +#if defined(CONFIG_STEPPER_ADI_TMC5041) + +#define TMC5XXX_CLOCK_FREQ_SHIFT 24 + +#endif + #ifdef CONFIG_STEPPER_ADI_TMC5041 #define TMC5041_MOTOR_ADDR(m) (0x20 << (m)) @@ -133,8 +140,6 @@ extern "C" { #define TMC5041_CHOPCONF_MRES_MASK GENMASK(27, 24) #define TMC5041_CHOPCONF_MRES_SHIFT 24 -#define TMC5041_CLOCK_FREQ_SHIFT 24 - #endif /**