# CMSIS Debug Support

This file describes the CMSIS Debug support available with CMSIS (starting V1.30).

Version: 1.02 - 25. July 2011

Information in this file, the accompany manuals, and software is
Copyright © ARM Ltd.
All rights reserved.

Revision History

- Version 1.00: Initial Release.
- Version 1.01: Internal Review.
- Version 1.02: Removed product specific information.

## Contents

1. About
2. Cortex-M3 / Cortex-M4 ITM Debug Access
3. Debug IN / OUT functions
4. ITM Debug Support in Debugger

## About

CMSIS provides for Cortex-M3 / Cortex-M4 processor based microcontrollers debug support via the Instrumented Trace Macrocell (ITM). This document describes the available CMSIS Debug functions and the used methods.

## Cortex-M3 / Cortex-M4 ITM Debug Access

The Cortex-M3 incorporates the Instrumented Trace Macrocell (ITM) that provides together with the Serial Viewer Output trace capabilities for the microcontroller system. The ITM has 32 communication channels which are able to transmit 32 / 16 / 8 bit values; two ITM communication channels are used by CMSIS to output the following information:

- ITM Channel 0: used for printf-style output via the debug interface.
- ITM Channel 31: is reserved for RTOS kernel awareness debugging.

## Debug IN / OUT functions

CMSIS provides following debug functions:

- ITM_SendChar (uses ITM channel 0)
- ITM_ReceiveChar (uses global variable)
- ITM_CheckChar (uses global variable)

### ITM_SendChar

ITM_SendChar is used to transmit a character over ITM channel 0 from the microcontroller system to the debug system.
Only a 8 bit value is transmitted.

    static __INLINE uint32_t ITM_SendChar (uint32_t ch)
    {
      /* check if debugger connected and ITM channel enabled for tracing */
      if ((CoreDebug->DEMCR & CoreDebug_DEMCR_TRCENA)  &&
          (ITM->TCR & ITM_TCR_ITMENA)                  &&
          (ITM->TER & (1UL << 0))  )
      {
        while (ITM->PORT[0].u32 == 0);
        ITM->PORT[0].u8 = (uint8_t)ch;
      }
      return (ch);
    }

### ITM_ReceiveChar

ITM communication channel is only capable for OUT direction. For IN direction a global variable is used. A simple mechanism detects if a character is received. The project to test need to be build with debug information.

The global variable ITM_RxBuffer is used to transmit a 8 bit value from debug system to microcontroller system. ITM_RxBuffer is 32 bit wide to ensure a proper handshake.

    extern volatile int32_t ITM_RxBuffer;                    /* variable to receive characters                             */

A dedicated bit pattern is used to determine if ITM_RxBuffer is empty or contains a valid value.

    #define                 ITM_RXBUFFER_EMPTY    0x5AA55AA5 /* value identifying ITM_RxBuffer is ready for next character */

ITM_ReceiveChar is used to receive a 8 bit value from the debug system. The function is nonblocking. It returns the received character or '-1' if no character was available.

    static __INLINE int32_t ITM_ReceiveChar (void) {
      int32_t ch = -1;                               /* no character available */

      if (ITM_RxBuffer != ITM_RXBUFFER_EMPTY) {
        ch = ITM_RxBuffer;
        ITM_RxBuffer = ITM_RXBUFFER_EMPTY;       /* ready for next character */
      }

      return (ch);
    }

### ITM_CheckChar

ITM_CheckChar is used to check if a character is received.

    static __INLINE int32_t ITM_CheckChar (void) {

      if (ITM_RxBuffer == ITM_RXBUFFER_EMPTY) {
        return (0);                                 /* no character available */
      } else {
        return (1);                                 /*    character available */
      }
    }

## ITM Debug Support in a Debugger

The Debugger shall offer a dedicated console window for printf style debug input and output using the CMSIS defined ITM methods described above.

Direction: Microcontroller -> Debugger:

- at the beginning of a debug session the debugger shall enable ITM trace on channel 0 and continuously snoop for channel 0 data on the ITM trace stream it receives from the Microcontroller's CoreSight ITM unit
- data received via the ITM communication channel 0 is interpreted as charater and gets redirected into the dedicated Console Window

Direction: Debugger -> Microcontroller:

- at the beginning of a debug session the debugger shall seek for the presence of the global variable named ITM_RxBuffer in the debug information of the application being loaded
- strings entered into the Console Window are written by the debugger as a stream of char values via the variable ITM_RxBuffer.
- the debugger writes the next character into the ITM_RxBuffer only once the value has been read and the ITM_RXBUFFER_EMPTY value being set. (refer to: ITM_ReceiveChar()).