Skip to content

Commit 1725e21

Browse files
Add semihosting support (SerialSemi and SemiFS) (#2670)
Enable ARM-only semihosting mode. This mode allows applications on the Pico to write to the OpenOCD console and read and write files on the host system (i.e. debugging dump information, etc.) It is not very fast because of the way it uses breakpoints on the Pico to communicate, but it is useful in cases when you want to get a single file off of the Pico while debugging. Note that this **requires** a connected OpenOCD and GDB or else the semihosting will cause a system panic.
1 parent f2d30ab commit 1725e21

File tree

10 files changed

+600
-1
lines changed

10 files changed

+600
-1
lines changed

docs/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ For the latest version, always check https://github.com/earlephilhower/arduino-p
4343
File Systems (SD, SDFS, LittleFS) <fs>
4444
USB (Arduino and Adafruit_TinyUSB) <usb>
4545
Multicore Processing <multicore>
46+
Semihosting <semihosting>
4647

4748
RP2350 Specific Notes <rp2350>
4849
RP2350 PSRAM <psram>

docs/semihosting.rst

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
Semihosting Support
2+
===================
3+
4+
Using special debugger breakpoints and commands, the Pico can read and write to the debugging console as well
5+
as read and write files on a development PC. The ``Semihosting`` library allows applications to use the
6+
semihosting support as a normal filesystem or serial port.
7+
8+
**NOTE** Semihosting only works when connected to an OpenOCD + GDB debug session. Running an application
9+
compiled for Semihosting without the debugger will cause a panic and hang the chip.
10+
11+
As of now, only ARM has support for Semihosting.
12+
13+
Running Semihosting on the Development Host
14+
-------------------------------------------
15+
16+
Start OpenOCD normally from inside a directory that you can read and write files within (i.e. do not run from
17+
``C:\\Program Files\\..`` on Windows where general users aren't allowed to write). The starting
18+
directory will be where the Pico will read and write files using the ``SemiFS`` class.
19+
Be sure to keep the terminal window you ran OpenOCD in open, because all ``SerialSemi`` input and output
20+
will go to **that** terminal and not ``gdb``'s.
21+
22+
Start GDB normally and connect to the OpenOCD debugger and enable semihosting support
23+
24+
.. code::
25+
26+
(gdb) target extended-remote localhost:3333
27+
(gdb) monitor arm semihosting enable
28+
29+
At this point load and run your ``ELF`` application as normal. Again, all ``SerialSemi`` output will go
30+
to the **OpenOCD** window, not GDB.
31+
32+
See the ``hellosemi`` example in the ``Semihosting`` library.
33+
34+
SerialSemi - Serial over Semihosting
35+
------------------------------------
36+
37+
Simply include ``<Semihosting.h>`` in your application and use ``SerialSemi`` as you would any other
38+
``Serial`` port with the following limitations:
39+
40+
* Baud rate, bit width, etc. are all ignored
41+
* Input is limited because ``read`` may hang indefinitely in the host and ``available`` is not part of the spec
42+
43+
SemiFS - Host filesystem access through Semihosting
44+
---------------------------------------------------
45+
46+
Use ``SemiFS`` the same way as any other file system. Note that only file creation and renaming are supported, with
47+
no provision for iterating over directories or listing files. In most cases simply opening a ``File`` and writing out
48+
a debug dump is all that's needed:
49+
50+
.. code::
51+
52+
SemiFS.begin();
53+
File f = SemiFS.open("debug.dmp", "w");
54+
f.write(buffer, size);
55+
f.close();
56+
SerialSemi.printf("Debug dump nopw available on host.\n");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// This example uses semihosting to send serial output to the OpenOcD screen
2+
// and write binary files to the host system.
3+
//
4+
// Semihosting **ONLY** works with an OpenOCD and GDB setup. If you build
5+
// and run a semihosting app without GDB connected, it **WILL CRASH**
6+
//
7+
// Start OpenOCD normally, but leave the terminal window visible because
8+
// is it OpenOCD, not GDB, which will display the semihosting output.
9+
// OpenOCD will also create files in the current working directory, so
10+
// be sure it is a place you can find and write to.
11+
//
12+
// In GDB,connect to OpenOCD and then enable semihosting
13+
// (gdb) target extended-remote localhost:3333
14+
// (gdb) monitor arm semihosting enable
15+
16+
#ifdef __riscv
17+
void setup() {
18+
// No semihosting for RISCV yet
19+
}
20+
void loop() {
21+
}
22+
#else
23+
24+
#include <Semihosting.h>
25+
26+
int c = 0;
27+
28+
void setup() {
29+
SerialSemi.begin();
30+
SerialSemi.printf("HELLO, GDB!\n");
31+
SemiFS.begin();
32+
File f = SemiFS.open("out.bin", "w");
33+
f.printf("I made a file!\n");
34+
f.close();
35+
SerialSemi.printf("Just wrote a file 'out.bin'\n");
36+
}
37+
38+
void loop() {
39+
SerialSemi.printf("SH Loop Count: %d\n", c++);
40+
Serial.printf("USB Loop Count: %d\n", c++);
41+
delay(1000);
42+
}
43+
#endif

libraries/Semihosting/keywords.txt

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#######################################
2+
# Syntax Coloring Map
3+
#######################################
4+
5+
#######################################
6+
# Datatypes (KEYWORD1)
7+
#######################################
8+
9+
SerialSemi KEYWORD1
10+
SemiFS KEYWORD1
11+
12+
#######################################
13+
# Methods and Functions (KEYWORD2)
14+
#######################################
15+
16+
#######################################
17+
# Constants (LITERAL1)
18+
#######################################
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name=Semihosting
2+
version=1.0.0
3+
author=Earle F. Philhower, III <earlephilhower@yahoo.com>
4+
maintainer=Earle F. Philhower, III <earlephilhower@yahoo.com>
5+
sentence=Semihosted serial and filesystem access for the Pico and OpenOCD
6+
paragraph=Semihosted serial and filesystem access for the Pico and OpenOCD
7+
category=Communications
8+
url=https://github.com/earlephilhower/arduino-pico
9+
architectures=rp2040
10+
dot_a_linkage=true

0 commit comments

Comments
 (0)