]> Repositories - Adafruit_Blinka-hackapet.git/blobdiff - src/adafruit_blinka/microcontroller/nova/spi.py
Merge pull request #216 from binhollc/master
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / nova / spi.py
index d92d2d7698eaf877d6468a5d6191302b9012e3b0..39269229db72879e7546cd6a268a9bf9bc66499a 100644 (file)
@@ -1,42 +1,34 @@
-#from adafruit_blinka.microcontroller.nova.pin import Pin
-
 class SPI:
+    MSB = 0
     PAYLOAD_MAX_LENGTH = 64
 
     def __init__(self, clock):
-        from binhoHostAdapter import binhoHostAdapter
-        from binhoHostAdapter import binhoUtilities
-
-        utilities = binhoUtilities.binhoUtilities()
-        devices = utilities.listAvailableDevices()
-
-        if len(devices) > 0:
-            self._nova = binhoHostAdapter.binhoHostAdapter(devices[0])
-            self._nova.setOperationMode(0, 'SPI')
-            self._nova.setClockSPI(0, clock)
-            self._nova.setModeSPI(0, 0)
-            self._nova.setIOpinMode(0, 'DOUT')
-            self._nova.setIOpinValue(0, 'HIGH')
-            self._nova.beginSPI(0)
-            # Cpol and Cpha set by mode
-            # Mode  Cpol Cpha
-            #  0     0    0
-            #  1     0    1
-            #  2     1    0
-            #  3     1    1
+        from adafruit_blinka.microcontroller.nova import Connection
+        self._nova = Connection.getInstance()
+        self._nova.setNumericalBase(10)
+        self._nova.setOperationMode(0, 'SPI')
+        self._nova.setClockSPI(0, clock)
+        self._nova.setModeSPI(0, 0)
+        self._nova.setIOpinMode(0, 'DOUT')
+        self._nova.setIOpinMode(1, 'DOUT')
+        self._nova.beginSPI(0)
 
-        else:
-            raise RuntimeError('No Binho host adapter found!')
+        # Cpol and Cpha set by mode
+        # Mode  Cpol Cpha
+        #  0     0    0
+        #  1     0    1
+        #  2     1    0
+        #  3     1    1
 
     def init(self, baudrate=100000, polarity=0, phase=0, bits=8,
-                  firstbit=MSB, sck=None, mosi=None, miso=None):
-        print("baudrate: " + baudrate)
-        print("mode: " + (polarity<<1) | (phase))
+             firstbit=MSB, sck=None, mosi=None, miso=None):
+        #print("baudrate: " + str(baudrate))
+        #print("mode: " + str((polarity<<1) | (phase)))
         self._nova.setClockSPI(0, baudrate)
         self._nova.setModeSPI(0, (polarity<<1) | (phase))
 
     @staticmethod
-    def getSpiReceivedData(lineOutput):
+    def get_received_data(lineOutput):
         return (lineOutput.split('RXD ')[1])
 
     @property
@@ -46,7 +38,6 @@ class SPI:
     def write(self, buf, start=0, end=None):
         end = end if end else len(buf)
         chunks, rest = divmod(end - start, self.PAYLOAD_MAX_LENGTH)
-        self._nova.setIOpinValue(0, 'LOW')
         for i in range(chunks):
             chunk_start = start + i * self.PAYLOAD_MAX_LENGTH
             chunk_end = chunk_start + self.PAYLOAD_MAX_LENGTH
@@ -59,17 +50,13 @@ class SPI:
             self._nova.clearBuffer(0)
             self._nova.writeToBuffer(0, 0, buffer_data)
             self._nova.transferBufferSPI(0, rest)
-        self._nova.setIOpinValue(0, 'HIGH')
-        print(self._nova.readBuffer(0, 4))
 
     def readinto(self, buf, start=0, end=None, write_value=0):
         end = end if end else len(buf)
-        self._nova.setIOpinValue(0, 'LOW')
         for i in range(start, end):
-            buf[start+i] = int(self.getSpiReceivedData(self._nova.transferSPI(0, write_value)), 16)
-        self._nova.setIOpinValue(0, 'HIGH')
+            buf[start+i] = int(self.get_received_data(self._nova.transferSPI(0, write_value)))
 
-    def write_readinto(self, buffer_out, buffer_in,  out_start=0, out_end=None, in_start=0, in_end=None):
+    def write_readinto(self, buffer_out, buffer_in, out_start=0, out_end=None, in_start=0, in_end=None):
         out_end = out_end if out_end else len(buffer_out)
         in_end = in_end if in_end else len(buffer_in)
         readlen = in_end-in_start
@@ -80,10 +67,8 @@ class SPI:
             tmp.extend([0] * (readlen - len(buffer_out)))
             buffer_out = tmp
         i = 0
-        self._nova.setIOpinValue(0, 'LOW')
         for data_out in buffer_out:
-            data_in = int(self.getSpiReceivedData(self._nova.transferSPI(0, data_out)), 16)
+            data_in = int(self.get_received_data(self._nova.transferSPI(0, data_out)))
             if i < readlen:
                 buffer_in[in_start+i] = data_in
             i += 1
-        self._nova.setIOpinValue(0, 'HIGH')