Skip to content

Commit 65b504a

Browse files
committed
always reload the spidev driver
1 parent 2876dd3 commit 65b504a

1 file changed

Lines changed: 21 additions & 22 deletions

File tree

  • qick_lib/qick/ipq_pynq_utils/ipq_pynq_utils

qick_lib/qick/ipq_pynq_utils/ipq_pynq_utils/spidev.py

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from pathlib import Path
2+
import os
23
import struct
34
import fcntl
45
import cffi
@@ -71,17 +72,18 @@ class SpiDev:
7172
XFER_FORMAT = '=QQIIHBBBBBB'
7273

7374
def __init__(self, bus, device):
74-
self.file_obj = open('/dev/spidev%d.%d'%(bus, device), 'w+b', buffering=0)
75+
spidev_bind(bus, device)
76+
self.fd = os.open('/dev/spidev%d.%d'%(bus, device), os.O_RDWR)
7577

7678
def __enter__(self):
7779
return self
7880
def __exit__(self, type, value, traceback):
79-
self.file_obj.close()
81+
os.close(self.fd)
8082

8183
def writebytes(self, msg):
8284
"""Write to the SPI device.
8385
"""
84-
self.file_obj.write(bytes(msg))
86+
os.write(self.fd, bytes(msg))
8587

8688
def xfer(self, msg):
8789
"""Do a single full-duplex transaction.
@@ -99,35 +101,32 @@ def xfer(self, msg):
99101
n,0,0,0,0,0,0,0,0)
100102
ioc = _IOC(_IOC_WRITE, self.SPI_IOC_MAGIC, self.SPI_IOCTL_MSG, len(xfer_bytes))
101103

102-
fcntl.ioctl(self.file_obj.fileno(), ioc, xfer_bytes)
104+
fcntl.ioctl(self.fd, ioc, xfer_bytes)
103105
return _ffi.unpack(rx_buf, n)
104106

105-
@property
106-
def bits_per_word(self):
107-
n = 1
107+
def _read(self, ioctl, n):
108108
buf = bytearray(n)
109-
ioc = _IOC(_IOC_READ, self.SPI_IOC_MAGIC, self.SPI_IOCTL_BITS_PER_WORD, n)
110-
fcntl.ioctl(self.file_obj.fileno(), ioc, buf)
109+
ioc = _IOC(_IOC_READ, self.SPI_IOC_MAGIC, ioctl, n)
110+
fcntl.ioctl(self.fd, ioc, buf)
111111
return int.from_bytes(buf, byteorder='little')
112112

113+
def _write(self, ioctl, n):
114+
buf = value.to_bytes(length=n, byteorder='little')
115+
ioc = _IOC(_IOC_WRITE, self.SPI_IOC_MAGIC, ioctl, n)
116+
fcntl.ioctl(self.fd, ioc, buf)
117+
118+
@property
119+
def bits_per_word(self):
120+
return self._read(self.SPI_IOCTL_BITS_PER_WORD, 1)
121+
113122
@bits_per_word.setter
114123
def bits_per_word(self, value):
115-
n = 1
116-
buf = value.to_bytes(length=n, byteorder='little')
117-
ioc = _IOC(_IOC_WRITE, self.SPI_IOC_MAGIC, self.SPI_IOCTL_BITS_PER_WORD, n)
118-
fcntl.ioctl(self.file_obj.fileno(), ioc, buf)
124+
self._write(self.SPI_IOCTL_BITS_PER_WORD, 1)
119125

120126
@property
121127
def max_speed_hz(self):
122-
n = 4
123-
buf = bytearray(n)
124-
ioc = _IOC(_IOC_READ, self.SPI_IOC_MAGIC, self.SPI_IOCTL_MAX_SPEED_HZ, n)
125-
fcntl.ioctl(self.file_obj.fileno(), ioc, buf)
126-
return int.from_bytes(buf, byteorder='little')
128+
return self._read(self.SPI_IOCTL_MAX_SPEED_HZ, 4)
127129

128130
@max_speed_hz.setter
129131
def max_speed_hz(self, value):
130-
n = 4
131-
buf = value.to_bytes(length=n, byteorder='little')
132-
ioc = _IOC(_IOC_WRITE, self.SPI_IOC_MAGIC, self.SPI_IOCTL_MAX_SPEED_HZ, n)
133-
fcntl.ioctl(self.file_obj.fileno(), ioc, buf)
132+
self._write(self.SPI_IOCTL_MAX_SPEED_HZ, 4)

0 commit comments

Comments
 (0)