PyTmcStepper 0.15.0__py3-none-any.whl
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.
- pytmcstepper-0.15.0.dist-info/METADATA +338 -0
- pytmcstepper-0.15.0.dist-info/RECORD +56 -0
- pytmcstepper-0.15.0.dist-info/WHEEL +5 -0
- pytmcstepper-0.15.0.dist-info/licenses/LICENSE +683 -0
- pytmcstepper-0.15.0.dist-info/top_level.txt +1 -0
- tmc_driver/__init__.py +96 -0
- tmc_driver/_tmc_exceptions.py +21 -0
- tmc_driver/_tmc_math.py +122 -0
- tmc_driver/_tmc_stallguard.py +340 -0
- tmc_driver/_tmc_stepperdriver.py +210 -0
- tmc_driver/_tmc_validation.py +29 -0
- tmc_driver/_tmc_xxxx.py +489 -0
- tmc_driver/com/__init__.py +64 -0
- tmc_driver/com/_tmc_com.py +148 -0
- tmc_driver/com/_tmc_com_spi.py +81 -0
- tmc_driver/com/_tmc_com_spi_base.py +159 -0
- tmc_driver/com/_tmc_com_spi_ftdi.py +48 -0
- tmc_driver/com/_tmc_com_spi_micropython.py +99 -0
- tmc_driver/com/_tmc_com_uart.py +113 -0
- tmc_driver/com/_tmc_com_uart_base.py +386 -0
- tmc_driver/com/_tmc_com_uart_micropython.py +115 -0
- tmc_driver/enable_control/__init__.py +55 -0
- tmc_driver/enable_control/_tmc_ec.py +59 -0
- tmc_driver/enable_control/_tmc_ec_pin.py +48 -0
- tmc_driver/enable_control/_tmc_ec_toff.py +30 -0
- tmc_driver/motion_control/__init__.py +111 -0
- tmc_driver/motion_control/_tmc_mc.py +280 -0
- tmc_driver/motion_control/_tmc_mc_int_ramp_generator.py +218 -0
- tmc_driver/motion_control/_tmc_mc_step_dir.py +415 -0
- tmc_driver/motion_control/_tmc_mc_step_pwm_dir.py +78 -0
- tmc_driver/motion_control/_tmc_mc_step_reg.py +36 -0
- tmc_driver/motion_control/_tmc_mc_vactual.py +221 -0
- tmc_driver/platform_utils.py +13 -0
- tmc_driver/reg/__init__.py +0 -0
- tmc_driver/reg/_tmc2209_reg.py +55 -0
- tmc_driver/reg/_tmc220x_reg.py +261 -0
- tmc_driver/reg/_tmc224x_reg.py +414 -0
- tmc_driver/reg/_tmc5160_reg.py +526 -0
- tmc_driver/reg/_tmc_reg.py +202 -0
- tmc_driver/reg/_tmc_shared_regs.py +314 -0
- tmc_driver/tmc_2208.py +17 -0
- tmc_driver/tmc_2209.py +75 -0
- tmc_driver/tmc_220x.py +383 -0
- tmc_driver/tmc_2240.py +340 -0
- tmc_driver/tmc_5160.py +344 -0
- tmc_driver/tmc_gpio/__init__.py +101 -0
- tmc_driver/tmc_gpio/_tmc_gpio_board_base.py +106 -0
- tmc_driver/tmc_gpio/_tmc_gpio_board_ftdi.py +162 -0
- tmc_driver/tmc_gpio/_tmc_gpio_board_gpiozero.py +135 -0
- tmc_driver/tmc_gpio/_tmc_gpio_board_micropython.py +108 -0
- tmc_driver/tmc_gpio/_tmc_gpio_board_periphery.py +87 -0
- tmc_driver/tmc_gpio/_tmc_gpio_board_rpi_gpio.py +182 -0
- tmc_driver/tmc_logger/__init__.py +15 -0
- tmc_driver/tmc_logger/_tmc_logger_base.py +111 -0
- tmc_driver/tmc_logger/_tmc_logger_cpython.py +155 -0
- tmc_driver/tmc_logger/_tmc_logger_micropython.py +52 -0
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
# pylint: disable=wildcard-import
|
|
2
|
+
# pylint: disable=unused-wildcard-import
|
|
3
|
+
# pylint: disable=unused-import
|
|
4
|
+
"""
|
|
5
|
+
TmcComUartBase - Abstract base class for UART communication
|
|
6
|
+
This class contains no hardware-specific imports (no serial/pyserial)
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from abc import abstractmethod
|
|
10
|
+
from ._tmc_com import *
|
|
11
|
+
from .._tmc_exceptions import TmcComException, TmcDriverException
|
|
12
|
+
from ..reg import _tmc_shared_regs as tmc_shared_reg
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class TmcComUartBase(TmcCom):
|
|
16
|
+
"""TmcComUartBase
|
|
17
|
+
|
|
18
|
+
Abstract base class for UART communication with TMC drivers.
|
|
19
|
+
This class contains common UART functionality without hardware-specific imports.
|
|
20
|
+
Subclasses must implement the actual UART transfer methods.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
def __init__(self):
|
|
24
|
+
"""constructor"""
|
|
25
|
+
super().__init__()
|
|
26
|
+
|
|
27
|
+
self.ser = None # To be set by subclass
|
|
28
|
+
|
|
29
|
+
self.r_frame = [0x55, 0, 0, 0]
|
|
30
|
+
self.w_frame = [0x55, 0, 0, 0, 0, 0, 0, 0]
|
|
31
|
+
|
|
32
|
+
@abstractmethod
|
|
33
|
+
def init(self):
|
|
34
|
+
"""init - to be implemented by subclass"""
|
|
35
|
+
|
|
36
|
+
@abstractmethod
|
|
37
|
+
def deinit(self):
|
|
38
|
+
"""destructor - to be implemented by subclass"""
|
|
39
|
+
|
|
40
|
+
@abstractmethod
|
|
41
|
+
def _uart_write(self, data: list) -> int:
|
|
42
|
+
"""Write data to UART - to be implemented by subclass
|
|
43
|
+
|
|
44
|
+
Args:
|
|
45
|
+
data: Data to send
|
|
46
|
+
|
|
47
|
+
Returns:
|
|
48
|
+
Number of bytes written
|
|
49
|
+
"""
|
|
50
|
+
|
|
51
|
+
@abstractmethod
|
|
52
|
+
def _uart_read(self, length: int) -> bytes:
|
|
53
|
+
"""Read data from UART - to be implemented by subclass
|
|
54
|
+
|
|
55
|
+
Args:
|
|
56
|
+
length: Number of bytes to read
|
|
57
|
+
|
|
58
|
+
Returns:
|
|
59
|
+
Received data
|
|
60
|
+
"""
|
|
61
|
+
|
|
62
|
+
@abstractmethod
|
|
63
|
+
def _uart_flush(self):
|
|
64
|
+
"""Flush UART buffers - to be implemented by subclass"""
|
|
65
|
+
|
|
66
|
+
def read_reg(self, addr: int) -> tuple[bytes, None]:
|
|
67
|
+
"""reads the registry on the TMC with a given address.
|
|
68
|
+
returns the binary value of that register
|
|
69
|
+
|
|
70
|
+
Args:
|
|
71
|
+
addr (int): HEX, which register to read
|
|
72
|
+
|
|
73
|
+
Returns:
|
|
74
|
+
bytes: raw response
|
|
75
|
+
Dict: flags (None for UART)
|
|
76
|
+
|
|
77
|
+
Raises:
|
|
78
|
+
TmcComException: if serial is not initialized or port is closed
|
|
79
|
+
"""
|
|
80
|
+
if self.ser is None:
|
|
81
|
+
raise TmcComException("Cannot read reg, serial is not initialized")
|
|
82
|
+
if not self.ser.is_open:
|
|
83
|
+
raise TmcComException("Cannot read reg, serial port is closed")
|
|
84
|
+
|
|
85
|
+
self._uart_flush()
|
|
86
|
+
|
|
87
|
+
self.r_frame[1] = self.driver_address
|
|
88
|
+
self.r_frame[2] = addr
|
|
89
|
+
self.r_frame[3] = compute_crc8_atm(self.r_frame[:-1])
|
|
90
|
+
|
|
91
|
+
rtn = self._uart_write(self.r_frame)
|
|
92
|
+
if rtn != len(self.r_frame):
|
|
93
|
+
raise TmcComException("Error in UART write")
|
|
94
|
+
|
|
95
|
+
# adjust per baud and hardware. Sequential reads without some delay fail.
|
|
96
|
+
time.sleep(self.communication_pause)
|
|
97
|
+
|
|
98
|
+
rtn = self._uart_read(12)
|
|
99
|
+
|
|
100
|
+
time.sleep(self.communication_pause)
|
|
101
|
+
|
|
102
|
+
return rtn, None
|
|
103
|
+
|
|
104
|
+
def read_int(self, addr: int, tries: int = 10) -> tuple[int, None]:
|
|
105
|
+
"""this function tries to read the registry of the TMC 10 times
|
|
106
|
+
if a valid answer is returned, this function returns it as an integer
|
|
107
|
+
|
|
108
|
+
Args:
|
|
109
|
+
addr (int): HEX, which register to read
|
|
110
|
+
tries (int): how many tries, before error is raised (Default value = 10)
|
|
111
|
+
|
|
112
|
+
Returns:
|
|
113
|
+
int: register value
|
|
114
|
+
Dict: flags
|
|
115
|
+
|
|
116
|
+
Raises:
|
|
117
|
+
TmcComException: if serial is not initialized or port is closed
|
|
118
|
+
"""
|
|
119
|
+
if self.ser is None:
|
|
120
|
+
raise TmcComException("Cannot read reg, serial is not initialized")
|
|
121
|
+
if not self.ser.is_open:
|
|
122
|
+
raise TmcComException("Cannot read reg, serial port is closed")
|
|
123
|
+
|
|
124
|
+
while True:
|
|
125
|
+
tries -= 1
|
|
126
|
+
rtn, flags = self.read_reg(addr)
|
|
127
|
+
if rtn is None:
|
|
128
|
+
return -1, None
|
|
129
|
+
rtn_data = rtn[7:11]
|
|
130
|
+
not_zero_count = len([elem for elem in rtn if elem != 0])
|
|
131
|
+
|
|
132
|
+
if len(rtn) < 12 or not_zero_count == 0:
|
|
133
|
+
self._tmc_logger.log(
|
|
134
|
+
f"""UART Communication Error:
|
|
135
|
+
{len(rtn_data)} data bytes |
|
|
136
|
+
{len(rtn)} total bytes""",
|
|
137
|
+
Loglevel.ERROR,
|
|
138
|
+
)
|
|
139
|
+
elif rtn[11] != compute_crc8_atm(rtn[4:11]):
|
|
140
|
+
self._tmc_logger.log(
|
|
141
|
+
"UART Communication Error: CRC MISMATCH", Loglevel.ERROR
|
|
142
|
+
)
|
|
143
|
+
else:
|
|
144
|
+
break
|
|
145
|
+
|
|
146
|
+
if tries <= 0:
|
|
147
|
+
raise TmcComException(
|
|
148
|
+
f"after 10 tries not valid answer; addr: {addr}; rtn: {rtn}"
|
|
149
|
+
)
|
|
150
|
+
|
|
151
|
+
val = struct.unpack(">i", rtn_data)[0]
|
|
152
|
+
return val, flags
|
|
153
|
+
|
|
154
|
+
def write_reg(self, addr: int, val: int) -> bool:
|
|
155
|
+
"""this function can write a value to the register of the tmc
|
|
156
|
+
1. use read_int to get the current setting of the TMC
|
|
157
|
+
2. then modify the settings as wished
|
|
158
|
+
3. write them back to the driver with this function
|
|
159
|
+
|
|
160
|
+
Args:
|
|
161
|
+
addr (int): HEX, which register to write
|
|
162
|
+
val (int): value for that register
|
|
163
|
+
|
|
164
|
+
Returns:
|
|
165
|
+
True if write was successful
|
|
166
|
+
|
|
167
|
+
Raises:
|
|
168
|
+
TmcComException: if serial is not initialized or port is closed
|
|
169
|
+
"""
|
|
170
|
+
if self.ser is None:
|
|
171
|
+
raise TmcComException("Cannot write reg, serial is not initialized")
|
|
172
|
+
if not self.ser.is_open:
|
|
173
|
+
raise TmcComException("Cannot write reg, serial port is closed")
|
|
174
|
+
|
|
175
|
+
self._uart_flush()
|
|
176
|
+
|
|
177
|
+
self.w_frame[1] = self.driver_address
|
|
178
|
+
self.w_frame[2] = addr | 0x80 # set write bit
|
|
179
|
+
|
|
180
|
+
self.w_frame[3] = 0xFF & (val >> 24)
|
|
181
|
+
self.w_frame[4] = 0xFF & (val >> 16)
|
|
182
|
+
self.w_frame[5] = 0xFF & (val >> 8)
|
|
183
|
+
self.w_frame[6] = 0xFF & val
|
|
184
|
+
|
|
185
|
+
self.w_frame[7] = compute_crc8_atm(self.w_frame[:-1])
|
|
186
|
+
|
|
187
|
+
rtn = self._uart_write(self.w_frame)
|
|
188
|
+
if rtn != len(self.w_frame):
|
|
189
|
+
self._tmc_logger.log("Err in write", Loglevel.ERROR)
|
|
190
|
+
return False
|
|
191
|
+
|
|
192
|
+
time.sleep(self.communication_pause)
|
|
193
|
+
|
|
194
|
+
return True
|
|
195
|
+
|
|
196
|
+
def write_reg_check(self, addr: int, val: int, tries: int = 10) -> bool:
|
|
197
|
+
"""this function als writes a value to the register of the TMC
|
|
198
|
+
but it also checks if the writing process was successfully by checking
|
|
199
|
+
the InterfaceTransmissionCounter before and after writing
|
|
200
|
+
|
|
201
|
+
Args:
|
|
202
|
+
addr: HEX, which register to write
|
|
203
|
+
val: value for that register
|
|
204
|
+
tries: how many tries, before error is raised (Default value = 10)
|
|
205
|
+
|
|
206
|
+
Returns:
|
|
207
|
+
True if write was successful
|
|
208
|
+
|
|
209
|
+
Raises:
|
|
210
|
+
TmcComException: if IFCNT register is not set or write fails after retries
|
|
211
|
+
"""
|
|
212
|
+
ifcnt: tmc_shared_reg.IfCnt = self.get_register("ifcnt") # type: ignore
|
|
213
|
+
|
|
214
|
+
ifcnt.read()
|
|
215
|
+
ifcnt1 = ifcnt.ifcnt
|
|
216
|
+
|
|
217
|
+
if ifcnt1 == 255:
|
|
218
|
+
ifcnt1 = -1
|
|
219
|
+
|
|
220
|
+
while True:
|
|
221
|
+
self.write_reg(addr, val)
|
|
222
|
+
tries -= 1
|
|
223
|
+
ifcnt.read()
|
|
224
|
+
ifcnt2 = ifcnt.ifcnt
|
|
225
|
+
if ifcnt2 >= ifcnt1:
|
|
226
|
+
return True
|
|
227
|
+
self._tmc_logger.log("writing not successful!", Loglevel.ERROR)
|
|
228
|
+
self._tmc_logger.log(f"ifcnt: {ifcnt1}, {ifcnt2}", Loglevel.DEBUG)
|
|
229
|
+
if tries <= 0:
|
|
230
|
+
raise TmcComException("after 10 tries writing not successful")
|
|
231
|
+
|
|
232
|
+
def flush_com_buffer(self):
|
|
233
|
+
"""this function clear the communication buffers"""
|
|
234
|
+
if self.ser is None:
|
|
235
|
+
return
|
|
236
|
+
self._uart_flush()
|
|
237
|
+
|
|
238
|
+
def test_com(self, ioin: tmc_shared_reg.Ioin | None = None) -> bool:
|
|
239
|
+
"""test UART connection
|
|
240
|
+
|
|
241
|
+
Args:
|
|
242
|
+
ioin: pre-created IOIN register instance (optional)
|
|
243
|
+
|
|
244
|
+
Returns:
|
|
245
|
+
bool: True if communication is OK, False otherwise
|
|
246
|
+
|
|
247
|
+
Raises:
|
|
248
|
+
TmcComException: if serial not initialized
|
|
249
|
+
"""
|
|
250
|
+
# pylint: disable=too-many-statements
|
|
251
|
+
# pylint: disable=too-many-branches
|
|
252
|
+
if self.ser is None:
|
|
253
|
+
raise TmcComException("Cannot test com, serial is not initialized")
|
|
254
|
+
if not self.ser.is_open:
|
|
255
|
+
raise TmcComException("Cannot test com, serial port is closed")
|
|
256
|
+
|
|
257
|
+
if ioin is None:
|
|
258
|
+
ioin = tmc_shared_reg.Ioin(self)
|
|
259
|
+
setattr(ioin, "ADDR", 0x6) # Default IOIN address
|
|
260
|
+
|
|
261
|
+
self.r_frame[1] = self.driver_address
|
|
262
|
+
self.r_frame[2] = ioin.ADDR
|
|
263
|
+
self.r_frame[3] = compute_crc8_atm(self.r_frame[:-1])
|
|
264
|
+
|
|
265
|
+
rtn = self._uart_write(self.r_frame)
|
|
266
|
+
if rtn != len(self.r_frame):
|
|
267
|
+
self._tmc_logger.log("Err in write", Loglevel.ERROR)
|
|
268
|
+
return False
|
|
269
|
+
|
|
270
|
+
snd = bytes(self.r_frame)
|
|
271
|
+
|
|
272
|
+
rtn = self._uart_read(12)
|
|
273
|
+
self._tmc_logger.log(
|
|
274
|
+
f"received {len(rtn)} bytes; {len(rtn)*8} bits", Loglevel.DEBUG
|
|
275
|
+
)
|
|
276
|
+
self._tmc_logger.log(f"hex: {rtn.hex()}", Loglevel.DEBUG)
|
|
277
|
+
|
|
278
|
+
self.tmc_logger.log(f"length snd: {len(snd)}", Loglevel.DEBUG)
|
|
279
|
+
self.tmc_logger.log(f"length rtn: {len(rtn)}", Loglevel.DEBUG)
|
|
280
|
+
|
|
281
|
+
self.tmc_logger.log("complete messages:", Loglevel.DEBUG)
|
|
282
|
+
self.tmc_logger.log(str(snd.hex()), Loglevel.DEBUG)
|
|
283
|
+
self.tmc_logger.log(str(rtn.hex()), Loglevel.DEBUG)
|
|
284
|
+
|
|
285
|
+
self.tmc_logger.log("just the first 4 bytes:", Loglevel.DEBUG)
|
|
286
|
+
self.tmc_logger.log(str(snd[0:4].hex()), Loglevel.DEBUG)
|
|
287
|
+
self.tmc_logger.log(str(rtn[0:4].hex()), Loglevel.DEBUG)
|
|
288
|
+
|
|
289
|
+
status = True
|
|
290
|
+
|
|
291
|
+
if len(rtn) == 12:
|
|
292
|
+
self.tmc_logger.log(
|
|
293
|
+
"""the Raspberry Pi received the sent
|
|
294
|
+
bytes and the answer from the TMC""",
|
|
295
|
+
Loglevel.DEBUG,
|
|
296
|
+
)
|
|
297
|
+
elif len(rtn) == 4:
|
|
298
|
+
self.tmc_logger.log(
|
|
299
|
+
"the Raspberry Pi received only the sent bytes", Loglevel.ERROR
|
|
300
|
+
)
|
|
301
|
+
status = False
|
|
302
|
+
elif len(rtn) == 0:
|
|
303
|
+
self.tmc_logger.log(
|
|
304
|
+
"the Raspberry Pi did not receive anything", Loglevel.ERROR
|
|
305
|
+
)
|
|
306
|
+
status = False
|
|
307
|
+
else:
|
|
308
|
+
self.tmc_logger.log(
|
|
309
|
+
f"the Raspberry Pi received an unexpected amount of bytes: {len(rtn)}",
|
|
310
|
+
Loglevel.ERROR,
|
|
311
|
+
)
|
|
312
|
+
status = False
|
|
313
|
+
|
|
314
|
+
if snd[0:4] == rtn[0:4]:
|
|
315
|
+
self.tmc_logger.log(
|
|
316
|
+
"""the Raspberry Pi received exactly the bytes it has send.
|
|
317
|
+
the first 4 bytes are the same""",
|
|
318
|
+
Loglevel.DEBUG,
|
|
319
|
+
)
|
|
320
|
+
else:
|
|
321
|
+
self.tmc_logger.log(
|
|
322
|
+
"""the Raspberry Pi did not received the bytes it has send.
|
|
323
|
+
the first 4 bytes are different""",
|
|
324
|
+
Loglevel.DEBUG,
|
|
325
|
+
)
|
|
326
|
+
status = False
|
|
327
|
+
# only check version if a specific ioin register is given
|
|
328
|
+
# pylint: disable=unidiomatic-typecheck
|
|
329
|
+
if status and type(ioin) is not tmc_shared_reg.Ioin:
|
|
330
|
+
ioin.read()
|
|
331
|
+
|
|
332
|
+
if ioin.version < 0x21:
|
|
333
|
+
self._tmc_logger.log(
|
|
334
|
+
f"No correct Version from TMC received: {ioin.version}",
|
|
335
|
+
Loglevel.ERROR,
|
|
336
|
+
)
|
|
337
|
+
status = False
|
|
338
|
+
|
|
339
|
+
self.tmc_logger.log("---")
|
|
340
|
+
if status:
|
|
341
|
+
self.tmc_logger.log("UART connection: OK", Loglevel.INFO)
|
|
342
|
+
else:
|
|
343
|
+
self.tmc_logger.log("UART connection: not OK", Loglevel.ERROR)
|
|
344
|
+
|
|
345
|
+
self.tmc_logger.log("---")
|
|
346
|
+
|
|
347
|
+
return status
|
|
348
|
+
|
|
349
|
+
def scan_for_devices(
|
|
350
|
+
self,
|
|
351
|
+
driver_ioin_regs: list | None = None,
|
|
352
|
+
address_range: range = range(0, 8),
|
|
353
|
+
) -> list[tuple[int, str | None]]:
|
|
354
|
+
"""scan for devices on the UART bus
|
|
355
|
+
|
|
356
|
+
Args:
|
|
357
|
+
driver_ioin_regs: list of IOIN register classes to test (default: None)
|
|
358
|
+
address_range: range of addresses to scan (default: range(0, 8))
|
|
359
|
+
|
|
360
|
+
Returns:
|
|
361
|
+
list of tuples: (address, driver name or None)
|
|
362
|
+
"""
|
|
363
|
+
found_devices = []
|
|
364
|
+
|
|
365
|
+
for address in address_range:
|
|
366
|
+
self.driver_address = address
|
|
367
|
+
if driver_ioin_regs is None:
|
|
368
|
+
if self.test_com():
|
|
369
|
+
self.tmc_logger.log(
|
|
370
|
+
f"Found device at address {address}", Loglevel.INFO
|
|
371
|
+
)
|
|
372
|
+
found_devices.append((address, None))
|
|
373
|
+
else:
|
|
374
|
+
for driver_ioin_reg in driver_ioin_regs:
|
|
375
|
+
ioin = driver_ioin_reg(self)
|
|
376
|
+
if self.test_com(ioin):
|
|
377
|
+
# Get driver name from DRIVER_NAME attribute if available
|
|
378
|
+
driver_name = getattr(driver_ioin_reg, "DRIVER_NAME", None)
|
|
379
|
+
self.tmc_logger.log(
|
|
380
|
+
f"Found device at address {address}: {driver_name}",
|
|
381
|
+
Loglevel.INFO,
|
|
382
|
+
)
|
|
383
|
+
found_devices.append((address, driver_name))
|
|
384
|
+
break # Move to the next address after finding a device
|
|
385
|
+
|
|
386
|
+
return found_devices
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
# pylint: disable=too-many-instance-attributes
|
|
2
|
+
# pylint: disable=too-many-arguments
|
|
3
|
+
# pylint: disable=too-many-positional-arguments
|
|
4
|
+
# pylint: disable=too-few-public-methods
|
|
5
|
+
"""
|
|
6
|
+
TmcComUartPico - UART communication for MicroPython
|
|
7
|
+
|
|
8
|
+
Uses the machine.UART interface for single-wire UART communication.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
# Detect if we're running on MicroPython
|
|
12
|
+
import time
|
|
13
|
+
from machine import UART, Pin # pylint: disable=import-error
|
|
14
|
+
from ._tmc_com_uart_base import TmcComUartBase
|
|
15
|
+
from .._tmc_exceptions import TmcComException
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class TmcComUartMicroPython(TmcComUartBase):
|
|
19
|
+
"""UART Communication class for MicroPython
|
|
20
|
+
|
|
21
|
+
RP2040 UART Pins (default):
|
|
22
|
+
- UART0: TX=GP0, RX=GP1
|
|
23
|
+
- UART1: TX=GP4, RX=GP5
|
|
24
|
+
|
|
25
|
+
For single-wire UART, connect TX to the TMC UART pin through a 1k resistor.
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
def __init__(self, uart_id=0, tx_pin=0, rx_pin=1, baudrate=115200):
|
|
29
|
+
"""Initialize UART communication
|
|
30
|
+
|
|
31
|
+
Args:
|
|
32
|
+
uart_id: UART bus ID (0 or 1)
|
|
33
|
+
tx_pin: TX GPIO pin
|
|
34
|
+
rx_pin: RX GPIO pin
|
|
35
|
+
baudrate: UART baudrate
|
|
36
|
+
"""
|
|
37
|
+
super().__init__()
|
|
38
|
+
|
|
39
|
+
self._uart_id = uart_id
|
|
40
|
+
self._tx_pin = tx_pin
|
|
41
|
+
self._rx_pin = rx_pin
|
|
42
|
+
self._baudrate = baudrate
|
|
43
|
+
|
|
44
|
+
self._uart = None
|
|
45
|
+
|
|
46
|
+
def init(self):
|
|
47
|
+
"""Initialize UART hardware"""
|
|
48
|
+
self._uart = UART(
|
|
49
|
+
self._uart_id,
|
|
50
|
+
baudrate=self._baudrate,
|
|
51
|
+
tx=Pin(self._tx_pin),
|
|
52
|
+
rx=Pin(self._rx_pin),
|
|
53
|
+
)
|
|
54
|
+
# Create a fake serial object for compatibility with base class
|
|
55
|
+
self.ser = _FakeSerial(self._uart)
|
|
56
|
+
|
|
57
|
+
def deinit(self):
|
|
58
|
+
"""Deinitialize UART hardware"""
|
|
59
|
+
if self._uart is not None:
|
|
60
|
+
self._uart.deinit()
|
|
61
|
+
self._uart = None
|
|
62
|
+
self.ser = None
|
|
63
|
+
|
|
64
|
+
def _uart_write(self, data):
|
|
65
|
+
"""Write data to UART
|
|
66
|
+
|
|
67
|
+
Args:
|
|
68
|
+
data: Bytes to write
|
|
69
|
+
|
|
70
|
+
Returns:
|
|
71
|
+
Number of bytes written
|
|
72
|
+
"""
|
|
73
|
+
if self._uart is None:
|
|
74
|
+
raise TmcComException("UART not initialized")
|
|
75
|
+
return self._uart.write(bytes(data))
|
|
76
|
+
|
|
77
|
+
def _uart_read(self, length):
|
|
78
|
+
"""Read data from UART
|
|
79
|
+
|
|
80
|
+
Args:
|
|
81
|
+
length: Number of bytes to read
|
|
82
|
+
|
|
83
|
+
Returns:
|
|
84
|
+
Bytes read or empty bytes if timeout
|
|
85
|
+
"""
|
|
86
|
+
if self._uart is None:
|
|
87
|
+
raise TmcComException("UART not initialized")
|
|
88
|
+
# Wait a bit for data to arrive
|
|
89
|
+
time.sleep_ms(10) # pylint: disable=no-member
|
|
90
|
+
|
|
91
|
+
result = self._uart.read(length)
|
|
92
|
+
if result is None:
|
|
93
|
+
return bytes()
|
|
94
|
+
return result
|
|
95
|
+
|
|
96
|
+
def _uart_flush(self):
|
|
97
|
+
"""Flush UART receive buffer"""
|
|
98
|
+
if self._uart is None:
|
|
99
|
+
raise TmcComException("UART not initialized")
|
|
100
|
+
# Read and discard any pending data
|
|
101
|
+
while self._uart.any():
|
|
102
|
+
self._uart.read()
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
class _FakeSerial:
|
|
106
|
+
"""Fake serial object for compatibility with base class"""
|
|
107
|
+
|
|
108
|
+
def __init__(self, uart):
|
|
109
|
+
"""Constructor for fake serial object"""
|
|
110
|
+
self._uart = uart
|
|
111
|
+
self.is_open = True
|
|
112
|
+
|
|
113
|
+
def close(self):
|
|
114
|
+
"""Close the fake serial port"""
|
|
115
|
+
self.is_open = False
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# pylint: disable=import-outside-toplevel
|
|
2
|
+
"""Enable control module for TMC stepper drivers
|
|
3
|
+
|
|
4
|
+
Provides motor enable/disable functionality:
|
|
5
|
+
- TmcEnableControlPin: Enable via dedicated GPIO pin
|
|
6
|
+
- TmcEnableControlToff: Enable via TOFF register field
|
|
7
|
+
|
|
8
|
+
Example:
|
|
9
|
+
>>> from tmc_driver.enable_control import TmcEnableControlPin
|
|
10
|
+
>>> ec = TmcEnableControlPin(enable_pin=21)
|
|
11
|
+
>>> ec.set_motor_enabled(True)
|
|
12
|
+
|
|
13
|
+
Note: Uses lazy imports to avoid circular import issues.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
try:
|
|
17
|
+
from typing import TYPE_CHECKING
|
|
18
|
+
|
|
19
|
+
if TYPE_CHECKING:
|
|
20
|
+
# Import for type checkers/IDE only - not executed at runtime
|
|
21
|
+
from ._tmc_ec import TmcEnableControl
|
|
22
|
+
from ._tmc_ec_pin import TmcEnableControlPin
|
|
23
|
+
from ._tmc_ec_toff import TmcEnableControlToff
|
|
24
|
+
|
|
25
|
+
if TYPE_CHECKING:
|
|
26
|
+
# Import for type checkers/IDE only - not executed at runtime
|
|
27
|
+
from ._tmc_ec import TmcEnableControl
|
|
28
|
+
from ._tmc_ec_pin import TmcEnableControlPin
|
|
29
|
+
from ._tmc_ec_toff import TmcEnableControlToff
|
|
30
|
+
except ImportError:
|
|
31
|
+
pass
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def __getattr__(name):
|
|
35
|
+
"""Lazy import of enable control classes to avoid circular imports"""
|
|
36
|
+
if name == "TmcEnableControl":
|
|
37
|
+
from ._tmc_ec import TmcEnableControl
|
|
38
|
+
|
|
39
|
+
return TmcEnableControl
|
|
40
|
+
if name == "TmcEnableControlPin":
|
|
41
|
+
from ._tmc_ec_pin import TmcEnableControlPin
|
|
42
|
+
|
|
43
|
+
return TmcEnableControlPin
|
|
44
|
+
if name == "TmcEnableControlToff":
|
|
45
|
+
from ._tmc_ec_toff import TmcEnableControlToff
|
|
46
|
+
|
|
47
|
+
return TmcEnableControlToff
|
|
48
|
+
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
__all__ = [
|
|
52
|
+
"TmcEnableControl",
|
|
53
|
+
"TmcEnableControlPin",
|
|
54
|
+
"TmcEnableControlToff",
|
|
55
|
+
]
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Enable Control base module
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from abc import abstractmethod
|
|
6
|
+
from ..tmc_logger import TmcLogger
|
|
7
|
+
from .._tmc_exceptions import TmcEnableControlException
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class TmcEnableControl:
|
|
11
|
+
"""Enable Control base class"""
|
|
12
|
+
|
|
13
|
+
def __init__(self):
|
|
14
|
+
"""constructor"""
|
|
15
|
+
self._tmc_logger: TmcLogger
|
|
16
|
+
self._get_register_callback = None
|
|
17
|
+
|
|
18
|
+
def init(self, tmc_logger: TmcLogger):
|
|
19
|
+
"""init: called by the Tmc class"""
|
|
20
|
+
self._tmc_logger = tmc_logger
|
|
21
|
+
|
|
22
|
+
def set_get_register_callback(self, callback):
|
|
23
|
+
"""Set callback to get registers from parent TMC class
|
|
24
|
+
|
|
25
|
+
Args:
|
|
26
|
+
callback: Function that takes register name (str) and returns register object
|
|
27
|
+
"""
|
|
28
|
+
self._get_register_callback = callback
|
|
29
|
+
|
|
30
|
+
def get_register(self, name: str):
|
|
31
|
+
"""Get register by name from parent TMC class
|
|
32
|
+
|
|
33
|
+
Args:
|
|
34
|
+
name: Register name (e.g. 'gconf', 'chopconf')
|
|
35
|
+
|
|
36
|
+
Returns:
|
|
37
|
+
Register object or None if callback not set
|
|
38
|
+
"""
|
|
39
|
+
if self._get_register_callback is None:
|
|
40
|
+
raise TmcEnableControlException(
|
|
41
|
+
"Get register callback not set in enable control"
|
|
42
|
+
)
|
|
43
|
+
return self._get_register_callback(name)
|
|
44
|
+
|
|
45
|
+
def __del__(self):
|
|
46
|
+
"""destructor"""
|
|
47
|
+
self.deinit()
|
|
48
|
+
|
|
49
|
+
def deinit(self):
|
|
50
|
+
"""destructor"""
|
|
51
|
+
self.set_motor_enabled(False)
|
|
52
|
+
|
|
53
|
+
@abstractmethod
|
|
54
|
+
def set_motor_enabled(self, en):
|
|
55
|
+
"""enables or disables the motor current output
|
|
56
|
+
|
|
57
|
+
Args:
|
|
58
|
+
en (bool): whether the motor current output should be enabled
|
|
59
|
+
"""
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Enable Control base module
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from ._tmc_ec import TmcEnableControl
|
|
6
|
+
from ..tmc_gpio import Gpio, GpioMode
|
|
7
|
+
from .. import tmc_gpio
|
|
8
|
+
from ..tmc_logger import TmcLogger, Loglevel
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class TmcEnableControlPin(TmcEnableControl):
|
|
12
|
+
"""Enable Control base class"""
|
|
13
|
+
|
|
14
|
+
@property
|
|
15
|
+
def pin_en(self):
|
|
16
|
+
"""pin_en property"""
|
|
17
|
+
return self._pin_en
|
|
18
|
+
|
|
19
|
+
def __init__(self, pin_en: int):
|
|
20
|
+
"""constructor"""
|
|
21
|
+
super().__init__()
|
|
22
|
+
self._pin_en = pin_en
|
|
23
|
+
|
|
24
|
+
def init(self, tmc_logger: TmcLogger):
|
|
25
|
+
"""init: called by the Tmc class"""
|
|
26
|
+
super().init(tmc_logger)
|
|
27
|
+
self._tmc_logger.log(f"EN Pin: {self._pin_en}", Loglevel.DEBUG)
|
|
28
|
+
tmc_gpio.tmc_gpio.gpio_setup(self._pin_en, GpioMode.OUT, initial=Gpio.HIGH)
|
|
29
|
+
|
|
30
|
+
def __del__(self):
|
|
31
|
+
self.deinit()
|
|
32
|
+
|
|
33
|
+
def deinit(self):
|
|
34
|
+
"""destructor"""
|
|
35
|
+
super().deinit()
|
|
36
|
+
if hasattr(self, "_pin_en"):
|
|
37
|
+
tmc_gpio.tmc_gpio.gpio_cleanup(self._pin_en)
|
|
38
|
+
del self._pin_en
|
|
39
|
+
|
|
40
|
+
def set_motor_enabled(self, en):
|
|
41
|
+
"""enables or disables the motor current output
|
|
42
|
+
|
|
43
|
+
Args:
|
|
44
|
+
en (bool): whether the motor current output should be enabled
|
|
45
|
+
"""
|
|
46
|
+
if hasattr(self, "_pin_en"):
|
|
47
|
+
tmc_gpio.tmc_gpio.gpio_output(self._pin_en, not en)
|
|
48
|
+
self._tmc_logger.log(f"Motor output active: {en}", Loglevel.INFO)
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Enable Control base module
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from ._tmc_ec import TmcEnableControl
|
|
6
|
+
from ..tmc_logger import Loglevel
|
|
7
|
+
from ..reg import _tmc_shared_regs as tmc_shared_reg
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class TmcEnableControlToff(TmcEnableControl):
|
|
11
|
+
"""Enable Control base class"""
|
|
12
|
+
|
|
13
|
+
def __init__(self):
|
|
14
|
+
"""constructor"""
|
|
15
|
+
super().__init__()
|
|
16
|
+
self._default_toff = 3
|
|
17
|
+
|
|
18
|
+
def set_motor_enabled(self, en):
|
|
19
|
+
"""enables or disables the motor current output
|
|
20
|
+
|
|
21
|
+
Args:
|
|
22
|
+
en (bool): whether the motor current output should be enabled
|
|
23
|
+
"""
|
|
24
|
+
self._tmc_logger.log(f"Motor output active: {en}", Loglevel.INFO)
|
|
25
|
+
|
|
26
|
+
val = self._default_toff if en else 0
|
|
27
|
+
|
|
28
|
+
chopconf: tmc_shared_reg.ChopConf = self.get_register("chopconf")
|
|
29
|
+
|
|
30
|
+
chopconf.modify("toff", val)
|