PyTmcStepper 0.9.2__tar.gz → 0.10.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. {pytmcstepper-0.9.2 → pytmcstepper-0.10.0}/PKG-INFO +1 -1
  2. {pytmcstepper-0.9.2 → pytmcstepper-0.10.0}/pyproject.toml +1 -1
  3. {pytmcstepper-0.9.2 → pytmcstepper-0.10.0}/src/PyTmcStepper.egg-info/PKG-INFO +1 -1
  4. {pytmcstepper-0.9.2 → pytmcstepper-0.10.0}/src/PyTmcStepper.egg-info/SOURCES.txt +4 -1
  5. pytmcstepper-0.10.0/src/tmc_driver/_tmc_stallguard.py +248 -0
  6. {pytmcstepper-0.9.2 → pytmcstepper-0.10.0}/src/tmc_driver/_tmc_stepperdriver.py +13 -24
  7. {pytmcstepper-0.9.2 → pytmcstepper-0.10.0}/src/tmc_driver/com/_tmc_com.py +40 -28
  8. {pytmcstepper-0.9.2 → pytmcstepper-0.10.0}/src/tmc_driver/com/_tmc_com_spi.py +1 -3
  9. {pytmcstepper-0.9.2 → pytmcstepper-0.10.0}/src/tmc_driver/com/_tmc_com_spi_base.py +25 -15
  10. {pytmcstepper-0.9.2 → pytmcstepper-0.10.0}/src/tmc_driver/com/_tmc_com_spi_ftdi.py +2 -3
  11. {pytmcstepper-0.9.2 → pytmcstepper-0.10.0}/src/tmc_driver/com/_tmc_com_spi_micropython.py +6 -4
  12. {pytmcstepper-0.9.2 → pytmcstepper-0.10.0}/src/tmc_driver/com/_tmc_com_uart.py +10 -10
  13. {pytmcstepper-0.9.2 → pytmcstepper-0.10.0}/src/tmc_driver/com/_tmc_com_uart_base.py +48 -27
  14. {pytmcstepper-0.9.2 → pytmcstepper-0.10.0}/src/tmc_driver/com/_tmc_com_uart_micropython.py +9 -5
  15. pytmcstepper-0.10.0/src/tmc_driver/enable_control/_tmc_ec.py +57 -0
  16. {pytmcstepper-0.9.2 → pytmcstepper-0.10.0}/src/tmc_driver/enable_control/_tmc_ec_pin.py +1 -0
  17. {pytmcstepper-0.9.2 → pytmcstepper-0.10.0}/src/tmc_driver/enable_control/_tmc_ec_toff.py +6 -1
  18. {pytmcstepper-0.9.2 → pytmcstepper-0.10.0}/src/tmc_driver/motion_control/_tmc_mc.py +27 -0
  19. {pytmcstepper-0.9.2 → pytmcstepper-0.10.0}/src/tmc_driver/motion_control/_tmc_mc_step_dir.py +1 -4
  20. {pytmcstepper-0.9.2 → pytmcstepper-0.10.0}/src/tmc_driver/motion_control/_tmc_mc_step_reg.py +7 -16
  21. {pytmcstepper-0.9.2 → pytmcstepper-0.10.0}/src/tmc_driver/motion_control/_tmc_mc_vactual.py +3 -2
  22. pytmcstepper-0.10.0/src/tmc_driver/reg/_tmc2209_reg.py +55 -0
  23. pytmcstepper-0.10.0/src/tmc_driver/reg/_tmc220x_reg.py +259 -0
  24. pytmcstepper-0.10.0/src/tmc_driver/reg/_tmc224x_reg.py +403 -0
  25. pytmcstepper-0.10.0/src/tmc_driver/reg/_tmc_reg.py +164 -0
  26. pytmcstepper-0.10.0/src/tmc_driver/reg/_tmc_shared_regs.py +54 -0
  27. {pytmcstepper-0.9.2 → pytmcstepper-0.10.0}/src/tmc_driver/tmc_2208.py +2 -1
  28. pytmcstepper-0.10.0/src/tmc_driver/tmc_2209.py +139 -0
  29. {pytmcstepper-0.9.2 → pytmcstepper-0.10.0}/src/tmc_driver/tmc_220x.py +31 -139
  30. {pytmcstepper-0.9.2 → pytmcstepper-0.10.0}/src/tmc_driver/tmc_2240.py +28 -146
  31. {pytmcstepper-0.9.2 → pytmcstepper-0.10.0}/src/tmc_driver/tmc_gpio/_tmc_gpio_board_rpi_gpio.py +3 -0
  32. pytmcstepper-0.10.0/src/tmc_driver/tmc_xxxx.py +230 -0
  33. {pytmcstepper-0.9.2 → pytmcstepper-0.10.0}/tests/test_tmc2209.py +0 -1
  34. {pytmcstepper-0.9.2 → pytmcstepper-0.10.0}/tests/test_tmc2240.py +0 -1
  35. {pytmcstepper-0.9.2 → pytmcstepper-0.10.0}/tests/test_tmc_com_spi.py +1 -1
  36. {pytmcstepper-0.9.2 → pytmcstepper-0.10.0}/tests/test_tmc_com_uart.py +1 -1
  37. pytmcstepper-0.10.0/tests/test_tmc_modules.py +175 -0
  38. {pytmcstepper-0.9.2 → pytmcstepper-0.10.0}/tests/test_tmc_move.py +0 -1
  39. pytmcstepper-0.10.0/tests/test_tmc_stallguard.py +112 -0
  40. pytmcstepper-0.9.2/src/tmc_driver/_tmc_stallguard.py +0 -157
  41. pytmcstepper-0.9.2/src/tmc_driver/enable_control/_tmc_ec.py +0 -29
  42. pytmcstepper-0.9.2/src/tmc_driver/reg/_tmc2209_reg.py +0 -66
  43. pytmcstepper-0.9.2/src/tmc_driver/reg/_tmc220x_reg.py +0 -287
  44. pytmcstepper-0.9.2/src/tmc_driver/reg/_tmc224x_reg.py +0 -464
  45. pytmcstepper-0.9.2/src/tmc_driver/reg/_tmc_reg.py +0 -120
  46. pytmcstepper-0.9.2/src/tmc_driver/tmc_2209.py +0 -276
  47. pytmcstepper-0.9.2/tests/test_tmc_modules.py +0 -64
  48. {pytmcstepper-0.9.2 → pytmcstepper-0.10.0}/LICENSE +0 -0
  49. {pytmcstepper-0.9.2 → pytmcstepper-0.10.0}/README.md +0 -0
  50. {pytmcstepper-0.9.2 → pytmcstepper-0.10.0}/setup.cfg +0 -0
  51. {pytmcstepper-0.9.2 → pytmcstepper-0.10.0}/src/PyTmcStepper.egg-info/dependency_links.txt +0 -0
  52. {pytmcstepper-0.9.2 → pytmcstepper-0.10.0}/src/PyTmcStepper.egg-info/requires.txt +0 -0
  53. {pytmcstepper-0.9.2 → pytmcstepper-0.10.0}/src/PyTmcStepper.egg-info/top_level.txt +0 -0
  54. {pytmcstepper-0.9.2 → pytmcstepper-0.10.0}/src/tmc_driver/__init__.py +0 -0
  55. {pytmcstepper-0.9.2 → pytmcstepper-0.10.0}/src/tmc_driver/_tmc_exceptions.py +0 -0
  56. {pytmcstepper-0.9.2 → pytmcstepper-0.10.0}/src/tmc_driver/_tmc_logger.py +0 -0
  57. {pytmcstepper-0.9.2 → pytmcstepper-0.10.0}/src/tmc_driver/_tmc_math.py +0 -0
  58. {pytmcstepper-0.9.2 → pytmcstepper-0.10.0}/src/tmc_driver/_tmc_validation.py +0 -0
  59. {pytmcstepper-0.9.2 → pytmcstepper-0.10.0}/src/tmc_driver/com/__init__.py +0 -0
  60. {pytmcstepper-0.9.2 → pytmcstepper-0.10.0}/src/tmc_driver/enable_control/__init__.py +0 -0
  61. {pytmcstepper-0.9.2 → pytmcstepper-0.10.0}/src/tmc_driver/motion_control/__init__.py +0 -0
  62. {pytmcstepper-0.9.2 → pytmcstepper-0.10.0}/src/tmc_driver/motion_control/_tmc_mc_step_pwm_dir.py +0 -0
  63. {pytmcstepper-0.9.2 → pytmcstepper-0.10.0}/src/tmc_driver/reg/__init__.py +0 -0
  64. {pytmcstepper-0.9.2 → pytmcstepper-0.10.0}/src/tmc_driver/tmc_gpio/__init__.py +0 -0
  65. {pytmcstepper-0.9.2 → pytmcstepper-0.10.0}/src/tmc_driver/tmc_gpio/_tmc_gpio_board_base.py +0 -0
  66. {pytmcstepper-0.9.2 → pytmcstepper-0.10.0}/src/tmc_driver/tmc_gpio/_tmc_gpio_board_ftdi.py +0 -0
  67. {pytmcstepper-0.9.2 → pytmcstepper-0.10.0}/src/tmc_driver/tmc_gpio/_tmc_gpio_board_gpiozero.py +0 -0
  68. {pytmcstepper-0.9.2 → pytmcstepper-0.10.0}/src/tmc_driver/tmc_gpio/_tmc_gpio_board_micropython.py +0 -0
  69. {pytmcstepper-0.9.2 → pytmcstepper-0.10.0}/src/tmc_driver/tmc_gpio/_tmc_gpio_board_periphery.py +0 -0
  70. {pytmcstepper-0.9.2 → pytmcstepper-0.10.0}/tests/test_tmc_ec_pin.py +0 -0
  71. {pytmcstepper-0.9.2 → pytmcstepper-0.10.0}/tests/test_tmc_math.py +0 -0
  72. {pytmcstepper-0.9.2 → pytmcstepper-0.10.0}/tests/test_tmc_set_current.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: PyTmcStepper
