]> Repositories - Adafruit_Blinka-hackapet.git/commitdiff
Merge pull request #313 from binhollc/master 5.1.0
authorMelissa LeBlanc-Williams <melissa@adafruit.com>
Mon, 29 Jun 2020 23:35:36 +0000 (16:35 -0700)
committerGitHub <noreply@github.com>
Mon, 29 Jun 2020 23:35:36 +0000 (16:35 -0700)
Binho Nova performance improvement, use new WHR command which consoli…

requirements.txt
src/adafruit_blinka/microcontroller/nova/__init__.py
src/adafruit_blinka/microcontroller/nova/i2c.py
src/adafruit_blinka/microcontroller/nova/pwmout.py
src/adafruit_blinka/microcontroller/nova/spi.py
src/adafruit_blinka/microcontroller/nova/uart.py

index 21b3728f46d3e8b6aa7771c694f3c8b6525364ff..67765e51dd640b6352b764f3933e288ad8180ef7 100755 (executable)
@@ -5,4 +5,4 @@ RPi.GPIO; platform_machine=='armv7l' or platform_machine=='armv6l'
 rpi_ws281x>=4.0.0; platform_machine=='armv7l' or platform_machine=='armv6l'
 sysv_ipc; sys_platform == 'linux' and platform_machine!='mips'
 pyftdi>=0.40.0
-binho-host-adapter>=0.1.4
+binho-host-adapter>=0.1.6
index bf4127b722f2605c6348da724b5f25106594b2c7..8ab7ce09e52ec6c9a2d0e6d7772dc84aef511be7 100644 (file)
@@ -23,9 +23,7 @@ class Connection:
         from binhoHostAdapter import binhoUtilities
 
         # pylint: enable=import-outside-toplevel
-
-        utilities = binhoUtilities.binhoUtilities()
-        devices = utilities.listAvailableDevices()
+        devices = binhoUtilities.listAvailableDevices()
 
         if len(devices) > 0:
             Connection.__instance = binhoHostAdapter.binhoHostAdapter(devices[0])
index 75306f8270fe7db68da1f458c400cb904e2d9d3f..17731fb837e4bb371eb3afa9231498e75fc5b45c 100644 (file)
@@ -6,6 +6,8 @@ from adafruit_blinka.microcontroller.nova import Connection
 class I2C:
     """Custom I2C Class for Binho Nova"""
 
+    WHR_PAYLOAD_MAX_LENGTH = 1024
+
     def __init__(self, *, frequency=400000):
         self._nova = Connection.getInstance()
         self._nova.setNumericalBase(10)
@@ -13,6 +15,16 @@ class I2C:
         self._nova.setPullUpStateI2C(0, "EN")
         self._nova.setClockI2C(0, frequency)
 
+        self._novaCMDVer = "0"
+        if hasattr(self._nova, "getCommandVer"):
+            response = self._nova.getCommandVer().split(" ")
+            if response[0] != "-NG":
+                self._novaCMDVer = response[1]
+
+    def __del__(self):
+        """Close Nova on delete"""
+        self._nova.close()
+
     def scan(self):
         """Perform an I2C Device Scan"""
         scanResults = []
@@ -30,22 +42,39 @@ class I2C:
     def writeto(self, address, buffer, *, start=0, end=None, stop=True):
         """Write data from the buffer to an address"""
         end = end if end else len(buffer)
+        readBytes = 0
+        if int(self._novaCMDVer) >= 1:
+            chunks, rest = divmod(end - start, self.WHR_PAYLOAD_MAX_LENGTH)
+            for i in range(chunks):
+                chunk_start = start + i * self.WHR_PAYLOAD_MAX_LENGTH
+                chunk_end = chunk_start + self.WHR_PAYLOAD_MAX_LENGTH
+                self._nova.writeToReadFromI2C(
+                    0,
+                    address << 1,
+                    stop,
+                    readBytes,
+                    chunk_end - chunk_start,
+                    buffer[chunk_start:chunk_end],
+                )
+            if rest:
+                self._nova.writeToReadFromI2C(
+                    0, address << 1, stop, readBytes, rest, buffer[-1 * rest :]
+                )
+        else:
+            self._nova.startI2C(0, address << 1)
 
-        self._nova.startI2C(0, address << 1)
-
-        for i in range(start, end):
-            self._nova.writeByteI2C(0, buffer[i])
+            for i in range(start, end):
+                self._nova.writeByteI2C(0, buffer[i])
 
