Skip to content

Commit a3ce6cc

Browse files
jilaypandyafabiobaltieri
authored andcommitted
drivers: tmc51xx: move DT_DRV_COMPAT to tmc51xx.c
TMC51XX would have to be remodeled as a mfd, this means that each device tmc51xx, tmc51xx_motion_controller and tmc51xx_stepper_driver will have their own DT_DRV_COMPATs. Whenever the common header is included as of now, the adi_tmc51xx DT_DRV_COMPAT would have to be undef-ed each time in order to define a new DT_DRV_COMPAT i.e. required by the devices of the tmc51xx mfd. Signed-off-by: Jilay Pandya <[email protected]>
1 parent d49fc11 commit a3ce6cc

File tree

5 files changed

+136
-189
lines changed

5 files changed

+136
-189
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# SPDX-FileCopyrightText: Copyright (c) 2025 Dipak Shetty
22
# SPDX-License-Identifier: Apache-2.0
33

4-
zephyr_library_sources_ifdef(CONFIG_STEPPER_ADI_TMC51XX tmc51xx.c tmc51xx_spi.c tmc51xx_uart.c)
4+
zephyr_library_sources_ifdef(CONFIG_STEPPER_ADI_TMC51XX tmc51xx.c)

drivers/stepper/adi_tmc/tmc51xx/tmc51xx.c

