GHIElectronics.Endpoint.Devices.Spi 0.5.3__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.
- GHIElectronics/Endpoint/Devices/Spi/__init__.py +111 -0
- ghielectronics_endpoint_devices_spi-0.5.3.dist-info/METADATA +20 -0
- ghielectronics_endpoint_devices_spi-0.5.3.dist-info/RECORD +5 -0
- ghielectronics_endpoint_devices_spi-0.5.3.dist-info/WHEEL +5 -0
- ghielectronics_endpoint_devices_spi-0.5.3.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
"""SPI for Endpoint, with the same API as .NET's System.Device.Spi:
|
|
2
|
+
|
|
3
|
+
from GHIElectronics.Endpoint.Core import SpiController, EPM815
|
|
4
|
+
from GHIElectronics.Endpoint.Devices.Spi import SpiDevice, SpiConnectionSettings, SpiMode
|
|
5
|
+
|
|
6
|
+
SpiController.Initialize(EPM815.Spi.Spi4)
|
|
7
|
+
settings = SpiConnectionSettings(EPM815.Spi.Spi4, 0)
|
|
8
|
+
settings.ClockFrequency = 1_000_000
|
|
9
|
+
device = SpiDevice.Create(settings)
|
|
10
|
+
received = bytearray(4)
|
|
11
|
+
device.TransferFullDuplex(bytes([1, 2, 3, 4]), received)
|
|
12
|
+
|
|
13
|
+
Chip select is not driven by the bus: use any GPIO pin.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from enum import IntEnum
|
|
17
|
+
|
|
18
|
+
from GHIElectronics.Endpoint.Core._errors import ArgumentException
|
|
19
|
+
from GHIElectronics.Endpoint.Core._native import native
|
|
20
|
+
|
|
21
|
+
__all__ = ["SpiDevice", "SpiConnectionSettings", "SpiMode", "DataFlow"]
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class SpiMode(IntEnum):
|
|
25
|
+
Mode0 = 0
|
|
26
|
+
Mode1 = 1
|
|
27
|
+
Mode2 = 2
|
|
28
|
+
Mode3 = 3
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class DataFlow(IntEnum):
|
|
32
|
+
MsbFirst = 0
|
|
33
|
+
LsbFirst = 1
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class SpiConnectionSettings:
|
|
37
|
+
def __init__(self, busId, chipSelectLine=-1):
|
|
38
|
+
self.BusId = busId
|
|
39
|
+
self.ChipSelectLine = chipSelectLine
|
|
40
|
+
self.Mode = SpiMode.Mode0
|
|
41
|
+
self.DataBitLength = 8
|
|
42
|
+
self.ClockFrequency = 500_000
|
|
43
|
+
self.DataFlow = DataFlow.MsbFirst
|
|
44
|
+
self.ChipSelectLineActiveState = 0 # PinValue.Low
|
|
45
|
+
|
|
46
|
+
def __repr__(self):
|
|
47
|
+
return (f"SpiConnectionSettings({self.BusId}, {self.ChipSelectLine}, Mode={self.Mode!r}, "
|
|
48
|
+
f"ClockFrequency={self.ClockFrequency})")
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
class SpiDevice:
|
|
52
|
+
"""A device on an SPI bus (call SpiController.Initialize for the bus first)"""
|
|
53
|
+
|
|
54
|
+
def __init__(self, settings):
|
|
55
|
+
handle = native.out_handle()
|
|
56
|
+
native.spidevice_create(settings.BusId, settings.ChipSelectLine, int(settings.Mode), settings.DataBitLength,
|
|
57
|
+
settings.ClockFrequency, int(settings.DataFlow),
|
|
58
|
+
int(settings.ChipSelectLineActiveState), native.ref(handle))
|
|
59
|
+
self._handle = handle
|
|
60
|
+
self.ConnectionSettings = settings
|
|
61
|
+
|
|
62
|
+
@staticmethod
|
|
63
|
+
def Create(settings):
|
|
64
|
+
return SpiDevice(settings)
|
|
65
|
+
|
|
66
|
+
def _h(self):
|
|
67
|
+
if self._handle is None:
|
|
68
|
+
raise native.disposed("SpiDevice")
|
|
69
|
+
return self._handle
|
|
70
|
+
|
|
71
|
+
def Read(self, buffer):
|
|
72
|
+
"""Reads len(buffer) bytes into the bytearray buffer (sends zeros)"""
|
|
73
|
+
pointer, length = native.writable(buffer)
|
|
74
|
+
native.spidevice_transfer(self._h(), None, pointer, length)
|
|
75
|
+
|
|
76
|
+
def ReadByte(self):
|
|
77
|
+
buffer = bytearray(1)
|
|
78
|
+
self.Read(buffer)
|
|
79
|
+
return buffer[0]
|
|
80
|
+
|
|
81
|
+
def Write(self, buffer):
|
|
82
|
+
pointer, length, _keep = native.readable(buffer)
|
|
83
|
+
native.spidevice_transfer(self._h(), pointer, None, length)
|
|
84
|
+
|
|
85
|
+
def WriteByte(self, value):
|
|
86
|
+
self.Write(bytes([value]))
|
|
87
|
+
|
|
88
|
+
def TransferFullDuplex(self, writeBuffer, readBuffer):
|
|
89
|
+
"""Sends writeBuffer while reading into readBuffer (the same length)"""
|
|
90
|
+
write, write_length, _keep = native.readable(writeBuffer)
|
|
91
|
+
read, read_length = native.writable(readBuffer)
|
|
92
|
+
if write_length != read_length:
|
|
93
|
+
raise ArgumentException("The write and read buffers must have the same length")
|
|
94
|
+
native.spidevice_transfer(self._h(), write, read, read_length)
|
|
95
|
+
|
|
96
|
+
def Dispose(self):
|
|
97
|
+
if self._handle is not None:
|
|
98
|
+
native.spidevice_dispose(self._handle)
|
|
99
|
+
self._handle = None
|
|
100
|
+
|
|
101
|
+
def __enter__(self):
|
|
102
|
+
return self
|
|
103
|
+
|
|
104
|
+
def __exit__(self, *exception):
|
|
105
|
+
self.Dispose()
|
|
106
|
+
|
|
107
|
+
def __del__(self):
|
|
108
|
+
try:
|
|
109
|
+
self.Dispose()
|
|
110
|
+
except Exception:
|
|
111
|
+
pass
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: GHIElectronics.Endpoint.Devices.Spi
|
|
3
|
+
Version: 0.5.3
|
|
4
|
+
Summary: SPI for GHI Electronics Endpoint, with the API of .NET's System.Device.Spi.
|
|
5
|
+
Author-email: "GHI Electronics, LLC" <support@ghielectronics.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://www.ghielectronics.com/
|
|
8
|
+
Project-URL: Repository, https://github.com/ghi-electronics/Endpoint-Libraries
|
|
9
|
+
Project-URL: Issues, https://github.com/ghi-electronics/Endpoint-Libraries/issues
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
12
|
+
Requires-Python: >=3.9
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
Requires-Dist: GHIElectronics.Endpoint.Core<0.6,>=0.5.3
|
|
15
|
+
|
|
16
|
+
# GHIElectronics.Endpoint.Devices.Spi
|
|
17
|
+
|
|
18
|
+
SPI for GHI Electronics Endpoint, with the API of .NET's System.Device.Spi.
|
|
19
|
+
|
|
20
|
+
Part of the [GHI Electronics Endpoint](https://github.com/ghi-electronics/Endpoint-Libraries) libraries. See the repository README for the full package list and getting-started guide.
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
GHIElectronics/Endpoint/Devices/Spi/__init__.py,sha256=n3-yd_ldYqevmTw6SC96nw4PH2A5YoQyYcdonxb52vQ,3590
|
|
2
|
+
ghielectronics_endpoint_devices_spi-0.5.3.dist-info/METADATA,sha256=cZ4HvuoNueRiNubuOTUWVnff74LIB_aTnJUyZ-itcmk,1005
|
|
3
|
+
ghielectronics_endpoint_devices_spi-0.5.3.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
4
|
+
ghielectronics_endpoint_devices_spi-0.5.3.dist-info/top_level.txt,sha256=NpOZNgd0iquwrRPrTOhUDxc2V9TtNFLv5Z_Dx3YZtIU,15
|
|
5
|
+
ghielectronics_endpoint_devices_spi-0.5.3.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
GHIElectronics
|