1 /*
2  * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include "pico/stdio/driver.h"
8 #include "pico/stdio_semihosting.h"
9 #include "pico/binary_info.h"
10 
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 //}
20 
semihosting_putc(char c)21 static void __attribute__((naked)) semihosting_putc(char c) {
22     __asm (
23 
24     "mov r1, r0\n"
25     "mov r0, #3\n"
26     "bkpt 0xab\n"
27     "bx lr\n"
28     );
29 }
30 
31 
stdio_semihosting_out_chars(const char * buf,int length)32 static void stdio_semihosting_out_chars(const char *buf, int length) {
33     for (uint i = 0; i <length; i++) {
34         semihosting_putc(buf[i]);
35     }
36 }
37 
38 stdio_driver_t stdio_semihosting = {
39         .out_chars = stdio_semihosting_out_chars,
40 #if PICO_STDIO_ENABLE_CRLF_SUPPORT
41         .crlf_enabled = PICO_STDIO_SEMIHOSTING_DEFAULT_CRLF
42 #endif
43 };
44 
stdio_semihosting_init()45 void stdio_semihosting_init() {
46 #if !PICO_NO_BI_STDIO_SEMIHOSTING
47     bi_decl_if_func_used(bi_program_feature("semihosting stdout"));
48 #endif
49     stdio_set_driver_enabled(&stdio_semihosting, true);
50 }
51 
52