Skip to content

Commit 5e9eb27

Browse files
authored
Implement whole-buffer write semihosting (earlephilhower#1013)
* Implement whole-buffer write semihosting Each semihosting call is very slow, so doing it for each character does not provide an ideal developer experience. This change allows the entire buffer to be printed out in a single call.
1 parent a4ec3c4 commit 5e9eb27

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

src/rp2_common/pico_stdio_semihosting/stdio_semihosting.c

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,33 @@
88
#include "pico/stdio_semihosting.h"
99
#include "pico/binary_info.h"
1010

11-
//static void __attribute__((naked)) semihosting_puts(const char *s) {
12-
// __asm (
13-
//
14-
// "mov r1, r0\n"
15-
// "mov r0, #4\n"
16-
// "bkpt 0xab\n"
17-
// "bx lr\n"
18-
// );
19-
//}
11+
static void stdio_semihosting_out_chars(const char *buf, int length) {
12+
// must be volatile or the buffer gets put in registers & optimized away
13+
volatile struct {
14+
// https://developer.arm.com/documentation/dui0375/g/What-is-Semihosting-/SYS-WRITE--0x05-
15+
// arguments, in order:
16+
// word 0 = file handle (1 = stdout)
17+
// word 1 = pointer to buffer
18+
// word 2 = length of buffer
19+
size_t fd;
20+
const char *buf;
21+
size_t len;
22+
} args;
23+
24+
args.fd = 1; // 1 = stdout
25+
args.buf = buf;
26+
args.len = length;
2027

21-
static void __attribute__((naked)) semihosting_putc(__unused const char *c) {
2228
__asm (
23-
24-
"mov r1, r0\n"
25-
"mov r0, #3\n"
29+
// r1 must contain a pointer to the arguments
30+
"mov r1, %[args]\n"
31+
// semihosting call number 0x05 = SYS_WRITE
32+
"mov r0, #5\n"
33+
// make the semihosting call: https://developer.arm.com/documentation/dui0375/g/What-is-Semihosting-/The-semihosting-interface
2634
"bkpt 0xab\n"
27-
"bx lr\n"
28-
);
29-
}
30-
31-
32-
static void stdio_semihosting_out_chars(const char *buf, int length) {
33-
for (int i = 0; i < length; i++) {
34-
semihosting_putc(&buf[i]);
35-
}
35+
:
36+
: [args] "r" (&args)
37+
: "r0", "r1");
3638
}
3739

3840
stdio_driver_t stdio_semihosting = {

0 commit comments

Comments
 (0)