3
- Version: 0.9.2
3
+ Version: 0.10.0
4
4
  Summary: This is a Python libary to drive a stepper motor with a Trinamic stepper driver and a single board computer like a Raspberry Pi
5
5
  Author-email: Christian Köhlke <christian@koehlke.de>
6
6
  License-Expression: GPL-3.0
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "PyTmcStepper"
7
- version = "0.9.2"
7
+ version = "0.10.0"
8
8
  authors = [{ name = "Christian Köhlke", email = "christian@koehlke.de" }]
9
9
  description = "This is a Python libary to drive a stepper motor with a Trinamic stepper driver and a single board computer like a Raspberry Pi"
10
10
  readme = "README.md"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: PyTmcStepper
3
- Version: 0.9.2
3
+ Version: 0.10.0
4
4
  Summary: This is a Python libary to drive a stepper motor with a Trinamic stepper driver and a single board computer like a Raspberry Pi
5
5
  Author-email: Christian Köhlke <christian@koehlke.de>
6
6
  License-Expression: GPL-3.0
@@ -17,6 +17,7 @@ src/tmc_driver/tmc_2208.py
17
17
  src/tmc_driver/tmc_2209.py
18
18
  src/tmc_driver/tmc_220x.py
19
19
  src/tmc_driver/tmc_2240.py
