PyTmcStepper 0.15.0__tar.gz → 0.16.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 (77) hide show
  1. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/PKG-INFO +3 -1
  2. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/pyproject.toml +3 -1
  3. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/src/PyTmcStepper.egg-info/PKG-INFO +3 -1
  4. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/src/PyTmcStepper.egg-info/SOURCES.txt +3 -0
  5. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/src/tmc_driver/com/__init__.py +15 -0
  6. pytmcstepper-0.16.0/src/tmc_driver/com/_tmc_com_spi_circuitpython.py +113 -0
  7. pytmcstepper-0.16.0/src/tmc_driver/com/_tmc_com_uart_circuitpython.py +127 -0
  8. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/src/tmc_driver/enable_control/_tmc_ec.py +4 -1
  9. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/src/tmc_driver/platform_utils.py +4 -1
  10. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/src/tmc_driver/reg/_tmc220x_reg.py +8 -5
  11. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/src/tmc_driver/reg/_tmc224x_reg.py +6 -3
  12. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/src/tmc_driver/reg/_tmc5160_reg.py +6 -3
  13. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/src/tmc_driver/tmc_gpio/__init__.py +27 -12
  14. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/src/tmc_driver/tmc_gpio/_tmc_gpio_board_base.py +1 -0
  15. pytmcstepper-0.16.0/src/tmc_driver/tmc_gpio/_tmc_gpio_board_circuitpython.py +124 -0
  16. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/src/tmc_driver/tmc_logger/__init__.py +2 -1
  17. pytmcstepper-0.16.0/tests/test_tmc2209.py +107 -0
  18. pytmcstepper-0.16.0/tests/test_tmc2240.py +148 -0
  19. pytmcstepper-0.16.0/tests/test_tmc_com_spi.py +32 -0
  20. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/tests/test_tmc_math.py +4 -0
  21. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/tests/test_tmc_modules.py +7 -1
  22. pytmcstepper-0.15.0/tests/test_tmc2209.py +0 -44
  23. pytmcstepper-0.15.0/tests/test_tmc2240.py +0 -44
  24. pytmcstepper-0.15.0/tests/test_tmc_com_spi.py +0 -27
  25. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/LICENSE +0 -0
  26. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/README.md +0 -0
  27. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/setup.cfg +0 -0
  28. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/src/PyTmcStepper.egg-info/dependency_links.txt +0 -0
  29. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/src/PyTmcStepper.egg-info/requires.txt +0 -0
  30. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/src/PyTmcStepper.egg-info/top_level.txt +0 -0
  31. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/src/tmc_driver/__init__.py +0 -0
  32. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/src/tmc_driver/_tmc_exceptions.py +0 -0
  33. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/src/tmc_driver/_tmc_math.py +0 -0
  34. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/src/tmc_driver/_tmc_stallguard.py +0 -0
  35. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/src/tmc_driver/_tmc_stepperdriver.py +0 -0
  36. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/src/tmc_driver/_tmc_validation.py +0 -0
  37. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/src/tmc_driver/_tmc_xxxx.py +0 -0
  38. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/src/tmc_driver/com/_tmc_com.py +0 -0
  39. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/src/tmc_driver/com/_tmc_com_spi.py +0 -0
  40. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/src/tmc_driver/com/_tmc_com_spi_base.py +0 -0
  41. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/src/tmc_driver/com/_tmc_com_spi_ftdi.py +0 -0
  42. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/src/tmc_driver/com/_tmc_com_spi_micropython.py +0 -0
  43. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/src/tmc_driver/com/_tmc_com_uart.py +0 -0
  44. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/src/tmc_driver/com/_tmc_com_uart_base.py +0 -0
  45. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/src/tmc_driver/com/_tmc_com_uart_micropython.py +0 -0
  46. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/src/tmc_driver/enable_control/__init__.py +0 -0
  47. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/src/tmc_driver/enable_control/_tmc_ec_pin.py +0 -0
  48. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/src/tmc_driver/enable_control/_tmc_ec_toff.py +0 -0
  49. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/src/tmc_driver/motion_control/__init__.py +0 -0
  50. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/src/tmc_driver/motion_control/_tmc_mc.py +0 -0
  51. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/src/tmc_driver/motion_control/_tmc_mc_int_ramp_generator.py +0 -0
  52. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/src/tmc_driver/motion_control/_tmc_mc_step_dir.py +0 -0
  53. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/src/tmc_driver/motion_control/_tmc_mc_step_pwm_dir.py +0 -0
  54. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/src/tmc_driver/motion_control/_tmc_mc_step_reg.py +0 -0
  55. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/src/tmc_driver/motion_control/_tmc_mc_vactual.py +0 -0
  56. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/src/tmc_driver/reg/__init__.py +0 -0
  57. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/src/tmc_driver/reg/_tmc2209_reg.py +0 -0
  58. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/src/tmc_driver/reg/_tmc_reg.py +0 -0
  59. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/src/tmc_driver/reg/_tmc_shared_regs.py +0 -0
  60. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/src/tmc_driver/tmc_2208.py +0 -0
  61. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/src/tmc_driver/tmc_2209.py +0 -0
  62. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/src/tmc_driver/tmc_220x.py +0 -0
  63. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/src/tmc_driver/tmc_2240.py +0 -0
  64. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/src/tmc_driver/tmc_5160.py +0 -0
  65. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/src/tmc_driver/tmc_gpio/_tmc_gpio_board_ftdi.py +0 -0
  66. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/src/tmc_driver/tmc_gpio/_tmc_gpio_board_gpiozero.py +0 -0
  67. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/src/tmc_driver/tmc_gpio/_tmc_gpio_board_micropython.py +0 -0
  68. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/src/tmc_driver/tmc_gpio/_tmc_gpio_board_periphery.py +0 -0
  69. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/src/tmc_driver/tmc_gpio/_tmc_gpio_board_rpi_gpio.py +0 -0
  70. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/src/tmc_driver/tmc_logger/_tmc_logger_base.py +0 -0
  71. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/src/tmc_driver/tmc_logger/_tmc_logger_cpython.py +0 -0
  72. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/src/tmc_driver/tmc_logger/_tmc_logger_micropython.py +0 -0
  73. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/tests/test_tmc_com_uart.py +0 -0
  74. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/tests/test_tmc_ec_pin.py +0 -0
  75. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/tests/test_tmc_move.py +0 -0
  76. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/tests/test_tmc_set_current.py +0 -0
  77. {pytmcstepper-0.15.0 → pytmcstepper-0.16.0}/tests/test_tmc_stallguard.py +0 -0
