Skip to content

Refactor tmc5xxx drivers & modules to reuse code #91988

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

dipakgmx
Copy link
Member

This pull request introduces refactoring of the TMC5xxx & TMC51xx series to reuse
the common code between the modules.

Now, a common handler is used for handling bus transactions for both TMC5xxx
stepper motor controllers. This consolidates the SPI and UART communication
code in a shared implementation that can be used by both TMC50xx and TMC51xx
controllers.

The changes now introduces a new adi_tmc5xxx_core files provide a unified API for basic
controller operations like register read/write, motor control and diagnostics.

This refactoring also moves the register definitions to the tmc5xxx subdirectory
and removes the no longer needed adi_tmc5xxx_common.h file.

Copy link
Member

@jilaypandya jilaypandya left a comment

Choose a reason for hiding this comment

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

At first glance, it looks good. Some minor refactoring comments. Do you want this to be in 4.2?

Copy link
Member

@jilaypandya jilaypandya Jun 22, 2025

Choose a reason for hiding this comment

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

I think it's best to move the Kconfigs of tmc5xxx drivers directly in the tmc5xxx folder

Copy link
Member

@jilaypandya jilaypandya Jun 22, 2025

Choose a reason for hiding this comment

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

can this file be renamed to adi_tmc5xxx_reg.h. Could you look at the unwanted formatting changes?

