xref: /btstack/3rd-party/segger-rtt/SEGGER_RTT_ASM_ARMv7M.S (revision 779af47be2a26fca484358b6dbf3172ffb019f79)
1*779af47bSMatthias Ringwald/*********************************************************************
2*779af47bSMatthias Ringwald*                   (c) SEGGER Microcontroller GmbH                  *
3*779af47bSMatthias Ringwald*                        The Embedded Experts                        *
4*779af47bSMatthias Ringwald*                           www.segger.com                           *
5*779af47bSMatthias Ringwald**********************************************************************
6*779af47bSMatthias Ringwald
7*779af47bSMatthias Ringwald-------------------------- END-OF-HEADER -----------------------------
8*779af47bSMatthias Ringwald
9*779af47bSMatthias RingwaldFile    : SEGGER_RTT_ASM_ARMv7M.S
10*779af47bSMatthias RingwaldPurpose : Assembler implementation of RTT functions for ARMv7M
11*779af47bSMatthias Ringwald
12*779af47bSMatthias RingwaldAdditional information:
13*779af47bSMatthias Ringwald  This module is written to be assembler-independent and works with
14*779af47bSMatthias Ringwald  GCC and clang (Embedded Studio) and IAR.
15*779af47bSMatthias Ringwald*/
16*779af47bSMatthias Ringwald
17*779af47bSMatthias Ringwald#define SEGGER_RTT_ASM      // Used to control processed input from header file
18*779af47bSMatthias Ringwald#include "SEGGER_RTT.h"
19*779af47bSMatthias Ringwald
20*779af47bSMatthias Ringwald/*********************************************************************
21*779af47bSMatthias Ringwald*
22*779af47bSMatthias Ringwald*       Defines, fixed
23*779af47bSMatthias Ringwald*
24*779af47bSMatthias Ringwald**********************************************************************
25*779af47bSMatthias Ringwald*/
26*779af47bSMatthias Ringwald#define _CCIAR   0
27*779af47bSMatthias Ringwald#define _CCCLANG 1
28*779af47bSMatthias Ringwald
29*779af47bSMatthias Ringwald#if (defined __SES_ARM) || (defined __GNUC__) || (defined __clang__)
30*779af47bSMatthias Ringwald  #define _CC_TYPE             _CCCLANG
31*779af47bSMatthias Ringwald  #define _PUB_SYM             .global
32*779af47bSMatthias Ringwald  #define _EXT_SYM             .extern
33*779af47bSMatthias Ringwald  #define _END                 .end
34*779af47bSMatthias Ringwald  #define _WEAK                .weak
35*779af47bSMatthias Ringwald  #define _THUMB_FUNC          .thumb_func
36*779af47bSMatthias Ringwald  #define _THUMB_CODE          .code 16
37*779af47bSMatthias Ringwald  #define _WORD                .word
38*779af47bSMatthias Ringwald  #define _SECTION(Sect, Type, AlignExp) .section Sect ##, "ax"
39*779af47bSMatthias Ringwald  #define _ALIGN(Exp)          .align Exp
40*779af47bSMatthias Ringwald  #define _PLACE_LITS          .ltorg
41*779af47bSMatthias Ringwald  #define _DATA_SECT_START
42*779af47bSMatthias Ringwald  #define _C_STARTUP           _start
43*779af47bSMatthias Ringwald  #define _STACK_END           __stack_end__
44*779af47bSMatthias Ringwald  #define _RAMFUNC
45*779af47bSMatthias Ringwald  //
46*779af47bSMatthias Ringwald  // .text     => Link to flash
47*779af47bSMatthias Ringwald  // .fast     => Link to RAM
48*779af47bSMatthias Ringwald  // OtherSect => Usually link to RAM
49*779af47bSMatthias Ringwald  // Alignment is 2^x
50*779af47bSMatthias Ringwald  //
51*779af47bSMatthias Ringwald#elif defined (__IASMARM__)
52*779af47bSMatthias Ringwald  #define _CC_TYPE             _CCIAR
53*779af47bSMatthias Ringwald  #define _PUB_SYM             PUBLIC
54*779af47bSMatthias Ringwald  #define _EXT_SYM             EXTERN
55*779af47bSMatthias Ringwald  #define _END                 END
56*779af47bSMatthias Ringwald  #define _WEAK                _WEAK
57*779af47bSMatthias Ringwald  #define _THUMB_FUNC
58*779af47bSMatthias Ringwald  #define _THUMB_CODE          THUMB
59*779af47bSMatthias Ringwald  #define _WORD                DCD
60*779af47bSMatthias Ringwald  #define _SECTION(Sect, Type, AlignExp) SECTION Sect ## : ## Type ## :REORDER:NOROOT ## (AlignExp)
61*779af47bSMatthias Ringwald  #define _ALIGN(Exp)          alignrom Exp
62*779af47bSMatthias Ringwald  #define _PLACE_LITS
63*779af47bSMatthias Ringwald  #define _DATA_SECT_START     DATA
64*779af47bSMatthias Ringwald  #define _C_STARTUP           __iar_program_start
65*779af47bSMatthias Ringwald  #define _STACK_END           sfe(CSTACK)
66*779af47bSMatthias Ringwald  #define _RAMFUNC             SECTION_TYPE SHT_PROGBITS, SHF_WRITE | SHF_EXECINSTR
67*779af47bSMatthias Ringwald  //
68*779af47bSMatthias Ringwald  // .text     => Link to flash
69*779af47bSMatthias Ringwald  // .textrw   => Link to RAM
70*779af47bSMatthias Ringwald  // OtherSect => Usually link to RAM
71*779af47bSMatthias Ringwald  // NOROOT    => Allows linker to throw away the function, if not referenced
72*779af47bSMatthias Ringwald  // Alignment is 2^x
73*779af47bSMatthias Ringwald  //
74*779af47bSMatthias Ringwald#endif
75*779af47bSMatthias Ringwald
76*779af47bSMatthias Ringwald#if (_CC_TYPE == _CCIAR)
77*779af47bSMatthias Ringwald        NAME SEGGER_RTT_ASM_ARMv7M
78*779af47bSMatthias Ringwald#else
79*779af47bSMatthias Ringwald        .syntax unified
80*779af47bSMatthias Ringwald#endif
81*779af47bSMatthias Ringwald
82*779af47bSMatthias Ringwald#if defined (RTT_USE_ASM) && (RTT_USE_ASM == 1)
83*779af47bSMatthias Ringwald        #define SHT_PROGBITS 0x1
84*779af47bSMatthias Ringwald
85*779af47bSMatthias Ringwald/*********************************************************************
86*779af47bSMatthias Ringwald*
87*779af47bSMatthias Ringwald*       Public / external symbols
88*779af47bSMatthias Ringwald*
89*779af47bSMatthias Ringwald**********************************************************************
90*779af47bSMatthias Ringwald*/
91*779af47bSMatthias Ringwald
92*779af47bSMatthias Ringwald        _EXT_SYM __aeabi_memcpy
93*779af47bSMatthias Ringwald        _EXT_SYM __aeabi_memcpy4
94*779af47bSMatthias Ringwald        _EXT_SYM _SEGGER_RTT
95*779af47bSMatthias Ringwald
96*779af47bSMatthias Ringwald        _PUB_SYM SEGGER_RTT_ASM_WriteSkipNoLock
97*779af47bSMatthias Ringwald
98*779af47bSMatthias Ringwald/*********************************************************************
99*779af47bSMatthias Ringwald*
100*779af47bSMatthias Ringwald*       SEGGER_RTT_WriteSkipNoLock
101*779af47bSMatthias Ringwald*
102*779af47bSMatthias Ringwald*  Function description
103*779af47bSMatthias Ringwald*    Stores a specified number of characters in SEGGER RTT
104*779af47bSMatthias Ringwald*    control block which is then read by the host.
105*779af47bSMatthias Ringwald*    SEGGER_RTT_WriteSkipNoLock does not lock the application and
106*779af47bSMatthias Ringwald*    skips all data, if the data does not fit into the buffer.
107*779af47bSMatthias Ringwald*
108*779af47bSMatthias Ringwald*  Parameters
109*779af47bSMatthias Ringwald*    BufferIndex  Index of "Up"-buffer to be used (e.g. 0 for "Terminal").
110*779af47bSMatthias Ringwald*    pBuffer      Pointer to character array. Does not need to point to a \0 terminated string.
111*779af47bSMatthias Ringwald*    NumBytes     Number of bytes to be stored in the SEGGER RTT control block.
112*779af47bSMatthias Ringwald*                 MUST be > 0!!!
113*779af47bSMatthias Ringwald*                 This is done for performance reasons, so no initial check has do be done.
114*779af47bSMatthias Ringwald*
115*779af47bSMatthias Ringwald*  Return value
116*779af47bSMatthias Ringwald*    1: Data has been copied
117*779af47bSMatthias Ringwald*    0: No space, data has not been copied
118*779af47bSMatthias Ringwald*
119*779af47bSMatthias Ringwald*  Notes
120*779af47bSMatthias Ringwald*    (1) If there is not enough space in the "Up"-buffer, all data is dropped.
121*779af47bSMatthias Ringwald*    (2) For performance reasons this function does not call Init()
122*779af47bSMatthias Ringwald*        and may only be called after RTT has been initialized.
123*779af47bSMatthias Ringwald*        Either by calling SEGGER_RTT_Init() or calling another RTT API function first.
124*779af47bSMatthias Ringwald*/
125*779af47bSMatthias Ringwald        _SECTION(.text, CODE, 2)
126*779af47bSMatthias Ringwald        _ALIGN(2)
127*779af47bSMatthias Ringwald        _THUMB_FUNC
128*779af47bSMatthias RingwaldSEGGER_RTT_ASM_WriteSkipNoLock:   // unsigned SEGGER_RTT_WriteSkipNoLock(unsigned BufferIndex, const void* pData, unsigned NumBytes) {
129*779af47bSMatthias Ringwald        //
130*779af47bSMatthias Ringwald        // Cases:
131*779af47bSMatthias Ringwald        //   1) RdOff <= WrOff => Space until wrap-around is sufficient
132*779af47bSMatthias Ringwald        //   2) RdOff <= WrOff => Space after wrap-around needed (copy in 2 chunks)
133*779af47bSMatthias Ringwald        //   3) RdOff <  WrOff => No space in buf
134*779af47bSMatthias Ringwald        //   4) RdOff >  WrOff => Space is sufficient
135*779af47bSMatthias Ringwald        //   5) RdOff >  WrOff => No space in buf
136*779af47bSMatthias Ringwald        //
137*779af47bSMatthias Ringwald        // 1) is the most common case for large buffers and assuming that J-Link reads the data fast enough
138*779af47bSMatthias Ringwald        //
139*779af47bSMatthias Ringwald        // Register usage:
140*779af47bSMatthias Ringwald        //   R0 Temporary needed as RdOff, <Tmp> register later on
141*779af47bSMatthias Ringwald        //   R1 pData
142*779af47bSMatthias Ringwald        //   R2 <NumBytes>
143*779af47bSMatthias Ringwald        //   R3 <Tmp> register. Hold free for subroutine calls
144*779af47bSMatthias Ringwald        //   R4 <Rem>
145*779af47bSMatthias Ringwald        //   R5 pRing->pBuffer
146*779af47bSMatthias Ringwald        //   R6 pRing (Points to active struct SEGGER_RTT_BUFFER_DOWN)
147*779af47bSMatthias Ringwald        //   R7 WrOff
148*779af47bSMatthias Ringwald        //
149*779af47bSMatthias Ringwald        PUSH     {R4-R7}
150*779af47bSMatthias Ringwald        ADD      R3,R0,R0, LSL #+1
151*779af47bSMatthias Ringwald        LDR.W    R0,=_SEGGER_RTT                 // pRing = &_SEGGER_RTT.aUp[BufferIndex];
152*779af47bSMatthias Ringwald        ADD      R0,R0,R3, LSL #+3
153*779af47bSMatthias Ringwald        ADD      R6,R0,#+24
154*779af47bSMatthias Ringwald        LDR      R0,[R6, #+16]                   // RdOff = pRing->RdOff;
155*779af47bSMatthias Ringwald        LDR      R7,[R6, #+12]                   // WrOff = pRing->WrOff;
156*779af47bSMatthias Ringwald        LDR      R5,[R6, #+4]                    // pRing->pBuffer
157*779af47bSMatthias Ringwald        CMP      R7,R0
158*779af47bSMatthias Ringwald        BCC.N    _CheckCase4                     // if (RdOff <= WrOff) {                           => Case 1), 2) or 3)
159*779af47bSMatthias Ringwald        //
160*779af47bSMatthias Ringwald        // Handling for case 1, later on identical to case 4
161*779af47bSMatthias Ringwald        //
162*779af47bSMatthias Ringwald        LDR      R3,[R6, #+8]                    //  Avail = pRing->SizeOfBuffer - WrOff - 1u;      => Space until wrap-around (assume 1 byte not usable for case that RdOff == 0)
163*779af47bSMatthias Ringwald        SUBS     R4,R3,R7                        // <Rem> (Used in case we jump into case 2 afterwards)
164*779af47bSMatthias Ringwald        SUBS     R3,R4,#+1                       // <Avail>
165*779af47bSMatthias Ringwald        CMP      R3,R2
166*779af47bSMatthias Ringwald        BCC.N    _CheckCase2                     // if (Avail >= NumBytes) {  => Case 1)?
167*779af47bSMatthias Ringwald_Case4:
168*779af47bSMatthias Ringwald        ADDS     R5,R7,R5                        // pBuffer += WrOff
169*779af47bSMatthias Ringwald        ADDS     R0,R2,R7                        // v = WrOff + NumBytes
170*779af47bSMatthias Ringwald        //
171*779af47bSMatthias Ringwald        // 2x unrolling for the copy loop that is used most of the time
172*779af47bSMatthias Ringwald        // This is a special optimization for small SystemView packets and makes them even faster
173*779af47bSMatthias Ringwald        //
174*779af47bSMatthias Ringwald        _ALIGN(2)
175*779af47bSMatthias Ringwald_LoopCopyStraight:                               // memcpy(pRing->pBuffer + WrOff, pData, NumBytes);
176*779af47bSMatthias Ringwald        LDRB     R3,[R1], #+1
177*779af47bSMatthias Ringwald        STRB     R3,[R5], #+1                    // *pDest++ = *pSrc++
178*779af47bSMatthias Ringwald        SUBS     R2,R2,#+1
179*779af47bSMatthias Ringwald        BEQ      _CSDone
180*779af47bSMatthias Ringwald        LDRB     R3,[R1], #+1
181*779af47bSMatthias Ringwald        STRB     R3,[R5], #+1                    // *pDest++ = *pSrc++
182*779af47bSMatthias Ringwald        SUBS     R2,R2,#+1
183*779af47bSMatthias Ringwald        BNE      _LoopCopyStraight
184*779af47bSMatthias Ringwald_CSDone:
185*779af47bSMatthias Ringwald        STR      R0,[R6, #+12]                   // pRing->WrOff = WrOff + NumBytes;
186*779af47bSMatthias Ringwald        MOVS     R0,#+1
187*779af47bSMatthias Ringwald        POP      {R4-R7}
188*779af47bSMatthias Ringwald        BX       LR                              // Return 1
189*779af47bSMatthias Ringwald_CheckCase2:
190*779af47bSMatthias Ringwald        ADDS     R0,R0,R3                        // Avail += RdOff; => Space incl. wrap-around
191*779af47bSMatthias Ringwald        CMP      R0,R2
192*779af47bSMatthias Ringwald        BCC.N    _Case3                          // if (Avail >= NumBytes) {           => Case 2? => If not, we have case 3) (does not fit)
193*779af47bSMatthias Ringwald        //
194*779af47bSMatthias Ringwald        // Handling for case 2
195*779af47bSMatthias Ringwald        //
196*779af47bSMatthias Ringwald        ADDS     R0,R7,R5                        // v = pRing->pBuffer + WrOff => Do not change pRing->pBuffer here because 2nd chunk needs org. value
197*779af47bSMatthias Ringwald        SUBS     R2,R2,R4                        // NumBytes -= Rem;  (Rem = pRing->SizeOfBuffer - WrOff; => Space until end of buffer)
198*779af47bSMatthias Ringwald_LoopCopyBeforeWrapAround:                       // memcpy(pRing->pBuffer + WrOff, pData, Rem); => Copy 1st chunk
199*779af47bSMatthias Ringwald        LDRB     R3,[R1], #+1
200*779af47bSMatthias Ringwald        STRB     R3,[R0], #+1                    // *pDest++ = *pSrc++
201*779af47bSMatthias Ringwald        SUBS     R4,R4,#+1
202*779af47bSMatthias Ringwald        BNE      _LoopCopyBeforeWrapAround
203*779af47bSMatthias Ringwald        //
204*779af47bSMatthias Ringwald        // Special case: First check that assumed RdOff == 0 calculated that last element before wrap-around could not be used
205*779af47bSMatthias Ringwald        // But 2nd check (considering space until wrap-around and until RdOff) revealed that RdOff is not 0, so we can use the last element
206*779af47bSMatthias Ringwald        // In this case, we may use a copy straight until buffer end anyway without needing to copy 2 chunks
207*779af47bSMatthias Ringwald        // Therefore, check if 2nd memcpy is necessary at all
208*779af47bSMatthias Ringwald        //
209*779af47bSMatthias Ringwald        ADDS     R4,R2,#+0                       // Save <NumBytes> (needed as counter in loop but must be written to <WrOff> after the loop). Also use this inst to update the flags to skip 2nd loop if possible
210*779af47bSMatthias Ringwald        BEQ.N    _No2ChunkNeeded                 // if (NumBytes) {
211*779af47bSMatthias Ringwald_LoopCopyAfterWrapAround:                        // memcpy(pRing->pBuffer, pData + Rem, NumBytes);
212*779af47bSMatthias Ringwald        LDRB     R3,[R1], #+1                    // pData already points to the next src byte due to copy loop increment before this loop
213*779af47bSMatthias Ringwald        STRB     R3,[R5], #+1                    // *pDest++ = *pSrc++
214*779af47bSMatthias Ringwald        SUBS     R2,R2,#+1
215*779af47bSMatthias Ringwald        BNE      _LoopCopyAfterWrapAround
216*779af47bSMatthias Ringwald_No2ChunkNeeded:
217*779af47bSMatthias Ringwald        STR      R4,[R6, #+12]                   // pRing->WrOff = NumBytes; => Must be written after copying data because J-Link may read control block asynchronously while writing into buffer
218*779af47bSMatthias Ringwald        MOVS     R0,#+1
219*779af47bSMatthias Ringwald        POP      {R4-R7}
220*779af47bSMatthias Ringwald        BX       LR                              // Return 1
221*779af47bSMatthias Ringwald_CheckCase4:
222*779af47bSMatthias Ringwald        SUBS     R0,R0,R7
223*779af47bSMatthias Ringwald        SUBS     R0,R0,#+1                       // Avail = RdOff - WrOff - 1u;
224*779af47bSMatthias Ringwald        CMP      R0,R2
225*779af47bSMatthias Ringwald        BCS.N    _Case4                          // if (Avail >= NumBytes) {      => Case 4) == 1) ? => If not, we have case 5) == 3) (does not fit)
226*779af47bSMatthias Ringwald_Case3:
227*779af47bSMatthias Ringwald        MOVS     R0,#+0
228*779af47bSMatthias Ringwald        POP      {R4-R7}
229*779af47bSMatthias Ringwald        BX       LR                              // Return 0
230*779af47bSMatthias Ringwald        _PLACE_LITS
231*779af47bSMatthias Ringwald
232*779af47bSMatthias Ringwald#endif  // defined (RTT_USE_ASM) && (RTT_USE_ASM == 1)
233*779af47bSMatthias Ringwald        _END
234*779af47bSMatthias Ringwald
235*779af47bSMatthias Ringwald/*************************** End of file ****************************/
236