1 from adafruit_blinka.microcontroller.nova.pin import Pin
7 from binhoHostAdapter import binhoHostAdapter
8 from binhoHostAdapter import binhoUtilities
10 utilities = binhoUtilities.binhoUtilities()
11 devices = utilities.listAvailableDevices()
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
29 raise RuntimeError('No Binho host adapter found!')
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))
38 return self._nova.getClockSPI(0)
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])
48 # self._port.write(buf[-1*rest:])
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')
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