]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/mips24kec/spi2.py
1ad3c9b6104bd083b2905c5b286862d0ec036f5a
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / mips24kec / spi2.py
1 from periphery import SPI
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         if isinstance(portid, tuple):
17             self._bus_id = portid[0]
18             self._device_id = portid[1]
19         else:
20             self._bus_id = portid
21             self._device_id = 0
22
23     def init(self, baudrate=100000, polarity=0, phase=0, bits=8,
24                   firstbit=MSB, sck=None, mosi=None, miso=None):
25         mode = 0
26         if polarity:
27             mode |= self.CPOL
28         if phase:
29             mode |= self.CPHA
30
31         self.clock_pin = sck
32         self.mosi_pin = mosi
33         self.miso_pin = miso
34         self.baudrate = baudrate
35         self.mode = mode
36         self.bits = bits
37         self.chip = detector.chip
38
39     def set_no_cs(self):
40         # No kernel seems to support this, so we're just going to pass
41         pass
42
43     @property
44     def frequency(self):
45         return self.baudrate
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 = SPI("/dev/spidev{}.{}".format(self._bus_id, self._device_id), self.mode, self.baudrate, bits_per_word=self.bits)
54             data_in = spi.transfer(buf[start:end])
55             self._spi.close()
56         except FileNotFoundError as not_found:
57             print("Could not open SPI device - check if SPI is enabled in kernel!")
58             raise
59
60     def readinto(self, buf, start=0, end=None, write_value=0):
61         if not buf:
62             return
63         if end is None:
64             end = len(buf)
65         try:
66             self._spi = SPI("/dev/spidev{}.{}".format(self._bus_id, self._device_id), self.mode, self.baudrate, bits_per_word=self.bits)
67             data = spi.transfer([write_value]*(end-start))
68             for i in range(end-start):  # 'readinto' the given buffer
69               buf[start+i] = data[i]
70             self._spi.close()
71         except FileNotFoundError as not_found:
72             print("Could not open SPI device - check if SPI is enabled in kernel!")
73             raise
74
75     def write_readinto(self, buffer_out, buffer_in, out_start=0,
76                        out_end=None, in_start=0, in_end=None):
77         if not buffer_out or not buffer_in:
78             return
79         if out_end is None:
80             out_end = len(buffer_out)
81         if in_end is None:
82             in_end = len(buffer_in)
83         if out_end - out_start != in_end - in_start:
84             raise RuntimeError('Buffer slices must be of equal length.')
85         try:
86             self._spi = SPI("/dev/spidev{}.{}".format(self._bus_id, self._device_id), self.mode, self.baudrate, bits_per_word=self.bits)
87             data = spi.transfer(list(buffer_out[out_start:out_end+1]))
88             for i in range((in_end - in_start)):
89                 buffer_in[i+in_start] = data[i]
90             self._spi.close()
91         except FileNotFoundError as not_found:
92             print("Could not open SPI device - check if SPI is enabled in kernel!")
93             raise