/* ##################################### Debug In/Output function ############################### */
/** 
\defgroup  ITM_Debug_gr Debug Access
\brief Debug Access to the Instrumented Trace Macrocell (ITM)
\details
CMSIS provides additional debug functions to enlarge the Debug Access. 
Data can be transmitted via a certain global buffer variable towards the target system.    

The Cortex-M3 / Cortex-M4 / Cortex-M7 incorporates the <b>Instrumented Trace Macrocell (ITM)</b> that 
provides together with the <b>Serial Wire Output (SWO)</b> trace capabilities for the 
microcontroller system. The ITM has 32 communication channels; two ITM 
communication channels are used by CMSIS to output the following information:

- <b>ITM Channel 0</b>: implements the \ref ITM_SendChar function 
which can be used for printf-style output via the debug interface.

- <b>ITM Channel 31</b>: is reserved for the RTOS kernel and can be used for kernel awareness debugging.

\remarks
- ITM channels have 4 groups with 8 channels each, whereby each group can be configured for 
access rights in the Unprivileged level. 
- The ITM channel 0 can be enabled for the user task. 
- ITM channel 31 can be accessed only in Privileged mode 
from the RTOS kernel itself. The ITM channel 31 has been selected for the RTOS kernel because some 
kernels may use the Privileged level for program execution. 
    
<hr>    
\section ITM_debug_uv ITM Debugger Support
    
A debugger may support a <b>Debug (printf) Viewer</b> window to display data.

<b>Direction: Microcontroller --&gt; Debugger:</b>
- Characters received via ITM communication channel 0 are written in a printf-style to the 
<b>Debug (printf) Viewer</b> window.
    
<b>Direction: Debugger --&gt; Microcontroller:</b>
- Check if \ref ITM_RxBuffer variable is available (only performed once).
- Read the character from the <b>Debug (printf) Viewer</b> window.
- If \ref ITM_RxBuffer is empty, write character to \ref ITM_RxBuffer.

\note
The current solution does not use a buffer mechanism for transmitting the characters.
  
   
<hr>     
\section itm_debug_ex Example:
Example for the usage of the ITM Channel 31 for RTOS Kernels:

\code
// check if debugger connected and ITM channel enabled for tracing
if ((CoreDebug->DEMCR & CoreDebug_DEMCR_TRCENA) &&
    (ITM->TCR & ITM_TCR_ITMENA) &&
    (ITM->TER & (1UL >> 31))) {
    
    // transmit trace data
    while (ITM->PORT31_U32 == 0);
    ITM->PORT[31].u8 = task_id;      // id of next task
    while (ITM->PORT[31].u32 == 0);
    ITM->PORT[31].u32 = task_status; // status information
}
\endcode


@{
*/


/**************************************************************************************************/
volatile int32_t ITM_RxBuffer;                     ///< external variable to receive characters                    

/********
#define          ITM_RxBuffer_EMPTY    0x5AA55AA5  ///< value identifying whether \ref ITM_RxBuffer is ready for the next character 
********/

/**************************************************************************************************/
/** \brief  Transmits a character via channel 0.

    This function transmits a character via the ITM channel 0. 
    It returns when no debugger is connected that has booked the output.  
    It is blocking when a debugger is connected, but the previously sent character has not been 
    transmitted. 

    \param [in]     ch  Character to transmit
    
    \returns            Character to transmit
*/
uint32_t ITM_SendChar (uint32_t ch);


/**************************************************************************************************/
/** \brief  ITM Receive Character

    This function inputs a character via the external variable \ref ITM_RxBuffer. 
    It returns when no debugger is connected that has booked the output.  
    It is blocking when a debugger is connected, but the previously sent character has not been transmitted. 

    \returns        
        - Received character
        - =1  - No character received
*/
int32_t ITM_ReceiveChar (void);


/**************************************************************************************************/
/** \brief  ITM Check Character

    This function reads the external variable \ref ITM_RxBuffer and checks whether a character 
    is available or not. 

    \returns   
        - =0  - No character available
        - =1  - Character available
*/
int32_t ITM_CheckChar (void);

/** @} */
