]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/nova/spi.py
Enable spi init and read
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / nova / spi.py
1 #from adafruit_blinka.microcontroller.nova.pin import Pin
2
3 class SPI:
4     MSB = 0
5
6     def __init__(self, clock):
7         from binhoHostAdapter import binhoHostAdapter
8         from binhoHostAdapter import binhoUtilities
9
10         utilities = binhoUtilities.binhoUtilities()
11         devices = utilities.listAvailableDevices()
12
13         if len(devices) > 0:
14             self._nova = binhoHostAdapter.binhoHostAdapter(devices[0])
15             self._nova.setOperationMode(0, 'SPI')
16             self._nova.setClockSPI(0, clock)
17             self._nova.setModeSPI(0, 0)
18             self._nova.setIOpinMode(0, 'DOUT')
19             self._nova.setIOpinValue(0, 'HIGH')
20             self._nova.beginSPI(0)
21             # Cpol and Cpha set by mode
22             # Mode  Cpol Cpha
23             #  0     0    0
24             #  1     0    1
25             #  2     1    0
26             #  3     1    1
27
28         else:
29             raise RuntimeError('No Binho host adapter found!')
30
31     def init(self, baudrate=100000, polarity=0, phase=0, bits=8,
32                   firstbit=MSB, sck=None, mosi=None, miso=None):
33         print("baudrate: " + baudrate)
34         print("mode: " + (polarity<<1) | (phase))
35         self._nova.setClockSPI(0, baudrate)
36         self._nova.setModeSPI(0, (polarity<<1) | (phase))
37
38     @staticmethod
39     def getSpiReceivedData(lineOutput):
40         return (lineOutput.split('RXD ')[1])
41
42     @property
43     def frequency(self):
44         return self._nova.getClockSPI(0)
45
46     def write(self, buf, start=0, end=None):
47         end = end if end else len(buf)
48         #chunks, rest = divmod(end - start, self._spi.PAYLOAD_MAX_LENGTH)
49         #for i in range(chunks):
50         #    chunk_start = start + i * self._spi.PAYLOAD_MAX_LENGTH
51         #    chunk_end = chunk_start + self._spi.PAYLOAD_MAX_LENGTH
52         #    self._port.write(buf[chunk_start:chunk_end])
53         #if rest:
54         #    self._port.write(buf[-1*rest:])
55
56     def readinto(self, buf, start=0, end=None, write_value=0):
57         end = end if end else len(buf)
58         self._nova.setIOpinValue(0, 'LOW')
59         for i in range(start, end):
60             buf[start+i] = int(self.getSpiReceivedData(self._nova.transferSPI(0, write_value)), 16)
61         self._nova.setIOpinValue(0, 'HIGH')
62 """
63     def write_readinto(self, buffer_out, buffer_in,  out_start=0, out_end=None, in_start=0, in_end=None):
64         out_end = out_end if out_end else len(buffer_out)
65         in_end = in_end if in_end else len(buffer_in)
66         result = self._port.exchange(buffer_out[out_start:out_end],
67                                      in_end-in_start, duplex=True)
68         for i, b in enumerate(result):
69             buffer_in[in_start+i] = b
70 """