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