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