20
+ src/tmc_driver/tmc_xxxx.py
20
21
  src/tmc_driver/com/__init__.py
21
22
  src/tmc_driver/com/_tmc_com.py
22
23
  src/tmc_driver/com/_tmc_com_spi.py
@@ -41,6 +42,7 @@ src/tmc_driver/reg/_tmc2209_reg.py
41
42
  src/tmc_driver/reg/_tmc220x_reg.py
42
43
  src/tmc_driver/reg/_tmc224x_reg.py
43
44
  src/tmc_driver/reg/_tmc_reg.py
45
+ src/tmc_driver/reg/_tmc_shared_regs.py
44
46
  src/tmc_driver/tmc_gpio/__init__.py
45
47
  src/tmc_driver/tmc_gpio/_tmc_gpio_board_base.py
46
48
  src/tmc_driver/tmc_gpio/_tmc_gpio_board_ftdi.py
@@ -56,4 +58,5 @@ tests/test_tmc_ec_pin.py
56
58
  tests/test_tmc_math.py
57
59
  tests/test_tmc_modules.py
58
60
  tests/test_tmc_move.py
59
- tests/test_tmc_set_current.py
61
+ tests/test_tmc_set_current.py
62
+ tests/test_tmc_stallguard.py
@@ -0,0 +1,248 @@
1
+ # pylint: disable=too-many-arguments
2
+ # pylint: disable=too-many-public-methods
3
+ # pylint: disable=too-many-branches
4
+ # pylint: disable=too-many-instance-attributes
5
+ # pylint: disable=too-many-positional-arguments
6
+ # pylint: disable=bare-except
7
+ # pylint: disable=no-member
8
+ # pylint: disable=unused-import
9
+ # pylint: disable=wildcard-import
10
+ # pylint: disable=unused-wildcard-import
11
+ """StallGuard mixin"""
12
+
13
+ import types
14
+ from ._tmc_stepperdriver import *
15
+ from .com._tmc_com import TmcCom
16
+ from .tmc_gpio import GpioPUD, GpioMode
17
+ from . import tmc_gpio
18
+ from ._tmc_logger import Loglevel
19
+ from .reg import _tmc_shared_regs as tmc_shared_regs
20
+ from . import _tmc_math as tmc_math
21
+ from ._tmc_exceptions import (
22
+ TmcComException,
23
+ TmcMotionControlException,
24
+ TmcDriverException,
25
+ )
26
+
27
+ _CB_SENTINEL = object()
28
+
29
+
30
+ class StallGuard:
31
+ """StallGuard
32
+
33
+ This class is used to control the stallguard feature of the TMC stepper driver.
34
+ The drivers class needs to inherit from this class to use the stallguard feature (mixin).
35
+ """
36
+
37
+ tmc_logger: TmcLogger
38
+ tmc_com: TmcCom | None
39
+ tmc_mc: TmcMotionControl
40
+
41
+ coolconf: tmc_shared_regs.CoolConf
42
+ sgthrs: tmc_shared_regs.SGThrs
43
+ sgresult: tmc_shared_regs.SGResult
44
+ tcoolthrs: tmc_shared_regs.TCoolThrs
45
+
46
+ @property
47
+ def sg_callback(self):
48
+ """stallguard callback function"""
49
+ return self._sg_callback
50
+
51
+ @sg_callback.setter
52
+ def sg_callback(self, callback):
53
+ if self._pin_stallguard is None:
54
+ raise TmcDriverException(
55
+ "StallGuard pin not set. Cannot set callback function."
56
+ )
57
+ self._sg_callback = callback
58
+ tmc_gpio.tmc_gpio.gpio_remove_event_detect(self._pin_stallguard)
59
+ if callback is not None:
60
+ tmc_gpio.tmc_gpio.gpio_add_event_detect(self._pin_stallguard, callback)
61
+
62
+ def __init__(self):
63
+ """initialize StallGuard instance variables"""
64
+ self._pin_stallguard: int | None = None
65
+ self._sg_threshold: int = 100 # threshold for stallguard
66
+
67
+ def __del__(self):
68
+ self.deinit()
69
+
70
+ def deinit(self):
71
+ """destructor"""
72
+ if (
73
+ getattr(self, "_pin_stallguard", None) is not None
74
+ and self._pin_stallguard is not None
75
+ ):
76
+ tmc_gpio.tmc_gpio.gpio_remove_event_detect(self._pin_stallguard)
77
+ tmc_gpio.tmc_gpio.gpio_cleanup(self._pin_stallguard)
78
+ self._pin_stallguard = None
79
+
80
+ def set_stallguard_callback(
81
+ self, pin_stallguard, threshold, callback, min_speed=100
82
+ ):
83
+ """set a function to call back, when the driver detects a stall
84
+ via stallguard
85
+ high value on the diag pin can also mean a driver error
86
+
87
+ Args:
88
+ pin_stallguard (int): pin needs to be connected to DIAG
89
+ threshold (int): value for SGTHRS
90
+ callback (func): will be called on StallGuard trigger
91
+ min_speed (int): min speed [steps/s] for StallGuard (Default value = 100)
92
+ """
93
+ self.tmc_logger.log(
94
+ f"setup stallguard callback on GPIO {pin_stallguard}", Loglevel.INFO
95
+ )
96
+ self.tmc_logger.log(
97
+ f"StallGuard Threshold: {threshold} | minimum Speed: {min_speed}",
98
+ Loglevel.INFO,
99
+ )
100
+
101
+ self._set_stallguard_threshold(threshold)
102
+ self._set_coolstep_threshold(
103
+ tmc_math.steps_to_tstep(min_speed, self.get_microstepping_resolution())
104
+ )
105
+ self._sg_callback = callback
106
+ self._pin_stallguard = pin_stallguard
107
+
108
+ if self._pin_stallguard is not None:
109
+ tmc_gpio.tmc_gpio.gpio_setup(
110
+ self._pin_stallguard, GpioMode.IN, pull_up_down=GpioPUD.PUD_DOWN
111
+ )
112
+ # first remove existing events
113
+ tmc_gpio.tmc_gpio.gpio_remove_event_detect(self._pin_stallguard)
114
+ if callback is not None:
115
+ tmc_gpio.tmc_gpio.gpio_add_event_detect(self._pin_stallguard, callback)
116
+
117
+ def enable_coolstep(
118
+ self,
119
+ semin_sg: int = 150,
120
+ semax_sg: int = 200,
121
+ seup: int = 1,
122
+ sedn: int = 3,
123
+ min_speed: int = 100,
124
+ ):
125
+ """enables coolstep and sets the parameters for coolstep
126
+ The values for semin etc. can be tested with the test_stallguard_threshold function
127
+
128
+ Args:
129
+ semin_sg (int): lower threshold. Current will be increased if SG_Result goes below this
130
+ semax_sg (int): upper threshold. Current will be decreased if SG_Result goes above this
131
+ seup (int): current increment step
132
+ sedn (int): number of SG_Result readings for each current decrement
133
+ """
134
+ semax_sg = semax_sg - semin_sg
135
+
136
+ self.coolconf.read()
137
+ self.coolconf.semin = round(max(0, min(semin_sg / 32, 15)))
138
+ self.coolconf.semax = round(max(0, min(semax_sg / 32, 15)))
139
+ self.coolconf.seimin = True # scale down to until 1/4 of IRun (7 - 31)
140
+ self.coolconf.seup = int(seup)
141
+ self.coolconf.sedn = int(sedn)
142
+ self.coolconf.write_check()
143
+
144
+ self._set_coolstep_threshold(
145
+ tmc_math.steps_to_tstep(int(min_speed), self.get_microstepping_resolution())
146
+ )
147
+
148
+ def get_stallguard_result(self):
149
+ """return the current stallguard result
150
+ its will be calculated with every fullstep
151
+ higher values means a lower motor load
152
+
153
+ Returns:
154
+ sgresult (int): StallGuard Result
155
+ """
156
+ self.sgresult.read()
157
+ return self.sgresult.sgresult
158
+
159
+ def _set_stallguard_threshold(self, threshold):
160
+ """sets the register bit "SGTHRS" to to a given value
161
+ this is needed for the stallguard interrupt callback
162
+ SGRESULT becomes compared to the double of this threshold.
163
+ SGRESULT ≤ SGTHRS*2
164
+
165
+ Args:
166
+ threshold (int): value for SGTHRS
167
+ """
168
+ self.sgthrs.modify("sgthrs", int(threshold))
169
+
170
+ def _set_coolstep_threshold(self, threshold):
171
+ """This is the lower threshold velocity for switching
172
+ on smart energy CoolStep and StallGuard to DIAG output. (unsigned)
173
+
174
+ Args:
175
+ threshold (int): threshold velocity for coolstep
176
+ """
177
+ self.tcoolthrs.modify("tcoolthrs", int(threshold))
178
+
179
+ def _reset_current_pos(self):
180
+ """resets the current position of the motor to 0"""
181
+ if self.tmc_mc is None:
182
+ raise TmcMotionControlException("tmc_mc is None; cannot reset current pos")
183
+ self.tmc_mc.current_pos = 0
184
+
185
+ def do_homing(
186
+ self,
187
+ diag_pin: int,
188
+ revolutions=10,
189
+ threshold=100,
190
+ max_speed: int | None = None,
191
+ cb_success: types.FunctionType | object | None = _CB_SENTINEL,
192
+ cb_failure: types.FunctionType | object | None = None,
193
+ ) -> bool:
194
+ """homes the motor in the given direction using stallguard.
195
+ this method is using vactual to move the motor and an interrupt on the DIAG pin
196
+
197
+ Args:
198
+ diag_pin (int): DIAG pin number
199
+ revolutions (int): max number of revolutions. Can be negative for inverse direction
200
+ (Default value = 10)
201
+ threshold (int): StallGuard detection threshold (Default value = 100)
202
+ max_speed (int): max speed for homing in steps/s (Default value = None)
203
+ cb_success (func|None): callback function on successful homing
204
+ cb_failure (func|None): callback function on failed homing
205
+
206
+ Returns:
207
+ not homing_failed (bool): true when homing was successful
208
+ """
209
+ if self.tmc_com is None:
210
+ raise TmcComException("tmc_com is None; cannot do homing")
211
+ if self.tmc_mc is None:
212
+ raise TmcMotionControlException("tmc_mc is None; cannot do homing")
213
+
214
+ self.tmc_logger.log(f"Stallguard threshold: {threshold}", Loglevel.DEBUG)
215
+
216
+ if max_speed is None:
217
+ max_speed = self.tmc_mc.max_speed_homing
218
+ if cb_success is _CB_SENTINEL:
219
+ cb_success = self._reset_current_pos
220
+
221
+ self.tmc_logger.log("---", Loglevel.INFO)
222
+ self.tmc_logger.log("homing", Loglevel.INFO)
223
+
224
+ self.set_spreadcycle(0)
225
+
226
+ self.set_stallguard_callback(
227
+ diag_pin,
228
+ threshold,
229
+ lambda _: self.tmc_mc.stop(),
230
+ round(0.5 * max_speed),
231
+ )
232
+
233
+ stop_mode = self.tmc_mc.run_to_position_steps(
234
+ revolutions * self.tmc_mc.steps_per_rev, MovementAbsRel.RELATIVE
235
+ )
236
+
237
+ homing_succeeded = stop_mode is StopMode.HARDSTOP
238
+
239
+ if homing_succeeded:
240
+ self.tmc_logger.log("homing successful", Loglevel.INFO)
241
+ if cb_success is not None:
242
+ cb_success()
243
+ else:
244
+ self.tmc_logger.log("homing failed", Loglevel.INFO)
245
+ if cb_failure is not None:
246
+ cb_failure()
247
+
248
+ return homing_succeeded
@@ -68,12 +68,10 @@ class TmcStepperDriver:
68
68
  '%(asctime)s - %(name)s - %(levelname)s - %(message)s').
