]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/generic_linux/spi.py
Added Raspberry Pi Compute Module
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / generic_linux / spi.py
1 import spidev
2 import time
3 from adafruit_blinka.agnostic import detector
4
5 class SPI:
6     MSB = 0
7     LSB = 1
8     CPHA = 1
9     CPOL = 2
10
11     baudrate = 100000
12     mode = 0
13     bits = 8
14
15     def __init__(self, portid):
16         self._port = portid
17         self._spi = spidev.SpiDev()
18
19     def init(self, baudrate=100000, polarity=0, phase=0, bits=8,
20                   firstbit=MSB, sck=None, mosi=None, miso=None):
21         mode = 0
22         if polarity:
23             mode |= self.CPOL
24         if phase:
25             mode |= self.CPHA
26
27         self.clock_pin = sck
28         self.mosi_pin = mosi
29         self.miso_pin = miso
30         self.baudrate = baudrate
31         self.mode = mode
32         self.bits = bits
33         self.chip = detector.chip
34
35     def set_no_cs(self):
36         # Linux SPI driver for AM33XX chip in BeagleBone and PocketBeagle
37         # does not support setting SPI_NO_CS mode bit (issue #104)
38         if not self.chip.AM33XX and not self.chip.IMX8MX and not self.chip.SAMA5 \
39          and not self.chip.APQ8016:
40             try:
41                 self._spi.no_cs = True  # this doesn't work but try anyways
42             except AttributeError:
43                 pass
44
45     def write(self, buf, start=0, end=None):
46         if not buf:
47             return
48         if end is None:
49             end = len(buf)
50         try:
51             self._spi.open(self._port, 0)
52             self.set_no_cs()
53             self._spi.max_speed_hz = self.baudrate
54             self._spi.mode = self.mode
55             self._spi.bits_per_word = self.bits
56             self._spi.writebytes([x for x in buf[start:end]])
57             self._spi.close()
58         except FileNotFoundError as not_found:
59             print("Could not open SPI device - check if SPI is enabled in kernel!")
60             raise
61
62     def readinto(self, buf, start=0, end=None, write_value=0):
63         if not buf:
64             return
65         if end is None:
66             end = len(buf)
67         try:
68             self._spi.open(self._port, 0)
69             self.set_no_cs()
70             self._spi.max_speed_hz = self.baudrate
71             self._spi.mode = self.mode
72             self._spi.bits_per_word = self.bits
73             data = self._spi.xfer([write_value]*(end-start))
74             for i in range(end-start):  # 'readinto' the given buffer
75               buf[start+i] = data[i]
76             self._spi.close()
77         except FileNotFoundError as not_found:
78             print("Could not open SPI device - check if SPI is enabled in kernel!")
79             raise
80
81     def write_readinto(self, buffer_out, buffer_in, out_start=0,
82                        out_end=None, in_start=0, in_end=None):
83         if not buffer_out or not buffer_in:
84             return
85         if out_end is None:
86             out_end = len(buffer_out)
87         if in_end is None:
88             in_end = len(buffer_in)
89         if out_end - out_start != in_end - in_start:
90             raise RuntimeError('Buffer slices must be of equal length.')
91         try:
92             self._spi.open(self._port, 0)
93             self.set_no_cs()
94             self._spi.max_speed_hz = self.baudrate
95             self._spi.mode = self.mode
96             self._spi.bits_per_word = self.bits
97             data = self._spi.xfer(list(buffer_out[out_start:out_end+1]))
98             for i in range((in_end - in_start)):
99                 buffer_in[i+in_start] = data[i]
100             self._spi.close()
101         except FileNotFoundError as not_found:
102             print("Could not open SPI device - check if SPI is enabled in kernel!")
103             raise