Skip to content

Commit

Permalink
Add open_path() Python method to permit persistent device symlinks.
Browse files Browse the repository at this point in the history
Adds to the functionality provided by the open() call (which hard-codes
the opening of /dev/spidev<bus>.<chipselect> device file paths).
Because the bus number allocation may not be knowable in advance (bus
numbers are dynamically allocated by the Linux kernel), open_path()
allows the user to employ udev rules to deterministically create a
symlink to the correct hardware device at boot and/or hot-plug time.

Part of doceme#129
  • Loading branch information
tim-seoss committed Apr 24, 2023
1 parent 6c884cf commit a94c858
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Usage
```python
import spidev
spi = spidev.SpiDev()
spi.open(bus, device)
spi.open_path(spidev_devicefile_path)
to_send = [0x01, 0x02, 0x03]
spi.xfer(to_send)
```
Expand All @@ -21,7 +21,7 @@ Settings
```python
import spidev
spi = spidev.SpiDev()
spi.open(bus, device)
spi.open_path("/dev/spidev0.0")

# Settings (for example)
spi.max_speed_hz = 5000
Expand All @@ -43,9 +43,16 @@ spi.mode = 0b01
Methods
-------

open_path(filesystem_path)

Connects to the specified SPI device special file, following symbolic links if
appropriate (see note on deterministic SPI bus numbering in the Linux kernel
below for why this can be advantageous in some configurations).

open(bus, device)

Connects to the specified SPI device, opening `/dev/spidev<bus>.<device>`
Equivalent to calling `open_path("/dev/spidev<bus>.<device>")`. n.b. **Either**
`open_path` or `open` should be used.

readbytes(n)

Expand Down
24 changes: 24 additions & 0 deletions spidev_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -1365,6 +1365,28 @@ SpiDev_open_dev(SpiDevObject *self, char *dev_path)
}


PyDoc_STRVAR(SpiDev_open_path_doc,
"open_path(spidev_path)\n\n"
"Connects the object to the specified SPI device.\n"
"open_path(X) will open the spidev character device <X> (following symbolic links if necessary).\n");

static PyObject *
SpiDev_open_path(SpiDevObject *self, PyObject *args, PyObject *kwds)
{
static char *kwlist[] = {"path", NULL};
PyObject *py_dev_path;
char *dev_path;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&:open", kwlist, PyUnicode_FSConverter, &py_dev_path))
return NULL;
if (py_dev_path == NULL)
return NULL;
dev_path = PyBytes_AsString(py_dev_path);
if (dev_path == NULL)
return NULL;
return SpiDev_open_dev(self, dev_path);
}


PyDoc_STRVAR(SpiDev_open_doc,
"open(bus, device)\n\n"
"Connects the object to the specified SPI device.\n"
Expand Down Expand Up @@ -1441,6 +1463,8 @@ PyObject *SpiDev_exit(SpiDevObject *self, PyObject *args)
static PyMethodDef SpiDev_methods[] = {
{"open", (PyCFunction)SpiDev_open, METH_VARARGS | METH_KEYWORDS,
SpiDev_open_doc},
{"open_path", (PyCFunction)SpiDev_open_path, METH_VARARGS | METH_KEYWORDS,
SpiDev_open_path_doc},
{"close", (PyCFunction)SpiDev_close, METH_NOARGS,
SpiDev_close_doc},
{"fileno", (PyCFunction)SpiDev_fileno, METH_NOARGS,
Expand Down

0 comments on commit a94c858

Please sign in to comment.