69
69
  """
70
70
  self.BOARD: Board = tmc_gpio.BOARD
71
- self.tmc_mc: TmcMotionControl | None = None
72
- self.tmc_ec: TmcEnableControl | None = None
71
+ self.tmc_ec = tmc_ec
72
+ self.tmc_mc = tmc_mc
73
73
  self.tmc_logger: TmcLogger
74
74
 
75
- self._deinit_finished: bool = False
76
-
77
75
  if logprefix is None:
78
76
  logprefix = "StepperDriver"
79
77
  self.tmc_logger = TmcLogger(loglevel, logprefix, log_handlers, log_formatter)
@@ -82,38 +80,29 @@ class TmcStepperDriver:
82
80
 
83
81
  tmc_gpio.tmc_gpio.init(gpio_mode)
84
82
 
85
- if tmc_mc is not None:
86
- self.tmc_mc = tmc_mc
83
+ if self.tmc_mc is not None:
87
84
  self.tmc_mc.init(self.tmc_logger)
88
85
 
89
- if tmc_ec is not None:
90
- self.tmc_ec = tmc_ec
86
+ if self.tmc_ec is not None:
91
87
  self.tmc_ec.init(self.tmc_logger)
92
88
 
93
- self.tmc_logger.log("GPIO Init finished", Loglevel.INFO)
94
-
95
- self.tmc_logger.log("Init finished", Loglevel.INFO)
96
-
97
89
  def __del__(self):
98
90
  self.deinit()
99
91
 
100
92
  def deinit(self):
101
93
  """destructor"""
102
- if self._deinit_finished is False:
103
- self.tmc_logger.log("Deinit", Loglevel.INFO)
104
-
105
- self.set_motor_enabled(False)
106
-
107
- self.tmc_logger.log("Deinit finished", Loglevel.INFO)
108
- self._deinit_finished = True
109
- else:
110
- self.tmc_logger.log("Deinit already finished", Loglevel.INFO)
111
- if self.tmc_ec is not None:
94
+ if getattr(self, "tmc_ec", None) is not None and self.tmc_ec is not None:
112
95
  self.tmc_ec.deinit()
113
- if self.tmc_mc is not None:
96
+ self.tmc_ec = None
97
+ if getattr(self, "tmc_mc", None) is not None and self.tmc_mc is not None:
114
98
  self.tmc_mc.deinit()
115
- if self.tmc_logger is not None:
99
+ self.tmc_mc = None
100
+ if (
101
+ getattr(self, "tmc_logger", None) is not None
102
+ and self.tmc_logger is not None
103
+ ):
116
104
  self.tmc_logger.deinit()
105
+ self.tmc_logger = None
117
106
 
118
107
  # TmcEnableControl Wrapper
119
108
  # ----------------------------
@@ -8,6 +8,7 @@ import struct
8
8
  from abc import abstractmethod
9
9
  from .._tmc_logger import TmcLogger, Loglevel
10
10
  from .._tmc_exceptions import TmcComException
11
+ from ..reg._tmc_reg import TmcReg
11
12
 
12
13
 
13
14
  def compute_crc8_atm(datagram, initial_value=0):
@@ -31,17 +32,6 @@ def compute_crc8_atm(datagram, initial_value=0):
31
32
  return crc
32
33
 
33
34
 
34
- class IfcntRegister:
35
- """Interface for IFCNT register - defines only what tmc_com needs to know"""
36
-
37
- # pylint: disable=too-few-public-methods
38
-
39
- ifcnt: int
40
-
41
- def read(self):
42
- """Read the register"""
43
-
44
-
45
35
  class TmcCom:
46
36
  """TmcCom"""
47
37
 
@@ -55,17 +45,22 @@ class TmcCom:
55
45
  """set the tmc_logger"""
56
46
  self._tmc_logger = tmc_logger
57
47
 
58
- def __init__(self, driver_address: int = 0):
59
- """constructor
48
+ @property
49
+ def driver_address(self) -> int:
50
+ """get the driver address"""
51
+ return self._driver_address
60
52
 
