Skip to content
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

makefiles: Add openocd-rtt #18526

Merged
merged 1 commit into from
Aug 30, 2022
Merged

Conversation

chrysn
Copy link
Member

@chrysn chrysn commented Aug 29, 2022

Contribution description

The RTT mechanism, by which a debugger emulates a serial interface, has been invented and described by Segger (creator of JLink), but was so far only available through their own JLink program. As the same interface is also implemented by OpenOCD, it can be used with that and thus any debug adapter (eg. STLink).

Open issues

  • Do we need any parametrization?
    • Channel 0 is hard-coded which is probably OK
    • No clue where the addresses come from.
    • The string "SEGGER JLINK" is the sentinel value also hardcoded in stdio_rtt.c, so that should be OK.
  • Do we need better autoselection? (See future steps)

Testing procedure

  • Pick any board that has an OpenOCD-style debugger and usually runs serial stdio; I picked microbit-v2, and an example that has nice shell (eg. saul)
  • Add lines to the Makefile:
    USEMODULE += stdio_rtt
    RIOT_TERMINAL = openocd-rtt
    
  • make -C examples/saul BOARD=microbit-v2 all flash term

Issues/PRs references

This is based on the existing code in jlink.sh, and notes from @nica-f.

Future steps

  • There's a lot of code duplication both internally to openocd.sh and between it and jlink.sh.
  • The process of picking a terminal happens inside these scripts, so they have their own JLINK_TERMPROG rather than having composition from the terminal selection.

Both of these are probably doable in shell and Makefile, but IMO too complex to be done there well and maintainably -- so some duplication it is for now, as it has been for some time.

@github-actions github-actions bot added Area: build system Area: Build system Area: tools Area: Supplementary tools labels Aug 29, 2022
@chrysn chrysn added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Aug 29, 2022
@benpicco
Copy link
Contributor

benpicco commented Aug 29, 2022

Nice, works well even with a particle debugger and just SWDIO/CLK!
(After bumping STDIO_TX_BUFSIZE, otherwise large output gets cut off.

I agree that

--- a/makefiles/tools/serial.inc.mk
+++ b/makefiles/tools/serial.inc.mk
@@ -19,6 +19,10 @@ PROG_DEV ?= $(PORT)
 
 export BAUD ?= 115200
 
+ifneq (,$(filter stdio_rtt,$(USEMODULE)))
+  RIOT_TERMINAL ?= openocd-rtt
+endif
+
 RIOT_TERMINAL ?= pyterm
 ifeq ($(RIOT_TERMINAL),pyterm)
   TERMPROG  ?= $(RIOTTOOLS)/pyterm/pyterm

would be more ergonomic - I had no idea about openocd-rtt.

@chrysn
Copy link
Member Author

chrysn commented Aug 29, 2022

Once we go for the more ergonomic default sizes, I'd also introspect the PROGRAMMER -- if it's jlink, RIOT_TERMINAL should become jlink-rtt instead. (I'd start from the diff you showed in a next iteration after this is through).

In the meantime I've found out the numbers: They're a start and length of memory to search for the magic string (provided in the argument) in. I'm a bit conflicted about how to best fill them in: My default mode is pedantic, for which I'd pick the rtt_cb symbol out of the ELF file. But that creates dependencies we don't currently have, and easily breaks if the flashed binary doesn't exactly match what was last built. So let's do as JLink does (apparently -- it isn't given the ELF file or data from it) and use autodiscovery, which AFAICT relies on the string "SEGGER RTT" being present only at one location in memory. As JLink presumably knows something about the RAM setup that OpenOCD doesn't (or doesn't evaluate automatically), picking RAM_START_ADDR and RAM_LEN from the build system (these shouldn't drastically change between builds). This is reflected in the second commit added now, answering the open questions I've had.

@maribu
Copy link
Member

maribu commented Aug 30, 2022

I love this one. Other than the ergonomic defaults, this looks ready for merge, right?

@chrysn
Copy link
Member Author

chrysn commented Aug 30, 2022

I think yes -- I didn't merge it because the second commit (which addresses the hardcoded numbers that were there before) wasn't part of the original ACK, and touches a few more places.

If you're happy with that, please bring it ahead :-)

Copy link
Member

@maribu maribu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK. Looks good to me and has already been tested. IMO you could squash the two commits. If you don't feel like squashing, you may hit merge instead.

JLink presumably has information about the device's RAM available
internally. Not passing the precise symbol area (which would be
available in the ELF file) because a) that'd make the terminal break
when the flashed firmware does not equal the built one, and b) that
would introduce a dependency from `term` to the ELF file that other
terminals don't have.
@chrysn
Copy link
Member Author

chrysn commented Aug 30, 2022

Squashed without code changes after a successful run, setting the "skip build" flag.

@chrysn chrysn added the CI: skip compile test If set, CI server will run only non-compile jobs, but no compile jobs or their dependent jobs label Aug 30, 2022
@chrysn chrysn enabled auto-merge August 30, 2022 07:37
@chrysn chrysn merged commit 2c9e853 into RIOT-OS:master Aug 30, 2022
@chrysn chrysn deleted the openocd-rtt branch August 30, 2022 08:43
@maribu maribu added this to the Release 2022.10 milestone Oct 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: build system Area: Build system Area: tools Area: Supplementary tools CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR CI: skip compile test If set, CI server will run only non-compile jobs, but no compile jobs or their dependent jobs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants