]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/generic_linux/spi.py
do not set SPI_NO_CS bit when chip is AM3358 #104
[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:
39             try:
40                 self._spi.no_cs = True  # this doesn't work but try anyways
41             except AttributeError:
42                 pass
43         else:
44             # TODO: remove this debug output
45             print("Adafruit_Blinka: generic_linux/spi.py: set_no_cs(): detected AM33XX, SKIP setting no_cs")
46
47     def write(self, buf, start=0, end=None):
48         if not buf:
49             return
50         if end is None:
51             end = len(buf)
52         try:
53             self._spi.open(self._port, 0)
54             self.set_no_cs()
55             self._spi.max_speed_hz = self.baudrate
56             self._spi.mode = self.mode
57             self._spi.bits_per_word = self.bits
58             self._spi.writebytes([x for x in buf[start:end]])
59             self._spi.close()
60         except FileNotFoundError as not_found:
61             print("Could not open SPI device - check if SPI is enabled in kernel!")
62             raise
63
64     def readinto(self, buf, start=0, end=None, write_value=0):
65         if not buf:
66             return
67         if end is None:
68             end = len(buf)
69         try:
70             self._spi.open(self._port, 0)
71             self.set_no_cs()
72             self._spi.max_speed_hz = self.baudrate
73             self._spi.mode = self.mode
74             self._spi.bits_per_word = self.bits
75             data = self._spi.xfer([write_value]*(end-start))
76             for i in range(end-start):  # 'readinto' the given buffer
77               buf[start+i] = data[i]
78             self._spi.close()
79         except FileNotFoundError as not_found:
80             print("Could not open SPI device - check if SPI is enabled in kernel!")
81             raise
82
83     def write_readinto(self, buffer_out, buffer_in, out_start=0,
84                        out_end=None, in_start=0, in_end=None):
85         if not buffer_out or not buffer_in:
86             return
87         if out_end is None:
88             out_end = len(buffer_out)
89         if in_end is None:
90             in_end = len(buffer_in)
91         if out_end - out_start != in_end - in_start:
92             raise RuntimeError('Buffer slices must be of equal length.')
93         try:
94             self._spi.open(self._port, 0)
95             self.set_no_cs()
96             self._spi.max_speed_hz = self.baudrate
97             self._spi.mode = self.mode
98             self._spi.bits_per_word = self.bits
99             data = self._spi.xfer(list(buffer_out[out_start:out_end+1]))
100             for i in range((in_end - in_start)):
101                 buffer_in[i+in_start] = data[i]
102             self._spi.close()
103         except FileNotFoundError as not_found:
104             print("Could not open SPI device - check if SPI is enabled in kernel!")
105             raise