-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy path_tmc_com_spi.py
More file actions
80 lines (65 loc) · 2.27 KB
/
_tmc_com_spi.py
File metadata and controls
80 lines (65 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# pylint: disable=broad-exception-caught
# pylint: disable=unused-import
# pylint: disable=too-few-public-methods
# pylint: disable=too-many-arguments
# pylint: disable=too-many-positional-arguments
"""TmcComSpi stepper driver spi module."""
import spidev
from ._tmc_com_spi_base import TmcComSpiBase, TmcLogger, Loglevel
from .._tmc_exceptions import TmcComException, TmcDriverException
class TmcComSpi(TmcComSpiBase):
"""TmcComSpi.
this class is used to communicate with the TMC via SPI it can be
used to change the settings of the TMC. like the current or the
microsteppingmode
"""
def __init__(
self,
spi_bus: int,
spi_dev: int,
spi_speed: int = 8000000,
):
"""constructor.
Args:
spi_bus (int): SPI bus number
spi_dev (int): SPI device number
spi_speed (int, optional): SPI speed in Hz. Defaults to 8000000.
"""
super().__init__()
self.spi = spidev.SpiDev()
self._spi_bus = spi_bus
self._spi_dev = spi_dev
self._spi_speed = spi_speed
def init(self):
"""init."""
try:
self.spi.open(self._spi_bus, self._spi_dev)
except Exception as e:
self._tmc_logger.log(f"Error opening SPI: {e}", Loglevel.ERROR)
errnum = e.args[0]
if errnum == 2:
self._tmc_logger.log(
f"SPI Device {self._spi_dev} on Bus {self._spi_bus} does not exist.",
Loglevel.ERROR,
)
self._tmc_logger.log(
'You need to activate the SPI interface with "sudo raspi-config"',
Loglevel.ERROR,
)
raise SystemExit from e
self.spi.max_speed_hz = self._spi_speed
self.spi.mode = 0b11
self.spi.lsbfirst = False
def __del__(self):
"""Destructor."""
self.deinit()
def deinit(self):
"""Destructor."""
def _spi_transfer(self, data: list) -> list:
"""Perform SPI transfer using spidev.
Args:
data: Data to send
Returns:
Received data
"""
return self.spi.xfer2(data)