1 /*********************************************************************
2 *                SEGGER Microcontroller GmbH & Co. KG                *
3 *                        The Embedded Experts                        *
4 **********************************************************************
5 *                                                                    *
6 *       (c) 2015 - 2017  SEGGER Microcontroller GmbH & Co. KG        *
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 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 * conditions are met:                                                *
25 *                                                                    *
26 * o Redistributions of source code must retain the above copyright   *
27 *   notice, this list of conditions and the following disclaimer.    *
28 *                                                                    *
29 * o Redistributions in binary form must reproduce the above          *
30 *   copyright notice, this list of conditions and the following      *
31 *   disclaimer in the documentation and/or other materials provided  *
32 *   with the distribution.                                           *
33 *                                                                    *
34 * o Neither the name of SEGGER Microcontroller GmbH & Co. KG         *
35 *   nor the names of its contributors may be used to endorse or      *
36 *   promote products derived from this software without specific     *
37 *   prior written permission.                                        *
38 *                                                                    *
39 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND             *
40 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,        *
41 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF           *
42 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE           *
43 * DISCLAIMED. IN NO EVENT SHALL SEGGER Microcontroller BE LIABLE FOR *
44 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR           *
45 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT  *
46 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;    *
47 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF      *
48 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT          *
49 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE  *
50 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH   *
51 * DAMAGE.                                                            *
52 *                                                                    *
53 **********************************************************************
54 *                                                                    *
55 *       SystemView version: V2.42                                    *
56 *                                                                    *
57 **********************************************************************
58 ---------------------------END-OF-HEADER------------------------------
59 File    : SEGGER_RTT_Conf.h
60 Purpose : Implementation of SEGGER real-time transfer (RTT) which
61           allows real-time communication on targets which support
62           debugger memory accesses while the CPU is running.
63 Revision: $Rev: 5626 $
64 
65 */
66 
67 #ifndef SEGGER_RTT_CONF_H
68 #define SEGGER_RTT_CONF_H
69 
70 #ifdef __IAR_SYSTEMS_ICC__
71   #include <intrinsics.h>
72 #endif
73 
74 /*********************************************************************
75 *
76 *       Defines, configurable
77 *
78 **********************************************************************
79 */
80 
81 #define SEGGER_RTT_MAX_NUM_UP_BUFFERS             (3)     // Max. number of up-buffers (T->H) available on this target    (Default: 3)
82 #define SEGGER_RTT_MAX_NUM_DOWN_BUFFERS           (3)     // Max. number of down-buffers (H->T) available on this target  (Default: 3)
83 
84 #define BUFFER_SIZE_UP                            (1024)  // Size of the buffer for terminal output of target, up to host (Default: 1k)
85 #define BUFFER_SIZE_DOWN                          (16)    // Size of the buffer for terminal input to target from host (Usually keyboard input) (Default: 16)
86 
87 #define SEGGER_RTT_PRINTF_BUFFER_SIZE             (64u)    // Size of buffer for RTT printf to bulk-send chars via RTT     (Default: 64)
88 
89 #define SEGGER_RTT_MODE_DEFAULT                   SEGGER_RTT_MODE_NO_BLOCK_SKIP // Mode for pre-initialized terminal channel (buffer 0)
90 
91 //
92 // Target is not allowed to perform other RTT operations while string still has not been stored completely.
93 // Otherwise we would probably end up with a mixed string in the buffer.
94 // If using  RTT from within interrupts, multiple tasks or multi processors, define the SEGGER_RTT_LOCK() and SEGGER_RTT_UNLOCK() function here.
95 //
96 // SEGGER_RTT_MAX_INTERRUPT_PRIORITY can be used in the sample lock routines on Cortex-M3/4.
97 // Make sure to mask all interrupts which can send RTT data, i.e. generate SystemView events, or cause task switches.
98 // When high-priority interrupts must not be masked while sending RTT data, SEGGER_RTT_MAX_INTERRUPT_PRIORITY needs to be adjusted accordingly.
99 // (Higher priority = lower priority number)
100 // Default value for embOS: 128u
101 // Default configuration in FreeRTOS: configMAX_SYSCALL_INTERRUPT_PRIORITY: ( configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY << (8 - configPRIO_BITS) )
102 // In case of doubt mask all interrupts: 1 << (8 - BASEPRI_PRIO_BITS) i.e. 1 << 5 when 3 bits are implemented in NVIC
103 // or define SEGGER_RTT_LOCK() to completely disable interrupts.
104 //
105 
106 #define SEGGER_RTT_MAX_INTERRUPT_PRIORITY         (0x20)   // Interrupt priority to lock on SEGGER_RTT_LOCK on Cortex-M3/4 (Default: 0x20)
107 
108 /*********************************************************************
109 *
110 *       RTT lock configuration for SEGGER Embedded Studio,
111 *       Rowley CrossStudio and GCC
112 */
113 #if (defined __SES_ARM) || (defined __CROSSWORKS_ARM) || (defined __GNUC__)
114   #ifdef __ARM_ARCH_6M__
115     #define SEGGER_RTT_LOCK()   {                                                                   \
116                                     unsigned int LockState;                                         \
117                                   __asm volatile ("mrs   %0, primask  \n\t"                         \
118                                                   "mov   r1, $1     \n\t"                           \
119                                                   "msr   primask, r1  \n\t"                         \
120                                                   : "=r" (LockState)                                \
121                                                   :                                                 \
122                                                   : "r1"                                            \
123                                                   );
124 
125     #define SEGGER_RTT_UNLOCK()   __asm volatile ("msr   primask, %0  \n\t"                         \
126                                                   :                                                 \
127                                                   : "r" (LockState)                                 \
128                                                   :                                                 \
129                                                   );                                                \
130                                 }
131 
132   #elif (defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7EM__))
133     #ifndef   SEGGER_RTT_MAX_INTERRUPT_PRIORITY
134       #define SEGGER_RTT_MAX_INTERRUPT_PRIORITY   (0x20)
135     #endif
136     #define SEGGER_RTT_LOCK()   {                                                                   \
137                                     unsigned int LockState;                                         \
138                                   __asm volatile ("mrs   %0, basepri  \n\t"                         \
139                                                   "mov   r1, %1       \n\t"                         \
140                                                   "msr   basepri, r1  \n\t"                         \
141                                                   : "=r" (LockState)                                \
142                                                   : "i"(SEGGER_RTT_MAX_INTERRUPT_PRIORITY)          \
143                                                   : "r1"                                            \
144                                                   );
145 
146     #define SEGGER_RTT_UNLOCK()   __asm volatile ("msr   basepri, %0  \n\t"                         \
147                                                   :                                                 \
148                                                   : "r" (LockState)                                 \
149                                                   :                                                 \
150                                                   );                                                \
151                                 }
152 
153   #elif defined(__ARM_ARCH_7A__)
154     #define SEGGER_RTT_LOCK() {                                                \
155                                  unsigned int LockState;                       \
156                                  __asm volatile ("mrs r1, CPSR \n\t"           \
157                                                  "mov %0, r1 \n\t"             \
158                                                  "orr r1, r1, #0xC0 \n\t"      \
159                                                  "msr CPSR_c, r1 \n\t"         \
160                                                  : "=r" (LockState)            \
161                                                  :                             \
162                                                  : "r1"                        \
163                                                  );
164 
165     #define SEGGER_RTT_UNLOCK() __asm volatile ("mov r0, %0 \n\t"              \
166                                                 "mrs r1, CPSR \n\t"            \
167                                                 "bic r1, r1, #0xC0 \n\t"       \
168                                                 "and r0, r0, #0xC0 \n\t"       \
169                                                 "orr r1, r1, r0 \n\t"          \
170                                                 "msr CPSR_c, r1 \n\t"          \
171                                                 :                              \
172                                                 : "r" (LockState)              \
173                                                 : "r0", "r1"                   \
174                                                 );                             \
175                             }
176 #else
177     #define SEGGER_RTT_LOCK()
178     #define SEGGER_RTT_UNLOCK()
179   #endif
180 #endif
181 
182 /*********************************************************************
183 *
184 *       RTT lock configuration for IAR EWARM
185 */
186 #ifdef __ICCARM__
187   #if (defined (__ARM6M__) && (__CORE__ == __ARM6M__))
188     #define SEGGER_RTT_LOCK()   {                                                                   \
189                                   unsigned int LockState;                                           \
190                                   LockState = __get_PRIMASK();                                      \
191                                   __set_PRIMASK(1);
192 
193     #define SEGGER_RTT_UNLOCK()   __set_PRIMASK(LockState);                                         \
194                                 }
195   #elif ((defined (__ARM7EM__) && (__CORE__ == __ARM7EM__)) || (defined (__ARM7M__) && (__CORE__ == __ARM7M__)))
196     #ifndef   SEGGER_RTT_MAX_INTERRUPT_PRIORITY
197       #define SEGGER_RTT_MAX_INTERRUPT_PRIORITY   (0x20)
198     #endif
199     #define SEGGER_RTT_LOCK()   {                                                                   \
200                                   unsigned int LockState;                                           \
201                                   LockState = __get_BASEPRI();                                      \
202                                   __set_BASEPRI(SEGGER_RTT_MAX_INTERRUPT_PRIORITY);
203 
204     #define SEGGER_RTT_UNLOCK()   __set_BASEPRI(LockState);                                         \
205                                 }
206   #endif
207 #endif
208 
209 /*********************************************************************
210 *
211 *       RTT lock configuration for IAR RX
212 */
213 #ifdef __ICCRX__
214   #define SEGGER_RTT_LOCK()   {                                                                     \
215                                 unsigned long LockState;                                            \
216                                 LockState = __get_interrupt_state();                                \
217                                 __disable_interrupt();
218 
219   #define SEGGER_RTT_UNLOCK()   __set_interrupt_state(LockState);                                   \
220                               }
221 #endif
222 
223 /*********************************************************************
224 *
225 *       RTT lock configuration for KEIL ARM
226 */
227 #ifdef __CC_ARM
228   #if (defined __TARGET_ARCH_6S_M)
229     #define SEGGER_RTT_LOCK()   {                                                                   \
230                                   unsigned int LockState;                                           \
231                                   register unsigned char PRIMASK __asm( "primask");                 \
232                                   LockState = PRIMASK;                                              \
233                                   PRIMASK = 1u;                                                     \
234                                   __schedule_barrier();
235 
236     #define SEGGER_RTT_UNLOCK()   PRIMASK = LockState;                                              \
237                                   __schedule_barrier();                                             \
238                                 }
239   #elif (defined(__TARGET_ARCH_7_M) || defined(__TARGET_ARCH_7E_M))
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 LockState;                                           \
245                                   register unsigned char BASEPRI __asm( "basepri");                 \
246                                   LockState = BASEPRI;                                              \
247                                   BASEPRI = SEGGER_RTT_MAX_INTERRUPT_PRIORITY;                      \
248                                   __schedule_barrier();
249 
250     #define SEGGER_RTT_UNLOCK()   BASEPRI = LockState;                                              \
251                                   __schedule_barrier();                                             \
252                                 }
253   #endif
254 #endif
255 
256 /*********************************************************************
257 *
258 *       RTT lock configuration for TI ARM
259 */
260 #ifdef __TI_ARM__
261   #if defined (__TI_ARM_V6M0__)
262     #define SEGGER_RTT_LOCK()   {                                                                   \
263                                   unsigned int LockState;                                           \
264                                   LockState = __get_PRIMASK();                                      \
265                                   __set_PRIMASK(1);
266 
267     #define SEGGER_RTT_UNLOCK()   __set_PRIMASK(LockState);                                         \
268                                 }
269   #elif (defined (__TI_ARM_V7M3__) || defined (__TI_ARM_V7M4__))
270     #ifndef   SEGGER_RTT_MAX_INTERRUPT_PRIORITY
271       #define SEGGER_RTT_MAX_INTERRUPT_PRIORITY   (0x20)
272     #endif
273     #define SEGGER_RTT_LOCK()   {                                                                   \
274                                   unsigned int LockState;                                           \
275                                   LockState = OS_GetBASEPRI();                                      \
276                                   OS_SetBASEPRI(SEGGER_RTT_MAX_INTERRUPT_PRIORITY);
277 
278     #define SEGGER_RTT_UNLOCK()   OS_SetBASEPRI(LockState);                                         \
279                                 }
280   #endif
281 #endif
282 
283 /*********************************************************************
284 *
285 *       RTT lock configuration fallback
286 */
287 #ifndef   SEGGER_RTT_LOCK
288   void SEGGER_SYSVIEW_X_RTT_Lock(void);
289   #define SEGGER_RTT_LOCK()                SEGGER_SYSVIEW_X_RTT_Lock() // Lock RTT (nestable)   (i.e. disable interrupts)
290 #endif
291 
292 #ifndef   SEGGER_RTT_UNLOCK
293   void SEGGER_SYSVIEW_X_RTT_Unlock(void);
294   #define SEGGER_RTT_UNLOCK()              SEGGER_SYSVIEW_X_RTT_Unlock() // Unlock RTT (nestable) (i.e. enable previous interrupt lock state)
295 #endif
296 
297 #endif
298 /*************************** End of file ****************************/
299