SEGGER_RTT_Conf.h 25 KB

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