class SPI_INTERFACE(object): def __init__(self, vendor, product, interface=1): self._ctrl = SpiController() self._ctrl.configure(vendor, product, interface) def set_port(self, cs=0): self.spi_port = self._ctrl.get_port(cs) def set_spi_frequency(self, frequency): self.spi_port.set_frequency(frequency) def spi(self, data, readlen=0): #bytes = self.pack_data(data) return self.spi_port.exchange(out=data, readlen=readlen) # these two functions are obsolete now def pack_data(self, data): fmt = '<' for b in data: fmt += "B" return struct.pack(fmt, *data) def unpack_data(self, data): fmt = '<' for i in range(len(data)): fmt += 'B' return list(struct.unpack(fmt, data))
def get_flash_device(vendor, product, interface=1, cs=0): """Obtain an instance of the detected flash device""" ctrl = SpiController(silent_clock=False) ctrl.configure(vendor, product, interface) spi = ctrl.get_port(cs) jedec = SerialFlashManager.read_jedec_id(spi) if not jedec: # it is likely that the latency setting is too low if this # condition is encountered raise SerialFlashUnknownJedec("Unable to read JEDEC Id") return SerialFlashManager._get_flash(spi, jedec)
class SerialFlashManager(object): """Serial flash manager. Automatically detects and instanciate the proper flash device class based on the JEDEC identifier which is read out from the device itself. """ CMD_JEDEC_ID = 0x9F def __init__(self, vendor, product, interface=1): self._ctrl = SpiController(silent_clock=False) self._ctrl.configure(vendor, product, interface) def get_flash_device(self, cs=0): """Obtain an instance of the detected flash device""" spi = self._ctrl.get_port(cs) jedec = SerialFlashManager.read_jedec_id(spi) if not jedec: # it is likely that the latency setting is too low if this # condition is encountered raise SerialFlashUnknownJedec("Unable to read JEDEC Id") return SerialFlashManager._get_flash(spi, jedec) @staticmethod def read_jedec_id(spi): """Read flash device JEDEC identifier (3 bytes)""" jedec_cmd = Array('B', [SerialFlashManager.CMD_JEDEC_ID]) return spi.exchange(jedec_cmd, 3).tostring() @staticmethod def _get_flash(spi, jedec): devices = [] contents = sys.modules[__name__].__dict__ for name in contents: if name.endswith('FlashDevice') and not name.startswith('_'): devices.append(contents[name]) for device in devices: if device.match(jedec): return device(spi, jedec) raise SerialFlashUnknownJedec(jedec)
def __init__(self, vendor, product, interface=1): self._ctrl = SpiController(silent_clock=False) self._ctrl.configure(vendor, product, interface)
def __init__(self, vendor, product, interface=1): self._ctrl = SpiController() self._ctrl.configure(vendor, product, interface)
class SerialFlashManager(object): """Serial flash manager. Automatically detects and instanciate the proper flash device class based on the JEDEC identifier which is read out from the device itself. """ CMD_JEDEC_ID = 0x9F def __init__(self, vendor, product, interface=1): self._ctrl = SpiController(silent_clock=False) self._ctrl.configure(vendor, product, interface) def get_flash_device(self, cs=0): """Obtain an instance of the detected flash device""" spi = self._ctrl.get_port(cs) jedec = SerialFlashManager.read_jedec_id(spi) if not jedec: # it is likely that the latency setting is too low if this # condition is encountered raise SerialFlashUnknownJedec("Unable to read JEDEC Id") return SerialFlashManager._get_flash(spi, jedec) @staticmethod def read_jedec_id(spi): """Read flash device JEDEC identifier (3 bytes)""" jedec_cmd = Array('B', [SerialFlashManager.CMD_JEDEC_ID]) return spi.exchange(jedec_cmd, 3).tostring() @staticmethod def _get_flash(spi, jedec): contents = sys.modules[__name__].__dict__ devices = [] plugin_dir = os.path.dirname(os.path.realpath(__file__)) plugin_files = [ x[:-3] for x in os.listdir(plugin_dir) if (x.endswith(".py") and not x.startswith("__init__")) ] sys.path.insert(0, plugin_dir) for p in plugin_files: mod = __import__(p) # print "mod: " + str(mod) sf = getattr(mod, "SerialFlash") # for sp in sf.__subclasses__(): # print "subclass: " + str(sp) # print "plugin file: " + str(p) # print "sf: " + str(sf) for name in dir(mod): if name.startswith('_'): continue obj = getattr(mod, name) if obj not in sf.__subclasses__(): continue # for p in SerialFlash.__subclasses__(): # print "subclass: " + p # print "name: " + name # print "base classes: " + str(inspect.getmro(obj)) # print "\tclass: %s is class %s" % (str(obj), str(SerialFlash)) # if not isinstance (obj, SerialFlash): # continue # if ("SerialFlash" in str(inspect.getmro(obj))): # print "found subclass" # # for bn in inspect.getmro(obj): # print "base name: " + str(bn) # # print "type: " + str(type(obj)) # print "sf type: " + str(type(SerialFlash)) # # if isclass(obj) and issubclass(obj, SerialFlash) and obj is not SerialFLash : # if isclass(obj) and "SerialFLash" not in str(obj) : # print "\t\tappend name: " + name devices.append(obj) for device in devices: if device.match(jedec): return device(spi, jedec) raise SerialFlashUnknownJedec(jedec)
class SerialFlashManager(object): """Serial flash manager. Automatically detects and instanciate the proper flash device class based on the JEDEC identifier which is read out from the device itself. """ CMD_JEDEC_ID = 0x9F def __init__(self, vendor, product, interface=1): self._ctrl = SpiController(silent_clock=False) self._ctrl.configure(vendor, product, interface) def get_flash_device(self, cs=0): """Obtain an instance of the detected flash device""" spi = self._ctrl.get_port(cs) jedec = SerialFlashManager.read_jedec_id(spi) if not jedec: # it is likely that the latency setting is too low if this # condition is encountered raise SerialFlashUnknownJedec("Unable to read JEDEC Id") return SerialFlashManager._get_flash(spi, jedec) @staticmethod def read_jedec_id(spi): """Read flash device JEDEC identifier (3 bytes)""" jedec_cmd = Array('B', [SerialFlashManager.CMD_JEDEC_ID]) return spi.exchange(jedec_cmd, 3).tostring() @staticmethod def _get_flash(spi, jedec): contents = sys.modules[__name__].__dict__ devices = [] plugin_dir = os.path.dirname(os.path.realpath(__file__)) plugin_files = [x[:-3] for x in os.listdir(plugin_dir) if (x.endswith(".py") and not x.startswith("__init__"))] sys.path.insert(0, plugin_dir) for p in plugin_files: mod = __import__(p) # print "mod: " + str(mod) sf = getattr(mod, "SerialFlash") # for sp in sf.__subclasses__(): # print "subclass: " + str(sp) # print "plugin file: " + str(p) # print "sf: " + str(sf) for name in dir (mod): if name.startswith('_'): continue obj = getattr(mod, name) if obj not in sf.__subclasses__(): continue # for p in SerialFlash.__subclasses__(): # print "subclass: " + p # print "name: " + name # print "base classes: " + str(inspect.getmro(obj)) # print "\tclass: %s is class %s" % (str(obj), str(SerialFlash)) # if not isinstance (obj, SerialFlash): # continue # if ("SerialFlash" in str(inspect.getmro(obj))): # print "found subclass" # # for bn in inspect.getmro(obj): # print "base name: " + str(bn) # # print "type: " + str(type(obj)) # print "sf type: " + str(type(SerialFlash)) # # if isclass(obj) and issubclass(obj, SerialFlash) and obj is not SerialFLash : # if isclass(obj) and "SerialFLash" not in str(obj) : # print "\t\tappend name: " + name devices.append(obj) for device in devices: if device.match(jedec): return device(spi, jedec) raise SerialFlashUnknownJedec(jedec)