1 /*
2  * This file is part of the MicroPython project, http://micropython.org/
3  *
4  * The MIT License (MIT)
5  *
6  * Copyright (c) 2018 Ayke van Laethem
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this software and associated documentation files (the "Software"), to deal
10  * in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12  * copies of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24  * THE SOFTWARE.
25  */
26 
27 #include "semihosting.h"
28 
29 // Resources:
30 // http://embed.rs/articles/2016/semi-hosting-rust/
31 // https://wiki.dlang.org/Minimal_semihosted_ARM_Cortex-M_%22Hello_World%22
32 // https://github.com/arduino/OpenOCD/blob/master/src/target/arm_semihosting.c
33 
34 #define SYS_OPEN   0x01
35 #define SYS_WRITEC 0x03
36 #define SYS_WRITE  0x05
37 #define SYS_READC  0x07
38 
39 // Constants:
40 #define OPEN_MODE_READ  (0) // mode "r"
41 #define OPEN_MODE_WRITE (4) // mode "w"
42 
43 #ifndef __thumb__
44 #error Semihosting is only implemented for ARM microcontrollers.
45 #endif
46 
47 static int mp_semihosting_stdout;
48 
mp_semihosting_call(uint32_t num,const void * arg)49 static uint32_t mp_semihosting_call(uint32_t num, const void *arg) {
50     // A semihosting call works as follows, similar to a SVCall:
51     //   * the call is invoked by a special breakpoint: 0xAB
52     //   * the command is placed in r0
53     //   * a pointer to the arguments is placed in r1
54     //   * the return value is placed in r0
55     // Note that because it uses the breakpoint instruction, applications
56     // will hang if they're not connected to a debugger. And they'll be
57     // stuck in a breakpoint if semihosting is not specifically enabled in
58     // the debugger.
59     // Also note that semihosting is extremely slow (sometimes >100ms per
60     // call).
61     register uint32_t num_reg __asm__ ("r0") = num;
62     register const void *args_reg __asm__ ("r1") = arg;
63     __asm__ __volatile__ (
64         "bkpt 0xAB\n"         // invoke semihosting call
65         : "+r" (num_reg)      // call number and result
66         : "r" (args_reg)      // arguments
67         : "memory");          // make sure args aren't optimized away
68     return num_reg; // r0, which became the result
69 }
70 
mp_semihosting_open_console(uint32_t mode)71 static int mp_semihosting_open_console(uint32_t mode) {
72     struct {
73         char *name;
74         uint32_t mode;
75         uint32_t name_len;
76     } args = {
77         .name = ":tt",     // magic path to console
78         .mode = mode,      // e.g. "r", "w" (see OPEN_MODE_* constants)
79         .name_len = 3,     // strlen(":tt")
80     };
81     return mp_semihosting_call(SYS_OPEN, &args);
82 }
83 
mp_semihosting_init()84 void mp_semihosting_init() {
85     mp_semihosting_stdout = mp_semihosting_open_console(OPEN_MODE_WRITE);
86 }
87 
mp_semihosting_rx_char()88 int mp_semihosting_rx_char() {
89     return mp_semihosting_call(SYS_READC, NULL);
90 }
91 
mp_semihosting_tx_char(char c)92 static void mp_semihosting_tx_char(char c) {
93     mp_semihosting_call(SYS_WRITEC, &c);
94 }
95 
mp_semihosting_tx_strn(const char * str,size_t len)96 uint32_t mp_semihosting_tx_strn(const char *str, size_t len) {
97     if (len == 0) {
98         return 0; // nothing to do
99     }
100     if (len == 1) {
101         mp_semihosting_tx_char(*str); // maybe faster?
102         return 0;
103     }
104 
105     struct {
106         uint32_t fd;
107         const char *str;
108         uint32_t len;
109     } args = {
110         .fd = mp_semihosting_stdout,
111         .str = str,
112         .len = len,
113     };
114     return mp_semihosting_call(SYS_WRITE, &args);
115 }
116 
mp_semihosting_tx_strn_cooked(const char * str,size_t len)117 uint32_t mp_semihosting_tx_strn_cooked(const char *str, size_t len) {
118     // Write chunks of data until (excluding) the first '\n' character,
119     // insert a '\r' character, and then continue with the next chunk
120     // (starting with '\n').
121     // Doing byte-by-byte writes would be easier to implement but is far
122     // too slow.
123     size_t start = 0;
124     for (size_t i = 0; i < len; i++) {
125         if (str[i] == '\n') {
126             mp_semihosting_tx_strn(str + start, i - start);
127             mp_semihosting_tx_char('\r');
128             start = i;
129         }
130     }
131     return mp_semihosting_tx_strn(str + start, len - start);
132 }
133