-
Notifications
You must be signed in to change notification settings - Fork 7.7k
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LOG_MODULE_REGISTER(tmc5xxx, CONFIG_STEPPER_LOG_LEVEL); | |
LOG_MODULE_REGISTER(tmc50xx, CONFIG_STEPPER_LOG_LEVEL); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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)); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
||
#include "tmc51xx.h" | ||
#include <zephyr/logging/log.h> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
trivial
#include "tmc51xx.h" | ||
#include <zephyr/logging/log.h> | ||
|
||
LOG_MODULE_REGISTER(tmc5xxx, CONFIG_STEPPER_LOG_LEVEL); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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) { |
There was a problem hiding this comment.
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
There was a problem hiding this 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.
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>
9de529c
to
0c11185
Compare
|
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. |
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) |
There was a problem hiding this comment.
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)) { |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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 */ |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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, \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DEVICE_DT_INST_DEFINE(inst, tmc51xx_controller_init, NULL, \ | |
DEVICE_DT_INST_DEFINE(inst, tmc50xx_controller_init, NULL, \ |
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.