61
- Args:
62
- driver_address (int, optional): driver address. Defaults to 0.
63
- """
53
+ @driver_address.setter
54
+ def driver_address(self, address: int):
55
+ """set the driver address"""
56
+ self._driver_address = address
57
+
58
+ def __init__(self):
59
+ """constructor"""
64
60
  self._tmc_logger: TmcLogger
65
- self.driver_address = driver_address
66
- self.ifcnt: IfcntRegister | None = None
61
+ self._driver_address: int = 0
67
62
  self.communication_pause: int = 0
68
- self.error_handler_running: bool = False
63
+ self._get_register_callback = None
69
64
 
70
65
  @abstractmethod
71
66
  def init(self):
@@ -75,8 +70,29 @@ class TmcCom:
75
70
  def deinit(self):
76
71
  """deinit communication"""
77
72
 
73
+ def set_get_register_callback(self, callback):
74
+ """Set callback to get registers from parent TMC class
75
+
76
+ Args:
77
+ callback: Function that takes register name (str) and returns register object
78
+ """
79
+ self._get_register_callback = callback
80
+
81
+ def get_register(self, name: str) -> TmcReg | None:
82
+ """Get register by name from parent TMC class
83
+
84
+ Args:
85
+ name: Register name (e.g. 'gconf', 'chopconf')
86
+
87
+ Returns:
88
+ Register object or None if callback not set
89
+ """
90
+ if self._get_register_callback is not None:
91
+ return self._get_register_callback(name)
92
+ return None
93
+
78
94
  @abstractmethod
79
- def read_reg(self, addr: int) -> tuple[int, dict]:
95
+ def read_reg(self, addr: int) -> tuple[int, dict | None]:
80
96
  """reads the registry on the TMC with a given address.
