From 8870a4677f7ec4e666c9eceb49882abb68e69f82 Mon Sep 17 00:00:00 2001 From: Tim Small Date: Tue, 13 Jun 2023 09:50:43 +0100 Subject: [PATCH] Change from using (unstable) spi device numbers to spidev path. See: https://github.com/doceme/py-spidev/pull/130 for rationale and pre-requisite changes to py-spidev. --- requirements.txt | 12 ++++--- src/scs_host/bus/spi.py | 33 +++++++++--------- src/scs_host/sys/host.py | 53 ++++------------------------- tests/sys/host_spi_location_test.py | 19 ----------- 4 files changed, 29 insertions(+), 88 deletions(-) delete mode 100755 tests/sys/host_spi_location_test.py diff --git a/requirements.txt b/requirements.txt index c3bb54f..94fcc4c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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/py-spidev.git@v3.6.1.dev1 +git+https://github.com/tim-seoss/py-spidev.git@v3.6.1.dev1#egg=spidev +pyserial ~=3.4 diff --git a/src/scs_host/bus/spi.py b/src/scs_host/bus/spi.py index 8d20872..dc35f76 100644 --- a/src/scs_host/bus/spi.py +++ b/src/scs_host/bus/spi.py @@ -1,8 +1,14 @@ """ Created on 4 Jul 2016 -http://tightdev.net/SpiDev_Doc.pdf -http://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 (bruno.beloff@southcoastscience.com) @@ -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 @@ -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 @@ -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('/', '_') # ---------------------------------------------------------------------------------------------------------------- @@ -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) diff --git a/src/scs_host/sys/host.py b/src/scs_host/sys/host.py index 7553bde..8327828 100644 --- a/src/scs_host/sys/host.py +++ b/src/scs_host/sys/host.py @@ -7,8 +7,6 @@ """ import os -import pyudev -import re import socket from pathlib import Path @@ -45,11 +43,9 @@ class Host(IoTNode, FilesystemPersistenceManager): # ---------------------------------------------------------------------------------------------------------------- # devices... - __OPC_SPI_ADDR = '48030000' # hard-coded memory-mapped io address - __OPC_SPI_DEVICE = 0 # hard-coded path + __OPC_SPI_DEV_PATH = '/dev/spi/by-connector/H1' # 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-connector/H3' # udev-managed symlink to spidev __GPS_DEVICE = 1 # hard-coded path @@ -86,32 +82,6 @@ class Host(IoTNode, FilesystemPersistenceManager): __SERVER_IPV4_ADDRESS = None # hard-coded abs path - # ---------------------------------------------------------------------------------------------------------------- - - @staticmethod - def spi_bus(spi_address, spi_device): - 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 @@ -284,23 +254,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 # ---------------------------------------------------------------------------------------------------------------- diff --git a/tests/sys/host_spi_location_test.py b/tests/sys/host_spi_location_test.py deleted file mode 100755 index c4ec2d6..0000000 --- a/tests/sys/host_spi_location_test.py +++ /dev/null @@ -1,19 +0,0 @@ -#!/usr/bin/env python3 - -""" -Created on 28 Nov 2018 - -@author: Bruno Beloff (bruno.beloff@southcoastscience.com) -""" - -from scs_host.sys.host import Host - - -# -------------------------------------------------------------------------------------------------------------------- - -spi_location = '48030000' -spi_device = 0 - -bus = Host.spi_bus(spi_location, spi_device) - -print("bus:%d" % bus)