|
1 | 1 | /* |
2 | 2 | * SPDX-FileCopyrightText: Copyright (c) 2025 Prevas A/S |
3 | | - * |
| 3 | + * SPDX-FileCopyrightText: Copyright (c) 2025 Dipak Shetty |
4 | 4 | * SPDX-License-Identifier: Apache-2.0 |
5 | 5 | */ |
6 | 6 |
|
| 7 | +#define DT_DRV_COMPAT adi_tmc51xx |
| 8 | + |
7 | 9 | #include <stdlib.h> |
8 | 10 |
|
9 | 11 | #include <zephyr/drivers/stepper.h> |
10 | 12 |
|
11 | 13 | #include <adi_tmc_bus.h> |
| 14 | +#include <adi_tmc_spi.h> |
| 15 | +#include <adi_tmc_uart.h> |
12 | 16 | #include <adi_tmc5xxx_common.h> |
13 | 17 | #include "tmc51xx.h" |
14 | 18 |
|
15 | 19 | #include <zephyr/logging/log.h> |
16 | 20 | LOG_MODULE_REGISTER(tmc51xx, CONFIG_STEPPER_LOG_LEVEL); |
17 | 21 |
|
| 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 | +#if TMC51XX_BUS_SPI |
| 27 | +/* SPI bus I/O operations for TMC51xx devices */ |
| 28 | +const struct tmc_bus_io tmc51xx_spi_bus_io; |
| 29 | +#endif |
| 30 | + |
| 31 | +#if TMC51XX_BUS_UART |
| 32 | +/* UART bus I/O operations for TMC51xx devices */ |
| 33 | +const struct tmc_bus_io tmc51xx_uart_bus_io; |
| 34 | +#endif |
| 35 | + |
| 36 | +/* Common configuration structure for TMC51xx */ |
| 37 | +struct tmc51xx_config { |
| 38 | + union tmc_bus bus; |
| 39 | + const struct tmc_bus_io *bus_io; |
| 40 | + uint8_t comm_type; |
| 41 | + const uint32_t gconf; |
| 42 | + const uint32_t clock_frequency; |
| 43 | + const uint16_t default_micro_step_res; |
| 44 | + const int8_t sg_threshold; |
| 45 | + const bool is_sg_enabled; |
| 46 | + const uint32_t sg_velocity_check_interval_ms; |
| 47 | + const uint32_t sg_threshold_velocity; |
| 48 | +#ifdef CONFIG_STEPPER_ADI_TMC51XX_RAMP_GEN |
| 49 | + const struct tmc_ramp_generator_data default_ramp_config; |
| 50 | +#endif |
| 51 | +#if TMC51XX_BUS_UART |
| 52 | + const struct gpio_dt_spec sw_sel_gpio; |
| 53 | + uint8_t uart_addr; |
| 54 | +#endif |
| 55 | +#if TMC51XX_BUS_SPI |
| 56 | + struct gpio_dt_spec diag0_gpio; |
| 57 | +#endif |
| 58 | +}; |
| 59 | + |
| 60 | +struct tmc51xx_data { |
| 61 | + struct k_sem sem; |
| 62 | + struct k_work_delayable stallguard_dwork; |
| 63 | + struct k_work_delayable rampstat_callback_dwork; |
| 64 | + struct gpio_callback diag0_cb; |
| 65 | + const struct device *stepper; |
| 66 | + stepper_event_callback_t callback; |
| 67 | + void *event_cb_user_data; |
| 68 | +}; |
| 69 | + |
| 70 | +#if TMC51XX_BUS_SPI |
| 71 | + |
| 72 | +static int tmc51xx_bus_check_spi(const union tmc_bus *bus, uint8_t comm_type) |
| 73 | +{ |
| 74 | + if (comm_type != TMC_COMM_SPI) { |
| 75 | + return -ENOTSUP; |
| 76 | + } |
| 77 | + return spi_is_ready_dt(&bus->spi) ? 0 : -ENODEV; |
| 78 | +} |
| 79 | + |
| 80 | +static int tmc51xx_reg_write_spi(const struct device *dev, const uint8_t reg_addr, |
| 81 | + const uint32_t reg_val) |
| 82 | +{ |
| 83 | + const struct tmc51xx_config *config = dev->config; |
| 84 | + int err; |
| 85 | + |
| 86 | + err = tmc_spi_write_register(&config->bus.spi, TMC5XXX_WRITE_BIT, reg_addr, reg_val); |
| 87 | + if (err < 0) { |
| 88 | + LOG_ERR("Failed to write register 0x%x with value 0x%x", reg_addr, reg_val); |
| 89 | + } |
| 90 | + |
| 91 | + return err; |
| 92 | +} |
| 93 | + |
| 94 | +static int tmc51xx_reg_read_spi(const struct device *dev, const uint8_t reg_addr, uint32_t *reg_val) |
| 95 | +{ |
| 96 | + const struct tmc51xx_config *config = dev->config; |
| 97 | + int err; |
| 98 | + |
| 99 | + err = tmc_spi_read_register(&config->bus.spi, TMC5XXX_ADDRESS_MASK, reg_addr, reg_val); |
| 100 | + if (err < 0) { |
| 101 | + LOG_ERR("Failed to read register 0x%x", reg_addr); |
| 102 | + } |
| 103 | + |
| 104 | + return err; |
| 105 | +} |
| 106 | + |
| 107 | +const struct tmc_bus_io tmc51xx_spi_bus_io = { |
| 108 | + .check = tmc51xx_bus_check_spi, |
| 109 | + .read = tmc51xx_reg_read_spi, |
| 110 | + .write = tmc51xx_reg_write_spi, |
| 111 | +}; |
| 112 | +#endif /* TMC51XX_BUS_SPI */ |
| 113 | + |
| 114 | +#if TMC51XX_BUS_UART |
| 115 | + |
| 116 | +static int tmc51xx_bus_check_uart(const union tmc_bus *bus, uint8_t comm_type) |
| 117 | +{ |
| 118 | + if (comm_type != TMC_COMM_UART) { |
| 119 | + return -ENOTSUP; |
| 120 | + } |
| 121 | + return device_is_ready(bus->uart) ? 0 : -ENODEV; |
| 122 | +} |
| 123 | + |
| 124 | +static int tmc51xx_reg_write_uart(const struct device *dev, const uint8_t reg_addr, |
| 125 | + const uint32_t reg_val) |
| 126 | +{ |
| 127 | + const struct tmc51xx_config *config = dev->config; |
| 128 | + int err; |
| 129 | + |
| 130 | + /* Route to the adi_tmc_uart.h implementation */ |
| 131 | + err = tmc_uart_write_register(config->bus.uart, config->uart_addr, reg_addr, reg_val); |
| 132 | + if (err < 0) { |
| 133 | + LOG_ERR("Failed to write register 0x%x with value 0x%x", reg_addr, reg_val); |
| 134 | + } |
| 135 | + |
| 136 | + /* Wait for the write to complete */ |
| 137 | + k_sleep(K_MSEC(1)); |
| 138 | + return err; |
| 139 | +} |
| 140 | + |
| 141 | +static int tmc51xx_reg_read_uart(const struct device *dev, const uint8_t reg_addr, |
| 142 | + uint32_t *reg_val) |
| 143 | +{ |
| 144 | + const struct tmc51xx_config *config = dev->config; |
| 145 | + int err; |
| 146 | + |
| 147 | + /* Route to the adi_tmc_uart.h implementation */ |
| 148 | + err = tmc_uart_read_register(config->bus.uart, config->uart_addr, reg_addr, reg_val); |
| 149 | + if (err < 0) { |
| 150 | + LOG_ERR("Failed to read register 0x%x", reg_addr); |
| 151 | + } |
| 152 | + |
| 153 | + /* Wait for the read to complete */ |
| 154 | + k_sleep(K_MSEC(1)); |
| 155 | + return err; |
| 156 | +} |
| 157 | + |
| 158 | +const struct tmc_bus_io tmc51xx_uart_bus_io = { |
| 159 | + .check = tmc51xx_bus_check_uart, |
| 160 | + .read = tmc51xx_reg_read_uart, |
| 161 | + .write = tmc51xx_reg_write_uart, |
| 162 | +}; |
| 163 | +#endif /* TMC51XX_BUS_UART */ |
| 164 | + |
18 | 165 | static inline int tmc51xx_bus_check(const struct device *dev) |
19 | 166 | { |
20 | 167 | const struct tmc51xx_config *config = dev->config; |
|
0 commit comments