Skip to content

Commit 073b56e

Browse files
authored
Add stdio re-targeting to RTT (#84)
1 parent e297630 commit 073b56e

5 files changed

Lines changed: 195 additions & 0 deletions

File tree

ARM.CMSIS-Compiler.pdsc

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
Active Development ...
1818
CLANG:
1919
- Added standard I/O stream support for LLVM-libc
20+
I/O Retarget:
21+
- Added reargeting to RTT
2022
</release>
2123
</releases>
2224

@@ -105,6 +107,11 @@
105107
<require condition="CORE"/>
106108
<require condition="CortexDevice_ITM"/>
107109
</condition>
110+
<condition id="CORE RTT">
111+
<description>CMSIS-Compiler CORE component and RTT</description>
112+
<require condition="CORE"/>
113+
<require Cclass="SEGGER" Cgroup="RTT"/>
114+
</condition>
108115
<condition id="CORE UART_CMSIS">
109116
<description>CMSIS-Compiler CORE component and CMSIS-Driver USART</description>
110117
<require condition="CORE"/>
@@ -138,6 +145,12 @@
138145
<require condition="CORE"/>
139146
<require condition="CortexDevice_ITM"/>
140147
</condition>
148+
<condition id="ARMCC CORE RTT">
149+
<description>ARMCC and CORE component and RTT</description>
150+
<require Tcompiler="ARMCC"/>
151+
<require condition="CORE"/>
152+
<require Cclass="SEGGER" Cgroup="RTT"/>
153+
</condition>
141154

142155
<condition id="ARMCC CORE CMSIS-RTOS2">
143156
<description>ARMCC and CORE component and CMSIS-RTOS2</description>
@@ -328,6 +341,16 @@
328341
<file category="sourceC" name="source/stderr/stderr_itm.c"/>
329342
</files>
330343
</component>
344+
<component Cclass="CMSIS-Compiler" Cgroup="STDERR" Csub="RTT" Capiversion="1.1.0" Cversion="1.0.0" condition="CORE RTT">
345+
<description>Redirect STDERR to RTT (Channel 0)</description>
346+
<RTE_Components_h>
347+
#define RTE_CMSIS_Compiler_STDERR /* CMSIS-Compiler STDERR */
348+
#define RTE_CMSIS_Compiler_STDERR_RTT /* CMSIS-Compiler STDERR: RTT */
349+
</RTE_Components_h>
350+
<files>
351+
<file category="sourceC" name="source/stderr/stderr_rtt.c"/>
352+
</files>
353+
</component>
331354
<component Cclass="CMSIS-Compiler" Cgroup="STDERR" Csub="UART" Cvariant="CMSIS" Capiversion="1.1.0" Cversion="1.0.0" condition="CORE UART_CMSIS">
332355
<description>Redirect STDERR to a CMSIS-Driver USART</description>
333356
<RTE_Components_h>
@@ -370,6 +393,16 @@
370393
<file category="sourceC" name="source/stdin/stdin_itm.c"/>
371394
</files>
372395
</component>
396+
<component Cclass="CMSIS-Compiler" Cgroup="STDIN" Csub="RTT" Capiversion="1.1.0" Cversion="1.0.0" condition="CORE RTT">
397+
<description>Retrieve STDIN from RTT (Channel 0)</description>
398+
<RTE_Components_h>
399+
#define RTE_CMSIS_Compiler_STDIN /* CMSIS-Compiler STDIN */
400+
#define RTE_CMSIS_Compiler_STDIN_RTT /* CMSIS-Compiler STDIN: RTT */
401+
</RTE_Components_h>
402+
<files>
403+
<file category="sourceC" name="source/stdin/stdin_rtt.c"/>
404+
</files>
405+
</component>
373406
<component Cclass="CMSIS-Compiler" Cgroup="STDIN" Csub="UART" Cvariant="CMSIS" Capiversion="1.1.0" Cversion="1.0.0" condition="CORE UART_CMSIS">
374407
<description>Retrieve STDIN from a CMSIS-Driver USART</description>
375408
<RTE_Components_h>
@@ -422,6 +455,16 @@
422455
<file category="sourceC" name="source/stdout/stdout_itm.c"/>
423456
</files>
424457
</component>
458+
<component Cclass="CMSIS-Compiler" Cgroup="STDOUT" Csub="RTT" Capiversion="1.1.0" Cversion="1.0.0" condition="CORE RTT">
459+
<description>Redirect STDOUT to RTT (Channel 0)</description>
460+
<RTE_Components_h>
461+
#define RTE_CMSIS_Compiler_STDOUT /* CMSIS-Compiler STDOUT */
462+
#define RTE_CMSIS_Compiler_STDOUT_RTT /* CMSIS-Compiler STDOUT: RTT */
463+
</RTE_Components_h>
464+
<files>
465+
<file category="sourceC" name="source/stdout/stdout_rtt.c"/>
466+
</files>
467+
</component>
425468
<component Cclass="CMSIS-Compiler" Cgroup="STDOUT" Csub="UART" Cvariant="CMSIS" Capiversion="1.1.0" Cversion="1.0.0" condition="CORE UART_CMSIS">
426469
<description>Redirect STDOUT to a CMSIS-Driver USART</description>
427470
<RTE_Components_h>
@@ -464,6 +507,16 @@
464507
<file category="sourceC" name="source/tty/tty_itm.c"/>
465508
</files>
466509
</component>
510+
<component Cclass="CMSIS-Compiler" Cgroup="TTY" Csub="RTT" Capiversion="1.1.0" Cversion="1.0.0" condition="ARMCC CORE RTT">
511+
<description>Redirect TTY to RTT (Channel 0)</description>
512+
<RTE_Components_h>
513+
#define RTE_CMSIS_Compiler_TTY /* CMSIS-Compiler TTY */
514+
#define RTE_CMSIS_Compiler_TTY_RTT /* CMSIS-Compiler TTY: RTT */
515+
</RTE_Components_h>
516+
<files>
517+
<file category="sourceC" name="source/tty/tty_rtt.c"/>
518+
</files>
519+
</component>
467520

468521
<!-- OS Interface -->
469522
<component Cclass="CMSIS-Compiler" Cgroup="OS Interface" Csub="Custom" Capiversion="1.1.0" Cversion="2.1.0" custom="1" condition="ARMCC CORE">

source/stderr/stderr_rtt.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (C) 2026 ARM Limited or its affiliates. All rights reserved.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the License); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
14+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#include "retarget_stderr.h"
20+
21+
#include "SEGGER_RTT.h"
22+
23+
/**
24+
Put a character to the stderr
25+
26+
\param[in] ch Character to output
27+
\return The character written, or -1 on write error.
28+
*/
29+
int stderr_putchar (int ch) {
30+
unsigned res;
31+
32+
res = SEGGER_RTT_PutChar(0U, (char)ch);
33+
34+
if (res != 1U) {
35+
return -1;
36+
}
37+
38+
return (ch);
39+
}

