SWO Viewer and cortex-m, multiple channels output simultaneously and in-target check for connection

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

  • SWO Viewer and cortex-m, multiple channels output simultaneously and in-target check for connection

    J-Link SWO Viewer functionality is poorly documented and I have several questions:
    1. Is it possible to run multiple channels views simultaneously ? CMSIS mentions this useful cortex-m feature but it seems like j-link polling mechanism will not like it.
    2. How target could check for swo "connection" or at least for some kind of being under j-link control before trying to send to ITM ? Currently my firmware doesn't and simply hangs trying to send output when j-link isn't connected.

    Thanks in advance,
    Artem
  • Hi Artem,

    to 1.) If you output SWO data on multiple ITM channels (or more precisely multiple stimulus ports) you can tell SWOViewer data from what channels it shall show
    by passing a command line option to it: "-itmmask"
    By default, J-Link SWO Viewer only shows SWO data output on ITM stimulus port 0.
    For example, if your application outputs data on stimulus port 0 & 1 and you want to see both in the Viewer, then you need to start J-Link SWO Viewer as follows:

    JLinkSWOViewer.exe -itmmask 0x00000003

    A '1'-bit in the mask tells SWOViewer to enable the appropriate channel and show data which is received on it.
    This command line option is pretty new and not in the manual yet. We will add it short term.
    Please make sure that you are using a current version of the SWOViewer utility when evaluating this option:
    segger.com/jlink-software.html

    Starting multiple instances of SWOViewer will not work since only one session can receive the SWO data from the target. Once the data is received by one session, there is no possibility for the other one to get the same data again.

    to 2) This is a common problem. Did you check our website?
    We provide target sample code to avoid exactly this problem.
    segger.com/j-link-swo-viewer.html


    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.
  • Trying to implement it I found that information provided on segger.com/j-link-swo-viewer.html have a lot of errors:

    1. Example code provided is not enough. J-Link initialises not all debug registers. There are MCU specific registers also. For example, STM32F2 requires:

    C Source Code

    1. *((volatile unsigned *)0xE0042004) = 0x00000020; // Enabling TRACE_IOEN in DBGMCU_CR

    2. Did you even tested that code ?

    C Source Code

    1. //
    2. // Check if ITM_TRC is enabled
    3. //
    4. if ((ITM_TCR & (1 << 22)) == 1) {
    5. return;
    6. }

    It doesn't match register bits in ARMv7-M Architecture Reference Manual.
    3. SWO_PrintChar() causes HardFault when executed by unprivileged code. It's because of access to DHCSR and DEMCR registers. Are you sure all of that checks are necessary ? I removed these two reduntant checks and it works for me.
    4. Target still hangs after folowing sequence of actions: power-on j-link, power-on board (program starts executing ignoring disabled swo), launch SWO Viewer.

    The post was edited 2 times, last by artem_pisarenko ().

  • For what it's worth, it doesn't work here either on a Toshiba motor controller part. I'm in the process of reading documentation and trying to debug now. I'll post here if I get anything working.

    Edit: and clearly the code is wrong. This expression:

    ((ITM_TCR & (1 << 22)) == 1)

    Can never be true.

    The post was edited 2 times, last by carlnorum ().

  • Hi,

    C Source Code

    1. ((ITM_TCR & (1 << 22)) == 1)

    I agree, this looks like a typo and will be corrected. I will make sure.

    Regarding the problem with unprivileged code: We will verify

    C Source Code

    1. It's because of access to DHCSR and DEMCR registers. Are you sure all of that checks are necessary ? I removed these two reduntant checks and it works for me.

    Without DEMCR.TRACENA being set, the complete ITM & DWT block is not enabled and reading from disabled registers is not a good idea. It may be read as 0 or it is implementation defined what happens.
    ARMv7-M architecture reference manual:
    When TRCENA is set to 0:
    • DWT registers return UNKNOWN values on reads. Whether the processor
    ignores writes to the DWT unit is IMPLEMENTATION DEFINED.
    • ITM registers return UNKNOWN values on reads. Whether the processor
    ignores writes to the ITM unit is IMPLEMENTATION DEFINED.


    The ITM section of the same documentation says something...
    I will check internally.


    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.
  • I got it to work here; the example code is OK as far as I can tell except for the line mentioned above.

    How do I get SWO output while JLinkGDBServer is running? Connecting to port 2332 didn't produce any output. Neither did 2333 as mentioned in the docs.
  • Without DEMCR.TRACENA being set, the complete ITM & DWT block is not enabled and reading from disabled registers is not a good idea. It may be read as 0 or it is implementation defined what happens.
    Agree, I missed that. Generic solution would be temporary raising privilege by implementing SVC call. For those who using FreeRTOS MPU (like me), last versions fortunately intruduced mechanism that allows application writers to execute certain functions in privileged mode even when a task is running in user mode.
    How do I get SWO output while JLinkGDBServer is running? Connecting to port 2332 didn't produce any output. Neither did 2333 as mentioned in the docs.
    STM32F2 + SWO + JLINK + JLINK server + GDB client elf console It worked for me in earlier J-Link versions before I migrated to SWO Viewer method.
  • Hi,

    We have tested and changed the code on the website.

    The first two checks caused the fault in unprivileged mode, but were not necessary, so they are eliminated now.
    The remaining checks are corrected.

    MCU specific register checks can be added, but this is not needed in most cases.
    The provided code is for generic use with Cortex-M3/4 devices.

    Best regards,
    Johannes

    C Source Code

    1. /*********************************************************************
    2. * SEGGER MICROCONTROLLER GmbH & Co KG *
    3. * Solutions for real time microcontroller applications *
    4. **********************************************************************
    5. * *
    6. * (c) 2012-2013 SEGGER Microcontroller GmbH & Co KG *
    7. * *
    8. * www.segger.com Support: support@segger.com *
    9. * *
    10. **********************************************************************
    11. ----------------------------------------------------------------------
    12. File : SWO.c
    13. Purpose : Simple implementation for output via SWO for Cortex-M processors.
    14. It can be used with any IDE. This sample implementation ensures that
    15. output via SWO is enabled in order to gurantee that the application
    16. does not hang.
    17. -------- END-OF-HEADER ---------------------------------------------
    18. */
    19. /*********************************************************************
    20. *
    21. * Prototypes (to be placed in a header file such as SWO.h)
    22. */
    23. void SWO_PrintChar (char c);
    24. void SWO_PrintString(const char *s);
    25. /*********************************************************************
    26. *
    27. * Defines for Cortex-M debug unit
    28. */
    29. #define ITM_STIM_U32 (*(volatile unsigned int*)0xE0000000) // Stimulus Port Register word acces
    30. #define ITM_STIM_U8 (*(volatile char*)0xE0000000) // Stimulus Port Register byte acces
    31. #define ITM_ENA (*(volatile unsigned int*)0xE0000E00) // Trace Enable Ports Register
    32. #define ITM_TCR (*(volatile unsigned int*)0xE0000E80) // Trace control register
    33. /*********************************************************************
    34. *
    35. * SWO_PrintChar()
    36. *
    37. * Function description
    38. * Checks if SWO is set up. If it is not, return,
    39. * to avoid program hangs if no debugger is connected.
    40. * If it is set up, print a character to the ITM_STIM register
    41. * in order to provide data for SWO.
    42. * Parameters
    43. * c: The Chacracter to be printed.
    44. * Notes
    45. * Additional checks for device specific registers can be added.
    46. */
    47. void SWO_PrintChar(char c) {
    48. //
    49. // Check if ITM_TCR.ITMENA is set
    50. //
    51. if ((ITM_TCR & 1) == 0) {
    52. return;
    53. }
    54. //
    55. // Check if stimulus port is enabled
    56. //
    57. if ((ITM_ENA & 1) == 0) {
    58. return;
    59. }
    60. //
    61. // Wait until STIMx is ready,
    62. // then send data
    63. //
    64. while ((ITM_STIM_U8 & 1) == 0);
    65. ITM_STIM_U8 = c;
    66. }
    67. /*********************************************************************
    68. *
    69. * SWO_PrintString()
    70. *
    71. * Function description
    72. * Print a string via SWO.
    73. *
    74. */
    75. void SWO_PrintString(const char *s) {
    76. //
    77. // Print out character per character
    78. //
    79. while (*s) {
    80. SWO_PrintChar(*s++);
    81. }
    82. }
    Display All
    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.