Skip to content

Commit 5b1de0f

Browse files
committed
added register definitions of TMC2240 (not tested) #95
1 parent 1cb0fc6 commit 5b1de0f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1188
-310
lines changed

src/tmc_driver/_tmc_stepperdriver.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import logging
1414
from ._tmc_gpio_board import Gpio, GpioMode, Board, BOARD, tmc_gpio
15-
from .motion_control._tmc_mc import TmcMotionControl, MovementAbsRel, MovementPhase, StopMode
15+
from .motion_control._tmc_mc import TmcMotionControl, MovementAbsRel, MovementPhase, StopMode, Direction
1616
from .enable_control._tmc_ec import TmcEnableControl
1717
from .enable_control._tmc_ec_pin import TmcEnableControlPin
1818
from .motion_control._tmc_mc_step_dir import TmcMotionControlStepDir
@@ -39,11 +39,6 @@ class TmcStepperDriver:
3939

4040

4141

42-
from ._tmc_test import (
43-
test_step
44-
)
45-
46-
4742
# Constructor/Destructor
4843
# ----------------------------
4944
def __init__(self,
@@ -264,3 +259,12 @@ def run_to_position_steps(self, steps, movement_abs_rel:MovementAbsRel = None):
264259
"""motioncontrol wrapper"""
265260
if self.tmc_mc is not None:
266261
self.tmc_mc.run_to_position_steps(steps, movement_abs_rel)
262+
263+
264+
# StepperDriver methods
265+
# ----------------------------
266+
def test_step(self):
267+
"""test method"""
268+
for _ in range(100):
269+
self.tmc_mc.set_direction(Direction.CW)
270+
self.tmc_mc.make_a_step()

src/tmc_driver/_tmc_test.py

-188
Original file line numberDiff line numberDiff line change
@@ -1,189 +1 @@
1-
#pylint: disable=too-many-public-methods
2-
#pylint: disable=too-many-branches
3-
#pylint: disable=protected-access
4-
#pylint: disable=no-member
5-
#pylint: disable=bare-except
6-
#pylint: disable=duplicate-code
7-
"""
8-
Tmc2209 stepper driver test module
9-
"""
101

11-
import time
12-
from ._tmc_gpio_board import tmc_gpio, Gpio
13-
from ._tmc_logger import Loglevel
14-
from .motion_control._tmc_mc import MovementAbsRel, MovementPhase
15-
from .reg.bitfields import _tmc_220x_ioin as tmc_ioin_reg
16-
from .reg._tmc_220x_reg_addr import TmcRegAddr
17-
18-
19-
def test_step(self):
20-
"""test method"""
21-
self.set_direction_pin(1)
22-
23-
for _ in range(100):
24-
self._current_pos += 1
25-
tmc_gpio.gpio_output(self._pin_step, Gpio.HIGH)
26-
time.sleep(0.001)
27-
tmc_gpio.gpio_output(self._pin_step, Gpio.LOW)
28-
time.sleep(0.01)
29-
30-
31-
def test_pin(self, pin, ioin_reg_bp):
32-
"""tests one pin
33-
34-
this function checks the connection to a pin
35-
by toggling it and reading the IOIN register
36-
"""
37-
pin_ok = True
38-
39-
tmc_gpio.gpio_output(self.tmc_mc._pin_dir, Gpio.HIGH)
40-
tmc_gpio.gpio_output(self.tmc_mc._pin_step, Gpio.HIGH)
41-
tmc_gpio.gpio_output(self._pin_en, Gpio.HIGH)
42-
43-
ioin = self.read_ioin()
44-
if not ioin.data >> ioin_reg_bp & 0x1:
45-
pin_ok = False
46-
47-
tmc_gpio.gpio_output(pin, Gpio.LOW)
48-
time.sleep(0.1)
49-
50-
ioin = self.read_ioin()
51-
if ioin.data >> ioin_reg_bp & 0x1:
52-
pin_ok = False
53-
54-
return pin_ok
55-
56-
57-
def test_dir_step_en(self):
58-
"""tests the EN, DIR and STEP pin
59-
60-
this sets the EN, DIR and STEP pin to HIGH, LOW and HIGH
61-
and checks the IOIN Register of the TMC meanwhile
62-
"""
63-
pin_dir_ok = self.test_pin(self.tmc_mc._pin_dir, tmc_ioin_reg.dir_bp)
64-
pin_step_ok = self.test_pin(self.tmc_mc._pin_step, tmc_ioin_reg.step_bp)
65-
pin_en_ok = self.test_pin(self._pin_en, tmc_ioin_reg.enn_bp)
66-
67-
self.set_motor_enabled(False)
68-
69-
self.tmc_logger.log("---")
70-
if pin_dir_ok:
71-
self.tmc_logger.log("Pin DIR: \tOK")
72-
else:
73-
self.tmc_logger.log("Pin DIR: \tnot OK")
74-
if pin_step_ok:
75-
self.tmc_logger.log("Pin STEP: \tOK")
76-
else:
77-
self.tmc_logger.log("Pin STEP: \tnot OK")
78-
if pin_en_ok:
79-
self.tmc_logger.log("Pin EN: \tOK")
80-
else:
81-
self.tmc_logger.log("Pin EN: \tnot OK")
82-
self.tmc_logger.log("---")
83-
84-
85-
def test_com(self):
86-
"""test method"""
87-
self.tmc_logger.log("---")
88-
self.tmc_logger.log("TEST COM")
89-
result = self.tmc_com.test_com(TmcRegAddr.IOIN)
90-
91-
snd = result[0]
92-
rtn = result[1]
93-
94-
status = True
95-
96-
self.tmc_logger.log(f"length snd: {len(snd)}", Loglevel.DEBUG)
97-
self.tmc_logger.log(f"length rtn: {len(rtn)}", Loglevel.DEBUG)
98-
99-
100-
self.tmc_logger.log("complete messages:", Loglevel.DEBUG)
101-
self.tmc_logger.log(str(snd.hex()), Loglevel.DEBUG)
102-
self.tmc_logger.log(str(rtn.hex()), Loglevel.DEBUG)
103-
104-
self.tmc_logger.log("just the first 4 bytes:", Loglevel.DEBUG)
105-
self.tmc_logger.log(str(snd[0:4].hex()), Loglevel.DEBUG)
106-
self.tmc_logger.log(str(rtn[0:4].hex()), Loglevel.DEBUG)
107-
108-
if len(rtn)==12:
109-
self.tmc_logger.log("""the Raspberry Pi received the sent
110-
bytes and the answer from the TMC""", Loglevel.DEBUG)
111-
elif len(rtn)==4:
112-
self.tmc_logger.log("the Raspberry Pi received only the sent bytes",
113-
Loglevel.ERROR)
114-
status = False
115-
elif len(rtn)==0:
116-
self.tmc_logger.log("the Raspberry Pi did not receive anything",
117-
Loglevel.ERROR)
118-
status = False
119-
else:
120-
self.tmc_logger.log(f"the Raspberry Pi received an unexpected amount of bytes: {len(rtn)}",
121-
Loglevel.ERROR)
122-
status = False
123-
124-
if snd[0:4] == rtn[0:4]:
125-
self.tmc_logger.log("""the Raspberry Pi received exactly the bytes it has send.
126-
the first 4 bytes are the same""", Loglevel.DEBUG)
127-
else:
128-
self.tmc_logger.log("""the Raspberry Pi did not received the bytes it has send.
129-
the first 4 bytes are different""", Loglevel.DEBUG)
130-
status = False
131-
132-
self.tmc_logger.log("---")
133-
if status:
134-
self.tmc_logger.log("UART connection: OK", Loglevel.INFO)
135-
else:
136-
self.tmc_logger.log("UART connection: not OK", Loglevel.ERROR)
137-
138-
self.tmc_logger.log("---")
139-
return status
140-
141-
142-
def test_stallguard_threshold(self, steps):
143-
"""test method for tuning stallguard threshold
144-
145-
run this function with your motor settings and your motor load
146-
the function will determine the minimum stallguard results for each movement phase
147-
148-
Args:
149-
steps (int):
150-
"""
151-
152-
self.tmc_logger.log("---", Loglevel.INFO)
153-
self.tmc_logger.log("test_stallguard_threshold", Loglevel.INFO)
154-
155-
self.set_spreadcycle(0)
156-
157-
min_stallguard_result_accel = 511
158-
min_stallguard_result_maxspeed = 511
159-
min_stallguard_result_decel = 511
160-
161-
self.tmc_mc.run_to_position_steps_threaded(steps, MovementAbsRel.RELATIVE)
162-
163-
164-
while self.tmc_mc.movement_phase != MovementPhase.STANDSTILL:
165-
stallguard_result = self.get_stallguard_result()
166-
167-
self.tmc_logger.log(f"{self.tmc_mc.movement_phase} | {stallguard_result}",
168-
Loglevel.INFO)
169-
170-
if (self.tmc_mc.movement_phase == MovementPhase.ACCELERATING and
171-
stallguard_result < min_stallguard_result_accel):
172-
min_stallguard_result_accel = stallguard_result
173-
if (self.tmc_mc.movement_phase == MovementPhase.MAXSPEED and
174-
stallguard_result < min_stallguard_result_maxspeed):
175-
min_stallguard_result_maxspeed = stallguard_result
176-
if (self.tmc_mc.movement_phase == MovementPhase.DECELERATING and
177-
stallguard_result < min_stallguard_result_decel):
178-
min_stallguard_result_decel = stallguard_result
179-
180-
self.tmc_mc.wait_for_movement_finished_threaded()
181-
182-
self.tmc_logger.log("---", Loglevel.INFO)
183-
self.tmc_logger.log(f"min StallGuard result during accel: {min_stallguard_result_accel}",
184-
Loglevel.INFO)
185-
self.tmc_logger.log(f"min StallGuard result during maxspeed: {min_stallguard_result_maxspeed}",
186-
Loglevel.INFO)
187-
self.tmc_logger.log(f"min StallGuard result during decel: {min_stallguard_result_decel}",
188-
Loglevel.INFO)
189-
self.tmc_logger.log("---", Loglevel.INFO)

src/tmc_driver/com/_tmc_com.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
import time
1010
import struct
1111
from typing import List
12-
from ..reg._tmc_220x_reg_addr import TmcRegAddr
13-
from ..reg._tmc_gstat import GStat
12+
from ..reg._tmc_reg_addr import TmcRegAddr
13+
from ..reg.tmc220x._tmc_gstat import GStat
1414
from .._tmc_logger import TmcLogger, Loglevel
1515

1616

src/tmc_driver/enable_control/_tmc_ec_toff.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from ._tmc_ec import TmcEnableControl
66
from ..com._tmc_com import TmcCom
7-
from ..reg._tmc_chopconf import ChopConf
7+
from ..reg.tmc220x._tmc_chopconf import ChopConf
88

99

1010
class TmcEnableControlToff(TmcEnableControl):

src/tmc_driver/motion_control/_tmc_mc_step_reg.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from ..com._tmc_com import TmcCom
99
from .._tmc_logger import Loglevel
1010
from .._tmc_gpio_board import tmc_gpio, Gpio, GpioMode
11-
from ..reg._tmc_gconf import GConf
11+
from ..reg.tmc220x._tmc_gconf import GConf
1212

1313

1414
class TmcMotionControlStepReg(TmcMotionControlStepDir):

src/tmc_driver/motion_control/_tmc_mc_vactual.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from ..com._tmc_com import TmcCom
1313
from .._tmc_logger import Loglevel
1414
from .. import _tmc_math as tmc_math
15-
from ..reg._tmc_220x_reg_addr import TmcRegAddr
15+
from ..reg._tmc_reg_addr import TmcRegAddr
1616

1717

1818
class TmcMotionControlVActual(TmcMotionControl):

src/tmc_driver/reg/_tmc_220x_reg_addr.py

-22
This file was deleted.

src/tmc_driver/reg/_tmc_reg.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@
55
"""
66

77
from .._tmc_logger import TmcLogger, Loglevel
8-
from ._tmc_220x_reg_addr import TmcRegAddr
98

109

1110
class TmcReg():
1211
"""Register class"""
1312

14-
addr: TmcRegAddr
15-
data: int
13+
addr = None
14+
data: int = None
1615

1716

1817
def deserialise(self, data:int):

src/tmc_driver/reg/_tmc_reg_addr.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"""
2+
this file contains the hexadecimal addresses of the different registers
3+
"""
4+
5+
from enum import Enum
6+
7+
8+
class TmcRegAddr(Enum):
9+
"""Enum for the register addresses of the TMC"""
10+
NONE = 0x0

src/tmc_driver/reg/bitfields/_tmc_220x_ioin.py

-25
This file was deleted.

src/tmc_driver/reg/_tmc_chopconf.py renamed to src/tmc_driver/reg/tmc220x/_tmc_chopconf.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@
77

88
import math
99
from .bitfields import _tmc_220x_chopconf as bit
10-
from ._tmc_reg import *
10+
from ._tmc_reg_addr import *
11+
from .._tmc_reg import *
1112

1213

1314
class ChopConf(TmcReg):
1415
"""Chopper Configuration register"""
1516

16-
data: int
17-
1817
diss2vs: bool
1918
diss2g: bool
2019
dedge: bool

src/tmc_driver/reg/_tmc_drvstatus.py renamed to src/tmc_driver/reg/tmc220x/_tmc_drvstatus.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@
66
"""
77

88
from .bitfields import _tmc_220x_drvstatus as bit
9-
from ._tmc_reg import *
9+
from ._tmc_reg_addr import *
10+
from .._tmc_reg import *
1011

1112

1213
class DrvStatus(TmcReg):
1314
"""Driver Status register"""
1415

15-
data: int
16-
1716
stst: bool # standstill indicator
1817
stealth: bool # StealthChop indicator
1918

src/tmc_driver/reg/_tmc_gconf.py renamed to src/tmc_driver/reg/tmc220x/_tmc_gconf.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@
66
"""
77

88
from .bitfields import _tmc_220x_gconf as bit
9-
from ._tmc_reg import *
9+
from ._tmc_reg_addr import *
10+
from .._tmc_reg import *
1011

1112

1213
class GConf(TmcReg):
1314
"""General Configuration register"""
1415

15-
data: int
16-
1716
i_scale_analog: bool
1817
internal_rsense: bool
1918
en_spreadcycle: bool

0 commit comments

Comments
 (0)