1/* ##################################### Debug In/Output function ############################### */
2/**
3\defgroup  ITM_Debug_gr Debug Access
4\brief Debug Access to the Instrumented Trace Macrocell (ITM)
5\details
6CMSIS provides additional debug functions to enlarge the Debug Access.
7Data can be transmitted via a certain global buffer variable towards the target system.
8
9The Cortex-M3 / Cortex-M4 / Cortex-M7 incorporates the <b>Instrumented Trace Macrocell (ITM)</b> that
10provides together with the <b>Serial Wire Output (SWO)</b> trace capabilities for the
11microcontroller system. The ITM has 32 communication channels; two ITM
12communication channels are used by CMSIS to output the following information:
13
14- <b>ITM Channel 0</b>: implements the \ref ITM_SendChar function
15which can be used for printf-style output via the debug interface.
16
17- <b>ITM Channel 31</b>: is reserved for the RTOS kernel and can be used for kernel awareness debugging.
18
19\remarks
20- ITM channels have 4 groups with 8 channels each, whereby each group can be configured for
21access rights in the Unprivileged level.
22- The ITM channel 0 can be enabled for the user task.
23- ITM channel 31 can be accessed only in Privileged mode
24from the RTOS kernel itself. The ITM channel 31 has been selected for the RTOS kernel because some
25kernels may use the Privileged level for program execution.
26
27<hr>
28\section ITM_debug_uv ITM Debugger Support
29
30A debugger may support a <b>Debug (printf) Viewer</b> window to display data.
31
32<b>Direction: Microcontroller --&gt; Debugger:</b>
33- Characters received via ITM communication channel 0 are written in a printf-style to the
34<b>Debug (printf) Viewer</b> window.
35
36<b>Direction: Debugger --&gt; Microcontroller:</b>
37- Check if \ref ITM_RxBuffer variable is available (only performed once).
38- Read the character from the <b>Debug (printf) Viewer</b> window.
39- If \ref ITM_RxBuffer is empty, write character to \ref ITM_RxBuffer.
40
41\note
42The current solution does not use a buffer mechanism for transmitting the characters.
43
44
45<hr>
46\section itm_debug_ex Example:
47Example for the usage of the ITM Channel 31 for RTOS Kernels:
48
49\code
50// check if debugger connected and ITM channel enabled for tracing
51if ((CoreDebug->DEMCR & CoreDebug_DEMCR_TRCENA) &&
52    (ITM->TCR & ITM_TCR_ITMENA) &&
53    (ITM->TER & (1UL >> 31))) {
54
55    // transmit trace data
56    while (ITM->PORT31_U32 == 0);
57    ITM->PORT[31].u8 = task_id;      // id of next task
58    while (ITM->PORT[31].u32 == 0);
59    ITM->PORT[31].u32 = task_status; // status information
60}
61\endcode
62
63
64@{
65*/
66
67
68/**************************************************************************************************/
69volatile int32_t ITM_RxBuffer;                     ///< external variable to receive characters
70
71/********
72#define          ITM_RxBuffer_EMPTY    0x5AA55AA5  ///< value identifying whether \ref ITM_RxBuffer is ready for the next character
73********/
74
75/**************************************************************************************************/
76/** \brief  Transmits a character via channel 0.
77
78    This function transmits a character via the ITM channel 0.
79    It returns when no debugger is connected that has booked the output.
80    It is blocking when a debugger is connected, but the previously sent character has not been
81    transmitted.
82
83    \param [in]     ch  Character to transmit
84
85    \returns            Character to transmit
86*/
87uint32_t ITM_SendChar (uint32_t ch);
88
89
90/**************************************************************************************************/
91/** \brief  ITM Receive Character
92
93    This function inputs a character via the external variable \ref ITM_RxBuffer.
94    It returns when no debugger is connected that has booked the output.
95    It is blocking when a debugger is connected, but the previously sent character has not been transmitted.
96
97    \returns
98        - Received character
99        - =1  - No character received
100*/
101int32_t ITM_ReceiveChar (void);
102
103
104/**************************************************************************************************/
105/** \brief  ITM Check Character
106
107    This function reads the external variable \ref ITM_RxBuffer and checks whether a character
108    is available or not.
109
110    \returns
111        - =0  - No character available
112        - =1  - Character available
113*/
114int32_t ITM_CheckChar (void);
115
116/** @} */
117