@@ -0,0 +1,335 @@
/*
* SPDX-FileCopyrightText: Copyright (c) 2024 Carl Zeiss Meditec AG
* SPDX-FileCopyrightText: Copyright (c) 2025 Jilay Sandeep Pandya
Copy link
Member

@jilaypandya jilaypandya Jun 22, 2025

Choose a reason for hiding this comment

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

you can add your name as well :)

#include "adi_tmc5xxx_core.h"

#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(tmc5xxx, CONFIG_STEPPER_LOG_LEVEL);
Copy link
Member

@jilaypandya jilaypandya Jun 22, 2025

Choose a reason for hiding this comment

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

Suggested change
LOG_MODULE_REGISTER(tmc5xxx, CONFIG_STEPPER_LOG_LEVEL);
LOG_MODULE_REGISTER(tmc50xx, CONFIG_STEPPER_LOG_LEVEL);

Copy link
Member Author

Choose a reason for hiding this comment

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

The problem here is, we have bunch of core functions, which are shared between the 2 models. Now, when we log, with tmc50xx, and we have logs coming out of core module, we cannot distinguish. Hence I went for the tmc5xxx for all the files, but with the device name logging, we can figure out what device is generating this message

Copy link
Member

@jilaypandya jilaypandya Jun 22, 2025

Choose a reason for hiding this comment

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

The reasoning is plausible :) @bjarki-andreasen what would one do in such a case? I would have still kept it as tmc50xx, considering that the log is coming out of the tmc50xx.c module.

k_work_init_delayable(&data->stallguard_dwork, tmc5xxx_stallguard_work_handler);

err = tmc5xxx_write_reg(ctx, TMC5XXX_SWMODE(ctx->motor_index), BIT(10));

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change


#include "tmc51xx.h"
#include <zephyr/logging/log.h>

Copy link
Member

Choose a reason for hiding this comment

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

trivial

Suggested change

#include "tmc51xx.h"
#include <zephyr/logging/log.h>

LOG_MODULE_REGISTER(tmc5xxx, CONFIG_STEPPER_LOG_LEVEL);
Copy link
Member

@jilaypandya jilaypandya Jun 22, 2025

Choose a reason for hiding this comment

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

Suggested change
LOG_MODULE_REGISTER(tmc5xxx, CONFIG_STEPPER_LOG_LEVEL);
LOG_MODULE_REGISTER(tmc51xx, CONFIG_STEPPER_LOG_LEVEL);


/* Configure DIAG0 GPIO interrupt pin */
IF_ENABLED(TMC51XX_BUS_SPI, ({
if (config->comm_type == TMC_COMM_SPI && config->diag0_gpio.port) {
Copy link
Member

@jilaypandya jilaypandya Jun 22, 2025

Choose a reason for hiding this comment

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

could you refactor this routine out into a separate function to enhance readability

Copy link
Member

@jilaypandya jilaypandya left a comment

Choose a reason for hiding this comment

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

I think the separate commits are to ease the review process. I am just dropping a Request Change, so that we don't forget to squash them before merge.

@jilaypandya jilaypandya changed the title Refactor tmc5xxx modules to reuse code Refactor tmc5xxx drivers & modules to reuse code Jun 22, 2025
dipakgmx added 8 commits June 22, 2025 22:12
This follows the same pattern as the tmc50xx series (wherein the controller) had
2 motors. Now the tmx51xx also considers the motor as a child binding, enabling
resuing the code between the 2 models.

Signed-off-by: Dipak Shetty <shetty.dipak@gmx.com>
Create a common handler for handling bus transactions for both TMC5xxx
stepper motor controllers. This consolidates the SPI and UART communication
code in a shared implementation that can be used by both TMC50xx and TMC51xx
controllers.

Signed-off-by: Dipak Shetty <shetty.dipak@gmx.com>
The implementation moves the common bus transaction code from individual driver
files into reusable core modules. The new adi_tmc5xxx_core files provide a unified
API for basic controller operations like register read/write, motor control, and
diagnostics.

This refactoring also moves the register definitions to the tmc5xxx subdirectory
and removes the no longer needed adi_tmc5xxx_common.h file.

Signed-off-by: Dipak Shetty <shetty.dipak@gmx.com>
The drivers are now refactored to use the core module for handling the common
functions. Rampstat polling, ramp setting and diagnostics are left to the
individual drivers since they are very chip specific.

Signed-off-by: Dipak Shetty <shetty.dipak@gmx.com>
Since the motor is now made a child of the controller, the macro to pull the
ramp parameters are now refactored to pull it out of the node.

Signed-off-by: Dipak Shetty <shetty.dipak@gmx.com>
Update tmc50xx driver to conditionally select SPI support

Signed-off-by: Dipak Shetty <shetty.dipak@gmx.com>
reorganize CMakeLists for TMC5xxx support subsequent to the changes.

Signed-off-by: Dipak Shetty <shetty.dipak@gmx.com>
The dts layout is updated for tmc51xx nodes, since the motor is now made
a child of the parent controller.

Signed-off-by: Dipak Shetty <shetty.dipak@gmx.com>
@dipakgmx dipakgmx force-pushed the feat/tmc5xxx-refactor branch from 9de529c to 0c11185 Compare June 22, 2025 20:13
Copy link

@dipakgmx
Copy link
Member Author

At first glance, it looks good. Some minor refactoring comments. Do you want this to be in 4.2?

Yes, that would be great. Would you mind testing on the tmc50xx unit? I have tested it now on spi & uart configuration of tmc51xx and it works.

@jilaypandya
Copy link
Member

Sure I'll do it tomorrow.

}

/* For UART or SPI without DIAG0, schedule RAMPSTAT polling */
#if defined(CONFIG_STEPPER_ADI_TMC50XX_RAMPSTAT_POLL_INTERVAL_IN_MSEC)
Copy link
Contributor

Choose a reason for hiding this comment

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

If you have a TMC50XX and TMC51XX on the same board this will produce odd behaviour. Only thing that I see is adding this into config?

struct tmc5xxx_stepper_data const *data = dev->data;
const struct tmc5xxx_core_context *ctx = &data->core;

if (!VALID_MICRO_STEP_RES(res)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

iirc this was moved to the API itself, right @jilaypandya . So not needed here

}

const uint8_t sg_result = FIELD_GET(TMC5XXX_DRV_STATUS_SG_RESULT_MASK, drv_status);
const bool sg_status = FIELD_GET(TMC5XXX_DRV_STATUS_SG_STATUS_MASK, drv_status);
Copy link
Contributor

Choose a reason for hiding this comment

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

I mean it does not hurt, but why const these specifically?

struct tmc5xxx_controller_config {
union tmc_bus bus; /* Bus connection (SPI/UART) */
const struct tmc_bus_io *bus_io; /* Bus I/O operations */
uint8_t comm_type; /* Communication type */
Copy link
Contributor

Choose a reason for hiding this comment

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

move the comm_type at the end of the struct, wastes 3 bytes less of padding on 32 bit


#endif

static int tmc51xx_controller_init(const struct device *dev)

Choose a reason for hiding this comment

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

Suggested change
static int tmc51xx_controller_init(const struct device *dev)
static int tmc50xx_controller_init(const struct device *dev)

DT_INST_FOREACH_CHILD_STATUS_OKAY(inst, TMC50XX_STEPPER_DEFINE); \
\
/* Define the controller device */ \
DEVICE_DT_INST_DEFINE(inst, tmc51xx_controller_init, NULL, \

Choose a reason for hiding this comment

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

Suggested change
DEVICE_DT_INST_DEFINE(inst, tmc51xx_controller_init, NULL, \
DEVICE_DT_INST_DEFINE(inst, tmc50xx_controller_init, NULL, \

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: Stepper platform: ADI Analog Devices, Inc.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants