-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
driver: Add driver for Dediprog SPI-flash emulator
The EM100-pro is a SPI-flash emulator which supports a variety of chips. It can be used to provide an image to a booting system. Add a driver which allows images to be loaded ready for use. Signed-off-by: Simon Glass <[email protected]>
- Loading branch information
Showing
6 changed files
with
188 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import subprocess | ||
from threading import Thread | ||
import time | ||
|
||
import attr | ||
from .common import Driver | ||
from ..factory import target_factory | ||
from ..step import step | ||
from ..util.helper import processwrapper, ProcessRunner | ||
from ..util.managedfile import ManagedFile | ||
|
||
@target_factory.reg_driver | ||
@attr.s(eq=False) | ||
class SFEmulatorDriver(Driver): | ||
"""Provides access to em100 features | ||
Args: | ||
bindings (dict): driver to use with | ||
_proc (subprocess.Popen): Process running em100 (used only in trace | ||
mode) | ||
_trace (str): Filename of trace file, if enabled | ||
_thread (Thread): Thread which monitors the subprocess for errors | ||
""" | ||
bindings = { | ||
'emul': {'SFEmulator', 'NetworkSFEmulator'}, | ||
} | ||
trace = attr.ib(default=False, validator=attr.validators.instance_of(bool)) | ||
|
||
def __attrs_post_init__(self): | ||
super().__attrs_post_init__() | ||
if self.target.env: | ||
self.tool = self.target.env.config.get_tool('em100') | ||
else: | ||
self.tool = 'em100' | ||
self._trace = None | ||
self._thread = None | ||
|
||
@Driver.check_active | ||
@step(title='write_image', args=['filename']) | ||
def write_image(self, filename): | ||
'''Write an image to the SPI-flash emulator | ||
Args: | ||
filename (str): Filename to write | ||
''' | ||
self._trace = None # Provide this filename later | ||
|
||
mf = ManagedFile(filename, self.emul) | ||
mf.sync_to_resource() | ||
cmd = self.emul.command_prefix + [ | ||
self.tool, | ||
'-x', str(self.emul.serial), | ||
'-s', | ||
'-p', 'LOW', | ||
'-c', self.emul.chip, | ||
'-d', mf.get_remote_path(), | ||
'-r', | ||
] | ||
|
||
# For trace mode, start a process which will run for the duration of | ||
# the Labgrid client, all going well | ||
if self._trace: | ||
cmd.append('-t') | ||
logfile = open(self._trace, 'w') | ||
self._thread = Thread(target=self.monitor_thread, | ||
args=(cmd, logfile)) | ||
self._thread.daemon = True | ||
self._thread.start() | ||
else: | ||
processwrapper.check_output(cmd) | ||
|
||
def __str__(self): | ||
return f'SFEmulatorDriver({self.emul.serial})' | ||
|
||
def monitor_thread(self, cmd, logfile): | ||
"""Thread to monitor the em100 process | ||
This is mostly just here to check if it dies, so a useful error can be | ||
shown. | ||
Args: | ||
cmd (list of str): Command to run | ||
logfile (file): Output file for trace log | ||
""" | ||
proc = ProcessRunner(cmd, stdin=subprocess.DEVNULL, stdout=logfile) | ||
while True: | ||
if proc.check(): | ||
break | ||
time.sleep(.1) | ||
try: | ||
proc.kill() | ||
proc.finish() | ||
except subprocess.CalledProcessError as exc: | ||
# If there is something wrong, the error will be in the logfile, so | ||
# print it out | ||
print('em100 failed', exc.returncode) | ||
with open(self._trace, 'r') as inf: | ||
print('err', inf.read(1024)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import attr | ||
|
||
from ..factory import target_factory | ||
from .common import NetworkResource, Resource | ||
|
||
@target_factory.reg_resource | ||
@attr.s(eq=False) | ||
class SFEmulator(Resource): | ||
""""This resource describes a Dediprog em100 SPI-Flash Emulator | ||
This provides serial consoles along with reset control | ||
Args: | ||
serial (str): serial number of the em100 device, e.g. DP025143 | ||
chip (str): SPI-flash chip to emulate, e.g. W25Q64CV | ||
""" | ||
serial = attr.ib(validator=attr.validators.instance_of(str)) | ||
chip = attr.ib(validator=attr.validators.instance_of(str)) | ||
|
||
@target_factory.reg_resource | ||
@attr.s(eq=False) | ||
class NetworkSFEmulator(NetworkResource): | ||
""""This resource describes a remote Dediprog em100 SPI-Flash Emulator | ||
This provides serial consoles along with reset control | ||
Args: | ||
serial (str): serial number of the em100 device, e.g. DP025143 | ||
chip (str): SPI-flash chip to emulate, e.g. W25Q64CV | ||
""" | ||
serial = attr.ib(validator=attr.validators.instance_of(str)) | ||
chip = attr.ib(validator=attr.validators.instance_of(str)) |