SWO.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* mbed SWO Library
  2. * Copyright (c) 2014, v01: WH. Ported from Segger example
  3. * v02: WH. Added Class with Stream support
  4. * 2017, v03: WH,PS. Added stream claim for stdout, proposed by Pavel Sorejs
  5. *
  6. * Simple implementation for tracing via Serial Wire Output(SWO) for Cortex-M processors.
  7. * It can be used with Host PC software such as ST-LINK Utility or Segger J-Link SWO viewer.
  8. * This sample implementation ensures that output via SWO is enabled in order to guarantee
  9. * that the application does not hang.
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy
  12. * of this software and associated documentation files (the "Software"), to deal
  13. * in the Software without restriction, including without limitation the rights
  14. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. * copies of the Software, and to permit persons to whom the Software is
  16. * furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included in
  19. * all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. * THE SOFTWARE.
  28. */
  29. #ifndef MBED_SWO_H
  30. #define MBED_SWO_H
  31. //
  32. // This is the Class implementation
  33. //
  34. /**
  35. * @code
  36. * #include "mbed.h"
  37. * #include "SWO.h"
  38. *
  39. * DigitalOut myled(LED1);
  40. *
  41. * Serial pc(SERIAL_TX, SERIAL_RX);
  42. *
  43. * SWO_Channel SWO();
  44. *
  45. * int main() {
  46. * pc.printf("Hello World\n\r");
  47. *
  48. * SWO.printf("\r\nHello World from SWO\r\n");
  49. * SWO.printf("CPU SystemCoreClock is %d Hz\r\n", SystemCoreClock);
  50. *
  51. * while(1) {
  52. * myled = 1; // LED is ON
  53. * wait(0.2); // 200 ms
  54. * myled = 0; // LED is OFF
  55. * wait(1.0); // 1 sec
  56. *
  57. * SWO.putc('#');
  58. * }
  59. * }
  60. * @endcode
  61. */
  62. /** An SWO interface for debugging that supports Stream
  63. *
  64. * @brief Currently works on nucleo ST-LINK using ST-Link Utility and other devices that support SWD/SWO using Segger SWO viewer
  65. *
  66. */
  67. class SWO_Channel : public Stream {
  68. public:
  69. /** Create an SWO interface for debugging that supports Stream
  70. *
  71. * @param const char *name Channel name (default = none)
  72. */
  73. SWO_Channel(const char *name=NULL);
  74. /**
  75. * Function: claim
  76. *
  77. * Redirect a stream to this SWO object
  78. *
  79. * Important: A name parameter must have been added when creating the SWO object:
  80. *
  81. * @code
  82. * #include "SWO.h"
  83. * ...
  84. * SWO_Channel pc("modser");
  85. *
  86. * int main() {
  87. * pc.claim(); // capture <stdout>
  88. * pc.printf("Uses the SWO library\r\n");
  89. * printf("So does this!\r\n");
  90. * }
  91. * @endcode
  92. *
  93. * @ingroup API
  94. * @param FILE *stream The stream to redirect (default = stdout)
  95. * @return true if succeeded, else false
  96. */
  97. bool claim(FILE *stream = stdout);
  98. #if DOXYGEN_ONLY
  99. /** Write a character to the display
  100. *
  101. * @param c The character to write to the display
  102. */
  103. int putc(int c);
  104. /** Write a formatted string to the display
  105. *
  106. * @param format A printf-style format string, followed by the
  107. * variables to use in formatting the string.
  108. */
  109. int printf(const char* format, ...);
  110. #endif
  111. protected:
  112. // Stream implementation functions
  113. virtual int _putc(int value);
  114. virtual int _getc();
  115. private:
  116. };
  117. //
  118. //This is the classic implementation
  119. //
  120. /**
  121. * @code
  122. * #include "mbed.h"
  123. * #include "SWO.h"
  124. *
  125. * DigitalOut myled(LED1);
  126. *
  127. * Serial pc(SERIAL_TX, SERIAL_RX);
  128. *
  129. * int main() {
  130. * pc.printf("Hello World\n\r");
  131. *
  132. * SWO_PrintString("\r\nHello World from SWO\r\n");
  133. * char message[64];
  134. * sprintf(message, "CPU SystemCoreClock is %d Hz\r\n", SystemCoreClock);
  135. * SWO_PrintString(message);
  136. *
  137. * while(1) {
  138. * myled = 1; // LED is ON
  139. * wait(0.2); // 200 ms
  140. * myled = 0; // LED is OFF
  141. * wait(1.0); // 1 sec
  142. *
  143. * SWO_PrintString("#");
  144. * }
  145. * }
  146. * @endcode
  147. */
  148. // Prototypes
  149. /**
  150. * @brief
  151. * Checks if SWO is set up. If it is not, return,
  152. * to avoid program hangs if no debugger is connected.
  153. * If it is set up, print a character to the ITM_STIM register
  154. * in order to provide data for SWO.
  155. * @param c The Character to be printed.
  156. * @notes Additional checks for device specific registers can be added.
  157. */
  158. void SWO_PrintChar (char c);
  159. /**
  160. *
  161. * SWO_PrintString()
  162. *
  163. * @brief Print a string via SWO.
  164. * @param *s The string to be printed.
  165. */
  166. void SWO_PrintString(const char *s);
  167. #endif