Skip to content

Commit

Permalink
Change from using (unstable) spi device numbers to spidev path.
Browse files Browse the repository at this point in the history
See: doceme/py-spidev#130 for rationale and
pre-requisite changes to py-spidev.
  • Loading branch information
tim-seoss committed Jun 13, 2023
1 parent 894e4a4 commit 8d7b726
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 90 deletions.
12 changes: 7 additions & 5 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
paho-mqtt
posix_ipc
pyudev
spidev
pyserial
# Versioning rules follow those specified in PEP 440
# https://peps.python.org/pep-0440/

posix_ipc ~=0.9
#spidev == 3.6.1.dev1 @ git+https://github.com/tim-seoss/[email protected]
git+https://github.com/tim-seoss/[email protected]#egg=spidev
pyserial ~=3.4
33 changes: 16 additions & 17 deletions src/scs_host/bus/spi.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
"""
Created on 4 Jul 2016
https://tightdev.net/SpiDev_Doc.pdf
https://www.takaitra.com/posts/492
https://github.com/doceme/py-spidev
https://www.takaitra.com/spi-device-raspberry-pi/
n.b. This is currently using an unmerged PR for py-spidev
https://github.com/doceme/py-spidev/pull/130
Until this PR is merged, this PR branch should be installed into the local
python virtualenv.
@author: Bruno Beloff ([email protected])
Expand All @@ -29,13 +35,12 @@ class SPI(object):

# ----------------------------------------------------------------------------------------------------------------

def __init__(self, bus, device, mode, max_speed):
def __init__(self, dev_path, mode, max_speed):
"""
Constructor
"""

self.__bus = bus
self.__device = device
self.__dev_path = dev_path
self.__mode = mode
self.__max_speed = max_speed

Expand All @@ -51,7 +56,7 @@ def open(self):
self.acquire_lock()

self.__connection = SpiDev()
self.__connection.open(self.__bus, self.__device)
self.__connection.open_path(self.__dev_path)

self.__connection.mode = self.__mode
self.__connection.max_speed_hz = self.__max_speed
Expand All @@ -76,10 +81,9 @@ def acquire_lock(self):
def release_lock(self):
Lock.release(self.__lock_name)


@property
def __lock_name(self):
return "%s-%s" % (self.__class__.__name__, self.__bus)
return ("%s-%s" % (self.__class__.__name__, self.__dev_path)).replace('/', '_')


# ----------------------------------------------------------------------------------------------------------------
Expand All @@ -95,17 +99,12 @@ def read_bytes(self, count):
# ----------------------------------------------------------------------------------------------------------------

@property
def bus(self):
return self.__bus


@property
def device(self):
return self.__device
def dev_path(self):
return self.__dev_path


# ----------------------------------------------------------------------------------------------------------------

def __str__(self, *args, **kwargs):
return "SPI:{bus:%d, device:%s, mode:%d, max_speed:%d, connection:%s}" % \
(self.__bus, self.__device, self.__mode, self.__max_speed, self.__connection)
return "SPI:{dev_path:%s, mode:%d, max_speed:%d, connection:%s}" % \
(self.__dev_path, self.__mode, self.__max_speed, self.__connection)
56 changes: 7 additions & 49 deletions src/scs_host/sys/host.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

class Host(IoTNode, FilesystemPersistenceManager):
"""
TI Sitara AM3358AZCZ100 processor
SCS Cube MB v1
"""

OS_ENV_PATH = 'SCS_ROOT_PATH'
Expand All @@ -45,12 +45,9 @@ class Host(IoTNode, FilesystemPersistenceManager):
# ----------------------------------------------------------------------------------------------------------------
# devices...

# n.b. this currently unused with CubeMB see spi_bus method below
__OPC_SPI_ADDR = '48030000' # hard-coded memory-mapped io address
__OPC_SPI_DEVICE = 0 # hard-coded path
__OPC_SPI_DEV_PATH = '/dev/spi/by-compat/scs_cubemb_v1_spi_j6' # udev-managed symlink to spidev

__NDIR_SPI_ADDR = '481a0000' # hard-coded memory-mapped io address
__NDIR_SPI_DEVICE = 0 # hard-coded path
__NDIR_SPI_DEV_PATH = '/dev/spi/by-compat/scs_cubemb_v1_spi_j12' # udev-managed symlink to spidev

# GPS serial port device
__GPS_DEVICE = '/dev/ttyS0' # hard-coded path
Expand Down Expand Up @@ -86,34 +83,6 @@ class Host(IoTNode, FilesystemPersistenceManager):
__SERVER_IPV4_ADDRESS = None # had-coded abs path


# ----------------------------------------------------------------------------------------------------------------

@staticmethod
def spi_bus(_spi_address, _spi_device):
return 0 # hard-code spi bus number FIXME bodge

# context = pyudev.Context()
#
# kernel_path = '/ocp/spi@' + spi_address + '/channel@' + str(spi_device)
#
# for device in context.list_devices(subsystem='spidev'):
# parent = device.parent
#
# if type(parent) and parent['OF_FULLNAME'] == kernel_path:
# node = device.device_node
#
# match = re.match(r'\D+(\d+).\d+', node) # e.g. /dev/spidev1.0
#
# if match is None:
# continue
#
# groups = match.groups()
#
# return int(groups[0])
#
# raise OSError("No SPI bus could be found for %s" % kernel_path)


# ----------------------------------------------------------------------------------------------------------------

@staticmethod
Expand Down Expand Up @@ -281,23 +250,12 @@ def __modem_list(cls):
# SPI...

@classmethod
def ndir_spi_bus(cls):
return cls.spi_bus(cls.__NDIR_SPI_ADDR, cls.__NDIR_SPI_DEVICE)


@classmethod
def ndir_spi_device(cls):
return cls.__NDIR_SPI_DEVICE


@classmethod
def opc_spi_bus(cls):
return cls.spi_bus(cls.__OPC_SPI_ADDR, cls.__OPC_SPI_DEVICE)

def ndir_spi_dev_path(cls):
return cls.__NDIR_SPI_DEV_PATH

@classmethod
def opc_spi_device(cls):
return cls.__OPC_SPI_DEVICE
def opc_spi_dev_path(cls):
return cls.__OPC_SPI_DEV_PATH


# ----------------------------------------------------------------------------------------------------------------
Expand Down
19 changes: 0 additions & 19 deletions tests/sys/host_spi_location_test.py

This file was deleted.

0 comments on commit 8d7b726

Please sign in to comment.