Lines changed: 135 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,153 @@
11
/*
22
* SPDX-FileCopyrightText: Copyright (c) 2025 Prevas A/S
3-
*
3+
* SPDX-FileCopyrightText: Copyright (c) 2025 Dipak Shetty
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7+
#define DT_DRV_COMPAT adi_tmc51xx
8+
79
#include <stdlib.h>
810

911
#include <zephyr/drivers/stepper.h>
12+
#include <zephyr/drivers/stepper/stepper_trinamic.h>
1013

1114
#include <adi_tmc_bus.h>
15+
#include <adi_tmc_spi.h>
16+
#include <adi_tmc_uart.h>
1217
#include <adi_tmc5xxx_common.h>
13-
#include "tmc51xx.h"
1418

1519
#include <zephyr/logging/log.h>
1620
LOG_MODULE_REGISTER(tmc51xx, CONFIG_STEPPER_LOG_LEVEL);
1721

22+
/* Check for supported bus types */
23+
#define TMC51XX_BUS_SPI DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
24+
#define TMC51XX_BUS_UART DT_ANY_INST_ON_BUS_STATUS_OKAY(uart)
25+
26+
/* Common configuration structure for TMC51xx */
27+
struct tmc51xx_config {
28+
union tmc_bus bus;
29+
const struct tmc_bus_io *bus_io;
30+
uint8_t comm_type;
31+
const uint32_t gconf;
32+
const uint32_t clock_frequency;
33+
const uint16_t default_micro_step_res;
34+
const int8_t sg_threshold;
35+
const bool is_sg_enabled;
36+
const uint32_t sg_velocity_check_interval_ms;
37+
const uint32_t sg_threshold_velocity;
38+
#ifdef CONFIG_STEPPER_ADI_TMC51XX_RAMP_GEN
39+
const struct tmc_ramp_generator_data default_ramp_config;
40+
#endif
41+
#if TMC51XX_BUS_UART
42+
const struct gpio_dt_spec sw_sel_gpio;
43+
uint8_t uart_addr;
44+
#endif
45+
#if TMC51XX_BUS_SPI
46+
struct gpio_dt_spec diag0_gpio;
47+
#endif
48+
};
49+
50+
struct tmc51xx_data {
51+
struct k_sem sem;
52+
struct k_work_delayable stallguard_dwork;
53+
struct k_work_delayable rampstat_callback_dwork;
54+
struct gpio_callback diag0_cb;
55+
const struct device *stepper;
56+
stepper_event_callback_t callback;
57+
void *event_cb_user_data;
58+
};
59+
60+
#if TMC51XX_BUS_SPI
61+
62+
static int tmc51xx_bus_check_spi(const union tmc_bus *bus, uint8_t comm_type)
63+
{
64+
if (comm_type != TMC_COMM_SPI) {
65+
return -ENOTSUP;
66+
}
67+
return spi_is_ready_dt(&bus->spi) ? 0 : -ENODEV;
68+
}
69+
70+
static int tmc51xx_reg_write_spi(const struct device *dev, const uint8_t reg_addr,
71+
const uint32_t reg_val)
72+
{
73+
const struct tmc51xx_config *config = dev->config;
74+
int err;
75+
76+
err = tmc_spi_write_register(&config->bus.spi, TMC5XXX_WRITE_BIT, reg_addr, reg_val);
77+
if (err < 0) {
78+
LOG_ERR("Failed to write register 0x%x with value 0x%x", reg_addr, reg_val);
79+
}
80+
81+
return err;
82+
}
83+
84+
static int tmc51xx_reg_read_spi(const struct device *dev, const uint8_t reg_addr, uint32_t *reg_val)
85+
{
86+
const struct tmc51xx_config *config = dev->config;
87+
int err;
88+
89+
err = tmc_spi_read_register(&config->bus.spi, TMC5XXX_ADDRESS_MASK, reg_addr, reg_val);
90+
if (err < 0) {
91+
LOG_ERR("Failed to read register 0x%x", reg_addr);
92+
}
93+
94+
return err;
95+
}
96+
97+
const struct tmc_bus_io tmc51xx_spi_bus_io = {
98+
.check = tmc51xx_bus_check_spi,
99+
.read = tmc51xx_reg_read_spi,
100+
.write = tmc51xx_reg_write_spi,
101+
};
102+
#endif /* TMC51XX_BUS_SPI */
103+
104+
#if TMC51XX_BUS_UART
105+
106+
static int tmc51xx_bus_check_uart(const union tmc_bus *bus, uint8_t comm_type)
107+
{
108+
if (comm_type != TMC_COMM_UART) {
109+
return -ENOTSUP;
110+
}
111+
return device_is_ready(bus->uart) ? 0 : -ENODEV;
112+
}
113+
114+
static int tmc51xx_reg_write_uart(const struct device *dev, const uint8_t reg_addr,
115+
const uint32_t reg_val)
116+
{
117+
const struct tmc51xx_config *config = dev->config;
118+
int err;
119+
120+
/* Route to the adi_tmc_uart.h implementation */
121+
err = tmc_uart_write_register(config->bus.uart, config->uart_addr, reg_addr, reg_val);
122+
if (err < 0) {
123+
LOG_ERR("Failed to write register 0x%x with value 0x%x", reg_addr, reg_val);
124+
}
125+
126+
return err;
127+
}
128+
129+
static int tmc51xx_reg_read_uart(const struct device *dev, const uint8_t reg_addr,
130+
uint32_t *reg_val)
131+
{
132+
const struct tmc51xx_config *config = dev->config;
133+
int err;
134+
135+
/* Route to the adi_tmc_uart.h implementation */
136+
err = tmc_uart_read_register(config->bus.uart, config->uart_addr, reg_addr, reg_val);
137+
if (err < 0) {
138+
LOG_ERR("Failed to read register 0x%x", reg_addr);
139+
}
140+
141+
return err;
142+
}
143+
144+
const struct tmc_bus_io tmc51xx_uart_bus_io = {
145+
.check = tmc51xx_bus_check_uart,
146+
.read = tmc51xx_reg_read_uart,
147+
.write = tmc51xx_reg_write_uart,
148+
};
149+
#endif /* TMC51XX_BUS_UART */
150+
18151
static inline int tmc51xx_bus_check(const struct device *dev)
19152
{
20153
const struct tmc51xx_config *config = dev->config;

drivers/stepper/adi_tmc/tmc51xx/tmc51xx.h

Lines changed: 0 additions & 65 deletions
This file was deleted.

drivers/stepper/adi_tmc/tmc51xx/tmc51xx_spi.c

Lines changed: 0 additions & 58 deletions
This file was deleted.

drivers/stepper/adi_tmc/tmc51xx/tmc51xx_uart.c

Lines changed: 0 additions & 63 deletions
This file was deleted.

0 commit comments

Comments
 (0)