Skip to content

Commit

Permalink
[scons] Add ITM convenience wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
salkinium committed May 23, 2019
1 parent f73e606 commit b3728d9
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
1 change: 1 addition & 0 deletions tools/build_script_generator/scons/module.lb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ def build(env):
"gdb",
"build_target",
"artifact",
"log_itm",
}
if has_xpcc_generator:
tools.add("xpcc_generator")
Expand Down
10 changes: 10 additions & 0 deletions tools/build_script_generator/scons/module.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,16 @@ argument.
See the `:platform:fault` module for details how to receive the coredump data.


#### scons itm

Configures OpenOCD in tracing mode to output ITM channel 0 on SWO pin and
displays the serial output stream.
(\* *only Cortex-M targets*)

See the `:platform:itm` module for details how to use the ITM as a logging
output.


#### scons symbols

Dumps the symbol table for your executable.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def build_target(env, sources):
env.Alias("program", env.OpenOcd(program, commands=["modm_program $SOURCE"]))
env.Alias("bmp", env.BlackMagicProbe(program))
%% endif
env.Alias("itm", env.OpenOcdItm())
env.Alias("gdb", env.OpenOcdGdb(program))
env.Alias("postmortem", env.PostMortemGdb(program))
%% elif core.startswith("avr")
Expand Down
65 changes: 65 additions & 0 deletions tools/build_script_generator/scons/site_tools/log_itm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
#
# Copyright (c) 2019, Niklas Hauser
#
# This file is part of the modm project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# -----------------------------------------------------------------------------

import os
from SCons.Script import *
import subprocess
import tempfile
import signal

# -----------------------------------------------------------------------------
def run_openocd_itm(target, source, env):
# Provide additional search paths via the OPENOCD_SCRIPTS environment variable
# See http://openocd.org/doc/html/Running.html
env["ENV"]["OPENOCD_SCRIPTS"] = os.environ.get("OPENOCD_SCRIPTS", "")
search = env.get("MODM_OPENOCD_SEARCHDIRS", [])
config = env.get("MODM_OPENOCD_CONFIGFILES", [])
openocdcmd = '{} {}'.format(
" ".join(map('-s "{}"'.format, map(env.subst, search))),
" ".join(map('-f "{}"'.format, map(env.subst, config))))

fcpu = ARGUMENTS.get("fcpu", None)
if fcpu is None:
print("\n You must provide the current CPU/HCLK frequency as `fcpu={Hz}` argument!\n")
return -1
baudrate = ARGUMENTS.get("baudrate", "")

with tempfile.NamedTemporaryFile() as tmpfile:
itm_config = "modm_itm_log {} {} {}".format(tmpfile.name, fcpu, baudrate)
openocdcmd += ' -c "{}"'.format(itm_config)
openocdcmd = '-c "log_output /dev/null"' + openocdcmd

# Start OpenOCD in the background
openocd = subprocess.Popen('openocd ' + openocdcmd, cwd=os.getcwd(), shell=True)
# Start a blocking call to monitor the log file
subprocess.call("tail -f {}".format(tmpfile.name), cwd=os.getcwd(), shell=True)
# kill OpenOCD process when Ctrl-C happened
os.killpg(os.getpgid(openocd.pid), signal.SIGTERM)

return 0

def openocd_itm(env, alias="openocd_itm"):
action = Action(run_openocd_itm, cmdstr="$OPENOCDITMSTR")
return env.AlwaysBuild(env.Alias(alias, None, action))

# -----------------------------------------------------------------------------
def generate(env, **kw):
env["OPENOCD"] = "openocd"
if ARGUMENTS.get('verbose') != '1':
env["OPENOCDITMSTR"] = ("{0}.----OpenOCD--> {1}Single Wire Viewer\n"
"{0}'------SWO----- {2}$CONFIG_DEVICE_NAME{3}"
.format("\033[;0;32m", "\033[;0;33m", "\033[;1;33m", "\033[;0;0m"))

env.AddMethod(openocd_itm, "OpenOcdItm")

def exists(env):
return env.Detect("openocd") and env.Detect("tail")

0 comments on commit b3728d9

Please sign in to comment.