@@ -1,12 +1,14 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: PyTmcStepper
3
- Version: 0.15.0
3
+ Version: 0.16.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
7
7
  Project-URL: Homepage, https://github.com/Chr157i4n/PyTmcStepper
8
8
  Project-URL: Bug Tracker, https://github.com/Chr157i4n/PyTmcStepper/issues
9
9
  Classifier: Programming Language :: Python :: 3
10
+ Classifier: Programming Language :: Python :: Implementation :: CPython
11
+ Classifier: Programming Language :: Python :: Implementation :: MicroPython
10
12
  Classifier: Operating System :: POSIX :: Linux
11
13
  Classifier: Development Status :: 4 - Beta
12
14
  Classifier: Intended Audience :: Developers
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "PyTmcStepper"
7
- version = "0.15.0"
7
+ version = "0.16.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"
@@ -12,6 +12,8 @@ license = "GPL-3.0"
12
12
  requires-python = ">=3.10"
13
13
  classifiers = [
14
14
  "Programming Language :: Python :: 3",
15
+ "Programming Language :: Python :: Implementation :: CPython",
16
+ "Programming Language :: Python :: Implementation :: MicroPython",
15
17
  "Operating System :: POSIX :: Linux",
16
18
  "Development Status :: 4 - Beta",
17
19
  "Intended Audience :: Developers",
@@ -1,12 +1,14 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: PyTmcStepper
3
- Version: 0.15.0
3
+ Version: 0.16.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
7
7
  Project-URL: Homepage, https://github.com/Chr157i4n/PyTmcStepper
8
8
  Project-URL: Bug Tracker, https://github.com/Chr157i4n/PyTmcStepper/issues
9
9
  Classifier: Programming Language :: Python :: 3
10
+ Classifier: Programming Language :: Python :: Implementation :: CPython
11
+ Classifier: Programming Language :: Python :: Implementation :: MicroPython
10
12
  Classifier: Operating System :: POSIX :: Linux
11
13
  Classifier: Development Status :: 4 - Beta
12
14
  Classifier: Intended Audience :: Developers
@@ -23,10 +23,12 @@ src/tmc_driver/com/__init__.py
23
23
  src/tmc_driver/com/_tmc_com.py
24
24
  src/tmc_driver/com/_tmc_com_spi.py
25
25
  src/tmc_driver/com/_tmc_com_spi_base.py
26
+ src/tmc_driver/com/_tmc_com_spi_circuitpython.py
26
27
  src/tmc_driver/com/_tmc_com_spi_ftdi.py
27
28
  src/tmc_driver/com/_tmc_com_spi_micropython.py
28
29
  src/tmc_driver/com/_tmc_com_uart.py
29
30
  src/tmc_driver/com/_tmc_com_uart_base.py
31
+ src/tmc_driver/com/_tmc_com_uart_circuitpython.py
30
32
  src/tmc_driver/com/_tmc_com_uart_micropython.py
31
33
  src/tmc_driver/enable_control/__init__.py
32
34
  src/tmc_driver/enable_control/_tmc_ec.py
@@ -48,6 +50,7 @@ src/tmc_driver/reg/_tmc_reg.py
48
50
  src/tmc_driver/reg/_tmc_shared_regs.py
49
51
  src/tmc_driver/tmc_gpio/__init__.py
50
52
  src/tmc_driver/tmc_gpio/_tmc_gpio_board_base.py
53
+ src/tmc_driver/tmc_gpio/_tmc_gpio_board_circuitpython.py
51
54
  src/tmc_driver/tmc_gpio/_tmc_gpio_board_ftdi.py
52
55
  src/tmc_driver/tmc_gpio/_tmc_gpio_board_gpiozero.py
53
56
  src/tmc_driver/tmc_gpio/_tmc_gpio_board_micropython.py
@@ -6,6 +6,8 @@ Provides UART and SPI communication implementations for different platforms:
6
6
  - TmcComSpi: Standard SPI communication (CPython with spidev)
7
7
  - TmcComUartMicroPython: UART for MicroPython
8
8
  - TmcComSpiMicroPython: SPI for MicroPython
9
+ - TmcComUartCircuitPython: UART for CircuitPython
10
+ - TmcComSpiCircuitPython: SPI for CircuitPython
9
11
  - TmcComSpiFtdi: SPI via FTDI USB devices
10
12
 
11
13
  Example:
@@ -25,12 +27,15 @@ try:
25
27
  from ._tmc_com_spi import TmcComSpi
26
28
  from ._tmc_com_uart_micropython import TmcComUartMicroPython
27
29
  from ._tmc_com_spi_micropython import TmcComSpiMicroPython
30
+ from ._tmc_com_uart_circuitpython import TmcComUartCircuitPython
31
+ from ._tmc_com_spi_circuitpython import TmcComSpiCircuitPython
28
32
  from ._tmc_com_spi_ftdi import TmcComSpiFtdi
29
33
  except ImportError:
30
34
  pass
31
35
 
32
36
 
33
37
  def __getattr__(name):
38
+ # pylint: disable=too-many-return-statements
34
39
  """Lazy import of communication classes to avoid circular imports"""
35
40
  if name == "TmcComUart":
36
41
  from ._tmc_com_uart import TmcComUart
@@ -48,6 +53,14 @@ def __getattr__(name):
48
53
  from ._tmc_com_spi_micropython import TmcComSpiMicroPython
49
54
 
50
55
  return TmcComSpiMicroPython
56
+ if name == "TmcComUartCircuitPython":
57
+ from ._tmc_com_uart_circuitpython import TmcComUartCircuitPython
58
+
59
+ return TmcComUartCircuitPython
60
+ if name == "TmcComSpiCircuitPython":
61
+ from ._tmc_com_spi_circuitpython import TmcComSpiCircuitPython
62
+
63
+ return TmcComSpiCircuitPython
51
64
  if name == "TmcComSpiFtdi":
52
65
  from ._tmc_com_spi_ftdi import TmcComSpiFtdi
53
66
 
@@ -60,5 +73,7 @@ __all__ = [
60
73
  "TmcComSpi",
61
74
  "TmcComUartMicroPython",
62
75
  "TmcComSpiMicroPython",
76
+ "TmcComUartCircuitPython",
77
+ "TmcComSpiCircuitPython",
63
78
  "TmcComSpiFtdi",
64
79
  ]
@@ -0,0 +1,113 @@
1
+ # pylint: disable=too-many-instance-attributes
2
+ # pylint: disable=too-many-arguments
3
+ # pylint: disable=too-many-positional-arguments
4
+ """
5
+ TmcComSpiCircuitPython - SPI communication for CircuitPython
6
+
7
+ Uses the busio.SPI interface available on CircuitPython boards.
8
+ """
9
+
10
+ import busio # pylint: disable=import-error
11
+ import digitalio # pylint: disable=import-error
12
+ from ._tmc_com_spi_base import TmcComSpiBase
13
+ from .._tmc_exceptions import TmcComException
14
+
15
+
16
+ class TmcComSpiCircuitPython(TmcComSpiBase):
17
+ """SPI Communication class for CircuitPython
18
+
19
+ Example usage:
20
+ import board
21
+ from tmc_driver.com import TmcComSpiCircuitPython
22
+
23
+ spi_com = TmcComSpiCircuitPython(
24
+ cs=board.D17,
25
+ clock=board.SCK,
26
+ mosi=board.MOSI,
27
+ miso=board.MISO,
28
+ baudrate=1000000
29
+ )
30
+
31
+ TMC2240/TMC5160 uses SPI Mode 3: CPOL=1, CPHA=1
32
+ """
33
+
34
+ def __init__(
35
+ self,
36
+ cs,
37
+ clock,
38
+ mosi,
39
+ miso,
40
+ baudrate=1000000,
41
+ ):
42
+ """Initialize SPI communication
43
+
44
+ Args:
45
+ cs: Chip select board pin (e.g., board.D17)
46
+ clock: SPI clock board pin (e.g., board.SCK)
47
+ mosi: MOSI board pin (e.g., board.MOSI)
48
+ miso: MISO board pin (e.g., board.MISO)
49
+ baudrate: SPI clock frequency in Hz (default 1MHz)
50
+ """
51
+ super().__init__()
52
+
53
+ self._cs_pin = cs
54
+ self._clock_pin = clock
55
+ self._mosi_pin = mosi
56
+ self._miso_pin = miso
57
+ self._baudrate = baudrate
58
+
59
+ self._spi = None
60
+ self._cs = None
61
+
62
+ def init(self):
63
+ """Initialize SPI hardware"""
64
+ # Create SPI bus
65
+ self._spi = busio.SPI(
66
+ clock=self._clock_pin, MOSI=self._mosi_pin, MISO=self._miso_pin
67
+ )
68
+
69
+ # Setup CS pin (active low)
70
+ self._cs = digitalio.DigitalInOut(self._cs_pin)
71
+ self._cs.direction = digitalio.Direction.OUTPUT
72
+ self._cs.value = True # CS high (inactive)
73
+
74
+ def deinit(self):
75
+ """Deinitialize SPI hardware"""
76
+ if self._spi is not None:
77
+ self._spi.deinit()
78
+ self._spi = None
79
+ if self._cs is not None:
80
+ self._cs.deinit()
81
+ self._cs = None
82
+
83
+ def _spi_transfer(self, data):
84
+ """Perform SPI transfer
85
+
86
+ Args:
87
+ data: List of bytes to send
88
+
89
+ Returns:
90
+ List of received bytes
91
+ """
92
+ if self._spi is None or self._cs is None:
93
+ raise TmcComException("SPI not initialized")
94
+
95
+ tx_data = bytes(data)
96
+ rx_data = bytearray(len(data))
97
+
98
+ # Lock the SPI bus and perform transfer
99
+ while not self._spi.try_lock():
100
+ pass
101
+
102
+ try:
103
+ # Configure SPI for TMC (Mode 3: CPOL=1, CPHA=1)
104
+ self._spi.configure(baudrate=self._baudrate, polarity=1, phase=1, bits=8)
105
+
106
+ # Perform transfer with CS control
107
+ self._cs.value = False # CS low (active)
108
+ self._spi.write_readinto(tx_data, rx_data)
109
+ self._cs.value = True # CS high (inactive)
110
+ finally:
111
+ self._spi.unlock()
112
+
113
+ return list(rx_data)
@@ -0,0 +1,127 @@
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
+ TmcComUartCircuitPython - UART communication for CircuitPython
7
+
8
+ Uses the busio.UART interface for single-wire UART communication.
9
+ """
10
+
11
+ import time
12
+ import busio # pylint: disable=import-error
13
+ from ._tmc_com_uart_base import TmcComUartBase
14
+ from .._tmc_exceptions import TmcComException
15
+
16
+
17
+ class TmcComUartCircuitPython(TmcComUartBase):
18
+ """UART Communication class for CircuitPython
19
+
20
+ Example usage:
21
+ import board
22
+ from tmc_driver.com import TmcComUartCircuitPython
23
+
24
+ uart_com = TmcComUartCircuitPython(
25
+ tx=board.TX,
26
+ rx=board.RX,
27
+ baudrate=115200
28
+ )
29
+
30
+ For single-wire UART, connect TX to the TMC UART pin through a 1k resistor.
31
+ """
32
+
33
+ def __init__(self, tx, rx, baudrate=115200, timeout=1.0):
34
+ """Initialize UART communication
35
+
36
+ Args:
37
+ tx: TX board pin (e.g., board.TX or board.GP0)
38
+ rx: RX board pin (e.g., board.RX or board.GP1)
39
+ baudrate: UART baudrate (default 115200)
40
+ timeout: Read timeout in seconds (default 1.0)
41
+ """
42
+ super().__init__()
43
+
44
+ self._tx_pin = tx
45
+ self._rx_pin = rx
46
+ self._baudrate = baudrate
47
+ self._timeout = timeout
48
+
49
+ self._uart = None
50
+
51
+ def init(self):
52
+ """Initialize UART hardware"""
53
+ self._uart = busio.UART(
54
+ tx=self._tx_pin,
55
+ rx=self._rx_pin,
56
+ baudrate=self._baudrate,
57
+ timeout=self._timeout,
58
+ )
59
+ # Create a fake serial object for compatibility with base class
60
+ self.ser = _FakeSerial(self._uart)
61
+
62
+ def deinit(self):
63
+ """Deinitialize UART hardware"""
64
+ if self._uart is not None:
65
+ self._uart.deinit()
66
+ self._uart = None
67
+ self.ser = None
68
+
69
+ def _uart_write(self, data):
70
+ """Write data to UART
71
+
72
+ Args:
73
+ data: Bytes to write
74
+
75
+ Returns:
76
+ Number of bytes written
77
+ """
78
+ if self._uart is None:
79
+ raise TmcComException("UART not initialized")
80
+
81
+ data_bytes = bytes(data)
82
+ self._uart.write(data_bytes)
83
+
84
+ time.sleep(0.01) # Small delay to ensure data is sent
85
+
86
+ return len(data_bytes)
87
+
88
+ def _uart_read(self, length):
89
+ """Read data from UART
90
+
91
+ Args:
92
+ length: Number of bytes to read
93
+
94
+ Returns:
95
+ Bytes read or empty bytes if timeout
96
+ """
97
+ if self._uart is None:
98
+ raise TmcComException("UART not initialized")
99
+
100
+ # Wait a bit for data to arrive
101
+ time.sleep(0.01) # 10ms in seconds (CircuitPython uses seconds)
102
+
103
+ result = self._uart.read(length)
104
+ if result is None:
105
+ return bytes()
106
+ return bytes(result)
107
+
108
+ def _uart_flush(self):
109
+ """Flush UART receive buffer"""
110
+ if self._uart is None:
111
+ raise TmcComException("UART not initialized")
112
+
113
+ # Reset input buffer
114
+ self._uart.reset_input_buffer()
115
+
116
+
117
+ class _FakeSerial:
118
+ """Fake serial object for compatibility with base class"""
119
+
120
+ def __init__(self, uart):
121
+ """Constructor for fake serial object"""
122
+ self._uart = uart
123
+ self.is_open = True
124
+
125
+ def close(self):
126
+ """Close the fake serial port"""
127
+ self.is_open = False
@@ -48,7 +48,10 @@ class TmcEnableControl:
48
48
 
49
49
  def deinit(self):
50
50
  """destructor"""
51
- self.set_motor_enabled(False)
51
+ # Only disable motor if callback is still available
52
+ # During garbage collection, parent object may already be destroyed
53
+ if self._get_register_callback is not None:
54
+ self.set_motor_enabled(False)
52
55
 
53
56
  @abstractmethod
54
57
  def set_motor_enabled(self, en):
@@ -4,10 +4,13 @@ import sys
4
4
  import time
5
5
 
6
6
  MICROPYTHON = sys.implementation.name == "micropython"
7
+ CIRCUITPYTHON = sys.implementation.name == "circuitpython"
7
8
 
8
9
 
9
10
  def get_time_us():
10
- """Get current time in microseconds, compatible with both CPython and MicroPython"""
11
+ """Get current time in microseconds, compatible with CPython, MicroPython and CircuitPython"""
11
12
  if MICROPYTHON:
12
13
  return time.ticks_us() # pylint: disable=no-member
14
+ if CIRCUITPYTHON:
15
+ return time.monotonic_ns() // 1000 # pylint: disable=no-member
13
16
  return time.time_ns() // 1000
@@ -55,12 +55,12 @@ class GStat(shared.GStat):
55
55
  def check(self):
56
56
  """check if the driver is ok"""
57
57
  self.read()
58
- if self.reset:
59
- raise TmcDriverException("TMC220X: reset detected")
60
58
  if self.uv_cp:
61
59
  raise TmcDriverException("TMC220X: undervoltage detected")
62
60
  if self.drv_err:
63
61
  raise TmcDriverException("TMC220X: driver error detected")
62
+ if self.reset:
63
+ raise TmcDriverException("TMC220X: reset detected")
64
64
 
65
65
 
66
66
  class IfCnt(shared.IfCnt):
@@ -192,9 +192,12 @@ class ChopConf(shared.ChopConf):
192
192
  @mres_ms.setter
193
193
  def mres_ms(self, mres: int):
194
194
  """set µstep resolution"""
195
- mres_bit = int(math.log2(mres))
196
- mres_bit = 8 - mres_bit
197
- self.mres = mres_bit
195
+ mres_to_bit = {1: 8, 2: 7, 4: 6, 8: 5, 16: 4, 32: 3, 64: 2, 128: 1, 256: 0}
196
+ if mres not in mres_to_bit:
197
+ raise ValueError(
198
+ f"Invalid mres value: {mres}. Must be power of 2 from 1 to 256"
199
+ )
200
+ self.mres = mres_to_bit[mres]
198
201
 
199
202
 
200
203
  class PwmConf(shared.PwmConf):
@@ -310,9 +310,12 @@ class ChopConf(shared.ChopConf):
310
310
  @mres_ms.setter
311
311
  def mres_ms(self, mres: int):
312
312
  """set µstep resolution"""
313
- mres_bit = int(math.log2(mres))
314
- mres_bit = 8 - mres_bit
315
- self.mres = mres_bit
313
+ mres_to_bit = {1: 8, 2: 7, 4: 6, 8: 5, 16: 4, 32: 3, 64: 2, 128: 1, 256: 0}
314
+ if mres not in mres_to_bit:
315
+ raise ValueError(
316
+ f"Invalid mres value: {mres}. Must be power of 2 from 1 to 256"
317
+ )
318
+ self.mres = mres_to_bit[mres]
316
319
 
317
320
 
318
321
  class CoolConf(shared.CoolConf):
@@ -452,9 +452,12 @@ class ChopConf(shared.ChopConf):
452
452
  @mres_ms.setter
453
453
  def mres_ms(self, mres: int):
454
454
  """set µstep resolution"""
455
- mres_bit = int(math.log2(mres))
456
- mres_bit = 8 - mres_bit
457
- self.mres = mres_bit
455
+ mres_to_bit = {1: 8, 2: 7, 4: 6, 8: 5, 16: 4, 32: 3, 64: 2, 128: 1, 256: 0}
456
+ if mres not in mres_to_bit:
457
+ raise ValueError(
458
+ f"Invalid mres value: {mres}. Must be power of 2 from 1 to 256"
459
+ )
460
+ self.mres = mres_to_bit[mres]
458
461
 
459
462
 
460
463
  class CoolConf(shared.CoolConf):
@@ -12,26 +12,32 @@ from ._tmc_gpio_board_base import *
12
12
 
13
13
 
14
14
  MICROPYTHON = sys.implementation.name == "micropython"
15
-
16
- # ------------------------------
17
- # LIB | BOARD
18
- # ------------------------------
19
- # RPi.GPIO | Pi4, Pi3 etc.
20
- # Jetson.GPIO | Nvidia Jetson
21
- # gpiozero | Pi5
22
- # pheriphery | Luckfox Pico
23
- # OPi.GPIO | Orange Pi
24
- # machine | MicroPython
25
- # ------------------------------
15
+ CIRCUITPYTHON = sys.implementation.name == "circuitpython"
16
+
17
+ # -------------------------------------
18
+ # LIB | BOARD
19
+ # -------------------------------------
20
+ # RPi.GPIO | Pi4, Pi3 etc.
21
+ # Jetson.GPIO | Nvidia Jetson
22
+ # gpiozero | Pi5
23
+ # pheriphery | Luckfox Pico
24
+ # OPi.GPIO | Orange Pi
25
+ # machine | MicroPython
26
+ # busio/digitalio | CircuitPython
27
+ # -------------------------------------
26
28
 
27
29
  if MICROPYTHON:
28
30
  from ._tmc_gpio_board_micropython import MicroPythonGPIOWrapper
29
31
 
30
32
  tmc_gpio = MicroPythonGPIOWrapper()
31
33
  BOARD = Board.MICROPYTHON
34
+ elif CIRCUITPYTHON:
35
+ from ._tmc_gpio_board_circuitpython import CircuitPythonGPIOWrapper
36
+
37
+ tmc_gpio = CircuitPythonGPIOWrapper()
38
+ BOARD = Board.CIRCUITPYTHON
32
39
  else:
33
40
  import os
34
- from ._tmc_gpio_board_ftdi import FtdiWrapper
35
41
 
36
42
  # Board mapping: (module_path, class_name, Board enum)
37
43
  board_mapping = {
@@ -99,3 +105,12 @@ else:
99
105
  return MockGPIOWrapper(), Board.UNKNOWN
100
106
 
101
107
  tmc_gpio, BOARD = initialize_gpio()
108
+
109
+ # Lazy import for FtdiWrapper to avoid importing pyftdi unless needed
110
+ def __getattr__(name):
111
+ """lazy import FtdiWrapper when accessed"""
112
+ if name == "FtdiWrapper":
113
+ from ._tmc_gpio_board_ftdi import FtdiWrapper
114
+
115
+ return FtdiWrapper
116
+ raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
@@ -19,6 +19,7 @@ class Board(IntEnum):
19
19
  LUCKFOX_PICO = 4
20
20
  ORANGE_PI = 5
21
21
  MICROPYTHON = 6
22
+ CIRCUITPYTHON = 7
22
23
 
23
24
 
24
25
  class Gpio(IntEnum):
@@ -0,0 +1,124 @@
1
+ # pylint: skip-file
2
+ """
3
+ CircuitPython GPIO module
4
+ """
5
+
6
+ import digitalio
7
+ import pwmio
8
+ from ._tmc_gpio_board_base import *
9
+
10
+
11
+ class CircuitPythonGPIOWrapper(BaseGPIOWrapper):
12
+ """CircuitPython GPIO wrapper for boards supporting CircuitPython.
13
+
14
+ Uses digitalio.DigitalInOut and pwmio.PWMOut for GPIO control.
15
+ Only available when running under CircuitPython.
16
+
17
+ Note: Pins should be board pin objects (e.g., board.D17) or
18
+ microcontroller pin objects, not integers.
19
+ """
20
+
21
+ def __init__(self):
22
+ """constructor"""
23
+ self._pins = {} # pin -> DigitalInOut object
24
+ self._pwms = {} # pin -> PWMOut object
25
+
26
+ def init(self, gpio_mode=None):
27
+ """initialize GPIO library - not needed for CircuitPython"""
28
+
29
+ def deinit(self):
30
+ """deinitialize GPIO library"""
31
+ for pin in list(self._pwms.keys()):
32
+ self.gpio_cleanup(pin)
33
+ for pin in list(self._pins.keys()):
34
+ self.gpio_cleanup(pin)
35
+
36
+ def gpio_setup(self, pin, mode, initial=0, pull_up_down=0):
37
+ """setup GPIO pin
38
+
39
+ Args:
40
+ pin: Board pin object (e.g., board.D17) or microcontroller pin
41
+ mode: GpioMode.OUT or GpioMode.IN
42
+ initial: Initial value for output pins
43
+ pull_up_down: Pull-up/down configuration
44
+ """
45
+ pin_obj = digitalio.DigitalInOut(pin)
46
+
47
+ if mode == GpioMode.OUT:
48
+ pin_obj.direction = digitalio.Direction.OUTPUT
49
+ pin_obj.value = bool(initial)
50
+ else:
51
+ pin_obj.direction = digitalio.Direction.INPUT
52
+ # Configure pull-up/down
53
+ if pull_up_down == GpioPUD.PUD_UP:
54
+ pin_obj.pull = digitalio.Pull.UP
55
+ elif pull_up_down == GpioPUD.PUD_DOWN:
56
+ pin_obj.pull = digitalio.Pull.DOWN
57
+ else:
58
+ pin_obj.pull = None
59
+
60
+ self._pins[pin] = pin_obj
61
+
62
+ def gpio_cleanup(self, pin):
63
+ """cleanup GPIO pin"""
64
+ if pin in self._pwms:
65
+ self._pwms[pin].deinit()
66
+ del self._pwms[pin]
67
+ if pin in self._pins:
68
+ self._pins[pin].deinit()
69
+ del self._pins[pin]
70
+
71
+ def gpio_input(self, pin):
72
+ """read GPIO pin
73
+
74
+ Returns:
75
+ 1 if HIGH, 0 if LOW
76
+ """
77
+ if pin in self._pins:
78
+ return 1 if self._pins[pin].value else 0
79
+ return 0
80
+
81
+ def gpio_output(self, pin, value):
82
+ """write GPIO pin"""
83
+ if pin in self._pins:
84
+ self._pins[pin].value = bool(value)
85
+
86
+ def gpio_pwm_setup(self, pin, frequency=10, duty_cycle=0):
87
+ """setup PWM
88
+
89
+ Args:
90
+ pin: Board pin object (e.g., board.D17)
91
+ frequency: PWM frequency in Hz
92
+ duty_cycle: Duty cycle in percent (0-100)
93
+ """
94
+ # CircuitPython PWM uses 0-65535 for duty_cycle
95
+ duty_u16 = int(duty_cycle / 100 * 65535)
96
+ self._pwms[pin] = pwmio.PWMOut(pin, frequency=frequency, duty_cycle=duty_u16)
97
+
98
+ def gpio_pwm_set_frequency(self, pin, frequency):
99
+ """set PWM frequency"""
100
+ if pin in self._pwms:
101
+ self._pwms[pin].frequency = frequency
102
+
103
+ def gpio_pwm_set_duty_cycle(self, pin, duty_cycle):
104
+ """set PWM duty cycle
105
+
106
+ Args:
107
+ pin: Board pin object
108
+ duty_cycle: Duty cycle in percent (0-100)
109
+ """
110
+ if pin in self._pwms:
111
+ # Convert percent to 0-65535
112
+ self._pwms[pin].duty_cycle = int(duty_cycle * 655.35)
113
+
114
+ def gpio_add_event_detect(self, pin, callback):
115
+ """add event detect (rising edge interrupt)"""
116
+ # CircuitPython doesn't have Pin.irq() - would need asyncio
117
+ raise NotImplementedError(
118
+ "GPIO interrupts not directly supported in CircuitPython."
119
+ )
120
+
121
+ def gpio_remove_event_detect(self, pin):
122
+ """remove event detect"""
123
+ # Not implemented - see gpio_add_event_detect
124
+ pass
@@ -7,8 +7,9 @@ import sys
7
7
  from ._tmc_logger_base import Loglevel
8
8
 
9
9
  MICROPYTHON = sys.implementation.name == "micropython"
10
+ CIRCUITPYTHON = sys.implementation.name == "circuitpython"
10
11
 
11
- if MICROPYTHON:
12
+ if MICROPYTHON or CIRCUITPYTHON:
12
13
  from ._tmc_logger_micropython import TmcLogger
13
14
  else:
14
15
  import logging