From 7db07c46e68b3c9301330e0973150bfc25000a33 Mon Sep 17 00:00:00 2001 From: Eric Callahan Date: Fri, 22 Apr 2022 19:48:39 -0400 Subject: [PATCH] spi_flash: support spi protocol changes Signed-off-by: Eric Callahan --- scripts/spi_flash/spi_flash.py | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/scripts/spi_flash/spi_flash.py b/scripts/spi_flash/spi_flash.py index 99cd4bae..c16d76d5 100644 --- a/scripts/spi_flash/spi_flash.py +++ b/scripts/spi_flash/spi_flash.py @@ -104,7 +104,10 @@ GET_CFG_RESPONSES = ( # Supported responses (sorted by newer revisions first). "config is_config=%c crc=%u move_count=%hu is_shutdown=%c" # Original ) ALLOC_OIDS_CMD = "allocate_oids count=%d" -SPI_CFG_CMD = "config_spi oid=%d pin=%s" +SPI_CFG_CMDS = ( + "config_spi oid=%d pin=%s cs_active_high=%d", # 7793784 + "config_spi oid=%d pin=%s" # Original +) SPI_BUS_CMD = "spi_set_bus oid=%d spi_bus=%s mode=%d rate=%d" SW_SPI_BUS_CMD = "spi_set_software_bus oid=%d " \ "miso_pin=%s mosi_pin=%s sclk_pin=%s mode=%d rate=%d" @@ -794,6 +797,7 @@ class MCUConnection: self.connected = False self.enumerations = {} self.raw_dictionary = None + self.proto_error = None def connect(self): output("Connecting to MCU..") @@ -821,6 +825,7 @@ class MCUConnection: % (build_mcu_type, mcu_type)) self.enumerations = msgparser.get_enumerations() self.raw_dictionary = msgparser.get_raw_data_dictionary() + self.proto_error = msgparser.error def _do_serial_connect(self, eventtime): endtime = eventtime + 60. @@ -903,15 +908,24 @@ class MCUConnection: bus_cmd = SPI_BUS_CMD % (SPI_OID, bus, SPI_MODE, SD_SPI_SPEED) if cs_pin not in pin_enums: raise SPIFlashError("Invalid CS Pin: %s" % (cs_pin,)) - cfg_cmds = [ - ALLOC_OIDS_CMD % (1), - SPI_CFG_CMD % (SPI_OID, cs_pin), - bus_cmd, + cfg_cmds = [ALLOC_OIDS_CMD % (1,), bus_cmd] + self._serial.send(cfg_cmds[0]) + spi_cfg_cmds = [ + SPI_CFG_CMDS[0] % (SPI_OID, cs_pin, False), + SPI_CFG_CMDS[1] % (SPI_OID, cs_pin), ] + for cmd in spi_cfg_cmds: + try: + self._serial.send(cmd) + except self.proto_error: + if cmd == spi_cfg_cmds[-1]: + raise + else: + cfg_cmds.insert(1, cmd) + break + self._serial.send(bus_cmd) config_crc = zlib.crc32('\n'.join(cfg_cmds).encode()) & 0xffffffff - cfg_cmds.append(FINALIZE_CFG_CMD % (config_crc,)) - for cmd in cfg_cmds: - self._serial.send(cmd) + self._serial.send(FINALIZE_CFG_CMD % (config_crc,)) self.fatfs = FatFS(self._serial) self.reactor.pause(self.reactor.monotonic() + .5) printfunc("Initializing SD Card and Mounting file system...")