]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/nova/spi.py
Fix some syntax errors and add binho nova to busio.py for i2c and spi
[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):
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
15             self._nova = binhoHostAdapter.binhoHostAdapter(devices[0])
16             self._nova.setOperationMode(0, 'SPI')
17             self._nova.setClockSPI(0, 12000000)
18             self._nova.setModeSPI(0, 0)
19             self._nova.setIOpinMode(0, 'DOUT')
20             self._nova.setIOpinValue(0, 'HIGH')
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         self._nova.setClockSPI(0, baudrate)
34         self._nova.setModeSPI(0, (polarity<<1) | (phase))
35
36     @property
37     def frequency(self):
38         return self._nova.getClockSPI(0)
39
40     def write(self, buf, start=0, end=None):
41         end = end if end else len(buf)
42         #chunks, rest = divmod(end - start, self._spi.PAYLOAD_MAX_LENGTH)
43         #for i in range(chunks):
44         #    chunk_start = start + i * self._spi.PAYLOAD_MAX_LENGTH
45         #    chunk_end = chunk_start + self._spi.PAYLOAD_MAX_LENGTH
46         #    self._port.write(buf[chunk_start:chunk_end])
47         #if rest:
48         #    self._port.write(buf[-1*rest:])
49
50     def readinto(self, buf, start=0, end=None, write_value=0):
51         end = end if end else len(buf)
52         self._nova.setIOpinValue(0, 'LOW')
53         for i in range(start, end):
54             buf[start+i] = int(getSpiReceivedData(self._nova.transferSPI(0, 0x00)), 16)
55         self._nova.setIOpinValue(0, 'HIGH')
56 """
57     def write_readinto(self, buffer_out, buffer_in,  out_start=0, out_end=None, in_start=0, in_end=None):
58         out_end = out_end if out_end else len(buffer_out)
59         in_end = in_end if in_end else len(buffer_in)
60         result = self._port.exchange(buffer_out[out_start:out_end],
61                                      in_end-in_start, duplex=True)
62         for i, b in enumerate(result):
63             buffer_in[in_start+i] = b
64 """