Skip to content

Commit

Permalink
intf: add support for RTT interface
Browse files Browse the repository at this point in the history
  • Loading branch information
raiden00pl committed Sep 30, 2023
1 parent 83a2ee8 commit 59a3691
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 0 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ requires-python = ">=3.10"
dependencies = [
"pyserial>=3.5",
"crcmod>=1.7",
"pylink-square>=1.2",
]
classifiers = [
"Development Status :: 4 - Beta",
Expand Down
116 changes: 116 additions & 0 deletions src/nxslib/intf/rtt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
"""Module containing the NxScope RTT interface implementation."""

import time

import pylink # type: ignore

from nxslib.intf.iintf import ICommInterface
from nxslib.logger import logger

###############################################################################
# Class: RTTDevice
###############################################################################


class RTTDevice(ICommInterface): # pragma: no cover
"""A class used to represent a serial port interface."""

def __init__(
self,
target_device: str,
buffer_index: int,
upsize: int,
interface: str,
blockaddr: str = "auto",
) -> None:
"""Intitialize a serial interface.
:param : target_device: target chip name
:param : buffer_index: nxscope RTT buffer index
:param : upsize: nxscope RTT buffer size
:param : interface: JLink interface: swd or jtag
:param : blockaddr: RTT block address as hex or `auto`
"""
# ger RTT block address
if blockaddr != "auto":
block_address = int(blockaddr, 16)
print("RTT block address = ", hex(block_address))
else:
block_address = None
print("Auto-search for RTT block address")

# get JLink interface
if interface in ["swd", "SWD"]:
jlinkinterface = pylink.enums.JLinkInterfaces.SWD
print("JLink interface is SWD")
elif interface in ["jtag", "JTAG"]:
jlinkinterface = pylink.enums.JLinkInterfaces.JTAG
print("JLink interface is JTAG")
else:
raise ValueError

self.buffer_index = buffer_index
self.upsize = upsize

# connect to JLink
while True:
try:
print("connecting to", target_device, "...")
self._jlink = pylink.JLink()
self._jlink.open()
self._jlink.set_tif(jlinkinterface)
self._jlink.connect(target_device)
print("connected, starting RTT...")
self._jlink.rtt_start(block_address)
break
except pylink.errors.JLinkException:
time.sleep(0.1)

# wait for RTT (revisit: do we need that ?)
while True:
try:
num_up = self._jlink.rtt_get_num_up_buffers()
num_down = self._jlink.rtt_get_num_down_buffers()
print(
"RTT started, %d up bufs, %d down bufs."
% (num_up, num_down)
)
break
except pylink.errors.JLinkRTTException:
time.sleep(0.1)

super().__init__()

def __del__(self) -> None:
"""Make sure that serial port is closed."""

def start(self) -> None:
"""Start the interface."""
logger.debug("start RTT interface")

def stop(self) -> None:
"""Stop the interface."""
logger.debug("Stop RTT interface")

def drop_all(self) -> None:
"""Drop all frames."""
for _ in range(10):
self._read()

def _read(self) -> bytes:
"""Interface specific read method."""
assert self._jlink
try:
_bytes = self._jlink.rtt_read(self.buffer_index, self.upsize)
return bytes(_bytes)
except Exception as exc:
logger.debug("pylink exception ignored: %s", str(exc))
return b""

def _write(self, data: bytes) -> None:
"""Interface specific write method.
:param data: bytes to send
"""
assert self._jlink
self._jlink.rtt_write(self.buffer_index, data)
8 changes: 8 additions & 0 deletions tests/intf/test_rtt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import pytest # type: ignore

from nxslib.intf.rtt import RTTDevice


def test_nxslibserial_init():
with pytest.raises(ValueError):
_ = RTTDevice("test", 1, 100, "x")

0 comments on commit 59a3691

Please sign in to comment.