81
97
  returns the binary value of that register
82
98
 
@@ -88,7 +104,7 @@ class TmcCom:
88
104
  """
89
105
 
90
106
  @abstractmethod
91
- def read_int(self, addr: int, tries: int = 10) -> tuple[int, dict]:
107
+ def read_int(self, addr: int, tries: int = 10) -> tuple[int, dict | None]:
92
108
  """this function tries to read the registry of the TMC 10 times
93
109
  if a valid answer is returned, this function returns it as an integer
94
110
 
@@ -125,13 +141,9 @@ class TmcCom:
125
141
  """
126
142
 
127
143
  @abstractmethod
128
- def flush_serial_buffer(self):
144
+ def flush_com_buffer(self):
129
145
  """this function clear the communication buffers of the Raspberry Pi"""
130
146
 
131
147
  @abstractmethod
132
- def test_com(self, addr):
133
- """test com connection
134
-
135
- Args:
136
- addr (int): HEX, which register to test
137
- """
148
+ def test_com(self):
149
+ """test com connection"""
@@ -25,7 +25,6 @@ class TmcComSpi(TmcComSpiBase):
25
25
  spi_bus: int,
26
26
  spi_dev: int,
27
27
  spi_speed: int = 8000000,
28
- driver_address: int = 0,
29
28
  ):
30
29
  """constructor
