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,81 @@
|
|
|
1
|
+
# pylint: disable=broad-exception-caught
|
|
2
|
+
# pylint: disable=unused-import
|
|
3
|
+
# pylint: disable=too-few-public-methods
|
|
4
|
+
# pylint: disable=too-many-arguments
|
|
5
|
+
# pylint: disable=too-many-positional-arguments
|
|
6
|
+
"""
|
|
7
|
+
TmcComSpi stepper driver spi module
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
import spidev
|
|
11
|
+
from ._tmc_com_spi_base import TmcComSpiBase, TmcLogger, Loglevel
|
|
12
|
+
from .._tmc_exceptions import TmcComException, TmcDriverException
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class TmcComSpi(TmcComSpiBase):
|
|
16
|
+
"""TmcComSpi
|
|
17
|
+
|
|
18
|
+
this class is used to communicate with the TMC via SPI
|
|
19
|
+
it can be used to change the settings of the TMC.
|
|
20
|
+
like the current or the microsteppingmode
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
def __init__(
|
|
24
|
+
self,
|
|
25
|
+
spi_bus: int,
|
|
26
|
+
spi_dev: int,
|
|
27
|
+
spi_speed: int = 8000000,
|
|
28
|
+
):
|
|
29
|
+
"""constructor
|
|
30
|
+
|
|
31
|
+
Args:
|
|
32
|
+
spi_bus (int): SPI bus number
|
|
33
|
+
spi_dev (int): SPI device number
|
|
34
|
+
spi_speed (int, optional): SPI speed in Hz. Defaults to 8000000.
|
|
35
|
+
"""
|
|
36
|
+
super().__init__()
|
|
37
|
+
|
|
38
|
+
self.spi = spidev.SpiDev()
|
|
39
|
+
|
|
40
|
+
self._spi_bus = spi_bus
|
|
41
|
+
self._spi_dev = spi_dev
|
|
42
|
+
self._spi_speed = spi_speed
|
|
43
|
+
|
|
44
|
+
def init(self):
|
|
45
|
+
"""init"""
|
|
46
|
+
try:
|
|
47
|
+
self.spi.open(self._spi_bus, self._spi_dev)
|
|
48
|
+
except Exception as e:
|
|
49
|
+
self._tmc_logger.log(f"Error opening SPI: {e}", Loglevel.ERROR)
|
|
50
|
+
errnum = e.args[0]
|
|
51
|
+
if errnum == 2:
|
|
52
|
+
self._tmc_logger.log(
|
|
53
|
+
f"SPI Device {self._spi_dev} on Bus {self._spi_bus} does not exist.",
|
|
54
|
+
Loglevel.ERROR,
|
|
55
|
+
)
|
|
56
|
+
self._tmc_logger.log(
|
|
57
|
+
'You need to activate the SPI interface with "sudo raspi-config"',
|
|
58
|
+
Loglevel.ERROR,
|
|
59
|
+
)
|
|
60
|
+
raise SystemExit from e
|
|
61
|
+
|
|
62
|
+
self.spi.max_speed_hz = self._spi_speed
|
|
63
|
+
self.spi.mode = 0b11
|
|
64
|
+
self.spi.lsbfirst = False
|
|
65
|
+
|
|
66
|
+
def __del__(self):
|
|
67
|
+
self.deinit()
|
|
68
|
+
|
|
69
|
+
def deinit(self):
|
|
70
|
+
"""destructor"""
|
|
71
|
+
|
|
72
|
+
def _spi_transfer(self, data: list) -> list:
|
|
73
|
+
"""Perform SPI transfer using spidev
|
|
74
|
+
|
|
75
|
+
Args:
|
|
76
|
+
data: Data to send
|
|
77
|
+
|
|
78
|
+
Returns:
|
|
79
|
+
Received data
|
|
80
|
+
"""
|
|
81
|
+
return self.spi.xfer2(data)
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
# pylint: disable=unused-import
|
|
2
|
+
# pylint: disable=wildcard-import
|
|
3
|
+
# pylint: disable=unused-wildcard-import
|
|
4
|
+
"""
|
|
5
|
+
TmcComSpiBase - Abstract base class for SPI communication
|
|
6
|
+
This class contains no hardware-specific imports (no spidev, no pyftdi)
|
|
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 TmcComSpiBase(TmcCom):
|
|
16
|
+
"""TmcComSpiBase
|
|
17
|
+
|
|
18
|
+
Abstract base class for SPI communication with TMC drivers.
|
|
19
|
+
This class contains common SPI functionality without hardware-specific imports.
|
|
20
|
+
Subclasses must implement the actual SPI transfer methods.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
def __init__(self):
|
|
24
|
+
"""constructor"""
|
|
25
|
+
super().__init__()
|
|
26
|
+
|
|
27
|
+
self.spi = None # To be set by subclass
|
|
28
|
+
|
|
29
|
+
self._r_frame = [0x55, 0, 0, 0, 0]
|
|
30
|
+
self._w_frame = [0x55, 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 _spi_transfer(self, data: list) -> list:
|
|
42
|
+
"""Perform SPI transfer - to be implemented by subclass
|
|
43
|
+
|
|
44
|
+
Args:
|
|
45
|
+
data: Data to send
|
|
46
|
+
|
|
47
|
+
Returns:
|
|
48
|
+
Received data
|
|
49
|
+
"""
|
|
50
|
+
|
|
51
|
+
def read_reg(self, addr: int) -> tuple[list, dict]:
|
|
52
|
+
"""reads the registry on the TMC with a given address.
|
|
53
|
+
returns the binary value of that register
|
|
54
|
+
|
|
55
|
+
Args:
|
|
56
|
+
addr (int): HEX, which register to read
|
|
57
|
+
Returns:
|
|
58
|
+
int: register value
|
|
59
|
+
Dict: flags
|
|
60
|
+
"""
|
|
61
|
+
self._w_frame = [addr, 0x00, 0x00, 0x00, 0x00]
|
|
62
|
+
dummy_data = [0x00, 0x00, 0x00, 0x00, 0x00]
|
|
63
|
+
|
|
64
|
+
self._spi_transfer(self._w_frame)
|
|
65
|
+
rtn = self._spi_transfer(dummy_data)
|
|
66
|
+
|
|
67
|
+
flags = {
|
|
68
|
+
"reset_flag": rtn[0] >> 0 & 0x01,
|
|
69
|
+
"driver_error": rtn[0] >> 1 & 0x01,
|
|
70
|
+
"sg2": rtn[0] >> 2 & 0x01,
|
|
71
|
+
"standstill": rtn[0] >> 3 & 0x01,
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if flags["reset_flag"]:
|
|
75
|
+
raise TmcDriverException("TMC224X: reset detected")
|
|
76
|
+
if flags["driver_error"]:
|
|
77
|
+
raise TmcDriverException("TMC224X: driver error detected")
|
|
78
|
+
if flags["sg2"]:
|
|
79
|
+
self._tmc_logger.log("TMC stallguard2 flag is set", Loglevel.MOVEMENT)
|
|
80
|
+
if flags["standstill"]:
|
|
81
|
+
self._tmc_logger.log("TMC standstill flag is set", Loglevel.MOVEMENT)
|
|
82
|
+
|
|
83
|
+
return rtn[1:], flags
|
|
84
|
+
|
|
85
|
+
def read_int(self, addr: int, tries: int = 10) -> tuple[int, dict]:
|
|
86
|
+
"""this function tries to read the registry of the TMC 10 times
|
|
87
|
+
if a valid answer is returned, this function returns it as an integer
|
|
88
|
+
|
|
89
|
+
Args:
|
|
90
|
+
addr (int): HEX, which register to read
|
|
91
|
+
tries (int): how many tries, before error is raised (Default value = 10)
|
|
92
|
+
Returns:
|
|
93
|
+
int: register value
|
|
94
|
+
Dict: flags
|
|
95
|
+
"""
|
|
96
|
+
data, flags = self.read_reg(addr)
|
|
97
|
+
return int.from_bytes(bytes(data), "big"), flags
|
|
98
|
+
|
|
99
|
+
def write_reg(self, addr: int, val: int) -> bool:
|
|
100
|
+
"""this function can write a value to the register of the tmc
|
|
101
|
+
1. use read_int to get the current setting of the TMC
|
|
102
|
+
2. then modify the settings as wished
|
|
103
|
+
3. write them back to the driver with this function
|
|
104
|
+
|
|
105
|
+
Args:
|
|
106
|
+
addr (int): HEX, which register to write
|
|
107
|
+
val (int): value for that register
|
|
108
|
+
|
|
109
|
+
Returns:
|
|
110
|
+
bool: always True (no check possible)
|
|
111
|
+
"""
|
|
112
|
+
self._w_frame[0] = addr | 0x80 # set write bit
|
|
113
|
+
|
|
114
|
+
self._w_frame[1] = 0xFF & (val >> 24)
|
|
115
|
+
self._w_frame[2] = 0xFF & (val >> 16)
|
|
116
|
+
self._w_frame[3] = 0xFF & (val >> 8)
|
|
117
|
+
self._w_frame[4] = 0xFF & val
|
|
118
|
+
|
|
119
|
+
self._spi_transfer(self._w_frame)
|
|
120
|
+
|
|
121
|
+
return True
|
|
122
|
+
|
|
123
|
+
def write_reg_check(self, addr: int, val: int, tries: int = 10) -> bool:
|
|
124
|
+
"""IFCNT is disabled in SPI mode. Therefore, no check is possible.
|
|
125
|
+
This only calls the write_reg function
|
|
126
|
+
|
|
127
|
+
Args:
|
|
128
|
+
addr: HEX, which register to write
|
|
129
|
+
val: value for that register
|
|
130
|
+
tries: how many tries, before error is raised (Default value = 10)
|
|
131
|
+
|
|
132
|
+
Returns:
|
|
133
|
+
bool: always True (no check possible)
|
|
134
|
+
"""
|
|
135
|
+
self.write_reg(addr, val)
|
|
136
|
+
return True
|
|
137
|
+
|
|
138
|
+
def flush_com_buffer(self):
|
|
139
|
+
"""this function clear the communication buffers of the Raspberry Pi"""
|
|
140
|
+
|
|
141
|
+
def test_com(self, ioin: tmc_shared_reg.Ioin):
|
|
142
|
+
"""test com connection
|
|
143
|
+
|
|
144
|
+
Args:
|
|
145
|
+
ioin: IOIN register instance
|
|
146
|
+
|
|
147
|
+
Returns:
|
|
148
|
+
bool: True if communication is OK, False otherwise
|
|
149
|
+
"""
|
|
150
|
+
data, flags = ioin.read()
|
|
151
|
+
del flags # unused
|
|
152
|
+
|
|
153
|
+
if data == 0:
|
|
154
|
+
self._tmc_logger.log("No answer from TMC received", Loglevel.ERROR)
|
|
155
|
+
return False
|
|
156
|
+
if ioin.version < 0x21:
|
|
157
|
+
self._tmc_logger.log("No correct Version from TMC received", Loglevel.ERROR)
|
|
158
|
+
return False
|
|
159
|
+
return True
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# pylint: disable=unused-import
|
|
2
|
+
# pylint: disable=too-few-public-methods
|
|
3
|
+
"""
|
|
4
|
+
TmcComSpiFtdi stepper driver spi module
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from pyftdi.spi import SpiPort
|
|
8
|
+
from .._tmc_exceptions import TmcComException, TmcDriverException
|
|
9
|
+
from ._tmc_com_spi_base import TmcComSpiBase, TmcLogger
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class TmcComSpiFtdi(TmcComSpiBase):
|
|
13
|
+
"""TmcComSpiFtdi
|
|
14
|
+
|
|
15
|
+
this class is used to communicate with the TMC via SPI via FT232H USB adapter
|
|
16
|
+
it can be used to change the settings of the TMC.
|
|
17
|
+
like the current or the microsteppingmode
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
def __init__(self, spi_port: SpiPort):
|
|
21
|
+
"""constructor
|
|
22
|
+
|
|
23
|
+
Args:
|
|
24
|
+
spi_port (SpiPort): pyftdi SpiPort object
|
|
25
|
+
"""
|
|
26
|
+
super().__init__()
|
|
27
|
+
|
|
28
|
+
self.spi = spi_port
|
|
29
|
+
|
|
30
|
+
def init(self):
|
|
31
|
+
"""init - SPI port is already configured via pyftdi"""
|
|
32
|
+
|
|
33
|
+
def __del__(self):
|
|
34
|
+
self.deinit()
|
|
35
|
+
|
|
36
|
+
def deinit(self):
|
|
37
|
+
"""destructor"""
|
|
38
|
+
|
|
39
|
+
def _spi_transfer(self, data: list) -> list:
|
|
40
|
+
"""Perform SPI transfer using pyftdi
|
|
41
|
+
|
|
42
|
+
Args:
|
|
43
|
+
data: Data to send
|
|
44
|
+
|
|
45
|
+
Returns:
|
|
46
|
+
Received data
|
|
47
|
+
"""
|
|
48
|
+
return list(self.spi.exchange(bytes(data), duplex=True))
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
# pylint: disable=too-many-instance-attributes
|
|
2
|
+
# pylint: disable=too-many-arguments
|
|
3
|
+
# pylint: disable=too-many-positional-arguments
|
|
4
|
+
"""
|
|
5
|
+
TmcComSpiPico - SPI communication for MicroPython
|
|
6
|
+
|
|
7
|
+
Uses the machine.SPI interface available on RP2040 and similar boards.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
# Detect if we're running on MicroPython
|
|
11
|
+
from machine import SPI, Pin # pylint: disable=import-error
|
|
12
|
+
from ._tmc_com_spi_base import TmcComSpiBase
|
|
13
|
+
from .._tmc_exceptions import TmcComException
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class TmcComSpiMicroPython(TmcComSpiBase):
|
|
17
|
+
"""SPI Communication class for MicroPython
|
|
18
|
+
|
|
19
|
+
RP2040 SPI Pins (default):
|
|
20
|
+
- SPI0: SCK=GP18, MOSI=GP19, MISO=GP16
|
|
21
|
+
- SPI1: SCK=GP10, MOSI=GP11, MISO=GP8
|
|
22
|
+
|
|
23
|
+
TMC2240 uses SPI Mode 3: CPOL=1, CPHA=1
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
def __init__(
|
|
27
|
+
self,
|
|
28
|
+
spi_id=0,
|
|
29
|
+
cs_pin=17,
|
|
30
|
+
sck_pin=18,
|
|
31
|
+
mosi_pin=19,
|
|
32
|
+
miso_pin=16,
|
|
33
|
+
baudrate=1000000,
|
|
34
|
+
):
|
|
35
|
+
"""Initialize SPI communication
|
|
36
|
+
|
|
37
|
+
Args:
|
|
38
|
+
spi_id: SPI bus ID (0 or 1)
|
|
39
|
+
cs_pin: Chip select GPIO pin
|
|
40
|
+
sck_pin: SPI clock GPIO pin
|
|
41
|
+
mosi_pin: MOSI GPIO pin
|
|
42
|
+
miso_pin: MISO GPIO pin
|
|
43
|
+
baudrate: SPI clock frequency (default 1MHz)
|
|
44
|
+
"""
|
|
45
|
+
super().__init__()
|
|
46
|
+
|
|
47
|
+
self._spi_id = spi_id
|
|
48
|
+
self._cs_pin = cs_pin
|
|
49
|
+
self._sck_pin = sck_pin
|
|
50
|
+
self._mosi_pin = mosi_pin
|
|
51
|
+
self._miso_pin = miso_pin
|
|
52
|
+
self._baudrate = baudrate
|
|
53
|
+
|
|
54
|
+
self._spi: SPI | None = None
|
|
55
|
+
self._cs = None
|
|
56
|
+
|
|
57
|
+
def init(self):
|
|
58
|
+
"""Initialize SPI hardware"""
|
|
59
|
+
# TMC2240 uses SPI Mode 3: CPOL=1, CPHA=1
|
|
60
|
+
self._spi = SPI(
|
|
61
|
+
self._spi_id,
|
|
62
|
+
baudrate=self._baudrate,
|
|
63
|
+
polarity=1,
|
|
64
|
+
phase=1,
|
|
65
|
+
sck=Pin(self._sck_pin),
|
|
66
|
+
mosi=Pin(self._mosi_pin),
|
|
67
|
+
miso=Pin(self._miso_pin),
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
# Setup CS pin (active low)
|
|
71
|
+
self._cs = Pin(self._cs_pin, Pin.OUT, value=1)
|
|
72
|
+
|
|
73
|
+
def deinit(self):
|
|
74
|
+
"""Deinitialize SPI hardware"""
|
|
75
|
+
if self._spi is not None:
|
|
76
|
+
self._spi.deinit()
|
|
77
|
+
self._spi = None
|
|
78
|
+
self._cs = None
|
|
79
|
+
|
|
80
|
+
def _spi_transfer(self, data):
|
|
81
|
+
"""Perform SPI transfer
|
|
82
|
+
|
|
83
|
+
Args:
|
|
84
|
+
data: List of bytes to send
|
|
85
|
+
|
|
86
|
+
Returns:
|
|
87
|
+
List of received bytes
|
|
88
|
+
"""
|
|
89
|
+
if self._spi is None or self._cs is None:
|
|
90
|
+
raise TmcComException("SPI not initialized")
|
|
91
|
+
|
|
92
|
+
tx_data = bytes(data)
|
|
93
|
+
rx_data = bytearray(len(data))
|
|
94
|
+
|
|
95
|
+
self._cs.value(0) # CS low
|
|
96
|
+
self._spi.write_readinto(tx_data, rx_data)
|
|
97
|
+
self._cs.value(1) # CS high
|
|
98
|
+
|
|
99
|
+
return list(rx_data)
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# pylint: disable=broad-exception-caught
|
|
2
|
+
# pylint: disable=unused-import
|
|
3
|
+
"""
|
|
4
|
+
TmcComUart stepper driver uart module
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import serial
|
|
8
|
+
from ._tmc_com_uart_base import TmcComUartBase, TmcLogger, Loglevel
|
|
9
|
+
from .._tmc_exceptions import TmcComException, TmcDriverException
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class TmcComUart(TmcComUartBase):
|
|
13
|
+
"""TmcComUart
|
|
14
|
+
|
|
15
|
+
this class is used to communicate with the TMC via UART using pyserial
|
|
16
|
+
it can be used to change the settings of the TMC.
|
|
17
|
+
like the current or the microsteppingmode
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
def __init__(self, serialport: str, baudrate: int = 11520):
|
|
21
|
+
"""constructor
|
|
22
|
+
|
|
23
|
+
Args:
|
|
24
|
+
serialport (string): serialport path
|
|
25
|
+
baudrate (int): baudrate
|
|
26
|
+
"""
|
|
27
|
+
super().__init__()
|
|
28
|
+
|
|
29
|
+
self.ser = serial.Serial()
|
|
30
|
+
|
|
31
|
+
if serialport is None:
|
|
32
|
+
return
|
|
33
|
+
|
|
34
|
+
self.ser.port = serialport
|
|
35
|
+
self.ser.baudrate = baudrate
|
|
36
|
+
|
|
37
|
+
def init(self):
|
|
38
|
+
"""init"""
|
|
39
|
+
try:
|
|
40
|
+
self.ser.open()
|
|
41
|
+
except Exception as e:
|
|
42
|
+
errnum = e.args[0]
|
|
43
|
+
self._tmc_logger.log(f"SERIAL ERROR: {e}")
|
|
44
|
+
if errnum == 2:
|
|
45
|
+
self._tmc_logger.log(
|
|
46
|
+
f""""{self.ser.serialport} does not exist.
|
|
47
|
+
You need to activate the serial port with \"sudo raspi-config\"""",
|
|
48
|
+
Loglevel.ERROR,
|
|
49
|
+
)
|
|
50
|
+
raise SystemExit from e
|
|
51
|
+
|
|
52
|
+
if errnum == 13:
|
|
53
|
+
self._tmc_logger.log(
|
|
54
|
+
"""you have no permission to use the serial port.
|
|
55
|
+
You may need to add your user to the dialout group
|
|
56
|
+
with \"sudo usermod -a -G dialout pi\"""",
|
|
57
|
+
Loglevel.ERROR,
|
|
58
|
+
)
|
|
59
|
+
raise SystemExit from e
|
|
60
|
+
|
|
61
|
+
if self.ser.baudrate is None:
|
|
62
|
+
raise TmcComException("Baudrate is not set")
|
|
63
|
+
|
|
64
|
+
# adjust per baud and hardware. Sequential reads without some delay fail.
|
|
65
|
+
self.communication_pause = 500 // self.ser.baudrate
|
|
66
|
+
|
|
67
|
+
if self.ser is None:
|
|
68
|
+
return
|
|
69
|
+
|
|
70
|
+
self.ser.bytesize = serial.EIGHTBITS
|
|
71
|
+
self.ser.parity = serial.PARITY_NONE
|
|
72
|
+
self.ser.stopbits = serial.STOPBITS_ONE
|
|
73
|
+
|
|
74
|
+
# adjust per baud and hardware. Sequential reads without some delay fail.
|
|
75
|
+
self.ser.timeout = 20000 // self.ser.baudrate
|
|
76
|
+
|
|
77
|
+
self._uart_flush()
|
|
78
|
+
|
|
79
|
+
def __del__(self):
|
|
80
|
+
self.deinit()
|
|
81
|
+
|
|
82
|
+
def deinit(self):
|
|
83
|
+
"""destructor"""
|
|
84
|
+
if self.ser is not None and isinstance(self.ser, serial.Serial):
|
|
85
|
+
self.ser.close()
|
|
86
|
+
|
|
87
|
+
def _uart_write(self, data: list) -> int:
|
|
88
|
+
"""Write data to UART using pyserial
|
|
89
|
+
|
|
90
|
+
Args:
|
|
91
|
+
data: Data to send
|
|
92
|
+
|
|
93
|
+
Returns:
|
|
94
|
+
Number of bytes written
|
|
95
|
+
"""
|
|
96
|
+
return self.ser.write(data)
|
|
97
|
+
|
|
98
|
+
def _uart_read(self, length: int) -> bytes:
|
|
99
|
+
"""Read data from UART using pyserial
|
|
100
|
+
|
|
101
|
+
Args:
|
|
102
|
+
length: Number of bytes to read
|
|
103
|
+
|
|
104
|
+
Returns:
|
|
105
|
+
Received data
|
|
106
|
+
"""
|
|
107
|
+
return self.ser.read(length)
|
|
108
|
+
|
|
109
|
+
def _uart_flush(self):
|
|
110
|
+
"""Flush UART buffers"""
|
|
111
|
+
if self.ser is not None:
|
|
112
|
+
self.ser.reset_output_buffer()
|
|
113
|
+
self.ser.reset_input_buffer()
|