Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 9 additions & 14 deletions drivers/stepper/adi_tmc/adi_tmc5041_stepper_controller.c
Original file line number Diff line number Diff line change
@@ -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
*/

Expand All @@ -10,8 +11,8 @@
#include <zephyr/drivers/stepper.h>
#include <zephyr/drivers/stepper/stepper_trinamic.h>

#include "adi_tmc_reg.h"
#include "adi_tmc_spi.h"
#include "adi_tmc5xxx_common.h"

#include <zephyr/logging/log.h>

Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
51 changes: 51 additions & 0 deletions drivers/stepper/adi_tmc/adi_tmc5xxx_common.h
Original file line number Diff line number Diff line change
@@ -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_ */
9 changes: 7 additions & 2 deletions drivers/stepper/adi_tmc/adi_tmc_reg.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@
extern "C" {
#endif

/** Common Registers for TMC5041 and TMC51XX */
#if defined(CONFIG_STEPPER_ADI_TMC5041)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this rely on the correct include order? The autoconf is not pulled in otherwise.


#define TMC5XXX_CLOCK_FREQ_SHIFT 24

#endif

#ifdef CONFIG_STEPPER_ADI_TMC5041

#define TMC5041_MOTOR_ADDR(m) (0x20 << (m))
Expand Down Expand Up @@ -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

/**
Expand Down
Loading