Skip to content

Commit 5032d8e

Browse files
jilaypandyanashif
authored andcommitted
drivers: stepper: add common helper header for tmc5xxx functions
This commit adds a common helper header for tmc5xxx driver Signed-off-by: Jilay Pandya <[email protected]>
1 parent 3825c85 commit 5032d8e

File tree

3 files changed

+67
-16
lines changed

3 files changed

+67
-16
lines changed

drivers/stepper/adi_tmc/adi_tmc5041_stepper_controller.c

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* SPDX-FileCopyrightText: Copyright (c) 2024 Carl Zeiss Meditec AG
3+
* SPDX-FileCopyrightText: Copyright (c) 2024 Jilay Sandeep Pandya
34
* SPDX-License-Identifier: Apache-2.0
45
*/
56

@@ -10,8 +11,8 @@
1011
#include <zephyr/drivers/stepper.h>
1112
#include <zephyr/drivers/stepper/stepper_trinamic.h>
1213

13-
#include "adi_tmc_reg.h"
1414
#include "adi_tmc_spi.h"
15+
#include "adi_tmc5xxx_common.h"
1516

1617
#include <zephyr/logging/log.h>
1718

@@ -93,17 +94,6 @@ static int tmc5041_read(const struct device *dev, const uint8_t reg_addr, uint32
9394
return 0;
9495
}
9596

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-
10797
static int tmc5041_stepper_set_event_callback(const struct device *dev,
10898
stepper_event_callback_t callback, void *user_data)
10999
{
@@ -356,10 +346,13 @@ static int tmc5041_stepper_move(const struct device *dev, const int32_t steps)
356346
static int tmc5041_stepper_set_max_velocity(const struct device *dev, uint32_t velocity)
357347
{
358348
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;
359351
uint32_t velocity_fclk;
360352
int err;
361353

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+
363356
err = tmc5041_write(config->controller, TMC5041_VMAX(config->index), velocity_fclk);
364357
if (err != 0) {
365358
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
482475
{
483476
LOG_DBG("Stepper motor controller %s enable constant velocity mode", dev->name);
484477
const struct tmc5041_stepper_config *config = dev->config;
478+
const struct tmc5041_config *tmc5041_config = config->controller->config;
485479
struct tmc5041_stepper_data *data = dev->data;
480+
const uint32_t clock_frequency = tmc5041_config->clock_frequency;
486481
uint32_t velocity_fclk;
487482
int err;
488483

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);
490485

491486
if (config->is_sg_enabled) {
492487
err = stallguard_enable(dev, false);
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* @file drivers/stepper/adi_tmc/adi_tmc5xxx_common.h
3+
*
4+
* @brief Common TMC5xxx stepper controller driver definitions
5+
*/
6+
7+
/**
8+
* SPDX-FileCopyrightText: Copyright (c) 2024 Jilay Sandeep Pandya
9+
* SPDX-License-Identifier: Apache-2.0
10+
*/
11+
12+
#ifndef ZEPHYR_DRIVERS_STEPPER_ADI_TMC_ADI_TMC5XXX_COMMON_H_
13+
#define ZEPHYR_DRIVERS_STEPPER_ADI_TMC_ADI_TMC5XXX_COMMON_H_
14+
15+
#include "adi_tmc_reg.h"
16+
17+
#ifdef __cplusplus
18+
extern "C" {
19+
#endif
20+
21+
/**
22+
* @name TMC5xxx module functions
23+
* @anchor TMC5XXX_FUNCTIONS
24+
*
25+
* @{
26+
*/
27+
28+
/**
29+
* @brief Calculate the velocity in full clock cycles from the velocity in Hz
30+
*
31+
* @param velocity_hz Velocity in Hz
32+
* @param clock_frequency Clock frequency in Hz
33+
*
34+
* @return Calculated velocity in full clock cycles
35+
*/
36+
static inline uint32_t tmc5xxx_calculate_velocity_from_hz_to_fclk(uint64_t velocity_hz,
37+
uint32_t clock_frequency)
38+
{
39+
__ASSERT_NO_MSG(clock_frequency);
40+
return (velocity_hz << TMC5XXX_CLOCK_FREQ_SHIFT) / clock_frequency;
41+
}
42+
43+
/**
44+
* @}
45+
*/
46+
47+
#ifdef __cplusplus
48+
}
49+
#endif
50+
51+
#endif /* ZEPHYR_DRIVERS_STEPPER_ADI_TMC_ADI_TMC5XXX_COMMON_H_ */

drivers/stepper/adi_tmc/adi_tmc_reg.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@
1717
extern "C" {
1818
#endif
1919

20+
/** Common Registers for TMC5041 and TMC51XX */
21+
#if defined(CONFIG_STEPPER_ADI_TMC5041)
22+
23+
#define TMC5XXX_CLOCK_FREQ_SHIFT 24
24+
25+
#endif
26+
2027
#ifdef CONFIG_STEPPER_ADI_TMC5041
2128

2229
#define TMC5041_MOTOR_ADDR(m) (0x20 << (m))
@@ -133,8 +140,6 @@ extern "C" {
133140
#define TMC5041_CHOPCONF_MRES_MASK GENMASK(27, 24)
134141
#define TMC5041_CHOPCONF_MRES_SHIFT 24
135142

136-
#define TMC5041_CLOCK_FREQ_SHIFT 24
137-
138143
#endif
139144

140145
/**

0 commit comments

Comments
 (0)