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 0830d0a commit 39822bb
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 88 deletions.
13 changes: 8 additions & 5 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
paho-mqtt
posix_ipc
pyudev
spidev
pyserial
# Versioning rules follow those specified in PEP 440
# https://peps.python.org/pep-0440/

# paho-mqtt
# 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
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 ([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)
53 changes: 6 additions & 47 deletions src/scs_host/sys/host.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
"""

import os
import pyudev
import re
import socket

from pathlib import Path
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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


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

This file was deleted.

0 comments on commit 39822bb

Please sign in to comment.