Getting ITM to work on Cortex-M3 (STM32) using SWV

This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

  • Getting ITM to work on Cortex-M3 (STM32) using SWV

    Hello!

    I'm trying to get the ITM 'printf' functionality working on a Cortex-M3 (STM32F103ZE, running at 72Mhz) with a J-Link Ultra, using SWV mode.
    The following code is used to enable the ITM output via TPUI:

    C Source Code

    1. const char mystring[] = "Hello world";
    2. char* s;
    3. CoreDebug->DEMCR = CoreDebug_DEMCR_TRCENA_Msk;
    4. *((uint32_t*)0xE0040010) = 12; //Async Clock Prescaler Register
    5. *((uint32_t*)0xE0040304) = 0; //Formatter and Flush Control Register
    6. *((uint32_t*)0xE00400F0) = 1; // Selected Pin Protocol Register
    7. DBGMCU->CR = 0x00000020; // Set asynchronuous communication via DBGMCU_CR
    8. ITM->LAR = 0xC5ACCE55;
    9. ITM->TCR |= ITM_TCR_ITMENA_Msk | ITM_TCR_SYNCENA_Msk | (1UL << ITM_TCR_ATBID_Pos);
    10. ITM->TER |= 1UL;
    11. ITM->TPR |= 1UL;
    Display All

    I'n not sure about the following settings:
    • Prescale register (72Mhz/12 = 6Mhz, the same as the J-Link, right?)
    • Formatter and flush control register
    • Protocol register (1 == manchester encoded, 2 == NRZ)
    • Maybe some more settings ;(
    Then, in J-Link commander, I use swostart followed by sworead to display the captured data:
    J-Link>sworead
    1 bytes read (1 bytes in host buffer)
    00000000 = 00
    After trying several settings, the above code at least returns some data, although its not what I expect...

    Can anybody help in getting this to work, and is there some way to display the captured SWO data as ASCII, instead of HEX-dump?

    Thanks!
  • Hi,

    below some sample code which has been used with J-Link for some SWO testing on an IAR STM32F103ZE-SK:

    C Source Code

    1. #define ITM_PORT *(volatile unsigned *)0xE0000000
    2. /*********************************************************************
    3. *
    4. * _Delay
    5. */
    6. static void _Delay(int NumLoops) {
    7. volatile int i;
    8. for (i = 0; i < NumLoops; i++);
    9. }
    10. /*********************************************************************
    11. *
    12. * main
    13. */
    14. int main(void) {
    15. int i;
    16. U32 SWOPrescaler;
    17. U32 SWOSpeed;
    18. <Init PLL, set CPU clock to 72 MHz> // Optional, so I do not pos it here
    19. SWOSpeed = 6000000;
    20. *((volatile unsigned *)0xE000EDFC) = 0x01000000; // "Debug Exception and Monitor Control Register (DEMCR)"
    21. *((volatile unsigned *)0xE0042004) = 0x00000027;
    22. *((volatile unsigned *)0xE00400F0) = 0x00000002; // "Selected PIN Protocol Register": Select which protocol to use for trace output (2: SWO)
    23. SWOPrescaler = (72000000 / SWOSpeed) - 1; // SWOSpeed in Hz
    24. *((volatile unsigned *)0xE0040010) = SWOPrescaler; // "Async Clock Prescaler Register". Scale the baud rate of the asynchronous output
    25. *((volatile unsigned *)0xE0000FB0) = 0xC5ACCE55; // ITM Lock Access Register, C5ACCE55 enables more write access to Control Register 0xE00 :: 0xFFC
    26. *((volatile unsigned *)0xE0000E80) = 0x0001000D; // ITM Trace Control Register
    27. *((volatile unsigned *)0xE0000E40) = 0x0000000F; // ITM Trace Privilege Register
    28. *((volatile unsigned *)0xE0000E00) = 0x00000001; // ITM Trace Enable Register. Enabled tracing on stimulus ports. One bit per stimulus port.
    29. *((volatile unsigned *)0xE0001000) = 0x400003FE; // DWT_CTRL
    30. *((volatile unsigned *)0xE0040304) = 0x00000100; // Formatter and Flush Control Register
    31. i = 0;
    32. while (1) {
    33. _Delay(100);
    34. while (ITM_PORT == 0); // Wait while Fifo full
    35. ITM_PORT = i; // Output counter
    36. i++;
    37. }
    Display All


    Hope it helps.


    Best regards
    Alex
    Please read the forum rules before posting.

    Keep in mind, this is *not* a support forum.
    Our engineers will try to answer your questions between their projects if possible but this can be delayed by longer periods of time.
    Should you be entitled to support you can contact us via our support system: segger.com/ticket/

    Or you can contact us via e-mail.
  • Hi Alex,

    Many thanks for your clear example.
    I ran the code exactly on my STM32, but no output is produced at all...
    Do I have to configure anything in J-Link commander before retrieving the output or could something else be wrong?

    Thanks!