SEGGER_RTT_Conf.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /*********************************************************************
  2. * SEGGER Microcontroller GmbH *
  3. * The Embedded Experts *
  4. **********************************************************************
  5. * *
  6. * (c) 1995 - 2021 SEGGER Microcontroller GmbH *
  7. * *
  8. * www.segger.com Support: support@segger.com *
  9. * *
  10. **********************************************************************
  11. * *
  12. * SEGGER SystemView * Real-time application analysis *
  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 SystemView and 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. * *
  45. * SystemView version: 3.32 *
  46. * *
  47. **********************************************************************
  48. ---------------------------END-OF-HEADER------------------------------
  49. File : SEGGER_RTT_Conf.h
  50. Purpose : Implementation of SEGGER real-time transfer (RTT) which
  51. allows real-time communication on targets which support
  52. debugger memory accesses while the CPU is running.
  53. Revision: $Rev: 24316 $
  54. */
  55. #ifndef SEGGER_RTT_CONF_H
  56. #define SEGGER_RTT_CONF_H
  57. #ifdef __IAR_SYSTEMS_ICC__
  58. #include <intrinsics.h>
  59. #endif
  60. /*********************************************************************
  61. *
  62. * Defines, configurable
  63. *
  64. **********************************************************************
  65. */
  66. //
  67. // Take in and set to correct values for Cortex-A systems with CPU cache
  68. //
  69. //#define SEGGER_RTT_CPU_CACHE_LINE_SIZE (32) // Largest cache line size (in bytes) in the current system
  70. //#define SEGGER_RTT_UNCACHED_OFF (0xFB000000) // Address alias where RTT CB and buffers can be accessed uncached
  71. //
  72. // Most common case:
  73. // Up-channel 0: RTT
  74. // Up-channel 1: SystemView
  75. //
  76. #ifndef SEGGER_RTT_MAX_NUM_UP_BUFFERS
  77. #define SEGGER_RTT_MAX_NUM_UP_BUFFERS (3) // Max. number of up-buffers (T->H) available on this target (Default: 3)
  78. #endif
  79. //
  80. // Most common case:
  81. // Down-channel 0: RTT
  82. // Down-channel 1: SystemView
  83. //
  84. #ifndef SEGGER_RTT_MAX_NUM_DOWN_BUFFERS
  85. #define SEGGER_RTT_MAX_NUM_DOWN_BUFFERS (3) // Max. number of down-buffers (H->T) available on this target (Default: 3)
  86. #endif
  87. #ifndef BUFFER_SIZE_UP
  88. #define BUFFER_SIZE_UP (1024) // Size of the buffer for terminal output of target, up to host (Default: 1k)
  89. #endif
  90. #ifndef BUFFER_SIZE_DOWN
  91. #define BUFFER_SIZE_DOWN (16) // Size of the buffer for terminal input to target from host (Usually keyboard input) (Default: 16)
  92. #endif
  93. #ifndef SEGGER_RTT_PRINTF_BUFFER_SIZE
  94. #define SEGGER_RTT_PRINTF_BUFFER_SIZE (64u) // Size of buffer for RTT printf to bulk-send chars via RTT (Default: 64)
  95. #endif
  96. #ifndef SEGGER_RTT_MODE_DEFAULT
  97. #define SEGGER_RTT_MODE_DEFAULT SEGGER_RTT_MODE_NO_BLOCK_SKIP // Mode for pre-initialized terminal channel (buffer 0)
  98. #endif
  99. /*********************************************************************
  100. *
  101. * RTT memcpy configuration
  102. *
  103. * memcpy() is good for large amounts of data,
  104. * but the overhead is big for small amounts, which are usually stored via RTT.
  105. * With SEGGER_RTT_MEMCPY_USE_BYTELOOP a simple byte loop can be used instead.
  106. *
  107. * SEGGER_RTT_MEMCPY() can be used to replace standard memcpy() in RTT functions.
  108. * This is may be required with memory access restrictions,
  109. * such as on Cortex-A devices with MMU.
  110. */
  111. #ifndef SEGGER_RTT_MEMCPY_USE_BYTELOOP
  112. #define SEGGER_RTT_MEMCPY_USE_BYTELOOP 0 // 0: Use memcpy/SEGGER_RTT_MEMCPY, 1: Use a simple byte-loop
  113. #endif
  114. //
  115. // Example definition of SEGGER_RTT_MEMCPY to external memcpy with GCC toolchains and Cortex-A targets
  116. //
  117. //#if ((defined __SES_ARM) || (defined __CROSSWORKS_ARM) || (defined __GNUC__)) && (defined (__ARM_ARCH_7A__))
  118. // #define SEGGER_RTT_MEMCPY(pDest, pSrc, NumBytes) SEGGER_memcpy((pDest), (pSrc), (NumBytes))
  119. //#endif
  120. //
  121. // Target is not allowed to perform other RTT operations while string still has not been stored completely.
  122. // Otherwise we would probably end up with a mixed string in the buffer.
  123. // If using RTT from within interrupts, multiple tasks or multi processors, define the SEGGER_RTT_LOCK() and SEGGER_RTT_UNLOCK() function here.
  124. //
  125. // SEGGER_RTT_MAX_INTERRUPT_PRIORITY can be used in the sample lock routines on Cortex-M3/4.
  126. // Make sure to mask all interrupts which can send RTT data, i.e. generate SystemView events, or cause task switches.
  127. // When high-priority interrupts must not be masked while sending RTT data, SEGGER_RTT_MAX_INTERRUPT_PRIORITY needs to be adjusted accordingly.
  128. // (Higher priority = lower priority number)
  129. // Default value for embOS: 128u
  130. // Default configuration in FreeRTOS: configMAX_SYSCALL_INTERRUPT_PRIORITY: ( configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY << (8 - configPRIO_BITS) )
  131. // In case of doubt mask all interrupts: 1 << (8 - BASEPRI_PRIO_BITS) i.e. 1 << 5 when 3 bits are implemented in NVIC
  132. // or define SEGGER_RTT_LOCK() to completely disable interrupts.
  133. //
  134. #ifndef SEGGER_RTT_MAX_INTERRUPT_PRIORITY
  135. #define SEGGER_RTT_MAX_INTERRUPT_PRIORITY (0x20) // Interrupt priority to lock on SEGGER_RTT_LOCK on Cortex-M3/4 (Default: 0x20)
  136. #endif
  137. /*********************************************************************
  138. *
  139. * RTT lock configuration for SEGGER Embedded Studio,
  140. * Rowley CrossStudio and GCC
  141. */
  142. #if ((defined(__SES_ARM) || defined(__SES_RISCV) || defined(__CROSSWORKS_ARM) || defined(__GNUC__) || defined(__clang__)) && !defined (__CC_ARM) && !defined(WIN32))
  143. #if (defined(__ARM_ARCH_6M__) || defined(__ARM_ARCH_8M_BASE__))
  144. #define SEGGER_RTT_LOCK() { \
  145. unsigned int _SEGGER_RTT__LockState; \
  146. __asm volatile ("mrs %0, primask \n\t" \
  147. "movs r1, #1 \n\t" \
  148. "msr primask, r1 \n\t" \
  149. : "=r" (_SEGGER_RTT__LockState) \
  150. : \
  151. : "r1", "cc" \
  152. );
  153. #define SEGGER_RTT_UNLOCK() __asm volatile ("msr primask, %0 \n\t" \
  154. : \
  155. : "r" (_SEGGER_RTT__LockState) \
  156. : \
  157. ); \
  158. }
  159. #elif (defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7EM__) || defined(__ARM_ARCH_8M_MAIN__))
  160. #ifndef SEGGER_RTT_MAX_INTERRUPT_PRIORITY
  161. #define SEGGER_RTT_MAX_INTERRUPT_PRIORITY (0x20)
  162. #endif
  163. #define SEGGER_RTT_LOCK() { \
  164. unsigned int _SEGGER_RTT__LockState; \
  165. __asm volatile ("mrs %0, basepri \n\t" \
  166. "mov r1, %1 \n\t" \
  167. "msr basepri, r1 \n\t" \
  168. : "=r" (_SEGGER_RTT__LockState) \
  169. : "i"(SEGGER_RTT_MAX_INTERRUPT_PRIORITY) \
  170. : "r1", "cc" \
  171. );
  172. #define SEGGER_RTT_UNLOCK() __asm volatile ("msr basepri, %0 \n\t" \
  173. : \
  174. : "r" (_SEGGER_RTT__LockState) \
  175. : \
  176. ); \
  177. }
  178. #elif (defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7R__))
  179. #define SEGGER_RTT_LOCK() { \
  180. unsigned int _SEGGER_RTT__LockState; \
  181. __asm volatile ("mrs r1, CPSR \n\t" \
  182. "mov %0, r1 \n\t" \
  183. "orr r1, r1, #0xC0 \n\t" \
  184. "msr CPSR_c, r1 \n\t" \
  185. : "=r" (_SEGGER_RTT__LockState) \
  186. : \
  187. : "r1", "cc" \
  188. );
  189. #define SEGGER_RTT_UNLOCK() __asm volatile ("mov r0, %0 \n\t" \
  190. "mrs r1, CPSR \n\t" \
  191. "bic r1, r1, #0xC0 \n\t" \
  192. "and r0, r0, #0xC0 \n\t" \
  193. "orr r1, r1, r0 \n\t" \
  194. "msr CPSR_c, r1 \n\t" \
  195. : \
  196. : "r" (_SEGGER_RTT__LockState) \
  197. : "r0", "r1", "cc" \
  198. ); \
  199. }
  200. #elif defined(__riscv) || defined(__riscv_xlen)
  201. #define SEGGER_RTT_LOCK() { \
  202. unsigned int _SEGGER_RTT__LockState; \
  203. __asm volatile ("csrr %0, mstatus \n\t" \
  204. "csrci mstatus, 8 \n\t" \
  205. "andi %0, %0, 8 \n\t" \
  206. : "=r" (_SEGGER_RTT__LockState) \
  207. : \
  208. : \
  209. );
  210. #define SEGGER_RTT_UNLOCK() __asm volatile ("csrr a1, mstatus \n\t" \
  211. "or %0, %0, a1 \n\t" \
  212. "csrs mstatus, %0 \n\t" \
  213. : \
  214. : "r" (_SEGGER_RTT__LockState) \
  215. : "a1" \
  216. ); \
  217. }
  218. #else
  219. #define SEGGER_RTT_LOCK()
  220. #define SEGGER_RTT_UNLOCK()
  221. #endif
  222. #endif
  223. /*********************************************************************
  224. *
  225. * RTT lock configuration for IAR EWARM
  226. */
  227. #ifdef __ICCARM__
  228. #if (defined (__ARM6M__) && (__CORE__ == __ARM6M__)) || \
  229. (defined (__ARM8M_BASELINE__) && (__CORE__ == __ARM8M_BASELINE__))
  230. #define SEGGER_RTT_LOCK() { \
  231. unsigned int _SEGGER_RTT__LockState; \
  232. _SEGGER_RTT__LockState = __get_PRIMASK(); \
  233. __set_PRIMASK(1);
  234. #define SEGGER_RTT_UNLOCK() __set_PRIMASK(_SEGGER_RTT__LockState); \
  235. }
  236. #elif (defined (__ARM7EM__) && (__CORE__ == __ARM7EM__)) || \
  237. (defined (__ARM7M__) && (__CORE__ == __ARM7M__)) || \
  238. (defined (__ARM8M_MAINLINE__) && (__CORE__ == __ARM8M_MAINLINE__)) || \
  239. (defined (__ARM8M_MAINLINE__) && (__CORE__ == __ARM8M_MAINLINE__))
  240. #ifndef SEGGER_RTT_MAX_INTERRUPT_PRIORITY
  241. #define SEGGER_RTT_MAX_INTERRUPT_PRIORITY (0x20)
  242. #endif
  243. #define SEGGER_RTT_LOCK() { \
  244. unsigned int _SEGGER_RTT__LockState; \
  245. _SEGGER_RTT__LockState = __get_BASEPRI(); \
  246. __set_BASEPRI(SEGGER_RTT_MAX_INTERRUPT_PRIORITY);
  247. #define SEGGER_RTT_UNLOCK() __set_BASEPRI(_SEGGER_RTT__LockState); \
  248. }
  249. #elif (defined (__ARM7A__) && (__CORE__ == __ARM7A__)) || \
  250. (defined (__ARM7R__) && (__CORE__ == __ARM7R__))
  251. #define SEGGER_RTT_LOCK() { \
  252. unsigned int _SEGGER_RTT__LockState; \
  253. __asm volatile ("mrs r1, CPSR \n\t" \
  254. "mov %0, r1 \n\t" \
  255. "orr r1, r1, #0xC0 \n\t" \
  256. "msr CPSR_c, r1 \n\t" \
  257. : "=r" (_SEGGER_RTT__LockState) \
  258. : \
  259. : "r1", "cc" \
  260. );
  261. #define SEGGER_RTT_UNLOCK() __asm volatile ("mov r0, %0 \n\t" \
  262. "mrs r1, CPSR \n\t" \
  263. "bic r1, r1, #0xC0 \n\t" \
  264. "and r0, r0, #0xC0 \n\t" \
  265. "orr r1, r1, r0 \n\t" \
  266. "msr CPSR_c, r1 \n\t" \
  267. : \
  268. : "r" (_SEGGER_RTT__LockState) \
  269. : "r0", "r1", "cc" \
  270. ); \
  271. }
  272. #endif
  273. #endif
  274. /*********************************************************************
  275. *
  276. * RTT lock configuration for IAR RX
  277. */
  278. #ifdef __ICCRX__
  279. #define SEGGER_RTT_LOCK() { \
  280. unsigned long _SEGGER_RTT__LockState; \
  281. _SEGGER_RTT__LockState = __get_interrupt_state(); \
  282. __disable_interrupt();
  283. #define SEGGER_RTT_UNLOCK() __set_interrupt_state(_SEGGER_RTT__LockState); \
  284. }
  285. #endif
  286. /*********************************************************************
  287. *
  288. * RTT lock configuration for IAR RL78
  289. */
  290. #ifdef __ICCRL78__
  291. #define SEGGER_RTT_LOCK() { \
  292. __istate_t _SEGGER_RTT__LockState; \
  293. _SEGGER_RTT__LockState = __get_interrupt_state(); \
  294. __disable_interrupt();
  295. #define SEGGER_RTT_UNLOCK() __set_interrupt_state(_SEGGER_RTT__LockState); \
  296. }
  297. #endif
  298. /*********************************************************************
  299. *
  300. * RTT lock configuration for KEIL ARM
  301. */
  302. #ifdef __CC_ARM
  303. #if (defined __TARGET_ARCH_6S_M)
  304. #define SEGGER_RTT_LOCK() { \
  305. unsigned int _SEGGER_RTT__LockState; \
  306. register unsigned char _SEGGER_RTT__PRIMASK __asm( "primask"); \
  307. _SEGGER_RTT__LockState = _SEGGER_RTT__PRIMASK; \
  308. _SEGGER_RTT__PRIMASK = 1u; \
  309. __schedule_barrier();
  310. #define SEGGER_RTT_UNLOCK() _SEGGER_RTT__PRIMASK = _SEGGER_RTT__LockState; \
  311. __schedule_barrier(); \
  312. }
  313. #elif (defined(__TARGET_ARCH_7_M) || defined(__TARGET_ARCH_7E_M))
  314. #ifndef SEGGER_RTT_MAX_INTERRUPT_PRIORITY
  315. #define SEGGER_RTT_MAX_INTERRUPT_PRIORITY (0x20)
  316. #endif
  317. #define SEGGER_RTT_LOCK() { \
  318. unsigned int _SEGGER_RTT__LockState; \
  319. register unsigned char BASEPRI __asm( "basepri"); \
  320. _SEGGER_RTT__LockState = BASEPRI; \
  321. BASEPRI = SEGGER_RTT_MAX_INTERRUPT_PRIORITY; \
  322. __schedule_barrier();
  323. #define SEGGER_RTT_UNLOCK() BASEPRI = _SEGGER_RTT__LockState; \
  324. __schedule_barrier(); \
  325. }
  326. #endif
  327. #endif
  328. /*********************************************************************
  329. *
  330. * RTT lock configuration for TI ARM
  331. */
  332. #ifdef __TI_ARM__
  333. #if defined (__TI_ARM_V6M0__)
  334. #define SEGGER_RTT_LOCK() { \
  335. unsigned int _SEGGER_RTT__LockState; \
  336. _SEGGER_RTT__LockState = __get_PRIMASK(); \
  337. __set_PRIMASK(1);
  338. #define SEGGER_RTT_UNLOCK() __set_PRIMASK(_SEGGER_RTT__LockState); \
  339. }
  340. #elif (defined (__TI_ARM_V7M3__) || defined (__TI_ARM_V7M4__))
  341. #ifndef SEGGER_RTT_MAX_INTERRUPT_PRIORITY
  342. #define SEGGER_RTT_MAX_INTERRUPT_PRIORITY (0x20)
  343. #endif
  344. #define SEGGER_RTT_LOCK() { \
  345. unsigned int _SEGGER_RTT__LockState; \
  346. _SEGGER_RTT__LockState = _set_interrupt_priority(SEGGER_RTT_MAX_INTERRUPT_PRIORITY);
  347. #define SEGGER_RTT_UNLOCK() _set_interrupt_priority(_SEGGER_RTT__LockState); \
  348. }
  349. #endif
  350. #endif
  351. /*********************************************************************
  352. *
  353. * RTT lock configuration for CCRX
  354. */
  355. #ifdef __RX
  356. #include <machine.h>
  357. #define SEGGER_RTT_LOCK() { \
  358. unsigned long _SEGGER_RTT__LockState; \
  359. _SEGGER_RTT__LockState = get_psw() & 0x010000; \
  360. clrpsw_i();
  361. #define SEGGER_RTT_UNLOCK() set_psw(get_psw() | _SEGGER_RTT__LockState); \
  362. }
  363. #endif
  364. /*********************************************************************
  365. *
  366. * RTT lock configuration for embOS Simulation on Windows
  367. * (Can also be used for generic RTT locking with embOS)
  368. */
  369. #if defined(WIN32) || defined(SEGGER_RTT_LOCK_EMBOS)
  370. void OS_SIM_EnterCriticalSection(void);
  371. void OS_SIM_LeaveCriticalSection(void);
  372. #define SEGGER_RTT_LOCK() { \
  373. OS_SIM_EnterCriticalSection();
  374. #define SEGGER_RTT_UNLOCK() OS_SIM_LeaveCriticalSection(); \
  375. }
  376. #endif
  377. /*********************************************************************
  378. *
  379. * RTT lock configuration fallback
  380. */
  381. #ifndef SEGGER_RTT_LOCK
  382. #define SEGGER_RTT_LOCK() // Lock RTT (nestable) (i.e. disable interrupts)
  383. #endif
  384. #ifndef SEGGER_RTT_UNLOCK
  385. #define SEGGER_RTT_UNLOCK() // Unlock RTT (nestable) (i.e. enable previous interrupt lock state)
  386. #endif
  387. #endif
  388. /*************************** End of file ****************************/