31
30
 
@@ -33,9 +32,8 @@ class TmcComSpi(TmcComSpiBase):
33
32
  spi_bus (int): SPI bus number
34
33
  spi_dev (int): SPI device number
35
34
  spi_speed (int, optional): SPI speed in Hz. Defaults to 8000000.
36
- driver_address (int, optional): driver address. Defaults to 0.
37
35
  """
38
- super().__init__(driver_address)
36
+ super().__init__()
39
37
 
40
38
  self.spi = spidev.SpiDev()
41
39
 
@@ -11,6 +11,14 @@ from ._tmc_com import *
11
11
  from .._tmc_exceptions import TmcComException, TmcDriverException
12
12
 
13
13
 
14
+ class IoinStub(TmcReg):
15
+ """IOIN Register Stub"""
16
+
17
+ # pylint: disable=too-few-public-methods
18
+
19
+ version: int
20
+
21
+
14
22
  class TmcComSpiBase(TmcCom):
15
23
  """TmcComSpiBase
16
24
 
@@ -19,13 +27,9 @@ class TmcComSpiBase(TmcCom):
19
27
  Subclasses must implement the actual SPI transfer methods.
20
28
  """
21
29
 
22
- def __init__(self, driver_address: int = 0):
23
- """constructor
24
-
25
- Args:
26
- driver_address (int, optional): driver address. Defaults to 0.
27
- """
28
- super().__init__(driver_address)
30
+ def __init__(self):
31
+ """constructor"""
32
+ super().__init__()
29
33
 
30
34
  self.spi = None # To be set by subclass
31
35
 
@@ -138,22 +142,28 @@ class TmcComSpiBase(TmcCom):
138
142
  self.write_reg(addr, val)
139
143
  return True
140
144
 
141
- def flush_serial_buffer(self):
145
+ def flush_com_buffer(self):
142
146
  """this function clear the communication buffers of the Raspberry Pi"""
143
147
 
144
- def test_com(self, addr):
148
+ def test_com(self):
145
149
  """test com connection
146
150
 
147
- Args:
148
- addr (int): HEX, which register to test
151
+ Returns:
152
+ bool: True if communication is OK, False otherwise
153
+
154
+ Raises:
155
+ TmcComException: if TMC register IOIN not available
149
156
  """
150
- data, flags = self.read_int(addr)
157
+ ioin: IoinStub = self.get_register("ioin") # type: ignore
158
+ if ioin is None:
159
+ raise TmcComException("TMC register IOIN not available")
160
+ data, flags = ioin.read()
151
161
  del flags # unused
152
162
 
153
163
  if data == 0:
154
164
  self._tmc_logger.log("No answer from TMC received", Loglevel.ERROR)
155
165
  return False
156
- # if self._tmc_registers["ioin"].version < 0x40:
157
- # self._tmc_logger.log("No correct Version from TMC received", Loglevel.ERROR)
158
- # return False
166
+ if ioin.version < 0x21:
167
+ self._tmc_logger.log("No correct Version from TMC received", Loglevel.ERROR)
168
+ return False
159
169
  return True
@@ -17,14 +17,13 @@ class TmcComSpiFtdi(TmcComSpiBase):
17
17
  like the current or the microsteppingmode
18
18
  """
