2019-06-03 15:31:17 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
#
|
2024-09-10 14:15:58 +02:00
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
#
|
2021-09-28 15:49:46 +02:00
|
|
|
# Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
# contributor license agreements. See the NOTICE file distributed with
|
|
|
|
# this work for additional information regarding copyright ownership. The
|
|
|
|
# ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
# "License"); you may not use this file except in compliance with the
|
|
|
|
# License. You may obtain a copy of the License at
|
2019-06-03 15:31:17 +02:00
|
|
|
#
|
2021-09-28 15:49:46 +02:00
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
2019-06-03 15:31:17 +02:00
|
|
|
#
|
2021-09-28 15:49:46 +02:00
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
# License for the specific language governing permissions and limitations
|
|
|
|
# under the License.
|
2019-06-03 15:31:17 +02:00
|
|
|
#
|
|
|
|
#
|
2021-01-15 21:34:52 +01:00
|
|
|
# Usage
|
|
|
|
# =====
|
2019-06-03 15:31:17 +02:00
|
|
|
#
|
|
|
|
# No special python modules are needed, it should be possible to run the
|
|
|
|
# application simply as shown below;
|
|
|
|
#
|
|
|
|
# ------------------------------------------
|
|
|
|
# $ ./ocdconsole.py
|
|
|
|
# ==Link Activated
|
|
|
|
#
|
2019-06-18 15:01:23 +02:00
|
|
|
# nsh>
|
2019-06-03 15:31:17 +02:00
|
|
|
# nsh> help
|
|
|
|
# help usage: help [-v] [<cmd>]
|
|
|
|
#
|
2019-06-18 15:01:23 +02:00
|
|
|
# ? echo exit hexdump ls mh sleep xd
|
|
|
|
# cat exec help kill mb mw usleep
|
|
|
|
# nsh>
|
2019-06-03 15:31:17 +02:00
|
|
|
# ------------------------------------------
|
|
|
|
#
|
|
|
|
# This code is designed to be 'hardy' and will survive a shutdown and
|
|
|
|
# restart of the openocd process. When your target application
|
|
|
|
# changes then the location of the upword and downword may change,
|
|
|
|
# so they are re-searched for again. To speed up the start process
|
|
|
|
# consider putting those words at fixed locations (e.g. via the
|
|
|
|
# linker file) and referencing them directly.
|
|
|
|
#
|
|
|
|
|
2021-04-09 18:35:47 +02:00
|
|
|
import os
|
|
|
|
import socket
|
|
|
|
import time
|
|
|
|
|
|
|
|
if os.name == "nt":
|
|
|
|
import msvcrt
|
|
|
|
else:
|
|
|
|
import select
|
|
|
|
import sys
|
|
|
|
import termios
|
|
|
|
import tty
|
|
|
|
|
2019-06-03 15:31:17 +02:00
|
|
|
LWL_ACTIVESHIFT = 31
|
|
|
|
LWL_DNSENSESHIFT = 30
|
|
|
|
LWL_UPSENSESHIFT = 29
|
|
|
|
LWL_OCTVALSHIFT = 27
|
|
|
|
LWL_PORTSHIFT = 24
|
|
|
|
|
2021-04-05 02:10:52 +02:00
|
|
|
LWL_PORTMASK = 7 << LWL_PORTSHIFT
|
|
|
|
LWL_SENSEMASK = 3 << LWL_UPSENSESHIFT
|
|
|
|
LWL_OCTVALMASK = 3 << LWL_OCTVALSHIFT
|
2019-06-03 15:31:17 +02:00
|
|
|
|
2021-04-05 02:10:52 +02:00
|
|
|
LWL_ACTIVE = 1 << LWL_ACTIVESHIFT
|
|
|
|
LWL_DNSENSEBIT = 1 << LWL_DNSENSESHIFT
|
|
|
|
LWL_UPSENSEBIT = 1 << LWL_UPSENSESHIFT
|
2019-06-03 15:31:17 +02:00
|
|
|
|
|
|
|
LWL_SIG = 0x7216A318
|
|
|
|
|
|
|
|
LWL_PORT_CONSOLE = 1
|
|
|
|
|
|
|
|
# Memory to scan through looking for signature
|
|
|
|
baseaddr = 0x20000000
|
|
|
|
length = 0x8000
|
|
|
|
|
2021-04-05 02:10:52 +02:00
|
|
|
|
2019-06-03 15:31:17 +02:00
|
|
|
def kbhit():
|
2021-04-05 02:10:52 +02:00
|
|
|
"""Returns True if a keypress is waiting to be read in stdin, False otherwise."""
|
|
|
|
if os.name == "nt":
|
2019-06-03 15:31:17 +02:00
|
|
|
return msvcrt.kbhit()
|
|
|
|
else:
|
2021-04-05 02:10:52 +02:00
|
|
|
dr, dw, de = select.select([sys.stdin], [], [], 0)
|
2019-06-03 15:31:17 +02:00
|
|
|
return dr != []
|
|
|
|
|
2021-04-05 02:10:52 +02:00
|
|
|
|
2019-06-03 15:31:17 +02:00
|
|
|
def dooutput(x):
|
2021-04-05 02:10:52 +02:00
|
|
|
if x & 255 == 10:
|
|
|
|
print("\r", flush=True)
|
2019-06-03 15:31:17 +02:00
|
|
|
else:
|
2021-04-05 02:10:52 +02:00
|
|
|
print(chr(x), end="", flush=True)
|
|
|
|
|
2019-06-03 15:31:17 +02:00
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# Code from here to *** below was taken from GPL'ed ocd_rpc_example.py and is
|
|
|
|
# available in its original form at contrib/rpc_examples in the openocd tree.
|
|
|
|
#
|
|
|
|
# This code was approved for re-release under BSD (licence at the head of this
|
|
|
|
# file) by the original author (Andreas Ortmann, ortmann@finf.uni-hannover.de)
|
2019-06-18 15:01:23 +02:00
|
|
|
# via email to Dave Marples on 3rd June 2019.
|
2019-06-03 15:31:17 +02:00
|
|
|
# email ID: 15e1f0a0-9592-bd07-c996-697f44860877@finf.uni-hannover.de
|
|
|
|
###############################################################################
|
|
|
|
|
2021-04-05 02:10:52 +02:00
|
|
|
|
2019-06-03 15:31:17 +02:00
|
|
|
def strToHex(data):
|
|
|
|
return map(strToHex, data) if isinstance(data, list) else int(data, 16)
|
|
|
|
|
2021-04-05 02:10:52 +02:00
|
|
|
|
2019-06-03 15:31:17 +02:00
|
|
|
class oocd:
|
2021-04-05 02:10:52 +02:00
|
|
|
NL = "\x1A"
|
|
|
|
|
2019-06-03 15:31:17 +02:00
|
|
|
def __init__(self, verbose=False):
|
|
|
|
self.verbose = verbose
|
2021-04-05 02:10:52 +02:00
|
|
|
self.tclRpcIp = "127.0.0.1"
|
|
|
|
self.tclRpcPort = 6666
|
|
|
|
self.bufferSize = 4096
|
2019-06-03 15:31:17 +02:00
|
|
|
|
|
|
|
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
self.sock.connect((self.tclRpcIp, self.tclRpcPort))
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __exit__(self, type, value, traceback):
|
|
|
|
try:
|
|
|
|
self.send("exit")
|
|
|
|
finally:
|
|
|
|
self.sock.close()
|
|
|
|
|
|
|
|
def send(self, cmd):
|
|
|
|
"""Send a command string to TCL RPC. Return the result that was read."""
|
|
|
|
data = (cmd + oocd.NL).encode("utf-8")
|
|
|
|
if self.verbose:
|
|
|
|
print("<- ", data)
|
|
|
|
|
|
|
|
self.sock.send(data)
|
|
|
|
return self._recv()
|
|
|
|
|
|
|
|
def _recv(self):
|
|
|
|
"""Read from the stream until the NL was received."""
|
|
|
|
data = bytes()
|
|
|
|
while True:
|
|
|
|
chunk = self.sock.recv(self.bufferSize)
|
|
|
|
data += chunk
|
|
|
|
if bytes(oocd.NL, encoding="utf-8") in chunk:
|
|
|
|
break
|
|
|
|
|
|
|
|
data = data.decode("utf-8").strip()
|
2021-04-05 02:10:52 +02:00
|
|
|
data = data[:-1] # strip trailing NL
|
2019-06-03 15:31:17 +02:00
|
|
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
def readVariable(self, address):
|
2021-04-05 02:10:52 +02:00
|
|
|
raw = self.send("%s 0x%x" % (self.mdwText, address)).split(": ")
|
2019-06-03 15:31:17 +02:00
|
|
|
return None if (len(raw) < 2) else strToHex(raw[1])
|
|
|
|
|
|
|
|
def writeVariable(self, address, value):
|
|
|
|
assert value is not None
|
|
|
|
self.send("mww 0x%x 0x%x" % (address, value))
|
2019-06-18 15:01:23 +02:00
|
|
|
|
|
|
|
def testInterface(self):
|
2021-04-05 02:10:52 +02:00
|
|
|
self.mdwText = "ocd_mdw"
|
2021-04-09 18:35:47 +02:00
|
|
|
if self.readVariable(baseaddr) is not None:
|
2019-06-18 15:01:23 +02:00
|
|
|
return
|
2021-04-05 02:10:52 +02:00
|
|
|
self.mdwText = "mdw"
|
2021-04-09 18:35:47 +02:00
|
|
|
if self.readVariable(baseaddr) is not None:
|
2019-06-18 15:01:23 +02:00
|
|
|
return
|
|
|
|
raise ConnectionRefusedError
|
|
|
|
|
2021-04-05 02:10:52 +02:00
|
|
|
|
2019-06-03 15:31:17 +02:00
|
|
|
# *** Incorporated code ends ######################################################
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
|
|
|
def show(*args):
|
|
|
|
print(*args, end="\n\n")
|
|
|
|
|
|
|
|
fd = sys.stdin.fileno()
|
2019-06-18 15:01:23 +02:00
|
|
|
old_settings = termios.tcgetattr(fd)
|
2019-06-03 15:31:17 +02:00
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
tty.setraw(fd)
|
|
|
|
with oocd() as ocd:
|
|
|
|
while True:
|
|
|
|
# Find the location for the communication variables
|
|
|
|
# =================================================
|
|
|
|
try:
|
2019-06-18 15:01:23 +02:00
|
|
|
ocd.testInterface()
|
2021-04-05 02:10:52 +02:00
|
|
|
downwordaddr = 0
|
|
|
|
while downwordaddr < length:
|
|
|
|
if ocd.readVariable(baseaddr + downwordaddr) == LWL_SIG:
|
2020-12-10 04:43:57 +01:00
|
|
|
break
|
2021-04-05 02:10:52 +02:00
|
|
|
downwordaddr = downwordaddr + 4
|
2019-06-03 15:31:17 +02:00
|
|
|
|
2021-04-05 02:10:52 +02:00
|
|
|
if downwordaddr >= length:
|
2019-06-03 15:31:17 +02:00
|
|
|
print("ERROR: Cannot find signature\r")
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
# We have the base address, so get the variables themselves
|
|
|
|
# =========================================================
|
2021-04-05 02:10:52 +02:00
|
|
|
downwordaddr = baseaddr + downwordaddr + 4
|
|
|
|
upwordaddr = downwordaddr + 4
|
|
|
|
downword = LWL_ACTIVE
|
2019-06-03 15:31:17 +02:00
|
|
|
|
|
|
|
# Now wake up the link...keep on trying if it goes down
|
|
|
|
# =====================================================
|
|
|
|
while True:
|
|
|
|
ocd.writeVariable(downwordaddr, downword)
|
|
|
|
upword = ocd.readVariable(upwordaddr)
|
2021-04-05 02:10:52 +02:00
|
|
|
if upword & LWL_ACTIVE != 0:
|
2019-06-03 15:31:17 +02:00
|
|
|
print("==Link Activated\r")
|
|
|
|
break
|
2021-04-05 02:10:52 +02:00
|
|
|
except (
|
|
|
|
BrokenPipeError,
|
|
|
|
ConnectionRefusedError,
|
|
|
|
ConnectionResetError,
|
|
|
|
) as e:
|
2019-06-03 15:31:17 +02:00
|
|
|
raise e
|
2019-06-18 15:01:23 +02:00
|
|
|
|
2019-06-03 15:31:17 +02:00
|
|
|
# Now run the comms loop until something fails
|
|
|
|
# ============================================
|
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
ocd.writeVariable(downwordaddr, downword)
|
|
|
|
upword = ocd.readVariable(upwordaddr)
|
2021-04-05 02:10:52 +02:00
|
|
|
if upword & LWL_ACTIVE == 0:
|
2019-06-03 15:31:17 +02:00
|
|
|
print("\r==Link Deactivated\r")
|
|
|
|
break
|
|
|
|
if kbhit():
|
|
|
|
charin = sys.stdin.read(1)
|
2021-04-05 02:10:52 +02:00
|
|
|
if ord(charin) == 3:
|
2019-06-03 15:31:17 +02:00
|
|
|
sys.exit(0)
|
2021-04-05 02:10:52 +02:00
|
|
|
if downword & LWL_DNSENSEBIT:
|
|
|
|
downword = downword & LWL_UPSENSEBIT
|
2019-06-03 15:31:17 +02:00
|
|
|
else:
|
2021-04-05 02:10:52 +02:00
|
|
|
downword = (
|
|
|
|
downword & LWL_UPSENSEBIT
|
|
|
|
) | LWL_DNSENSEBIT
|
|
|
|
downword |= (
|
|
|
|
(LWL_PORT_CONSOLE << LWL_PORTSHIFT)
|
|
|
|
| (1 << LWL_OCTVALSHIFT)
|
|
|
|
| LWL_ACTIVE
|
|
|
|
| ord(charin)
|
|
|
|
)
|
|
|
|
|
|
|
|
if (upword & LWL_UPSENSEBIT) != (downword & LWL_UPSENSEBIT):
|
|
|
|
incomingPort = (upword & LWL_PORTMASK) >> LWL_PORTSHIFT
|
|
|
|
if incomingPort == LWL_PORT_CONSOLE:
|
|
|
|
incomingBytes = (
|
|
|
|
upword & LWL_OCTVALMASK
|
|
|
|
) >> LWL_OCTVALSHIFT
|
|
|
|
if incomingBytes >= 1:
|
|
|
|
dooutput(upword & 255)
|
|
|
|
if incomingBytes >= 2:
|
|
|
|
dooutput((upword >> 8) & 255)
|
|
|
|
if incomingBytes == 3:
|
|
|
|
dooutput((upword >> 16) & 255)
|
|
|
|
|
|
|
|
if downword & LWL_UPSENSEBIT:
|
|
|
|
downword = downword & ~LWL_UPSENSEBIT
|
2019-06-03 15:31:17 +02:00
|
|
|
else:
|
2021-04-05 02:10:52 +02:00
|
|
|
downword = downword | LWL_UPSENSEBIT
|
|
|
|
except (
|
|
|
|
ConnectionResetError,
|
|
|
|
ConnectionResetError,
|
|
|
|
BrokenPipeError,
|
|
|
|
) as e:
|
2019-06-03 15:31:17 +02:00
|
|
|
print("\r==Link Lost\r")
|
|
|
|
raise e
|
2019-06-18 15:01:23 +02:00
|
|
|
|
2021-04-09 18:35:47 +02:00
|
|
|
except (BrokenPipeError, ConnectionRefusedError, ConnectionResetError):
|
2019-06-03 15:31:17 +02:00
|
|
|
time.sleep(1)
|
|
|
|
continue
|
|
|
|
finally:
|
|
|
|
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
|