Skip to content

Commit

Permalink
added register definitions of TMC2240 (not tested) #95
Browse files Browse the repository at this point in the history
  • Loading branch information
Chr157i4n committed Mar 5, 2025
1 parent 1cb0fc6 commit 5b1de0f
Show file tree
Hide file tree
Showing 44 changed files with 1,188 additions and 310 deletions.
16 changes: 10 additions & 6 deletions src/tmc_driver/_tmc_stepperdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import logging
from ._tmc_gpio_board import Gpio, GpioMode, Board, BOARD, tmc_gpio
from .motion_control._tmc_mc import TmcMotionControl, MovementAbsRel, MovementPhase, StopMode
from .motion_control._tmc_mc import TmcMotionControl, MovementAbsRel, MovementPhase, StopMode, Direction
from .enable_control._tmc_ec import TmcEnableControl
from .enable_control._tmc_ec_pin import TmcEnableControlPin
from .motion_control._tmc_mc_step_dir import TmcMotionControlStepDir
Expand All @@ -39,11 +39,6 @@ class TmcStepperDriver:



from ._tmc_test import (
test_step
)


# Constructor/Destructor
# ----------------------------
def __init__(self,
Expand Down Expand Up @@ -264,3 +259,12 @@ def run_to_position_steps(self, steps, movement_abs_rel:MovementAbsRel = None):
"""motioncontrol wrapper"""
if self.tmc_mc is not None:
self.tmc_mc.run_to_position_steps(steps, movement_abs_rel)


# StepperDriver methods
# ----------------------------
def test_step(self):
"""test method"""
for _ in range(100):
self.tmc_mc.set_direction(Direction.CW)
self.tmc_mc.make_a_step()
188 changes: 0 additions & 188 deletions src/tmc_driver/_tmc_test.py
Original file line number Diff line number Diff line change
@@ -1,189 +1 @@
#pylint: disable=too-many-public-methods
#pylint: disable=too-many-branches
#pylint: disable=protected-access
#pylint: disable=no-member
#pylint: disable=bare-except
#pylint: disable=duplicate-code
"""
Tmc2209 stepper driver test module
"""

import time
from ._tmc_gpio_board import tmc_gpio, Gpio
from ._tmc_logger import Loglevel
from .motion_control._tmc_mc import MovementAbsRel, MovementPhase
from .reg.bitfields import _tmc_220x_ioin as tmc_ioin_reg
from .reg._tmc_220x_reg_addr import TmcRegAddr


def test_step(self):
"""test method"""
self.set_direction_pin(1)

for _ in range(100):
self._current_pos += 1
tmc_gpio.gpio_output(self._pin_step, Gpio.HIGH)
time.sleep(0.001)
tmc_gpio.gpio_output(self._pin_step, Gpio.LOW)
time.sleep(0.01)


def test_pin(self, pin, ioin_reg_bp):
"""tests one pin
this function checks the connection to a pin
by toggling it and reading the IOIN register
"""
pin_ok = True

tmc_gpio.gpio_output(self.tmc_mc._pin_dir, Gpio.HIGH)
tmc_gpio.gpio_output(self.tmc_mc._pin_step, Gpio.HIGH)
tmc_gpio.gpio_output(self._pin_en, Gpio.HIGH)

ioin = self.read_ioin()
if not ioin.data >> ioin_reg_bp & 0x1:
pin_ok = False

tmc_gpio.gpio_output(pin, Gpio.LOW)
time.sleep(0.1)

ioin = self.read_ioin()
if ioin.data >> ioin_reg_bp & 0x1:
pin_ok = False

return pin_ok


def test_dir_step_en(self):
"""tests the EN, DIR and STEP pin
this sets the EN, DIR and STEP pin to HIGH, LOW and HIGH
and checks the IOIN Register of the TMC meanwhile
"""
pin_dir_ok = self.test_pin(self.tmc_mc._pin_dir, tmc_ioin_reg.dir_bp)
pin_step_ok = self.test_pin(self.tmc_mc._pin_step, tmc_ioin_reg.step_bp)
pin_en_ok = self.test_pin(self._pin_en, tmc_ioin_reg.enn_bp)

self.set_motor_enabled(False)

self.tmc_logger.log("---")
if pin_dir_ok:
self.tmc_logger.log("Pin DIR: \tOK")
else:
self.tmc_logger.log("Pin DIR: \tnot OK")
if pin_step_ok:
self.tmc_logger.log("Pin STEP: \tOK")
else:
self.tmc_logger.log("Pin STEP: \tnot OK")
if pin_en_ok:
self.tmc_logger.log("Pin EN: \tOK")
else:
self.tmc_logger.log("Pin EN: \tnot OK")
self.tmc_logger.log("---")


def test_com(self):
"""test method"""
self.tmc_logger.log("---")
self.tmc_logger.log("TEST COM")
result = self.tmc_com.test_com(TmcRegAddr.IOIN)

snd = result[0]
rtn = result[1]

status = True

self.tmc_logger.log(f"length snd: {len(snd)}", Loglevel.DEBUG)
self.tmc_logger.log(f"length rtn: {len(rtn)}", Loglevel.DEBUG)


self.tmc_logger.log("complete messages:", Loglevel.DEBUG)
self.tmc_logger.log(str(snd.hex()), Loglevel.DEBUG)
self.tmc_logger.log(str(rtn.hex()), Loglevel.DEBUG)

self.tmc_logger.log("just the first 4 bytes:", Loglevel.DEBUG)
self.tmc_logger.log(str(snd[0:4].hex()), Loglevel.DEBUG)
self.tmc_logger.log(str(rtn[0:4].hex()), Loglevel.DEBUG)

if len(rtn)==12:
self.tmc_logger.log("""the Raspberry Pi received the sent
bytes and the answer from the TMC""", Loglevel.DEBUG)
elif len(rtn)==4:
self.tmc_logger.log("the Raspberry Pi received only the sent bytes",
Loglevel.ERROR)
status = False
elif len(rtn)==0:
self.tmc_logger.log("the Raspberry Pi did not receive anything",
Loglevel.ERROR)
status = False
else:
self.tmc_logger.log(f"the Raspberry Pi received an unexpected amount of bytes: {len(rtn)}",
Loglevel.ERROR)
status = False

if snd[0:4] == rtn[0:4]:
self.tmc_logger.log("""the Raspberry Pi received exactly the bytes it has send.
the first 4 bytes are the same""", Loglevel.DEBUG)
else:
self.tmc_logger.log("""the Raspberry Pi did not received the bytes it has send.
the first 4 bytes are different""", Loglevel.DEBUG)
status = False

self.tmc_logger.log("---")
if status:
self.tmc_logger.log("UART connection: OK", Loglevel.INFO)
else:
self.tmc_logger.log("UART connection: not OK", Loglevel.ERROR)

self.tmc_logger.log("---")
return status


def test_stallguard_threshold(self, steps):
"""test method for tuning stallguard threshold
run this function with your motor settings and your motor load
the function will determine the minimum stallguard results for each movement phase
Args:
steps (int):
"""

self.tmc_logger.log("---", Loglevel.INFO)
self.tmc_logger.log("test_stallguard_threshold", Loglevel.INFO)

self.set_spreadcycle(0)

min_stallguard_result_accel = 511
min_stallguard_result_maxspeed = 511
min_stallguard_result_decel = 511

self.tmc_mc.run_to_position_steps_threaded(steps, MovementAbsRel.RELATIVE)


while self.tmc_mc.movement_phase != MovementPhase.STANDSTILL:
stallguard_result = self.get_stallguard_result()

self.tmc_logger.log(f"{self.tmc_mc.movement_phase} | {stallguard_result}",
Loglevel.INFO)

if (self.tmc_mc.movement_phase == MovementPhase.ACCELERATING and
stallguard_result < min_stallguard_result_accel):
min_stallguard_result_accel = stallguard_result
if (self.tmc_mc.movement_phase == MovementPhase.MAXSPEED and
stallguard_result < min_stallguard_result_maxspeed):
min_stallguard_result_maxspeed = stallguard_result
if (self.tmc_mc.movement_phase == MovementPhase.DECELERATING and
stallguard_result < min_stallguard_result_decel):
min_stallguard_result_decel = stallguard_result

self.tmc_mc.wait_for_movement_finished_threaded()

self.tmc_logger.log("---", Loglevel.INFO)
self.tmc_logger.log(f"min StallGuard result during accel: {min_stallguard_result_accel}",
Loglevel.INFO)
self.tmc_logger.log(f"min StallGuard result during maxspeed: {min_stallguard_result_maxspeed}",
Loglevel.INFO)
self.tmc_logger.log(f"min StallGuard result during decel: {min_stallguard_result_decel}",
Loglevel.INFO)
self.tmc_logger.log("---", Loglevel.INFO)
4 changes: 2 additions & 2 deletions src/tmc_driver/com/_tmc_com.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import time
import struct
from typing import List
from ..reg._tmc_220x_reg_addr import TmcRegAddr
from ..reg._tmc_gstat import GStat
from ..reg._tmc_reg_addr import TmcRegAddr
from ..reg.tmc220x._tmc_gstat import GStat
from .._tmc_logger import TmcLogger, Loglevel


Expand Down
2 changes: 1 addition & 1 deletion src/tmc_driver/enable_control/_tmc_ec_toff.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from ._tmc_ec import TmcEnableControl
from ..com._tmc_com import TmcCom
from ..reg._tmc_chopconf import ChopConf
from ..reg.tmc220x._tmc_chopconf import ChopConf


class TmcEnableControlToff(TmcEnableControl):
Expand Down
2 changes: 1 addition & 1 deletion src/tmc_driver/motion_control/_tmc_mc_step_reg.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from ..com._tmc_com import TmcCom
from .._tmc_logger import Loglevel
from .._tmc_gpio_board import tmc_gpio, Gpio, GpioMode
from ..reg._tmc_gconf import GConf
from ..reg.tmc220x._tmc_gconf import GConf


class TmcMotionControlStepReg(TmcMotionControlStepDir):
Expand Down
2 changes: 1 addition & 1 deletion src/tmc_driver/motion_control/_tmc_mc_vactual.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from ..com._tmc_com import TmcCom
from .._tmc_logger import Loglevel
from .. import _tmc_math as tmc_math
from ..reg._tmc_220x_reg_addr import TmcRegAddr
from ..reg._tmc_reg_addr import TmcRegAddr


class TmcMotionControlVActual(TmcMotionControl):
Expand Down
22 changes: 0 additions & 22 deletions src/tmc_driver/reg/_tmc_220x_reg_addr.py

This file was deleted.

5 changes: 2 additions & 3 deletions src/tmc_driver/reg/_tmc_reg.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
"""

from .._tmc_logger import TmcLogger, Loglevel
from ._tmc_220x_reg_addr import TmcRegAddr


class TmcReg():
"""Register class"""

addr: TmcRegAddr
data: int
addr = None
data: int = None


def deserialise(self, data:int):
Expand Down
10 changes: 10 additions & 0 deletions src/tmc_driver/reg/_tmc_reg_addr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"""
this file contains the hexadecimal addresses of the different registers
"""

from enum import Enum


class TmcRegAddr(Enum):
"""Enum for the register addresses of the TMC"""
NONE = 0x0
25 changes: 0 additions & 25 deletions src/tmc_driver/reg/bitfields/_tmc_220x_ioin.py

This file was deleted.

File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@

import math
from .bitfields import _tmc_220x_chopconf as bit
from ._tmc_reg import *
from ._tmc_reg_addr import *
from .._tmc_reg import *


class ChopConf(TmcReg):
"""Chopper Configuration register"""

data: int

diss2vs: bool
diss2g: bool
dedge: bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@
"""

from .bitfields import _tmc_220x_drvstatus as bit
from ._tmc_reg import *
from ._tmc_reg_addr import *
from .._tmc_reg import *


class DrvStatus(TmcReg):
"""Driver Status register"""

data: int

stst: bool # standstill indicator
stealth: bool # StealthChop indicator

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@
"""

from .bitfields import _tmc_220x_gconf as bit
from ._tmc_reg import *
from ._tmc_reg_addr import *
from .._tmc_reg import *


class GConf(TmcReg):
"""General Configuration register"""

data: int

i_scale_analog: bool
internal_rsense: bool
en_spreadcycle: bool
Expand Down
Loading

0 comments on commit 5b1de0f

Please sign in to comment.