SEGGER_RTT_Conf.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*********************************************************************
  2. * SEGGER Microcontroller GmbH *
  3. * The Embedded Experts *
  4. **********************************************************************
  5. * *
  6. * (c) 1995 - 2019 SEGGER Microcontroller GmbH *
  7. * *
  8. * www.segger.com Support: support@segger.com *
  9. * *
  10. **********************************************************************
  11. * *
  12. * SEGGER RTT * Real Time Transfer for embedded targets *
  13. * *
  14. **********************************************************************
  15. * *
  16. * All rights reserved. *
  17. * *
  18. * SEGGER strongly recommends to not make any changes *
  19. * to or modify the source code of this software in order to stay *
  20. * compatible with the RTT protocol and J-Link. *
  21. * *
  22. * Redistribution and use in source and binary forms, with or *
  23. * without modification, are permitted provided that the following *
  24. * condition is met: *
  25. * *
  26. * o Redistributions of source code must retain the above copyright *
  27. * notice, this condition and the following disclaimer. *
  28. * *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND *
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, *
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF *
  32. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE *
  33. * DISCLAIMED. IN NO EVENT SHALL SEGGER Microcontroller BE LIABLE FOR *
  34. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR *
  35. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT *
  36. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; *
  37. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
  38. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
  39. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE *
  40. * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH *
  41. * DAMAGE. *
  42. * *
  43. **********************************************************************
  44. ---------------------------END-OF-HEADER------------------------------
  45. File : SEGGER_RTT_Conf.h
  46. Purpose : Implementation of SEGGER real-time transfer (RTT) which
  47. allows real-time communication on targets which support
  48. debugger memory accesses while the CPU is running.
  49. Revision: $Rev: 18601 $
  50. */
  51. #ifndef SEGGER_RTT_CONF_H
  52. #define SEGGER_RTT_CONF_H
  53. #ifdef __IAR_SYSTEMS_ICC__
  54. #include <intrinsics.h>
  55. #endif
  56. /*********************************************************************
  57. *
  58. * Defines, configurable
  59. *
  60. **********************************************************************
  61. */
  62. #ifndef SEGGER_RTT_MAX_NUM_UP_BUFFERS
  63. #define SEGGER_RTT_MAX_NUM_UP_BUFFERS (3) // Max. number of up-buffers (T->H) available on this target (Default: 3)
  64. #endif
  65. #ifndef SEGGER_RTT_MAX_NUM_DOWN_BUFFERS
  66. #define SEGGER_RTT_MAX_NUM_DOWN_BUFFERS (3) // Max. number of down-buffers (H->T) available on this target (Default: 3)
  67. #endif
  68. #ifndef BUFFER_SIZE_UP
  69. #define BUFFER_SIZE_UP (1024) // Size of the buffer for terminal output of target, up to host (Default: 1k)
  70. #endif
  71. #ifndef BUFFER_SIZE_DOWN
  72. #define BUFFER_SIZE_DOWN (16) // Size of the buffer for terminal input to target from host (Usually keyboard input) (Default: 16)
  73. #endif
  74. #ifndef SEGGER_RTT_PRINTF_BUFFER_SIZE
  75. #define SEGGER_RTT_PRINTF_BUFFER_SIZE (64u) // Size of buffer for RTT printf to bulk-send chars via RTT (Default: 64)
  76. #endif
  77. #ifndef SEGGER_RTT_MODE_DEFAULT
  78. #define SEGGER_RTT_MODE_DEFAULT SEGGER_RTT_MODE_NO_BLOCK_SKIP // Mode for pre-initialized terminal channel (buffer 0)
  79. #endif
  80. /*********************************************************************
  81. *
  82. * RTT memcpy configuration
  83. *
  84. * memcpy() is good for large amounts of data,
  85. * but the overhead is big for small amounts, which are usually stored via RTT.
  86. * With SEGGER_RTT_MEMCPY_USE_BYTELOOP a simple byte loop can be used instead.
  87. *
  88. * SEGGER_RTT_MEMCPY() can be used to replace standard memcpy() in RTT functions.
  89. * This is may be required with memory access restrictions,
  90. * such as on Cortex-A devices with MMU.
  91. */
  92. #ifndef SEGGER_RTT_MEMCPY_USE_BYTELOOP
  93. #define SEGGER_RTT_MEMCPY_USE_BYTELOOP 0 // 0: Use memcpy/SEGGER_RTT_MEMCPY, 1: Use a simple byte-loop
  94. #endif
  95. //
  96. // Example definition of SEGGER_RTT_MEMCPY to external memcpy with GCC toolchains and Cortex-A targets
  97. //
  98. //#if ((defined __SES_ARM) || (defined __CROSSWORKS_ARM) || (defined __GNUC__)) && (defined (__ARM_ARCH_7A__))
  99. // #define SEGGER_RTT_MEMCPY(pDest, pSrc, NumBytes) SEGGER_memcpy((pDest), (pSrc), (NumBytes))
  100. //#endif
  101. //
  102. // Target is not allowed to perform other RTT operations while string still has not been stored completely.
  103. // Otherwise we would probably end up with a mixed string in the buffer.
  104. // If using RTT from within interrupts, multiple tasks or multi processors, define the SEGGER_RTT_LOCK() and SEGGER_RTT_UNLOCK() function here.
  105. //
  106. // SEGGER_RTT_MAX_INTERRUPT_PRIORITY can be used in the sample lock routines on Cortex-M3/4.
  107. // Make sure to mask all interrupts which can send RTT data, i.e. generate SystemView events, or cause task switches.
  108. // When high-priority interrupts must not be masked while sending RTT data, SEGGER_RTT_MAX_INTERRUPT_PRIORITY needs to be adjusted accordingly.
  109. // (Higher priority = lower priority number)
  110. // Default value for embOS: 128u
  111. // Default configuration in FreeRTOS: configMAX_SYSCALL_INTERRUPT_PRIORITY: ( configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY << (8 - configPRIO_BITS) )
  112. // In case of doubt mask all interrupts: 1 << (8 - BASEPRI_PRIO_BITS) i.e. 1 << 5 when 3 bits are implemented in NVIC
  113. // or define SEGGER_RTT_LOCK() to completely disable interrupts.
  114. //
  115. #ifndef SEGGER_RTT_MAX_INTERRUPT_PRIORITY
  116. #define SEGGER_RTT_MAX_INTERRUPT_PRIORITY (0x20) // Interrupt priority to lock on SEGGER_RTT_LOCK on Cortex-M3/4 (Default: 0x20)
  117. #endif
  118. /*********************************************************************
  119. *
  120. * RTT lock configuration for SEGGER Embedded Studio,
  121. * Rowley CrossStudio and GCC
  122. */
  123. #if ((defined(__SES_ARM) || defined(__SES_RISCV) || defined(__CROSSWORKS_ARM) || defined(__GNUC__) || defined(__clang__)) && !defined (__CC_ARM) && !defined(WIN32))
  124. #if (defined(__ARM_ARCH_6M__) || defined(__ARM_ARCH_8M_BASE__))
  125. #define SEGGER_RTT_LOCK() { \
  126. unsigned int LockState; \
  127. __asm volatile ("mrs %0, primask \n\t" \
  128. "movs r1, $1 \n\t" \
  129. "msr primask, r1 \n\t" \
  130. : "=r" (LockState) \
  131. : \
  132. : "r1" \
  133. );
  134. #define SEGGER_RTT_UNLOCK() __asm volatile ("msr primask, %0 \n\t" \
  135. : \
  136. : "r" (LockState) \
  137. : \
  138. ); \
  139. }
  140. #elif (defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7EM__) || defined(__ARM_ARCH_8M_MAIN__))
  141. #ifndef SEGGER_RTT_MAX_INTERRUPT_PRIORITY
  142. #define SEGGER_RTT_MAX_INTERRUPT_PRIORITY (0x20)
  143. #endif
  144. #define SEGGER_RTT_LOCK() { \
  145. unsigned int LockState; \
  146. __asm volatile ("mrs %0, basepri \n\t" \
  147. "mov r1, %1 \n\t" \
  148. "msr basepri, r1 \n\t" \
  149. : "=r" (LockState) \
  150. : "i"(SEGGER_RTT_MAX_INTERRUPT_PRIORITY) \
  151. : "r1" \
  152. );
  153. #define SEGGER_RTT_UNLOCK() __asm volatile ("msr basepri, %0 \n\t" \
  154. : \
  155. : "r" (LockState) \
  156. : \
  157. ); \
  158. }
  159. #elif defined(__ARM_ARCH_7A__)
  160. #define SEGGER_RTT_LOCK() { \
  161. unsigned int LockState; \
  162. __asm volatile ("mrs r1, CPSR \n\t" \
  163. "mov %0, r1 \n\t" \
  164. "orr r1, r1, #0xC0 \n\t" \
  165. "msr CPSR_c, r1 \n\t" \
  166. : "=r" (LockState) \
  167. : \
  168. : "r1" \
  169. );
  170. #define SEGGER_RTT_UNLOCK() __asm volatile ("mov r0, %0 \n\t" \
  171. "mrs r1, CPSR \n\t" \
  172. "bic r1, r1, #0xC0 \n\t" \
  173. "and r0, r0, #0xC0 \n\t" \
  174. "orr r1, r1, r0 \n\t" \
  175. "msr CPSR_c, r1 \n\t" \
  176. : \
  177. : "r" (LockState) \
  178. : "r0", "r1" \
  179. ); \
  180. }
  181. #elif defined(__riscv) || defined(__riscv_xlen)
  182. #define SEGGER_RTT_LOCK() { \
  183. unsigned int LockState; \
  184. __asm volatile ("csrr %0, mstatus \n\t" \
  185. "csrci mstatus, 8 \n\t" \
  186. "andi %0, %0, 8 \n\t" \
  187. : "=r" (LockState) \
  188. : \
  189. : \
  190. );
  191. #define SEGGER_RTT_UNLOCK() __asm volatile ("csrr a1, mstatus \n\t" \
  192. "or %0, %0, a1 \n\t" \
  193. "csrs mstatus, %0 \n\t" \
  194. : \
  195. : "r" (LockState) \
  196. : "a1" \
  197. ); \
  198. }
  199. #else
  200. #define SEGGER_RTT_LOCK()
  201. #define SEGGER_RTT_UNLOCK()
  202. #endif
  203. #endif
  204. /*********************************************************************
  205. *
  206. * RTT lock configuration for IAR EWARM
  207. */
  208. #ifdef __ICCARM__
  209. #if (defined (__ARM6M__) && (__CORE__ == __ARM6M__)) || \
  210. (defined (__ARM8M_BASELINE__) && (__CORE__ == __ARM8M_BASELINE__))
  211. #define SEGGER_RTT_LOCK() { \
  212. unsigned int LockState; \
  213. LockState = __get_PRIMASK(); \
  214. __set_PRIMASK(1);
  215. #define SEGGER_RTT_UNLOCK() __set_PRIMASK(LockState); \
  216. }
  217. #elif (defined (__ARM7EM__) && (__CORE__ == __ARM7EM__)) || \
  218. (defined (__ARM7M__) && (__CORE__ == __ARM7M__)) || \
  219. (defined (__ARM8M_MAINLINE__) && (__CORE__ == __ARM8M_MAINLINE__)) || \
  220. (defined (__ARM8M_MAINLINE__) && (__CORE__ == __ARM8M_MAINLINE__))
  221. #ifndef SEGGER_RTT_MAX_INTERRUPT_PRIORITY
  222. #define SEGGER_RTT_MAX_INTERRUPT_PRIORITY (0x20)
  223. #endif
  224. #define SEGGER_RTT_LOCK() { \
  225. unsigned int LockState; \
  226. LockState = __get_BASEPRI(); \
  227. __set_BASEPRI(SEGGER_RTT_MAX_INTERRUPT_PRIORITY);
  228. #define SEGGER_RTT_UNLOCK() __set_BASEPRI(LockState); \
  229. }
  230. #endif
  231. #endif
  232. /*********************************************************************
  233. *
  234. * RTT lock configuration for IAR RX
  235. */
  236. #ifdef __ICCRX__
  237. #define SEGGER_RTT_LOCK() { \
  238. unsigned long LockState; \
  239. LockState = __get_interrupt_state(); \
  240. __disable_interrupt();
  241. #define SEGGER_RTT_UNLOCK() __set_interrupt_state(LockState); \
  242. }
  243. #endif
  244. /*********************************************************************
  245. *
  246. * RTT lock configuration for IAR RL78
  247. */
  248. #ifdef __ICCRL78__
  249. #define SEGGER_RTT_LOCK() { \
  250. __istate_t LockState; \
  251. LockState = __get_interrupt_state(); \
  252. __disable_interrupt();
  253. #define SEGGER_RTT_UNLOCK() __set_interrupt_state(LockState); \
  254. }
  255. #endif
  256. /*********************************************************************
  257. *
  258. * RTT lock configuration for KEIL ARM
  259. */
  260. #ifdef __CC_ARM
  261. #if (defined __TARGET_ARCH_6S_M)
  262. #define SEGGER_RTT_LOCK() { \
  263. unsigned int LockState; \
  264. register unsigned char PRIMASK __asm( "primask"); \
  265. LockState = PRIMASK; \
  266. PRIMASK = 1u; \
  267. __schedule_barrier();
  268. #define SEGGER_RTT_UNLOCK() PRIMASK = LockState; \
  269. __schedule_barrier(); \
  270. }
  271. #elif (defined(__TARGET_ARCH_7_M) || defined(__TARGET_ARCH_7E_M))
  272. #ifndef SEGGER_RTT_MAX_INTERRUPT_PRIORITY
  273. #define SEGGER_RTT_MAX_INTERRUPT_PRIORITY (0x20)
  274. #endif
  275. #define SEGGER_RTT_LOCK() { \
  276. unsigned int LockState; \
  277. register unsigned char BASEPRI __asm( "basepri"); \
  278. LockState = BASEPRI; \
  279. BASEPRI = SEGGER_RTT_MAX_INTERRUPT_PRIORITY; \
  280. __schedule_barrier();
  281. #define SEGGER_RTT_UNLOCK() BASEPRI = LockState; \
  282. __schedule_barrier(); \
  283. }
  284. #endif
  285. #endif
  286. /*********************************************************************
  287. *
  288. * RTT lock configuration for TI ARM
  289. */
  290. #ifdef __TI_ARM__
  291. #if defined (__TI_ARM_V6M0__)
  292. #define SEGGER_RTT_LOCK() { \
  293. unsigned int LockState; \
  294. LockState = __get_PRIMASK(); \
  295. __set_PRIMASK(1);
  296. #define SEGGER_RTT_UNLOCK() __set_PRIMASK(LockState); \
  297. }
  298. #elif (defined (__TI_ARM_V7M3__) || defined (__TI_ARM_V7M4__))
  299. #ifndef SEGGER_RTT_MAX_INTERRUPT_PRIORITY
  300. #define SEGGER_RTT_MAX_INTERRUPT_PRIORITY (0x20)
  301. #endif
  302. #define SEGGER_RTT_LOCK() { \
  303. unsigned int LockState; \
  304. LockState = _set_interrupt_priority(SEGGER_RTT_MAX_INTERRUPT_PRIORITY);
  305. #define SEGGER_RTT_UNLOCK() _set_interrupt_priority(LockState); \
  306. }
  307. #endif
  308. #endif
  309. /*********************************************************************
  310. *
  311. * RTT lock configuration for CCRX
  312. */
  313. #ifdef __RX
  314. #define SEGGER_RTT_LOCK() { \
  315. unsigned long LockState; \
  316. LockState = get_psw() & 0x010000; \
  317. clrpsw_i();
  318. #define SEGGER_RTT_UNLOCK() set_psw(get_psw() | LockState); \
  319. }
  320. #endif
  321. /*********************************************************************
  322. *
  323. * RTT lock configuration for embOS Simulation on Windows
  324. * (Can also be used for generic RTT locking with embOS)
  325. */
  326. #if defined(WIN32) || defined(SEGGER_RTT_LOCK_EMBOS)
  327. void OS_SIM_EnterCriticalSection(void);
  328. void OS_SIM_LeaveCriticalSection(void);
  329. #define SEGGER_RTT_LOCK() { \
  330. OS_SIM_EnterCriticalSection();
  331. #define SEGGER_RTT_UNLOCK() OS_SIM_LeaveCriticalSection(); \
  332. }
  333. #endif
  334. /*********************************************************************
  335. *
  336. * RTT lock configuration fallback
  337. */
  338. #ifndef SEGGER_RTT_LOCK
  339. #define SEGGER_RTT_LOCK() // Lock RTT (nestable) (i.e. disable interrupts)
  340. #endif
  341. #ifndef SEGGER_RTT_UNLOCK
  342. #define SEGGER_RTT_UNLOCK() // Unlock RTT (nestable) (i.e. enable previous interrupt lock state)
  343. #endif
  344. #endif
  345. /*************************** End of file ****************************/