SWO.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /* mbed SWO Library
  2. * Copyright (c) 2014, v01: WH. Ported from Segger example (www.segger.com)
  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. #include "mbed.h"
  30. #include "SWO.h"
  31. //
  32. // This the Class implementation
  33. //
  34. /** Create and SWO interface for debugging that supports Stream
  35. * @brief Currently works on nucleo ST-LINK using ST-Link Utility and other devices that support SWD/SWO using Segger SWO viewer
  36. */
  37. SWO_Channel::SWO_Channel (const char *name) : Stream(name) {
  38. //May want to add initialisation stuff here
  39. }
  40. /** Write a single character (Stream implementation)
  41. *
  42. * @param value character to be displayed
  43. * @return value
  44. */
  45. int SWO_Channel::_putc(int value) {
  46. //Use CMSIS_core_DebugFunctions. See core_cm3.h
  47. ITM_SendChar(value);
  48. return value;
  49. }
  50. /** Get a single character (Stream implementation)
  51. * @return -1 Not supported
  52. */
  53. int SWO_Channel::_getc() {
  54. return -1;
  55. }
  56. /**
  57. * Claim and redirect a stream to this SWO object
  58. * Important: A name parameter must have been added when creating the SWO object.
  59. *
  60. * @param FILE *stream The stream to redirect (default = stdout)
  61. * @return true if succeeded, else false
  62. */
  63. bool SWO_Channel::claim (FILE *stream) {
  64. if ( FileBase::getName() == NULL) {
  65. error("claim requires a name to be given in the instantiator of the SWO instance!\r\n");
  66. }
  67. //Add '/' before name:
  68. char *path = new char[strlen(FileBase::getName()) + 2];
  69. sprintf(path, "/%s", FileBase::getName());
  70. if (freopen(path, "w", stream) == NULL) {
  71. // Failed, should not happen
  72. return false;
  73. }
  74. delete(path);
  75. //No buffering
  76. setvbuf(stream, NULL, _IONBF, 32);
  77. return true;
  78. }
  79. //
  80. //This is the classic implementation
  81. //
  82. /**
  83. * Defines for Cortex-M debug unit
  84. */
  85. #define ITM_STIM_U32(n) (*(volatile unsigned int*) (0xE0000000+4*n)) // Stimulus Port n Register word access
  86. #define ITM_STIM_U8(n) (*(volatile unsigned char*)(0xE0000000+4*n)) // Stimulus Port n Register byte access
  87. //#define ITM_STIM_U32_0 (*(volatile unsigned int*)0xE0000000) // Stimulus Port 0 Register word access
  88. //#define ITM_STIM_U8_0 (*(volatile char*)0xE0000000) // Stimulus Port 0 Register byte access
  89. #define ITM_ENA (*(volatile unsigned int*)0xE0000E00) // Trace Enable Ports Register
  90. #define ITM_TCR (*(volatile unsigned int*)0xE0000E80) // Trace control register
  91. #define ITM_STIM_FIFOREADY 0x00000001 // FIFO empty
  92. //Stuff below is for documentation and needs further testing
  93. // It seems that the Segger SWO Viewer and the ST-Link Utility do most/all of these
  94. // initialisations on the target before starting the session. This is probably not the case
  95. // when using GDB/OpenOCD.
  96. //
  97. //
  98. #if(0)
  99. #include <libopencm3/stm32/rcc.h>
  100. #include <libopencm3/stm32/gpio.h>
  101. #include <libopencm3/stm32/dbgmcu.h>
  102. #include <libopencm3/cm3/scs.h>
  103. #include <libopencm3/cm3/tpiu.h>
  104. #include <libopencm3/cm3/itm.h>
  105. /**
  106. * SWO_Setup() Example
  107. *
  108. * This file is part of the libopencm3 project.
  109. *
  110. * Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
  111. * https://github.com/1divf/libopenstm32/blob/master/examples/stm32/stm32-h103/traceswo/traceswo.c
  112. *
  113. */
  114. void SWO_Setup(void) {
  115. /* Enable trace subsystem (we'll use ITM and TPIU) */
  116. SCS_DEMCR |= SCS_DEMCR_TRCENA;
  117. /* Use Manchester code for asynchronous transmission */
  118. TPIU_SPPR = TPIU_SPPR_ASYNC_MANCHESTER;
  119. TPIU_ACPR = 7;
  120. /* Data width is 1 byte */
  121. TPIU_CSPSR = TPIU_CSPSR_BYTE;
  122. /* Formatter and flush control */
  123. TPIU_FFCR &= ~TPIU_FFCR_ENFCONT;
  124. /* Enable TRACESWO pin for async mode */
  125. DBGMCU_CR = DBGMCU_CR_TRACE_IOEN | DBGMCU_CR_TRACE_MODE_ASYNC;
  126. /* Unlock access to ITM registers */
  127. /* FIXME: Magic numbers... Is this Cortex-M3 generic? */
  128. *((volatile uint32_t*)0xE0000FB0) = 0xC5ACCE55;
  129. /* Enable ITM with ID = 1 */
  130. ITM_TCR = (1 << 16) | ITM_TCR_ITMENA;
  131. /* Enable stimulus port 1 */
  132. ITM_TER[0] = 1;
  133. }
  134. /**
  135. * SWO_Setup() Example
  136. *
  137. * http://forum.segger.com/index.php?page=Thread&threadID=608
  138. *
  139. */
  140. void SWO_Setup_1(void) {
  141. U32 SWOPrescaler;
  142. U32 SWOSpeed;
  143. //<Init PLL, set CPU clock to 72 MHz> // Optional, so I do not pos it here
  144. SWOSpeed = 6000000;
  145. *((volatile unsigned *)0xE000EDFC) = 0x01000000; // "Debug Exception and Monitor Control Register (DEMCR)"
  146. *((volatile unsigned *)0xE0042004) = 0x00000027;
  147. *((volatile unsigned *)0xE00400F0) = 0x00000002; // "Selected PIN Protocol Register": Select which protocol to use for trace output (2: SWO)
  148. SWOPrescaler = (72000000 / SWOSpeed) - 1; // SWOSpeed in Hz
  149. *((volatile unsigned *)0xE0040010) = SWOPrescaler; // "Async Clock Prescaler Register". Scale the baud rate of the asynchronous output
  150. *((volatile unsigned *)0xE0000FB0) = 0xC5ACCE55; // ITM Lock Access Register, C5ACCE55 enables more write access to Control Register 0xE00 :: 0xFFC
  151. *((volatile unsigned *)0xE0000E80) = 0x0001000D; // ITM Trace Control Register
  152. *((volatile unsigned *)0xE0000E40) = 0x0000000F; // ITM Trace Privilege Register
  153. *((volatile unsigned *)0xE0000E00) = 0x00000001; // ITM Trace Enable Register. Enabled tracing on stimulus ports. One bit per stimulus port.
  154. *((volatile unsigned *)0xE0001000) = 0x400003FE; // DWT_CTRL
  155. *((volatile unsigned *)0xE0040304) = 0x00000100; // Formatter and Flush Control Register
  156. }
  157. #endif
  158. /**
  159. * SWO_PrintChar()
  160. *
  161. * @brief
  162. * Checks if SWO is set up. If it is not, return,
  163. * to avoid program hangs if no debugger is connected.
  164. * If it is set up, print a character to the ITM_STIM register
  165. * in order to provide data for SWO.
  166. * @param c The Character to be printed.
  167. * @notes Additional checks for device specific registers can be added.
  168. */
  169. void SWO_PrintChar(char c) {
  170. #if(1)
  171. //Use CMSIS_core_DebugFunctions. See core_cm3.h
  172. ITM_SendChar (c);
  173. #else
  174. //Use Segger example. Basically same as CMSIS
  175. // Check if ITM_TCR.ITMENA is set
  176. if ((ITM_TCR & 1) == 0) {
  177. return;
  178. }
  179. // Check if stimulus port is enabled
  180. if ((ITM_ENA & 1) == 0) {
  181. return;
  182. }
  183. // Wait until STIMx FIFO is ready, then send data
  184. // while ((ITM_STIM_U8(0) & 1) == 0);
  185. while (!(ITM_STIM_U8(0) & ITM_STIM_FIFOREADY));
  186. ITM_STIM_U8(0) = c;
  187. // while ((ITM_STIM_U32(0) & 1) == 0);
  188. // ITM_STIM_U32(0) = c;
  189. #endif
  190. }
  191. /**
  192. * SWO_PrintString()
  193. *
  194. * @brief Print a string via SWO.
  195. * @param *s The string to be printed.
  196. *
  197. */
  198. void SWO_PrintString(const char *s) {
  199. // Print out characters until \0
  200. while (*s) {
  201. SWO_PrintChar(*s++);
  202. }
  203. }