source/stdin/stdin_rtt.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright (C) 2026 ARM Limited or its affiliates. All rights reserved.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the License); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
14+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#include "retarget_stdin.h"
20+
21+
#include "SEGGER_RTT.h"
22+
23+
/**
24+
Get a character from the stdio
25+
26+
\return The next character from the input, or -1 on read error.
27+
*/
28+
int stdin_getchar (void) {
29+
int ret;
30+
31+
ret = SEGGER_RTT_WaitKey();
32+
33+
return ret;
34+
}

source/stdout/stdout_rtt.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (C) 2026 ARM Limited or its affiliates. All rights reserved.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the License); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
14+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#include "retarget_stdout.h"
20+
21+
#include "SEGGER_RTT.h"
22+
23+
/**
24+
Put a character to the stdout
25+
26+
\param[in] ch Character to output
27+
\return The character written, or -1 on write error.
28+
*/
29+
int stdout_putchar (int ch) {
30+
unsigned res;
31+
32+
res = SEGGER_RTT_PutChar(0U, (char)ch);
33+
34+
if (res != 1U) {
35+
return -1;
36+
}
37+
38+
return (ch);
39+
}

source/tty/tty_rtt.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (C) 2026 ARM Limited or its affiliates. All rights reserved.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the License); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
14+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#include "retarget_tty.h"
20+
21+
#include "SEGGER_RTT.h"
22+
23+
/**
24+
Put a character to the teletypewritter
25+
26+
\param[in] ch Character to output
27+
*/
28+
void ttywrch (int ch) {
29+
SEGGER_RTT_PutChar(0U, (char)ch);
30+
}

0 commit comments

Comments
 (0)