Skip to content

Commit

Permalink
Merge pull request #49 from ydamigos/remote-jlink
Browse files Browse the repository at this point in the history
ezFlashCLI: Support remote JLink devices
  • Loading branch information
ezflash authored Apr 25, 2024
2 parents 661fa5b + d45ce6d commit 888a08d
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 58 deletions.
95 changes: 54 additions & 41 deletions ezFlashCLI/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,53 +71,58 @@ def __init__(self):
if not self.args.port:
self.link = pyjlink()
self.link.init()
self.rawdevicelist = self.link.browse()

if self.rawdevicelist is None:
logging.error("No JLink device found")
import platform

if platform.system() in "Linux" and not os.path.exists(
"/etc/udev/rules.d/99-jlink.rules"
):
rulefile = os.path.join(
os.path.dirname(__file__),
"third-party",
"segger",
"99-jlink.rules",
)
logging.info(
"This may be caused by missing udev rules, run this command to add them: sudo cp {} /etc/udev/rules.d/".format(
rulefile
if self.args.host:
self.link.iphost = self.args.host
else:
self.rawdevicelist = self.link.browse()
if self.rawdevicelist is None:
logging.error("No JLink device found")
import platform

if platform.system() in "Linux" and not os.path.exists(
"/etc/udev/rules.d/99-jlink.rules"
):
rulefile = os.path.join(
os.path.dirname(__file__),
"third-party",
"segger",
"99-jlink.rules",
)
logging.info(
"This may be caused by missing udev rules, run this command to add them: sudo cp {} /etc/udev/rules.d/".format(
rulefile
)
)
sys.exit(1)

self.devicelist = []
for device in self.rawdevicelist:
if device.SerialNumber != 0:
self.devicelist.append(device)

self.devicelist.sort()
if (
len(self.devicelist) > 1
and not self.args.jlink
and self.args.operation != "list"
):
logging.warning("JLink interface must be selected using -j option")
logging.warning(
"Multiple interfaces detected, the lowest serial number is selected"
)
sys.exit(1)

self.devicelist = []
for device in self.rawdevicelist:
if device.SerialNumber != 0:
self.devicelist.append(device)

self.devicelist.sort()
if (
len(self.devicelist) > 1
and not self.args.jlink
and self.args.operation != "list"
):
logging.warning("JLink interface must be selected using -j option")
logging.warning(
"Multiple interfaces detected, the lowest serial number is selected"
)
self.display_jlink_devices()
self.display_jlink_devices()

logging.warning(
"Selecting interface {}".format(self.devicelist[0].SerialNumber)
)
self.args.jlink = self.devicelist[0].SerialNumber
logging.warning(
"Selecting interface {}".format(self.devicelist[0].SerialNumber)
)
self.args.jlink = self.devicelist[0].SerialNumber

# list the jlink interfaces
if self.args.operation == "list":
self.display_jlink_devices()
if self.link.iphost is None:
self.display_jlink_devices()
else:
logging.info("Do not use command list with --host option")

elif self.args.operation == "probe":
self.probeDevice()
Expand Down Expand Up @@ -405,6 +410,8 @@ def importAndAssignDevice(self, device):
"""
assert sbdev # appease Flake8
self.da = eval("sbdev.{}".format(device))()
if self.link.iphost:
self.da.link.iphost = self.link.iphost

def go(self):
"""Reset the device and run."""
Expand Down Expand Up @@ -488,6 +495,12 @@ def argument_parser(self):
default=os.environ.get("SMARTBOND_JLINK_ID", None),
)

self.parser.add_argument(
"--host",
help="Jlink device TCP/IP host",
default=os.environ.get("SMARTBOND_JLINK_HOST", None),
)

self.parser.add_argument(
"-v", "--verbose", help="increase verbosity", action="store_true"
)
Expand Down
40 changes: 23 additions & 17 deletions ezFlashCLI/ezFlash/pyjlink.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,25 +253,31 @@ def browse(self):

def connect(self, serialno):
"""Initialize the connection to the target system."""
if type(serialno) != type(int):
try:
serialno = int(serialno)
except Exception as ex:
self.logger.debug(
"Failed to interpret JLink id: {}, will use default interface\nErr: {}".format(
serialno, ex
if self.iphost is not None:
addr, port = self.iphost.rsplit(":", 1)
self.jl.JLINKARM_SelectIP(addr.encode(), int(port))
else:
if type(serialno) != type(int):
try:
serialno = int(serialno)
except Exception as ex:
self.logger.debug(
"Failed to interpret JLink id: {}, will use default interface\nErr: {}".format(
serialno, ex
)
)
)
# return
# return

if serialno:
self.logger.debug(
"Selecting J-Link with the serial number: " + str(serialno)
)
c_serialno = ctypes.c_uint32(serialno)
r = self.jl.JLINKARM_EMU_SelectByUSBSN(c_serialno)
if r < 0:
raise pyJLinkException("Error: Specific serial number not found on USB")
if serialno:
self.logger.debug(
"Selecting J-Link with the serial number: " + str(serialno)
)
c_serialno = ctypes.c_uint32(serialno)
r = self.jl.JLINKARM_EMU_SelectByUSBSN(c_serialno)
if r < 0:
raise pyJLinkException(
"Error: Specific serial number not found on USB"
)

self.logger.debug("Opens the connection to J-Link")
self.jl.JLINKARM_Open.restype = ctypes.c_char_p
Expand Down

0 comments on commit 888a08d

Please sign in to comment.