-        if stop:
-            self._nova.endI2C(0)
-        else:
-            self._nova.endI2C(0, True)
+            if stop:
+                self._nova.endI2C(0)
+            else:
+                self._nova.endI2C(0, True)
 
     # pylint: disable=unused-argument
     def readfrom_into(self, address, buffer, *, start=0, end=None, stop=True):
         """Read data from an address and into the buffer"""
         end = end if end else len(buffer)
-
         result = self._nova.readBytesI2C(0, address << 1, len(buffer[start:end]))
 
         if result != "-NG":
@@ -58,6 +87,7 @@ class I2C:
                 "Received error response from Binho Nova, result = " + result
             )
 
+    # pylint: disable=too-many-locals,too-many-branches
     def writeto_then_readfrom(
         self,
         address,
@@ -75,27 +105,75 @@ class I2C:
         """
         out_end = out_end if out_end else len(buffer_out)
         in_end = in_end if in_end else len(buffer_in)
+        if int(self._novaCMDVer) >= 1:
+            totalIn = in_end - in_start
+            totalOut = out_end - out_start
+            totalBytes = totalIn
+            if totalOut > totalIn:
+                totalBytes = totalOut
+            chunks, rest = divmod(totalBytes, self.WHR_PAYLOAD_MAX_LENGTH)
+            if rest > 0:
+                chunks += 1
+
+            for i in range(chunks):
+                # calculate the number of bytes to be written and read
+                numInBytes = self.WHR_PAYLOAD_MAX_LENGTH
+                if totalIn < self.WHR_PAYLOAD_MAX_LENGTH:
+                    numInBytes = totalIn
+                numOutBytes = self.WHR_PAYLOAD_MAX_LENGTH
+                if totalOut < self.WHR_PAYLOAD_MAX_LENGTH:
+                    numOutBytes = totalOut
+
+                # setup the buffer out chunk offset
+                buffer = "0"
+                if numOutBytes > 0:
+                    chunk_start = out_start + i * self.WHR_PAYLOAD_MAX_LENGTH
+                    chunk_end = chunk_start + numOutBytes
+                    buffer = buffer_out[chunk_start:chunk_end]
+
+                result = self._nova.writeToReadFromI2C(
+                    0, address << 1, stop, numInBytes, numOutBytes, buffer
+                )
+                totalIn -= numInBytes
+                totalOut -= numOutBytes
+
+                if result != "-NG":
+                    if numInBytes:
+                        resp = result.split(" ")
+                        resp = resp[2]
+
+                        for j in range(numInBytes):
+                            buffer_in[
+                                in_start + i * self.WHR_PAYLOAD_MAX_LENGTH + j
+                            ] = int(resp[j * 2] + resp[j * 2 + 1], 16)
+                else:
+                    raise RuntimeError(
+                        "Received error response from Binho Nova, result = " + result
+                    )
+        else:
+            self._nova.startI2C(0, address << 1)
 
-        self._nova.startI2C(0, address << 1)
-
-        for i in range(out_start, out_end):
-            self._nova.writeByteI2C(0, buffer_out[i])
+            for i in range(out_start, out_end):
+                self._nova.writeByteI2C(0, buffer_out[i])
 
-        self._nova.endI2C(0, True)
+            if stop:
+                self._nova.endI2C(0)
+            else:
+                self._nova.endI2C(0, True)
 
-        result = self._nova.readBytesI2C(
-            0, address << 1, len(buffer_in[in_start:in_end])
-        )
+            result = self._nova.readBytesI2C(
+                0, address << 1, len(buffer_in[in_start:in_end])
+            )
 
-        if result != "-NG":
-            resp = result.split(" ")
+            if result != "-NG":
+                resp = result.split(" ")
 
-            for i in range(len(buffer_in[in_start:in_end])):
-                buffer_in[in_start + i] = int(resp[2 + i])
-        else:
-            raise RuntimeError(
-                "Received error response from Binho Nova, result = " + result
-            )
+                for i in range(len(buffer_in[in_start:in_end])):
+                    buffer_in[in_start + i] = int(resp[2 + i])
+            else:
+                raise RuntimeError(
+                    "Received error response from Binho Nova, result = " + result
+                )
 
 
-# pylint: enable=unused-argument
+# pylint: enable=unused-argument,too-many-locals,too-many-branches
index 00beba767064bb64a7c4d8289894aa1e6a02a532..413938028eb6335cd849d2713a07b8424c9ab411 100644 (file)
@@ -60,6 +60,7 @@ class PWMOut:
 
     def __del__(self):
         self.deinit()
+        PWMOut._nova.close()
 
     def __enter__(self):
         return self
index ce61fd631d48f5594830052ce8f0b828c7bcb44b..e03853dfc217af8061f1efadddfa7b1b2faa1f85 100644 (file)
@@ -6,7 +6,8 @@ class SPI:
     """Custom SPI Class for Binho Nova"""
 
     MSB = 0
-    PAYLOAD_MAX_LENGTH = 64
+    BUFFER_PAYLOAD_MAX_LENGTH = 64
+    WHR_PAYLOAD_MAX_LENGTH = 1024
 
     def __init__(self, clock):
         self._nova = Connection.getInstance()
@@ -17,6 +18,11 @@ class SPI:
         self._nova.setIOpinMode(0, "DOUT")
         self._nova.setIOpinMode(1, "DOUT")
         self._nova.beginSPI(0)
+        self._novaCMDVer = "0"
+        if hasattr(self._nova, "getCommandVer"):
+            response = self._nova.getCommandVer().split(" ")
+            if response[0] != "-NG":
+                self._novaCMDVer = response[1]
 
         # Cpol and Cpha set by mode
         # Mode  Cpol Cpha
@@ -25,10 +31,14 @@ class SPI:
         #  2     1    0
         #  3     1    1
 
+    def __del__(self):
+        """Close Nova on delete"""
+        self._nova.close()
+
     # pylint: disable=too-many-arguments,unused-argument
     def init(
         self,
-        baudrate=100000,
+        baudrate=1000000,
         polarity=0,
         phase=0,
         bits=8,
@@ -38,8 +48,6 @@ class SPI:
         miso=None,
     ):
         """Initialize the Port"""
-        # print("baudrate: " + str(baudrate))
-        # print("mode: " + str((polarity<<1) | (phase)))
         self._nova.setClockSPI(0, baudrate)
         self._nova.setModeSPI(0, (polarity << 1) | (phase))
 
@@ -58,29 +66,80 @@ class SPI:
     def write(self, buf, start=0, end=None):
         """Write data from the buffer to SPI"""
         end = end if end else len(buf)
-        chunks, rest = divmod(end - start, self.PAYLOAD_MAX_LENGTH)
+        payloadMaxLength = self.BUFFER_PAYLOAD_MAX_LENGTH
+        if int(self._novaCMDVer) >= 1:
+            payloadMaxLength = self.WHR_PAYLOAD_MAX_LENGTH
+        chunks, rest = divmod(end - start, payloadMaxLength)
+
         for i in range(chunks):
-            chunk_start = start + i * self.PAYLOAD_MAX_LENGTH
-            chunk_end = chunk_start + self.PAYLOAD_MAX_LENGTH
-            buffer_data = buf[chunk_start:chunk_end]
-            self._nova.clearBuffer(0)
-            self._nova.writeToBuffer(0, 0, buffer_data)
-            self._nova.transferBufferSPI(0, chunk_end - chunk_start + 1)
+            chunk_start = start + i * payloadMaxLength
+            chunk_end = chunk_start + payloadMaxLength
+            if int(self._novaCMDVer) >= 1:
+                self._nova.writeToReadFromSPI(
+                    0, True, False, chunk_end - chunk_start, buf[chunk_start:chunk_end]
+                )
+            else:
+                self._nova.clearBuffer(0)
+                self._nova.writeToBuffer(0, 0, buf[chunk_start:chunk_end])
+                self._nova.transferBufferSPI(0, chunk_end - chunk_start + 1)
         if rest:
-            buffer_data = buf[-1 * rest :]
-            self._nova.clearBuffer(0)
-            self._nova.writeToBuffer(0, 0, buffer_data)
-            self._nova.transferBufferSPI(0, rest)
+            if int(self._novaCMDVer) >= 1:
+                self._nova.writeToReadFromSPI(0, True, False, rest, buf[-1 * rest :])
+            else:
+                self._nova.clearBuffer(0)
+                self._nova.writeToBuffer(0, 0, buf[-1 * rest :])
+                self._nova.transferBufferSPI(0, rest)
 
     def readinto(self, buf, start=0, end=None, write_value=0):
         """Read data from SPI and into the buffer"""
         end = end if end else len(buf)
-        for i in range(start, end):
-            buf[start + i] = int(
-                self.get_received_data(self._nova.transferSPI(0, write_value))
-            )
+        if int(self._novaCMDVer) >= 1:
+            chunks, rest = divmod(end - start, self.WHR_PAYLOAD_MAX_LENGTH)
+            i = 0
+            for i in range(chunks):
+                chunk_start = start + i * self.WHR_PAYLOAD_MAX_LENGTH
+                chunk_end = chunk_start + self.WHR_PAYLOAD_MAX_LENGTH
+                result = self._nova.writeToReadFromSPI(
+                    0, False, True, chunk_end - chunk_start, write_value
+                )
+                if result != "-NG":
+                    resp = result.split(" ")
+                    resp = resp[2]
+                    # loop over half of resp len as we're reading 2 chars at a time to form a byte
+                    loops = int(len(resp) / 2)
+                    for j in range(loops):
+                        buf[(i * self.WHR_PAYLOAD_MAX_LENGTH) + start + j] = int(
+                            resp[j * 2] + resp[j * 2 + 1], 16
+                        )
+                else:
+                    raise RuntimeError(
+                        "Received error response from Binho Nova, result = " + result
+                    )
+            if rest:
+                result = self._nova.writeToReadFromSPI(
+                    0, False, True, rest, write_value
+                )
+                if result != "-NG":
+                    resp = result.split(" ")
+                    resp = resp[2]
 
-    # pylint: disable=too-many-arguments
+                    # loop over half of resp len as we're reading 2 chars at a time to form a byte
+                    loops = int(len(resp) / 2)
+                    for j in range(loops):
+                        buf[(i * self.WHR_PAYLOAD_MAX_LENGTH) + start + j] = int(
+                            resp[j * 2] + resp[j * 2 + 1], 16
+                        )
+                else:
+                    raise RuntimeError(
+                        "Received error response from Binho Nova, result = " + result
+                    )
+        else:
+            for i in range(start, end):
+                buf[start + i] = int(
+                    self.get_received_data(self._nova.transferSPI(0, write_value))
+                )
+
+    # pylint: disable=too-many-arguments,too-many-locals,too-many-branches
     def write_readinto(
         self, buffer_out, buffer_in, out_start=0, out_end=None, in_start=0, in_end=None
     ):
@@ -96,11 +155,61 @@ class SPI:
             tmp = bytearray(buffer_out)
             tmp.extend([0] * (readlen - len(buffer_out)))
             buffer_out = tmp
-        i = 0
-        for data_out in buffer_out:
-            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
-
-    # pylint: enable=too-many-arguments
+
+        if int(self._novaCMDVer) >= 1:
+            chunks, rest = divmod(len(buffer_out), self.WHR_PAYLOAD_MAX_LENGTH)
+            i = 0
+            for i in range(chunks):
+                chunk_start = out_start + i * self.WHR_PAYLOAD_MAX_LENGTH
+                chunk_end = chunk_start + self.WHR_PAYLOAD_MAX_LENGTH
+                result = self._nova.writeToReadFromSPI(
+                    0,
+                    True,
+                    True,
+                    chunk_end - chunk_start,
+                    buffer_out[chunk_start:chunk_end],
+                )
+
+                if result != "-NG":
+                    resp = result.split(" ")
+                    resp = resp[2]
+
+                    # loop over half of resp len as we're reading 2 chars at a time to form a byte
+                    loops = int(len(resp) / 2)
+                    for j in range(loops):
+                        buffer_in[
+                            (i * self.WHR_PAYLOAD_MAX_LENGTH) + in_start + j
+                        ] = int(resp[j * 2] + resp[j * 2 + 1], 16)
+                else:
+                    raise RuntimeError(
+                        "Received error response from Binho Nova, result = " + result
+                    )
+            if rest:
+                result = self._nova.writeToReadFromSPI(
+                    0, True, True, rest, buffer_out[-1 * rest :]
+                )
+                if result != "-NG":
+                    resp = result.split(" ")
+                    resp = resp[2]
+
+                    # loop over half of resp len as we're reading 2 chars at a time to form a byte
+                    loops = int(len(resp) / 2)
+                    for j in range(loops):
+                        buffer_in[
+                            (i * self.WHR_PAYLOAD_MAX_LENGTH) + in_start + j
+                        ] = int(resp[j * 2] + resp[j * 2 + 1], 16)
+                else:
+                    raise RuntimeError(
+                        "Received error response from Binho Nova, result = " + result
+                    )
+            print(buffer_in)
+        else:
+            for data_out in buffer_out:
+                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
+
+    # pylint: enable=too-many-arguments,too-many-locals,too-many-branches
index d84a339178f455eb1364b513e962b7062379982a..5d698f8eabc4b06a7a933abe9f8781422b351627 100644 (file)
@@ -43,6 +43,10 @@ class UART:
         self._nova.beginBridgeUART(self._id)
 
     # pylint: enable=too-many-arguments,unused-argument
+    def __del__(self):
+        """Close Nova on delete"""
+        self.deinit()
+        self._nova.close()
 
     def deinit(self):
         """Deinitialize"""