µOS++ IIIe Reference 7.0.0
The third edition of µOS++, a POSIX inspired open source framework, written in C++
Loading...
Searching...
No Matches
trace-itm.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the µOS++ project (https://micro-os-plus.github.io/).
3 * Copyright (c) 2015-2025 Liviu Ionescu. All rights reserved.
4 *
5 * Permission to use, copy, modify, and/or distribute this software
6 * for any purpose is hereby granted, under the terms of the MIT license.
7 *
8 * If a copy of the license was not distributed with this file, it can
9 * be obtained from https://opensource.org/licenses/mit.
10 */
11
12#if defined(__clang__)
13#pragma clang diagnostic ignored "-Wempty-translation-unit"
14#endif
15
16// ----------------------------------------------------------------------------
17
18#if defined(__ARM_EABI__)
19
20// ----------------------------------------------------------------------------
21
22#if defined(TRACE)
23
24#if defined(OS_USE_OS_APP_CONFIG_H)
25#include <cmsis-plus/os-app-config.h>
26#endif
27
28#if defined(OS_USE_TRACE_ITM)
29
31
32// TODO: Find a better way to include the ITM definitions (including
33// the entire vendor header is averkill).
34#include <cmsis_device.h>
35
36// ----------------------------------------------------------------------------
37
38namespace os
39{
40 namespace trace
41 {
42 // ------------------------------------------------------------------------
43
44 void
45 initialize (void)
46 {
47 // For ITM no inits required.
48 // The debug registers are set the JTAG software.
49 }
50
51 // ------------------------------------------------------------------------
52
53#if defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7EM__)
54
55 // ITM is the ARM standard mechanism, running over SWD/SWO on Cortex-M3/M4
56 // devices, and is the recommended setting, if available.
57 //
58 // The JLink probe and the GDB server fully support SWD/SWO
59 // and the JLink Debugging plug-in enables it by default.
60 // The current OpenOCD does not include support to parse the SWO stream,
61 // so this configuration will not work on OpenOCD (will not crash, but
62 // nothing will be displayed in the output console).
63
64#if !defined(OS_INTEGER_TRACE_ITM_STIMULUS_PORT)
65#define OS_INTEGER_TRACE_ITM_STIMULUS_PORT (0)
66#endif
67
68 ssize_t
69 write (const void* buf, std::size_t nbyte)
70 {
71 if (buf == nullptr || nbyte == 0)
72 {
73 return 0;
74 }
75
76 const char* cbuf = (const char*)buf;
77
78 for (size_t i = 0; i < nbyte; i++)
79 {
80 // Check if ITM or the stimulus port are not enabled.
81 if (((ITM->TCR & ITM_TCR_ITMENA_Msk) == 0)
82 || ((ITM->TER & (1UL << OS_INTEGER_TRACE_ITM_STIMULUS_PORT))
83 == 0))
84 {
85 // Return the number of sent characters (may be 0).
86 return (ssize_t)i;
87 }
88
89 // Wait until STIMx is ready...
90 while (ITM->PORT[OS_INTEGER_TRACE_ITM_STIMULUS_PORT].u32 == 0)
91 ;
92 // then send data, one byte at a time
94 = (uint8_t)(*cbuf++);
95 }
96
97 // All characters successfully sent.
98 return (ssize_t)nbyte;
99 }
100
101#else
102
103#error "ITM available only on ARCH 7M"
104
105#endif /* defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7EM__) */
106
107 } /* namespace trace */
108} /* namespace os */
109
110#endif /* defined(OS_USE_TRACE_ITM) */
111#endif /* defined(TRACE) */
112
113// ----------------------------------------------------------------------------
114
115#endif /* defined(__ARM_EABI__) */
#define OS_INTEGER_TRACE_ITM_STIMULUS_PORT
Define the ITM stimulus port used for the trace messages.
void initialize(void)
Definition trace.cpp:37
ssize_t write(const void *buf, std::size_t nbyte)
Write the given number of bytes to the trace output channel.
Definition trace.cpp:46
System namespace.