![]() |
no-OS
|
Implementation of TMC5240 driver. More...
#include "no_os_alloc.h"#include "no_os_error.h"#include "no_os_spi.h"#include "no_os_util.h"#include "tmc/ic/TMC5240/TMC5240.h"#include "tmc5240.h"#include "no_os_print_log.h"Macros | |
| #define | TMC5240_MAX_DEVICES 4 |
Functions | |
| void | tmc5240_readWriteSPI (uint16_t icID, uint8_t *data, size_t dataLength) |
| Read/Write SPI function for TMC-API. | |
| bool | tmc5240_readWriteUART (uint16_t icID, uint8_t *data, size_t writeLength, size_t readLength) |
| Read/Write UART function for TMC-API (stub for SPI-only implementation) | |
| uint8_t | tmc5240_getNodeAddress (uint16_t icID) |
| Function that returns the node address of TMC IC. | |
| TMC5240BusType | tmc5240_getBusType (uint16_t icID) |
| Function that returns the type of bus used by the TMC IC. | |
| int | tmc5240_init (struct tmc5240_dev **device, struct tmc5240_init_param *init_param) |
| Initialize the TMC5240 device. | |
| int | tmc5240_remove (struct tmc5240_dev *device) |
| Free the resources allocated by the tmc5240_init() | |
| int | tmc5240_reg_read (struct tmc5240_dev *device, uint8_t reg_addr, uint32_t *reg_data) |
| Reads data from a given TMC5240 Register. | |
| int | tmc5240_reg_write (struct tmc5240_dev *device, uint8_t reg_addr, uint32_t reg_data) |
| Writes data to a given TMC5240 Register. | |
| int | tmc5240_reg_update (struct tmc5240_dev *device, uint8_t reg_addr, uint32_t mask, uint32_t val) |
| Read-modify-write a TMC5240 register. | |
| int | tmc5240_set_motion_profile (struct tmc5240_dev *device) |
| Set motion profile parameters for the TMC5240 motor driver. | |
| int | tmc5240_set_target_pos (struct tmc5240_dev *device, int32_t position) |
| Set the target position for the TMC5240 motor driver. | |
| int | tmc5240_get_target_pos (struct tmc5240_dev *device, int32_t *position) |
| Get the target position of the motor. | |
| int | tmc5240_set_current_pos (struct tmc5240_dev *device, int32_t position) |
| Replace the current position of the motor with a new absolute position value. | |
| int | tmc5240_get_current_pos (struct tmc5240_dev *device, int32_t *position) |
| Get the current position of the motor. | |
| int | tmc5240_get_current_accel (struct tmc5240_dev *device, int32_t *acceleration) |
| Get the current acceleration of the motor. | |
| int | tmc5240_set_target_vel (struct tmc5240_dev *device, int32_t velocity) |
| Set the target velocity for the TMC5240 motor driver. | |
| int | tmc5240_get_current_vel (struct tmc5240_dev *device, int32_t *velocity) |
| Get the current velocity of the motor. | |
| int | tmc5240_get_shaft_dir (struct tmc5240_dev *device, uint8_t *dir) |
| Get the shaft direction setting from the TMC5240 device. | |
| int | tmc5240_set_shaft_dir (struct tmc5240_dev *device, uint8_t dir) |
| Set the shaft direction for the TMC5240 device. | |
| int | tmc5240_get_amax (struct tmc5240_dev *device, int32_t *amax) |
| Get the maximum acceleration (AMAX) value. | |
| int | tmc5240_set_amax (struct tmc5240_dev *device, int32_t amax) |
| Set the maximum acceleration (AMAX) value. | |
| int | tmc5240_get_vmax (struct tmc5240_dev *device, int32_t *vmax) |
| Get the maximum velocity (VMAX) value. | |
| int | tmc5240_set_vmax (struct tmc5240_dev *device, int32_t vmax) |
| Set the maximum velocity (VMAX) value. | |
| int | tmc5240_get_dmax (struct tmc5240_dev *device, int32_t *dmax) |
| Get the maximum deceleration (DMAX) value. | |
| int | tmc5240_set_dmax (struct tmc5240_dev *device, int32_t dmax) |
| Set the maximum deceleration (DMAX) value. | |
| int | tmc5240_get_vstart (struct tmc5240_dev *device, int32_t *vstart) |
| Get the start velocity (VSTART) value. | |
| int | tmc5240_set_vstart (struct tmc5240_dev *device, int32_t vstart) |
| Set the start velocity (VSTART) value. | |
| int | tmc5240_get_a1 (struct tmc5240_dev *device, int32_t *a1) |
| Get the first acceleration phase (A1) value. | |
| int | tmc5240_set_a1 (struct tmc5240_dev *device, int32_t a1) |
| Set the first acceleration phase (A1) value. | |
| int | tmc5240_get_v1 (struct tmc5240_dev *device, int32_t *v1) |
| Get the first velocity threshold (V1) value. | |
| int | tmc5240_set_v1 (struct tmc5240_dev *device, int32_t v1) |
| Set the first velocity threshold (V1) value. | |
| int | tmc5240_get_a2 (struct tmc5240_dev *device, int32_t *a2) |
| Get the second acceleration phase (A2) value. | |
| int | tmc5240_set_a2 (struct tmc5240_dev *device, int32_t a2) |
| Set the second acceleration phase (A2) value. | |
| int | tmc5240_get_v2 (struct tmc5240_dev *device, int32_t *v2) |
| Get the second velocity threshold (V2) value. | |
| int | tmc5240_set_v2 (struct tmc5240_dev *device, int32_t v2) |
| Set the second velocity threshold (V2) value. | |
| int | tmc5240_get_d1 (struct tmc5240_dev *device, int32_t *d1) |
| Get the first deceleration phase (D1) value. | |
| int | tmc5240_set_d1 (struct tmc5240_dev *device, int32_t d1) |
| Set the first deceleration phase (D1) value. | |
| int | tmc5240_get_d2 (struct tmc5240_dev *device, int32_t *d2) |
| Get the second deceleration phase (D2) value. | |
| int | tmc5240_set_d2 (struct tmc5240_dev *device, int32_t d2) |
| Set the second deceleration phase (D2) value. | |
| int | tmc5240_get_vstop (struct tmc5240_dev *device, int32_t *vstop) |
| Get the stop velocity (VSTOP) value. | |
| int | tmc5240_set_vstop (struct tmc5240_dev *device, int32_t vstop) |
| Set the stop velocity (VSTOP) value. | |
| int | tmc5240_get_rampmode (struct tmc5240_dev *device, int32_t *rampmode) |
| Get the ramp mode configuration. | |
| int | tmc5240_set_rampmode (struct tmc5240_dev *device, int32_t rampmode) |
| Set the ramp mode configuration. | |
| int | tmc5240_stop (struct tmc5240_dev *device) |
| Stop the motor by setting VMAX to 0. | |
Implementation of TMC5240 driver.
Copyright 2026(c) Analog Devices, Inc.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES, INC. “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ANALOG DEVICES, INC. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
| #define TMC5240_MAX_DEVICES 4 |
| int tmc5240_get_a1 | ( | struct tmc5240_dev * | device, |
| int32_t * | a1 ) |
Get the first acceleration phase (A1) value.
| device | - Pointer to the TMC5240 device structure. |
| a1 | - Pointer to store the retrieved A1 value. |
| int tmc5240_get_a2 | ( | struct tmc5240_dev * | device, |
| int32_t * | a2 ) |
Get the second acceleration phase (A2) value.
| device | - Pointer to the TMC5240 device structure. |
| a2 | - Pointer to store the retrieved A2 value. |
| int tmc5240_get_amax | ( | struct tmc5240_dev * | device, |
| int32_t * | amax ) |
Get the maximum acceleration (AMAX) value.
| device | - Pointer to the TMC5240 device structure. |
| amax | - Pointer to store the retrieved AMAX value. |
| int tmc5240_get_current_accel | ( | struct tmc5240_dev * | device, |
| int32_t * | acceleration ) |
Get the current acceleration of the motor.
This function reads the actual acceleration value from the TMC5240 device.
| device | - Pointer to the TMC5240 device structure. |
| acceleration | - Pointer to store the read acceleration value. |
| int tmc5240_get_current_pos | ( | struct tmc5240_dev * | device, |
| int32_t * | position ) |
Get the current position of the motor.
This function reads the actual position register (XACTUAL) from the TMC5240 device and stores the result in the provided position pointer.
| device | - Pointer to the TMC5240 device structure. |
| position | - Pointer to store the current motor position value. |
| int tmc5240_get_current_vel | ( | struct tmc5240_dev * | device, |
| int32_t * | velocity ) |
Get the current velocity of the motor.
This function reads the actual velocity register (VACTUAL) from the TMC5240 device and returns the velocity value as a signed 24-bit integer converted to 32-bit format.
| device | - Pointer to the TMC5240 device structure. |
| velocity | - Pointer to store the current velocity value. The value is sign-extended from 24-bit to 32-bit. |
| int tmc5240_get_d1 | ( | struct tmc5240_dev * | device, |
| int32_t * | d1 ) |
Get the first deceleration phase (D1) value.
| device | - Pointer to the TMC5240 device structure. |
| d1 | - Pointer to store the retrieved D1 value. |
| int tmc5240_get_d2 | ( | struct tmc5240_dev * | device, |
| int32_t * | d2 ) |
Get the second deceleration phase (D2) value.
| device | - Pointer to the TMC5240 device structure. |
| d2 | - Pointer to store the retrieved D2 value. |
| int tmc5240_get_dmax | ( | struct tmc5240_dev * | device, |
| int32_t * | dmax ) |
Get the maximum deceleration (DMAX) value.
| device | - Pointer to the TMC5240 device structure. |
| dmax | - Pointer to store the retrieved DMAX value. |
| int tmc5240_get_rampmode | ( | struct tmc5240_dev * | device, |
| int32_t * | rampmode ) |
Get the ramp mode configuration.
| device | - Pointer to the TMC5240 device structure. |
| rampmode | - Pointer to store the retrieved ramp mode value. Possible values:
|
| int tmc5240_get_shaft_dir | ( | struct tmc5240_dev * | device, |
| uint8_t * | dir ) |
Get the shaft direction setting from the TMC5240 device.
This function reads the GCONF register and extracts the shaft direction bit. The shaft direction determines whether the motor rotates in normal or inverted direction.
| device | - Pointer to the TMC5240 device structure |
| dir | - Pointer to store the shaft direction value:
|
| int tmc5240_get_target_pos | ( | struct tmc5240_dev * | device, |
| int32_t * | position ) |
Get the target position of the motor.
This function reads the target position register (XTARGET) from the TMC5240 device and stores the result in the provided position pointer. The target position represents the desired destination when operating in position mode.
| device | - Pointer to the TMC5240 device structure. |
| position | - Pointer to store the target position value in microsteps. |
| int tmc5240_get_v1 | ( | struct tmc5240_dev * | device, |
| int32_t * | v1 ) |
Get the first velocity threshold (V1) value.
| device | - Pointer to the TMC5240 device structure. |
| v1 | - Pointer to store the retrieved V1 value. |
| int tmc5240_get_v2 | ( | struct tmc5240_dev * | device, |
| int32_t * | v2 ) |
Get the second velocity threshold (V2) value.
| device | - Pointer to the TMC5240 device structure. |
| v2 | - Pointer to store the retrieved V2 value. |
| int tmc5240_get_vmax | ( | struct tmc5240_dev * | device, |
| int32_t * | vmax ) |
Get the maximum velocity (VMAX) value.
| device | - Pointer to the TMC5240 device structure. |
| vmax | - Pointer to store the retrieved VMAX value. |
| int tmc5240_get_vstart | ( | struct tmc5240_dev * | device, |
| int32_t * | vstart ) |
Get the start velocity (VSTART) value.
| device | - Pointer to the TMC5240 device structure. |
| vstart | - Pointer to store the retrieved VSTART value. |
| int tmc5240_get_vstop | ( | struct tmc5240_dev * | device, |
| int32_t * | vstop ) |
Get the stop velocity (VSTOP) value.
| device | - Pointer to the TMC5240 device structure. |
| vstop | - Pointer to store the retrieved VSTOP value. |
| TMC5240BusType tmc5240_getBusType | ( | uint16_t | icID | ) |
Function that returns the type of bus used by the TMC IC.
This function returns the type of bus used by the TMC IC.
| icID | ID of the IC. |
| uint8_t tmc5240_getNodeAddress | ( | uint16_t | icID | ) |
Function that returns the node address of TMC IC.
This function returns the node address of TMC IC.
| icID | ID of the IC. |
| int tmc5240_init | ( | struct tmc5240_dev ** | device, |
| struct tmc5240_init_param * | init_param ) |
Initialize the TMC5240 device.
| device | - TMC5240 device descriptor |
| init_param | - Initialization parameter containing information about the TMC5240 device to be initialized. |
| void tmc5240_readWriteSPI | ( | uint16_t | icID, |
| uint8_t * | data, | ||
| size_t | dataLength ) |
Read/Write SPI function for TMC-API.
This function is used to read and write data to the TMC ICs via SPI.
| icID | ID of the IC. |
| data | Pointer to the data storage. |
| dataLength | Length of data to transfer. |
| bool tmc5240_readWriteUART | ( | uint16_t | icID, |
| uint8_t * | data, | ||
| size_t | writeLength, | ||
| size_t | readLength ) |
Read/Write UART function for TMC-API (stub for SPI-only implementation)
This function is required by the TMC-API interface but not used in this SPI-only implementation. It always returns false to indicate UART is not available.
| icID | ID of the IC. |
| data | Pointer to the data storage. |
| writeLength | Length of data to write. |
| readLength | Length of data to read. |
| int tmc5240_reg_read | ( | struct tmc5240_dev * | device, |
| uint8_t | reg_addr, | ||
| uint32_t * | reg_data ) |
Reads data from a given TMC5240 Register.
This function wraps the TMC-API readRegister function to read data from a specified register of the TMC5240 device.
| device | - The device structure. |
| reg_addr | - Address of the register. |
| reg_data | - Stores data read from the register. |
| int tmc5240_reg_update | ( | struct tmc5240_dev * | device, |
| uint8_t | reg_addr, | ||
| uint32_t | mask, | ||
| uint32_t | val ) |
Read-modify-write a TMC5240 register.
This function reads the current value of a register, clears the bits specified by the mask, sets new bits from val prepared with the same mask, and writes the result back.
| device | - Pointer to the TMC5240 device structure. |
| reg_addr | - Address of the register. |
| mask | - Bitmask of the field(s) to update. |
| val | - New value for the field, pre-shifted to the correct position. |
| int tmc5240_reg_write | ( | struct tmc5240_dev * | device, |
| uint8_t | reg_addr, | ||
| uint32_t | reg_data ) |
Writes data to a given TMC5240 Register.
This function wraps the TMC-API writeRegister function to write data to a specified register of the TMC5240 device.
| device | - The device structure. |
| reg_addr | - Address of the register to write to. |
| reg_data | - 32-bit data value to write to the register. |
| int tmc5240_remove | ( | struct tmc5240_dev * | device | ) |
Free the resources allocated by the tmc5240_init()
| device | - TMC5240 device descriptor |
| int tmc5240_set_a1 | ( | struct tmc5240_dev * | device, |
| int32_t | a1 ) |
Set the first acceleration phase (A1) value.
| device | - Pointer to the TMC5240 device structure. |
| a1 | - The A1 value to set. |
| int tmc5240_set_a2 | ( | struct tmc5240_dev * | device, |
| int32_t | a2 ) |
Set the second acceleration phase (A2) value.
| device | - Pointer to the TMC5240 device structure. |
| a2 | - The A2 value to set. |
| int tmc5240_set_amax | ( | struct tmc5240_dev * | device, |
| int32_t | amax ) |
Set the maximum acceleration (AMAX) value.
| device | - Pointer to the TMC5240 device structure. |
| amax | - The AMAX value to set. |
| int tmc5240_set_current_pos | ( | struct tmc5240_dev * | device, |
| int32_t | position ) |
Replace the current position of the motor with a new absolute position value.
This function sets the motor's current position register (XACTUAL) to a specified value. Before updating the position, it switches the motor to hold mode (RAMPMODE = MODE_HOLD) to ensure the position can be safely updated without triggering unwanted motion. This is useful for rehoming or synchronizing the motor's internal position counter with an external reference system.
| device | - Pointer to the TMC5240 device structure. |
| position | - The new position value to set in microsteps. |
| int tmc5240_set_d1 | ( | struct tmc5240_dev * | device, |
| int32_t | d1 ) |
Set the first deceleration phase (D1) value.
| device | - Pointer to the TMC5240 device structure. |
| d1 | - The D1 value to set. |
| int tmc5240_set_d2 | ( | struct tmc5240_dev * | device, |
| int32_t | d2 ) |
Set the second deceleration phase (D2) value.
| device | - Pointer to the TMC5240 device structure. |
| d2 | - The D2 value to set. |
| int tmc5240_set_dmax | ( | struct tmc5240_dev * | device, |
| int32_t | dmax ) |
Set the maximum deceleration (DMAX) value.
| device | - Pointer to the TMC5240 device structure. |
| dmax | - The DMAX value to set. |
| int tmc5240_set_motion_profile | ( | struct tmc5240_dev * | device | ) |
Set motion profile parameters for the TMC5240 motor driver.
This function configures all motion profile parameters including multi-phase acceleration and deceleration settings. It applies default values for d1, d2, and vstop if they are zero, and ensures vstop is not less than vstart.
| device | - Pointer to the TMC5240 device structure. |
| int tmc5240_set_rampmode | ( | struct tmc5240_dev * | device, |
| int32_t | rampmode ) |
Set the ramp mode configuration.
| device | - Pointer to the TMC5240 device structure. |
| rampmode | - The ramp mode value to set. Possible values:
|
| int tmc5240_set_shaft_dir | ( | struct tmc5240_dev * | device, |
| uint8_t | dir ) |
Set the shaft direction for the TMC5240 device.
This function modifies the GCONF register to set the shaft direction bit. Setting the shaft direction allows reversing the motor rotation direction without changing the wiring.
| device | - Pointer to the TMC5240 device structure |
| dir | - Shaft direction value to set:
|
| int tmc5240_set_target_pos | ( | struct tmc5240_dev * | device, |
| int32_t | position ) |
Set the target position for the TMC5240 motor driver.
This function configures the TMC5240 to move to a specified target position in position mode. It ensures that the acceleration (AMAX) and maximum velocity (VMAX) parameters are properly configured before setting the target. If either AMAX or VMAX is zero, the function restores them to the device's stored default values to enable motion. The motor will ramp up to VMAX, move toward the target, and decelerate according to the configured motion profile.
| device | - Pointer to the TMC5240 device structure. |
| position | - Target position value to move to in microsteps. |
| int tmc5240_set_target_vel | ( | struct tmc5240_dev * | device, |
| int32_t | velocity ) |
Set the target velocity for the TMC5240 motor driver.
This function sets the target velocity for the motor by configuring the VMAX register and the appropriate ramp mode (positive or negative velocity). If the maximum acceleration (AMAX) is zero, it will be set to the device's default value before setting the velocity.
| device | - Pointer to the TMC5240 device structure. |
| velocity | - Target velocity value. Positive values result in positive velocity ramp mode, negative values result in negative velocity ramp mode. The absolute value is written to VMAX. |
| int tmc5240_set_v1 | ( | struct tmc5240_dev * | device, |
| int32_t | v1 ) |
Set the first velocity threshold (V1) value.
| device | - Pointer to the TMC5240 device structure. |
| v1 | - The V1 value to set. |
| int tmc5240_set_v2 | ( | struct tmc5240_dev * | device, |
| int32_t | v2 ) |
Set the second velocity threshold (V2) value.
| device | - Pointer to the TMC5240 device structure. |
| v2 | - The V2 value to set. |
| int tmc5240_set_vmax | ( | struct tmc5240_dev * | device, |
| int32_t | vmax ) |
Set the maximum velocity (VMAX) value.
| device | - Pointer to the TMC5240 device structure. |
| vmax | - The VMAX value to set. |
| int tmc5240_set_vstart | ( | struct tmc5240_dev * | device, |
| int32_t | vstart ) |
Set the start velocity (VSTART) value.
| device | - Pointer to the TMC5240 device structure. |
| vstart | - The VSTART value to set. |
| int tmc5240_set_vstop | ( | struct tmc5240_dev * | device, |
| int32_t | vstop ) |
Set the stop velocity (VSTOP) value.
| device | - Pointer to the TMC5240 device structure. |
| vstop | - The VSTOP value to set. |
| int tmc5240_stop | ( | struct tmc5240_dev * | device | ) |
Stop the motor by setting VMAX to 0.
| device | - Pointer to the TMC5240 device structure. |