-
Notifications
You must be signed in to change notification settings - Fork 234
driver/quartushpsdriver: add support for Quartus HPS #206
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,67 @@ | ||
| import enum | ||
|
|
||
| import attr | ||
|
|
||
| from labgrid.driver import QuartusHPSDriver, SerialDriver | ||
| from labgrid.factory import target_factory | ||
| from labgrid.protocol import PowerProtocol | ||
| from labgrid.step import step | ||
| from labgrid.strategy.common import Strategy | ||
|
|
||
|
|
||
| @attr.s(cmp=False) | ||
| class StrategyError(Exception): | ||
| msg = attr.ib(validator=attr.validators.instance_of(str)) | ||
|
|
||
|
|
||
| class Status(enum.Enum): | ||
| unknown = 0 | ||
| flashed_xload = 1 | ||
| flashed = 2 | ||
|
|
||
|
|
||
| @target_factory.reg_driver | ||
| @attr.s(cmp=False) | ||
| class QuartusHPSStrategy(Strategy): | ||
| """QuartusHPSStrategy - Strategy to flash QSPI via 'Quartus Prime Programmer and Tools'""" | ||
| bindings = { | ||
| "power": PowerProtocol, | ||
| "quartushps": QuartusHPSDriver, | ||
| "serial": SerialDriver, | ||
| } | ||
|
|
||
| image = attr.ib(validator=attr.validators.instance_of(str)) | ||
| image_xload = attr.ib(validator=attr.validators.instance_of(str)) | ||
| status = attr.ib(default=Status.unknown) | ||
|
|
||
| def __attrs_post_init__(self): | ||
| super().__attrs_post_init__() | ||
|
|
||
| @step(args=['status']) | ||
| def transition(self, status, *, step): | ||
| if not isinstance(status, Status): | ||
| status = Status[status] | ||
| if status == Status.unknown: | ||
| raise StrategyError("can not transition to {}".format(status)) | ||
| elif status == self.status: | ||
| step.skip("nothing to do") | ||
| return # nothing to do | ||
| elif status == Status.flashed_xload: | ||
| self.target.activate(self.power) | ||
| self.power.cycle() | ||
| self.target.activate(self.quartushps) | ||
| # flash bootloader xload image to 0x0 | ||
| self.quartushps.flash(self.image_xload, 0x0) | ||
| elif status == Status.flashed: | ||
| self.transition(Status.flashed_xload) | ||
| # flash bootloader image to 0x40000 | ||
| self.quartushps.flash(self.image, 0x40000) | ||
| self.power.cycle() | ||
| # activate serial in order to make 'labgrid-client -s $STATE con' work | ||
| self.target.activate(self.serial) | ||
| else: | ||
| raise StrategyError( | ||
| "no transition found from {} to {}". | ||
| format(self.status, status) | ||
| ) | ||
| self.status = status |
This file contains hidden or 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 hidden or 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,82 @@ | ||
| # pylint: disable=no-member | ||
| import attr | ||
| import subprocess | ||
| import os.path | ||
| import re | ||
|
|
||
| from ..factory import target_factory | ||
| from ..resource.remote import NetworkAlteraUSBBlaster | ||
| from ..resource.udev import AlteraUSBBlaster | ||
| from ..step import step | ||
| from .common import Driver, check_file | ||
| from .exception import ExecutionError | ||
|
|
||
|
|
||
| @target_factory.reg_driver | ||
| @attr.s(cmp=False) | ||
| class QuartusHPSDriver(Driver): | ||
| bindings = { | ||
| "interface": {AlteraUSBBlaster, NetworkAlteraUSBBlaster}, | ||
| } | ||
|
|
||
| image = attr.ib(default=None, validator=attr.validators.optional(attr.validators.instance_of(str))) | ||
| cable_number = attr.ib(default=None, validator=attr.validators.optional(attr.validators.instance_of(str))) | ||
|
|
||
| def __attrs_post_init__(self): | ||
| super().__attrs_post_init__() | ||
| # FIXME make sure we always have an environment or config | ||
| if self.target.env: | ||
| self.tool = self.target.env.config.get_tool('quartus_hps') or 'quartus_hps' | ||
| else: | ||
| self.tool = 'quartus_hps' | ||
|
|
||
| def on_deactivate(self): | ||
| # forget cable number as it might change | ||
| self.cable_number = None | ||
|
|
||
| def _get_cable_number(self): | ||
| """Returns the JTAG cable numer for the USB path of the device""" | ||
| # FIXME make sure we always have an environment or config | ||
| if self.target.env: | ||
| jtagconfig_tool = self.target.env.config.get_tool('jtagconfig') or 'jtagconfig' | ||
| else: | ||
| jtagconfig_tool = 'jtagconfig' | ||
|
|
||
| cmd = self.interface.command_prefix + [jtagconfig_tool] | ||
| jtagconfig_process = subprocess.Popen( | ||
| cmd, | ||
| stdout=subprocess.PIPE | ||
| ) | ||
| stdout, _ = jtagconfig_process.communicate() | ||
|
|
||
| regex = re.compile(r".*(\d+)\) .* \[(.*)\]") | ||
| for line in stdout.decode("utf-8").split("\n"): | ||
| jtag_mapping = regex.match(line) | ||
| if jtag_mapping: | ||
| cable_number, usb_path = jtag_mapping.groups() | ||
| if usb_path == self.interface.path: | ||
| return int(cable_number) | ||
|
|
||
| raise ExecutionError("Could not get cable number for USB path {}" | ||
| .format(self.interface.path)) | ||
|
|
||
| @Driver.check_active | ||
| @step(args=['filename', 'address']) | ||
| def flash(self, filename=None, address=0x0): | ||
| if filename is None and self.image is not None: | ||
| filename = self.target.env.config.get_image_path(self.image) | ||
| filename = os.path.abspath(os.path.expanduser(filename)) | ||
| check_file(filename, command_prefix=self.interface.command_prefix) | ||
|
|
||
| assert(isinstance(address, int)) | ||
|
|
||
| if self.cable_number is None: | ||
| self.cable_number = self._get_cable_number() | ||
|
|
||
| cmd = self.interface.command_prefix + [self.tool] | ||
| cmd += [ | ||
| "--cable={}".format(self.cable_number), | ||
| "--addr=0x{:X}".format(address), | ||
| "--operation=P {}".format(filename), | ||
| ] | ||
| subprocess.check_call(cmd) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to implement a Protocol. BootstrapProtocol would be the obvious choice, however your driver implements an additional feature: choosing the address of where to load the file. I am not sure that we want to extend the existing protocol or define a new one for this.