X-Git-Url: https://git.ayoreis.com/Adafruit_Blinka-hackapet.git/blobdiff_plain/3395c53b2390013098f543dbe6c810c34a5d9705..0ab99d44db2e62b79e57ebb751459ac4b371daab:/src/adafruit_blinka/microcontroller/nova/spi.py diff --git a/src/adafruit_blinka/microcontroller/nova/spi.py b/src/adafruit_blinka/microcontroller/nova/spi.py index 345468c..ce61fd6 100644 --- a/src/adafruit_blinka/microcontroller/nova/spi.py +++ b/src/adafruit_blinka/microcontroller/nova/spi.py @@ -1,64 +1,106 @@ -from adafruit_blinka.microcontroller.nova.pin import Pin +"""SPI Class for Binho Nova""" +from adafruit_blinka.microcontroller.nova import Connection + class SPI: + """Custom SPI Class for Binho Nova""" + MSB = 0 + PAYLOAD_MAX_LENGTH = 64 - def __init__(self): - from binhoHostAdapter import binhoHostAdapter - from binhoHostAdapter import binhoUtilities + def __init__(self, clock): + 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) - utilities = binhoUtilities.binhoUtilities() - devices = utilities.listAvailableDevices() + # Cpol and Cpha set by mode + # Mode Cpol Cpha + # 0 0 0 + # 1 0 1 + # 2 1 0 + # 3 1 1 - if len(devices) > 0: + # pylint: disable=too-many-arguments,unused-argument + def init( + self, + baudrate=100000, + polarity=0, + phase=0, + bits=8, + firstbit=MSB, + sck=None, + mosi=None, + 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)) - self._nova = binhoHostAdapter.binhoHostAdapter(devices[0]) - self._nova.setOperationMode(0, 'SPI') - self._nova.setClockSPI(0, 12000000) - self._nova.setModeSPI(0, 0) - self._nova.setIOpinMode(0, 'DOUT') - self._nova.setIOpinValue(0, 'HIGH') - # Cpol and Cpha set by mode - # Mode Cpol Cpha - # 0 0 0 - # 1 0 1 - # 2 1 0 - # 3 1 1 + # pylint: enable=too-many-arguments,unused-argument - else: - raise RuntimeError('No Binho host adapter found!') - - def init(self, baudrate=100000, polarity=0, phase=0, bits=8, - firstbit=MSB, sck=None, mosi=None, miso=None): - self._nova.setClockSPI(0, baudrate) - self._nova.setModeSPI(0, (polarity<<1) | (phase)) + @staticmethod + def get_received_data(lineOutput): + """Return any received data""" + return lineOutput.split("RXD ")[1] @property def frequency(self): - return self._nova.getClockSPI(0) + """Return the current frequency""" + return self._nova.getClockSPI(0).split("CLK ")[1] 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._spi.PAYLOAD_MAX_LENGTH) - #for i in range(chunks): - # chunk_start = start + i * self._spi.PAYLOAD_MAX_LENGTH - # chunk_end = chunk_start + self._spi.PAYLOAD_MAX_LENGTH - # self._port.write(buf[chunk_start:chunk_end]) - #if rest: - # self._port.write(buf[-1*rest:]) + chunks, rest = divmod(end - start, self.PAYLOAD_MAX_LENGTH) + 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) + if rest: + buffer_data = buf[-1 * rest :] + self._nova.clearBuffer(0) + self._nova.writeToBuffer(0, 0, buffer_data) + 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) - self._nova.setIOpinValue(0, 'LOW') for i in range(start, end): - buf[start+i] = int(getSpiReceivedData(self._nova.transferSPI(0, 0x00)), 16) - self._nova.setIOpinValue(0, 'HIGH') -""" - def write_readinto(self, buffer_out, buffer_in, out_start=0, out_end=None, in_start=0, in_end=None): + buf[start + i] = int( + self.get_received_data(self._nova.transferSPI(0, write_value)) + ) + + # pylint: disable=too-many-arguments + def write_readinto( + self, buffer_out, buffer_in, out_start=0, out_end=None, in_start=0, in_end=None + ): + """Perform a half-duplex write from buffer_out and then + read data into buffer_in + """ out_end = out_end if out_end else len(buffer_out) in_end = in_end if in_end else len(buffer_in) - result = self._port.exchange(buffer_out[out_start:out_end], - in_end-in_start, duplex=True) - for i, b in enumerate(result): - buffer_in[in_start+i] = b -""" \ No newline at end of file + readlen = in_end - in_start + writelen = out_end - out_start + if readlen > writelen: + # resize out and pad with 0's + 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