19
19
 
20
- def __init__(self, spi_port: SpiPort, driver_address: int = 0):
20
+ def __init__(self, spi_port: SpiPort):
21
21
  """constructor
22
22
 
23
23
  Args:
24
24
  spi_port (SpiPort): pyftdi SpiPort object
25
- driver_address (int, optional): driver address. Defaults to 0.
26
25
  """
27
- super().__init__(driver_address)
26
+ super().__init__()
28
27
 
29
28
  self.spi = spi_port
30
29
 
@@ -10,6 +10,7 @@ Uses the machine.SPI interface available on RP2040 and similar boards.
10
10
  # Detect if we're running on MicroPython
11
11
  from machine import SPI, Pin # pylint: disable=import-error
12
12
  from ._tmc_com_spi_base import TmcComSpiBase
13
+ from .._tmc_exceptions import TmcComException
13
14
 
14
15
 
15
16
  class TmcComSpiMicroPython(TmcComSpiBase):
@@ -30,7 +31,6 @@ class TmcComSpiMicroPython(TmcComSpiBase):
30
31
  mosi_pin=19,
31
32
  miso_pin=16,
32
33
  baudrate=1000000,
33
- driver_address=0,
34
34
  ):
35
35
  """Initialize SPI communication
36
36
 
@@ -41,9 +41,8 @@ class TmcComSpiMicroPython(TmcComSpiBase):
41
41
  mosi_pin: MOSI GPIO pin
42
42
  miso_pin: MISO GPIO pin
43
43
  baudrate: SPI clock frequency (default 1MHz)
44
- driver_address: driver address (default 0)
45
44
  """
46
- super().__init__(driver_address)
45
+ super().__init__()
47
46
 
48
47
  self._spi_id = spi_id
49
48
  self._cs_pin = cs_pin
@@ -52,7 +51,7 @@ class TmcComSpiMicroPython(TmcComSpiBase):
52
51
  self._miso_pin = miso_pin
53
52
  self._baudrate = baudrate
54
53
 
55
- self._spi = None
54
+ self._spi: SPI | None = None
56
55
  self._cs = None
57
56
 
58
57
  def init(self):
@@ -87,6 +86,9 @@ class TmcComSpiMicroPython(TmcComSpiBase):
87
86
  Returns:
88
87
  List of received bytes
89
88
  """
89
+ if self._spi is None or self._cs is None:
90
+ raise TmcComException("SPI not initialized")
91
+
90
92
  tx_data = bytes(data)
91
93
  rx_data = bytearray(len(data))
92
94
 
@@ -17,17 +17,14 @@ class TmcComUart(TmcComUartBase):
17
17
  like the current or the microsteppingmode
18
18
  """
19
19
 
20
- def __init__(
21
- self, serialport: str, baudrate: int = 115200, driver_address: int = 0
22
- ):
20
+ def __init__(self, serialport: str, baudrate: int = 11520):
23
21
  """constructor
24
22
 
25
23
  Args:
26
24
  serialport (string): serialport path
27
25
  baudrate (int): baudrate
28
- driver_address (int, optional): driver address. Defaults to 0.
29
26
  """
30
- super().__init__(driver_address)
27
+ super().__init__()
31
28
 
32
29
  self.ser = serial.Serial()
33
30
 
@@ -61,18 +58,21 @@ class TmcComUart(TmcComUartBase):
61
58
  )
62
59
  raise SystemExit from e
63
60
 
61
+ if self.ser.baudrate is None:
62
+ raise TmcComException("Baudrate is not set")
63
+
64
64
  # adjust per baud and hardware. Sequential reads without some delay fail.
65
- self.communication_pause = 500 / self.ser.baudrate
65
+ self.communication_pause = 500 // self.ser.baudrate
66
66
 
67
67
  if self.ser is None:
68
68
  return
69
69
 
70
- self.ser.BYTESIZES = 1
71
- self.ser.PARITIES = serial.PARITY_NONE
72
- self.ser.STOPBITS = 1
70
+ self.ser.bytesize = serial.EIGHTBITS
71
+ self.ser.parity = serial.PARITY_NONE
72
+ self.ser.stopbits = serial.STOPBITS_ONE
73
73
 
74
74
  # adjust per baud and hardware. Sequential reads without some delay fail.
75
- self.ser.timeout = 20000 / self.ser.baudrate
75
+ self.ser.timeout = 20000 // self.ser.baudrate
76
76
 
77
77
  self._uart_flush()
78
78