Segger rtt lib
Dependents: acnSensa_LIS acd52832_Indoor_Posit_Peripheral acd52832_Indoor_Posit_Central iBeacon acnsensa ... more
SEGGER_RTT_printf.c@2:abdc4190029b, 2018-05-31 (annotated)
- Committer:
- jurica238814
- Date:
- Thu May 31 13:03:29 2018 +0000
- Revision:
- 2:abdc4190029b
- Parent:
- 0:e61e7fc7cfe1
_write function definition removed.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jurica238814 | 0:e61e7fc7cfe1 | 1 | /********************************************************************* |
jurica238814 | 0:e61e7fc7cfe1 | 2 | * SEGGER MICROCONTROLLER GmbH & Co. KG * |
jurica238814 | 0:e61e7fc7cfe1 | 3 | * Solutions for real time microcontroller applications * |
jurica238814 | 0:e61e7fc7cfe1 | 4 | ********************************************************************** |
jurica238814 | 0:e61e7fc7cfe1 | 5 | * * |
jurica238814 | 0:e61e7fc7cfe1 | 6 | * (c) 2014 - 2015 SEGGER Microcontroller GmbH & Co. KG * |
jurica238814 | 0:e61e7fc7cfe1 | 7 | * * |
jurica238814 | 0:e61e7fc7cfe1 | 8 | * www.segger.com Support: support@segger.com * |
jurica238814 | 0:e61e7fc7cfe1 | 9 | * * |
jurica238814 | 0:e61e7fc7cfe1 | 10 | ********************************************************************** |
jurica238814 | 0:e61e7fc7cfe1 | 11 | * * |
jurica238814 | 0:e61e7fc7cfe1 | 12 | * All rights reserved. * |
jurica238814 | 0:e61e7fc7cfe1 | 13 | * * |
jurica238814 | 0:e61e7fc7cfe1 | 14 | * * This software may in its unmodified form be freely redistributed * |
jurica238814 | 0:e61e7fc7cfe1 | 15 | * in source form. * |
jurica238814 | 0:e61e7fc7cfe1 | 16 | * * The source code may be modified, provided the source code * |
jurica238814 | 0:e61e7fc7cfe1 | 17 | * retains the above copyright notice, this list of conditions and * |
jurica238814 | 0:e61e7fc7cfe1 | 18 | * the following disclaimer. * |
jurica238814 | 0:e61e7fc7cfe1 | 19 | * * Modified versions of this software in source or linkable form * |
jurica238814 | 0:e61e7fc7cfe1 | 20 | * may not be distributed without prior consent of SEGGER. * |
jurica238814 | 0:e61e7fc7cfe1 | 21 | * * This software may only be used for communication with SEGGER * |
jurica238814 | 0:e61e7fc7cfe1 | 22 | * J-Link debug probes. * |
jurica238814 | 0:e61e7fc7cfe1 | 23 | * * |
jurica238814 | 0:e61e7fc7cfe1 | 24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND * |
jurica238814 | 0:e61e7fc7cfe1 | 25 | * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, * |
jurica238814 | 0:e61e7fc7cfe1 | 26 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * |
jurica238814 | 0:e61e7fc7cfe1 | 27 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * |
jurica238814 | 0:e61e7fc7cfe1 | 28 | * DISCLAIMED. IN NO EVENT SHALL SEGGER Microcontroller BE LIABLE FOR * |
jurica238814 | 0:e61e7fc7cfe1 | 29 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * |
jurica238814 | 0:e61e7fc7cfe1 | 30 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT * |
jurica238814 | 0:e61e7fc7cfe1 | 31 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * |
jurica238814 | 0:e61e7fc7cfe1 | 32 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * |
jurica238814 | 0:e61e7fc7cfe1 | 33 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * |
jurica238814 | 0:e61e7fc7cfe1 | 34 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE * |
jurica238814 | 0:e61e7fc7cfe1 | 35 | * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH * |
jurica238814 | 0:e61e7fc7cfe1 | 36 | * DAMAGE. * |
jurica238814 | 0:e61e7fc7cfe1 | 37 | * * |
jurica238814 | 0:e61e7fc7cfe1 | 38 | ********************************************************************** |
jurica238814 | 0:e61e7fc7cfe1 | 39 | ---------------------------END-OF-HEADER------------------------------ |
jurica238814 | 0:e61e7fc7cfe1 | 40 | File : SEGGER_RTT_printf.c |
jurica238814 | 0:e61e7fc7cfe1 | 41 | Purpose : Replacement for printf to write formatted data via RTT |
jurica238814 | 0:e61e7fc7cfe1 | 42 | ---------------------------------------------------------------------- |
jurica238814 | 0:e61e7fc7cfe1 | 43 | */ |
jurica238814 | 0:e61e7fc7cfe1 | 44 | #include "SEGGER_RTT.h" |
jurica238814 | 0:e61e7fc7cfe1 | 45 | #include "SEGGER_RTT_Conf.h" |
jurica238814 | 0:e61e7fc7cfe1 | 46 | |
jurica238814 | 0:e61e7fc7cfe1 | 47 | /********************************************************************* |
jurica238814 | 0:e61e7fc7cfe1 | 48 | * |
jurica238814 | 0:e61e7fc7cfe1 | 49 | * Defines, configurable |
jurica238814 | 0:e61e7fc7cfe1 | 50 | * |
jurica238814 | 0:e61e7fc7cfe1 | 51 | ********************************************************************** |
jurica238814 | 0:e61e7fc7cfe1 | 52 | */ |
jurica238814 | 0:e61e7fc7cfe1 | 53 | |
jurica238814 | 0:e61e7fc7cfe1 | 54 | #ifndef SEGGER_RTT_PRINTF_BUFFER_SIZE |
jurica238814 | 0:e61e7fc7cfe1 | 55 | #define SEGGER_RTT_PRINTF_BUFFER_SIZE (64) |
jurica238814 | 0:e61e7fc7cfe1 | 56 | #endif |
jurica238814 | 0:e61e7fc7cfe1 | 57 | |
jurica238814 | 0:e61e7fc7cfe1 | 58 | #include <stdlib.h> |
jurica238814 | 0:e61e7fc7cfe1 | 59 | #include <stdarg.h> |
jurica238814 | 0:e61e7fc7cfe1 | 60 | |
jurica238814 | 0:e61e7fc7cfe1 | 61 | |
jurica238814 | 0:e61e7fc7cfe1 | 62 | #define FORMAT_FLAG_LEFT_JUSTIFY (1u << 0) |
jurica238814 | 0:e61e7fc7cfe1 | 63 | #define FORMAT_FLAG_PAD_ZERO (1u << 1) |
jurica238814 | 0:e61e7fc7cfe1 | 64 | #define FORMAT_FLAG_PRINT_SIGN (1u << 2) |
jurica238814 | 0:e61e7fc7cfe1 | 65 | #define FORMAT_FLAG_ALTERNATE (1u << 3) |
jurica238814 | 0:e61e7fc7cfe1 | 66 | |
jurica238814 | 0:e61e7fc7cfe1 | 67 | /********************************************************************* |
jurica238814 | 0:e61e7fc7cfe1 | 68 | * |
jurica238814 | 0:e61e7fc7cfe1 | 69 | * Types |
jurica238814 | 0:e61e7fc7cfe1 | 70 | * |
jurica238814 | 0:e61e7fc7cfe1 | 71 | ********************************************************************** |
jurica238814 | 0:e61e7fc7cfe1 | 72 | */ |
jurica238814 | 0:e61e7fc7cfe1 | 73 | |
jurica238814 | 0:e61e7fc7cfe1 | 74 | typedef struct { |
jurica238814 | 0:e61e7fc7cfe1 | 75 | char* pBuffer; |
jurica238814 | 0:e61e7fc7cfe1 | 76 | unsigned BufferSize; |
jurica238814 | 0:e61e7fc7cfe1 | 77 | unsigned Cnt; |
jurica238814 | 0:e61e7fc7cfe1 | 78 | |
jurica238814 | 0:e61e7fc7cfe1 | 79 | int ReturnValue; |
jurica238814 | 0:e61e7fc7cfe1 | 80 | |
jurica238814 | 0:e61e7fc7cfe1 | 81 | unsigned RTTBufferIndex; |
jurica238814 | 0:e61e7fc7cfe1 | 82 | } SEGGER_RTT_PRINTF_DESC; |
jurica238814 | 0:e61e7fc7cfe1 | 83 | |
jurica238814 | 0:e61e7fc7cfe1 | 84 | /********************************************************************* |
jurica238814 | 0:e61e7fc7cfe1 | 85 | * |
jurica238814 | 0:e61e7fc7cfe1 | 86 | * Function prototypes |
jurica238814 | 0:e61e7fc7cfe1 | 87 | * |
jurica238814 | 0:e61e7fc7cfe1 | 88 | ********************************************************************** |
jurica238814 | 0:e61e7fc7cfe1 | 89 | */ |
jurica238814 | 0:e61e7fc7cfe1 | 90 | int SEGGER_RTT_vprintf(unsigned BufferIndex, const char * sFormat, va_list * pParamList); |
jurica238814 | 0:e61e7fc7cfe1 | 91 | |
jurica238814 | 0:e61e7fc7cfe1 | 92 | /********************************************************************* |
jurica238814 | 0:e61e7fc7cfe1 | 93 | * |
jurica238814 | 0:e61e7fc7cfe1 | 94 | * Static code |
jurica238814 | 0:e61e7fc7cfe1 | 95 | * |
jurica238814 | 0:e61e7fc7cfe1 | 96 | ********************************************************************** |
jurica238814 | 0:e61e7fc7cfe1 | 97 | */ |
jurica238814 | 0:e61e7fc7cfe1 | 98 | /********************************************************************* |
jurica238814 | 0:e61e7fc7cfe1 | 99 | * |
jurica238814 | 0:e61e7fc7cfe1 | 100 | * _StoreChar |
jurica238814 | 0:e61e7fc7cfe1 | 101 | */ |
jurica238814 | 0:e61e7fc7cfe1 | 102 | static void _StoreChar(SEGGER_RTT_PRINTF_DESC * p, char c) { |
jurica238814 | 0:e61e7fc7cfe1 | 103 | unsigned Cnt; |
jurica238814 | 0:e61e7fc7cfe1 | 104 | |
jurica238814 | 0:e61e7fc7cfe1 | 105 | Cnt = p->Cnt; |
jurica238814 | 0:e61e7fc7cfe1 | 106 | if ((Cnt + 1u) <= p->BufferSize) { |
jurica238814 | 0:e61e7fc7cfe1 | 107 | *(p->pBuffer + Cnt) = c; |
jurica238814 | 0:e61e7fc7cfe1 | 108 | p->Cnt = Cnt + 1u; |
jurica238814 | 0:e61e7fc7cfe1 | 109 | p->ReturnValue++; |
jurica238814 | 0:e61e7fc7cfe1 | 110 | } |
jurica238814 | 0:e61e7fc7cfe1 | 111 | // |
jurica238814 | 0:e61e7fc7cfe1 | 112 | // Write part of string, when the buffer is full |
jurica238814 | 0:e61e7fc7cfe1 | 113 | // |
jurica238814 | 0:e61e7fc7cfe1 | 114 | if (p->Cnt == p->BufferSize) { |
jurica238814 | 0:e61e7fc7cfe1 | 115 | if (SEGGER_RTT_Write(p->RTTBufferIndex, p->pBuffer, p->Cnt) != p->Cnt) { |
jurica238814 | 0:e61e7fc7cfe1 | 116 | p->ReturnValue = -1; |
jurica238814 | 0:e61e7fc7cfe1 | 117 | } else { |
jurica238814 | 0:e61e7fc7cfe1 | 118 | p->Cnt = 0u; |
jurica238814 | 0:e61e7fc7cfe1 | 119 | } |
jurica238814 | 0:e61e7fc7cfe1 | 120 | } |
jurica238814 | 0:e61e7fc7cfe1 | 121 | } |
jurica238814 | 0:e61e7fc7cfe1 | 122 | |
jurica238814 | 0:e61e7fc7cfe1 | 123 | /********************************************************************* |
jurica238814 | 0:e61e7fc7cfe1 | 124 | * |
jurica238814 | 0:e61e7fc7cfe1 | 125 | * _PrintUnsigned |
jurica238814 | 0:e61e7fc7cfe1 | 126 | */ |
jurica238814 | 0:e61e7fc7cfe1 | 127 | static void _PrintUnsigned(SEGGER_RTT_PRINTF_DESC * pBufferDesc, unsigned v, unsigned Base, unsigned NumDigits, unsigned FieldWidth, unsigned FormatFlags) { |
jurica238814 | 0:e61e7fc7cfe1 | 128 | static const char _aV2C[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; |
jurica238814 | 0:e61e7fc7cfe1 | 129 | unsigned Div; |
jurica238814 | 0:e61e7fc7cfe1 | 130 | unsigned Digit; |
jurica238814 | 0:e61e7fc7cfe1 | 131 | unsigned Number; |
jurica238814 | 0:e61e7fc7cfe1 | 132 | unsigned Width; |
jurica238814 | 0:e61e7fc7cfe1 | 133 | char c; |
jurica238814 | 0:e61e7fc7cfe1 | 134 | |
jurica238814 | 0:e61e7fc7cfe1 | 135 | Number = v; |
jurica238814 | 0:e61e7fc7cfe1 | 136 | Digit = 1u; |
jurica238814 | 0:e61e7fc7cfe1 | 137 | // |
jurica238814 | 0:e61e7fc7cfe1 | 138 | // Get actual field width |
jurica238814 | 0:e61e7fc7cfe1 | 139 | // |
jurica238814 | 0:e61e7fc7cfe1 | 140 | Width = 1u; |
jurica238814 | 0:e61e7fc7cfe1 | 141 | while (Number >= Base) { |
jurica238814 | 0:e61e7fc7cfe1 | 142 | Number = (Number / Base); |
jurica238814 | 0:e61e7fc7cfe1 | 143 | Width++; |
jurica238814 | 0:e61e7fc7cfe1 | 144 | } |
jurica238814 | 0:e61e7fc7cfe1 | 145 | if (NumDigits > Width) { |
jurica238814 | 0:e61e7fc7cfe1 | 146 | Width = NumDigits; |
jurica238814 | 0:e61e7fc7cfe1 | 147 | } |
jurica238814 | 0:e61e7fc7cfe1 | 148 | // |
jurica238814 | 0:e61e7fc7cfe1 | 149 | // Print leading chars if necessary |
jurica238814 | 0:e61e7fc7cfe1 | 150 | // |
jurica238814 | 0:e61e7fc7cfe1 | 151 | if ((FormatFlags & FORMAT_FLAG_LEFT_JUSTIFY) == 0u) { |
jurica238814 | 0:e61e7fc7cfe1 | 152 | if (FieldWidth != 0u) { |
jurica238814 | 0:e61e7fc7cfe1 | 153 | if (((FormatFlags & FORMAT_FLAG_PAD_ZERO) == FORMAT_FLAG_PAD_ZERO) && (NumDigits == 0u)) { |
jurica238814 | 0:e61e7fc7cfe1 | 154 | c = '0'; |
jurica238814 | 0:e61e7fc7cfe1 | 155 | } else { |
jurica238814 | 0:e61e7fc7cfe1 | 156 | c = ' '; |
jurica238814 | 0:e61e7fc7cfe1 | 157 | } |
jurica238814 | 0:e61e7fc7cfe1 | 158 | while ((FieldWidth != 0u) && (Width < FieldWidth)) { |
jurica238814 | 0:e61e7fc7cfe1 | 159 | FieldWidth--; |
jurica238814 | 0:e61e7fc7cfe1 | 160 | _StoreChar(pBufferDesc, c); |
jurica238814 | 0:e61e7fc7cfe1 | 161 | if (pBufferDesc->ReturnValue < 0) { |
jurica238814 | 0:e61e7fc7cfe1 | 162 | break; |
jurica238814 | 0:e61e7fc7cfe1 | 163 | } |
jurica238814 | 0:e61e7fc7cfe1 | 164 | } |
jurica238814 | 0:e61e7fc7cfe1 | 165 | } |
jurica238814 | 0:e61e7fc7cfe1 | 166 | } |
jurica238814 | 0:e61e7fc7cfe1 | 167 | if (pBufferDesc->ReturnValue >= 0) { |
jurica238814 | 0:e61e7fc7cfe1 | 168 | // |
jurica238814 | 0:e61e7fc7cfe1 | 169 | // Compute Digit. |
jurica238814 | 0:e61e7fc7cfe1 | 170 | // Loop until Digit has the value of the highest digit required. |
jurica238814 | 0:e61e7fc7cfe1 | 171 | // Example: If the output is 345 (Base 10), loop 2 times until Digit is 100. |
jurica238814 | 0:e61e7fc7cfe1 | 172 | // |
jurica238814 | 0:e61e7fc7cfe1 | 173 | while (1) { |
jurica238814 | 0:e61e7fc7cfe1 | 174 | if (NumDigits > 1u) { // User specified a min number of digits to print? => Make sure we loop at least that often, before checking anything else (> 1 check avoids problems with NumDigits being signed / unsigned) |
jurica238814 | 0:e61e7fc7cfe1 | 175 | NumDigits--; |
jurica238814 | 0:e61e7fc7cfe1 | 176 | } else { |
jurica238814 | 0:e61e7fc7cfe1 | 177 | Div = v / Digit; |
jurica238814 | 0:e61e7fc7cfe1 | 178 | if (Div < Base) { // Is our divider big enough to extract the highest digit from value? => Done |
jurica238814 | 0:e61e7fc7cfe1 | 179 | break; |
jurica238814 | 0:e61e7fc7cfe1 | 180 | } |
jurica238814 | 0:e61e7fc7cfe1 | 181 | } |
jurica238814 | 0:e61e7fc7cfe1 | 182 | Digit *= Base; |
jurica238814 | 0:e61e7fc7cfe1 | 183 | } |
jurica238814 | 0:e61e7fc7cfe1 | 184 | // |
jurica238814 | 0:e61e7fc7cfe1 | 185 | // Output digits |
jurica238814 | 0:e61e7fc7cfe1 | 186 | // |
jurica238814 | 0:e61e7fc7cfe1 | 187 | do { |
jurica238814 | 0:e61e7fc7cfe1 | 188 | Div = v / Digit; |
jurica238814 | 0:e61e7fc7cfe1 | 189 | v -= Div * Digit; |
jurica238814 | 0:e61e7fc7cfe1 | 190 | _StoreChar(pBufferDesc, _aV2C[Div]); |
jurica238814 | 0:e61e7fc7cfe1 | 191 | if (pBufferDesc->ReturnValue < 0) { |
jurica238814 | 0:e61e7fc7cfe1 | 192 | break; |
jurica238814 | 0:e61e7fc7cfe1 | 193 | } |
jurica238814 | 0:e61e7fc7cfe1 | 194 | Digit /= Base; |
jurica238814 | 0:e61e7fc7cfe1 | 195 | } while (Digit); |
jurica238814 | 0:e61e7fc7cfe1 | 196 | // |
jurica238814 | 0:e61e7fc7cfe1 | 197 | // Print trailing spaces if necessary |
jurica238814 | 0:e61e7fc7cfe1 | 198 | // |
jurica238814 | 0:e61e7fc7cfe1 | 199 | if ((FormatFlags & FORMAT_FLAG_LEFT_JUSTIFY) == FORMAT_FLAG_LEFT_JUSTIFY) { |
jurica238814 | 0:e61e7fc7cfe1 | 200 | if (FieldWidth != 0u) { |
jurica238814 | 0:e61e7fc7cfe1 | 201 | while ((FieldWidth != 0u) && (Width < FieldWidth)) { |
jurica238814 | 0:e61e7fc7cfe1 | 202 | FieldWidth--; |
jurica238814 | 0:e61e7fc7cfe1 | 203 | _StoreChar(pBufferDesc, ' '); |
jurica238814 | 0:e61e7fc7cfe1 | 204 | if (pBufferDesc->ReturnValue < 0) { |
jurica238814 | 0:e61e7fc7cfe1 | 205 | break; |
jurica238814 | 0:e61e7fc7cfe1 | 206 | } |
jurica238814 | 0:e61e7fc7cfe1 | 207 | } |
jurica238814 | 0:e61e7fc7cfe1 | 208 | } |
jurica238814 | 0:e61e7fc7cfe1 | 209 | } |
jurica238814 | 0:e61e7fc7cfe1 | 210 | } |
jurica238814 | 0:e61e7fc7cfe1 | 211 | } |
jurica238814 | 0:e61e7fc7cfe1 | 212 | |
jurica238814 | 0:e61e7fc7cfe1 | 213 | /********************************************************************* |
jurica238814 | 0:e61e7fc7cfe1 | 214 | * |
jurica238814 | 0:e61e7fc7cfe1 | 215 | * _PrintInt |
jurica238814 | 0:e61e7fc7cfe1 | 216 | */ |
jurica238814 | 0:e61e7fc7cfe1 | 217 | static void _PrintInt(SEGGER_RTT_PRINTF_DESC * pBufferDesc, int v, unsigned Base, unsigned NumDigits, unsigned FieldWidth, unsigned FormatFlags) { |
jurica238814 | 0:e61e7fc7cfe1 | 218 | unsigned Width; |
jurica238814 | 0:e61e7fc7cfe1 | 219 | int Number; |
jurica238814 | 0:e61e7fc7cfe1 | 220 | |
jurica238814 | 0:e61e7fc7cfe1 | 221 | Number = (v < 0) ? -v : v; |
jurica238814 | 0:e61e7fc7cfe1 | 222 | |
jurica238814 | 0:e61e7fc7cfe1 | 223 | // |
jurica238814 | 0:e61e7fc7cfe1 | 224 | // Get actual field width |
jurica238814 | 0:e61e7fc7cfe1 | 225 | // |
jurica238814 | 0:e61e7fc7cfe1 | 226 | Width = 1u; |
jurica238814 | 0:e61e7fc7cfe1 | 227 | while (Number >= (int)Base) { |
jurica238814 | 0:e61e7fc7cfe1 | 228 | Number = (Number / (int)Base); |
jurica238814 | 0:e61e7fc7cfe1 | 229 | Width++; |
jurica238814 | 0:e61e7fc7cfe1 | 230 | } |
jurica238814 | 0:e61e7fc7cfe1 | 231 | if (NumDigits > Width) { |
jurica238814 | 0:e61e7fc7cfe1 | 232 | Width = NumDigits; |
jurica238814 | 0:e61e7fc7cfe1 | 233 | } |
jurica238814 | 0:e61e7fc7cfe1 | 234 | if ((FieldWidth > 0u) && ((v < 0) || ((FormatFlags & FORMAT_FLAG_PRINT_SIGN) == FORMAT_FLAG_PRINT_SIGN))) { |
jurica238814 | 0:e61e7fc7cfe1 | 235 | FieldWidth--; |
jurica238814 | 0:e61e7fc7cfe1 | 236 | } |
jurica238814 | 0:e61e7fc7cfe1 | 237 | |
jurica238814 | 0:e61e7fc7cfe1 | 238 | // |
jurica238814 | 0:e61e7fc7cfe1 | 239 | // Print leading spaces if necessary |
jurica238814 | 0:e61e7fc7cfe1 | 240 | // |
jurica238814 | 0:e61e7fc7cfe1 | 241 | if ((((FormatFlags & FORMAT_FLAG_PAD_ZERO) == 0u) || (NumDigits != 0u)) && ((FormatFlags & FORMAT_FLAG_LEFT_JUSTIFY) == 0u)) { |
jurica238814 | 0:e61e7fc7cfe1 | 242 | if (FieldWidth != 0u) { |
jurica238814 | 0:e61e7fc7cfe1 | 243 | while ((FieldWidth != 0u) && (Width < FieldWidth)) { |
jurica238814 | 0:e61e7fc7cfe1 | 244 | FieldWidth--; |
jurica238814 | 0:e61e7fc7cfe1 | 245 | _StoreChar(pBufferDesc, ' '); |
jurica238814 | 0:e61e7fc7cfe1 | 246 | if (pBufferDesc->ReturnValue < 0) { |
jurica238814 | 0:e61e7fc7cfe1 | 247 | break; |
jurica238814 | 0:e61e7fc7cfe1 | 248 | } |
jurica238814 | 0:e61e7fc7cfe1 | 249 | } |
jurica238814 | 0:e61e7fc7cfe1 | 250 | } |
jurica238814 | 0:e61e7fc7cfe1 | 251 | } |
jurica238814 | 0:e61e7fc7cfe1 | 252 | // |
jurica238814 | 0:e61e7fc7cfe1 | 253 | // Print sign if necessary |
jurica238814 | 0:e61e7fc7cfe1 | 254 | // |
jurica238814 | 0:e61e7fc7cfe1 | 255 | if (pBufferDesc->ReturnValue >= 0) { |
jurica238814 | 0:e61e7fc7cfe1 | 256 | if (v < 0) { |
jurica238814 | 0:e61e7fc7cfe1 | 257 | v = -v; |
jurica238814 | 0:e61e7fc7cfe1 | 258 | _StoreChar(pBufferDesc, '-'); |
jurica238814 | 0:e61e7fc7cfe1 | 259 | } else if ((FormatFlags & FORMAT_FLAG_PRINT_SIGN) == FORMAT_FLAG_PRINT_SIGN) { |
jurica238814 | 0:e61e7fc7cfe1 | 260 | _StoreChar(pBufferDesc, '+'); |
jurica238814 | 0:e61e7fc7cfe1 | 261 | } else { |
jurica238814 | 0:e61e7fc7cfe1 | 262 | |
jurica238814 | 0:e61e7fc7cfe1 | 263 | } |
jurica238814 | 0:e61e7fc7cfe1 | 264 | if (pBufferDesc->ReturnValue >= 0) { |
jurica238814 | 0:e61e7fc7cfe1 | 265 | // |
jurica238814 | 0:e61e7fc7cfe1 | 266 | // Print leading zeros if necessary |
jurica238814 | 0:e61e7fc7cfe1 | 267 | // |
jurica238814 | 0:e61e7fc7cfe1 | 268 | if (((FormatFlags & FORMAT_FLAG_PAD_ZERO) == FORMAT_FLAG_PAD_ZERO) && ((FormatFlags & FORMAT_FLAG_LEFT_JUSTIFY) == 0u) && (NumDigits == 0u)) { |
jurica238814 | 0:e61e7fc7cfe1 | 269 | if (FieldWidth != 0u) { |
jurica238814 | 0:e61e7fc7cfe1 | 270 | while ((FieldWidth != 0u) && (Width < FieldWidth)) { |
jurica238814 | 0:e61e7fc7cfe1 | 271 | FieldWidth--; |
jurica238814 | 0:e61e7fc7cfe1 | 272 | _StoreChar(pBufferDesc, '0'); |
jurica238814 | 0:e61e7fc7cfe1 | 273 | if (pBufferDesc->ReturnValue < 0) { |
jurica238814 | 0:e61e7fc7cfe1 | 274 | break; |
jurica238814 | 0:e61e7fc7cfe1 | 275 | } |
jurica238814 | 0:e61e7fc7cfe1 | 276 | } |
jurica238814 | 0:e61e7fc7cfe1 | 277 | } |
jurica238814 | 0:e61e7fc7cfe1 | 278 | } |
jurica238814 | 0:e61e7fc7cfe1 | 279 | if (pBufferDesc->ReturnValue >= 0) { |
jurica238814 | 0:e61e7fc7cfe1 | 280 | // |
jurica238814 | 0:e61e7fc7cfe1 | 281 | // Print number without sign |
jurica238814 | 0:e61e7fc7cfe1 | 282 | // |
jurica238814 | 0:e61e7fc7cfe1 | 283 | _PrintUnsigned(pBufferDesc, (unsigned)v, Base, NumDigits, FieldWidth, FormatFlags); |
jurica238814 | 0:e61e7fc7cfe1 | 284 | } |
jurica238814 | 0:e61e7fc7cfe1 | 285 | } |
jurica238814 | 0:e61e7fc7cfe1 | 286 | } |
jurica238814 | 0:e61e7fc7cfe1 | 287 | } |
jurica238814 | 0:e61e7fc7cfe1 | 288 | |
jurica238814 | 0:e61e7fc7cfe1 | 289 | /********************************************************************* |
jurica238814 | 0:e61e7fc7cfe1 | 290 | * |
jurica238814 | 0:e61e7fc7cfe1 | 291 | * Public code |
jurica238814 | 0:e61e7fc7cfe1 | 292 | * |
jurica238814 | 0:e61e7fc7cfe1 | 293 | ********************************************************************** |
jurica238814 | 0:e61e7fc7cfe1 | 294 | */ |
jurica238814 | 0:e61e7fc7cfe1 | 295 | /********************************************************************* |
jurica238814 | 0:e61e7fc7cfe1 | 296 | * |
jurica238814 | 0:e61e7fc7cfe1 | 297 | * SEGGER_RTT_vprintf |
jurica238814 | 0:e61e7fc7cfe1 | 298 | * |
jurica238814 | 0:e61e7fc7cfe1 | 299 | * Function description |
jurica238814 | 0:e61e7fc7cfe1 | 300 | * Stores a formatted string in SEGGER RTT control block. |
jurica238814 | 0:e61e7fc7cfe1 | 301 | * This data is read by the host. |
jurica238814 | 0:e61e7fc7cfe1 | 302 | * |
jurica238814 | 0:e61e7fc7cfe1 | 303 | * Parameters |
jurica238814 | 0:e61e7fc7cfe1 | 304 | * BufferIndex Index of "Up"-buffer to be used. (e.g. 0 for "Terminal") |
jurica238814 | 0:e61e7fc7cfe1 | 305 | * sFormat Pointer to format string |
jurica238814 | 0:e61e7fc7cfe1 | 306 | * pParamList Pointer to the list of arguments for the format string |
jurica238814 | 0:e61e7fc7cfe1 | 307 | * |
jurica238814 | 0:e61e7fc7cfe1 | 308 | * Return values |
jurica238814 | 0:e61e7fc7cfe1 | 309 | * >= 0: Number of bytes which have been stored in the "Up"-buffer. |
jurica238814 | 0:e61e7fc7cfe1 | 310 | * < 0: Error |
jurica238814 | 0:e61e7fc7cfe1 | 311 | */ |
jurica238814 | 0:e61e7fc7cfe1 | 312 | int SEGGER_RTT_vprintf(unsigned BufferIndex, const char * sFormat, va_list * pParamList) { |
jurica238814 | 0:e61e7fc7cfe1 | 313 | char c; |
jurica238814 | 0:e61e7fc7cfe1 | 314 | SEGGER_RTT_PRINTF_DESC BufferDesc; |
jurica238814 | 0:e61e7fc7cfe1 | 315 | int v; |
jurica238814 | 0:e61e7fc7cfe1 | 316 | unsigned NumDigits; |
jurica238814 | 0:e61e7fc7cfe1 | 317 | unsigned FormatFlags; |
jurica238814 | 0:e61e7fc7cfe1 | 318 | unsigned FieldWidth; |
jurica238814 | 0:e61e7fc7cfe1 | 319 | char acBuffer[SEGGER_RTT_PRINTF_BUFFER_SIZE]; |
jurica238814 | 0:e61e7fc7cfe1 | 320 | |
jurica238814 | 0:e61e7fc7cfe1 | 321 | BufferDesc.pBuffer = acBuffer; |
jurica238814 | 0:e61e7fc7cfe1 | 322 | BufferDesc.BufferSize = SEGGER_RTT_PRINTF_BUFFER_SIZE; |
jurica238814 | 0:e61e7fc7cfe1 | 323 | BufferDesc.Cnt = 0u; |
jurica238814 | 0:e61e7fc7cfe1 | 324 | BufferDesc.RTTBufferIndex = BufferIndex; |
jurica238814 | 0:e61e7fc7cfe1 | 325 | BufferDesc.ReturnValue = 0; |
jurica238814 | 0:e61e7fc7cfe1 | 326 | |
jurica238814 | 0:e61e7fc7cfe1 | 327 | do { |
jurica238814 | 0:e61e7fc7cfe1 | 328 | c = *sFormat; |
jurica238814 | 0:e61e7fc7cfe1 | 329 | sFormat++; |
jurica238814 | 0:e61e7fc7cfe1 | 330 | if (c == 0u) { |
jurica238814 | 0:e61e7fc7cfe1 | 331 | break; |
jurica238814 | 0:e61e7fc7cfe1 | 332 | } |
jurica238814 | 0:e61e7fc7cfe1 | 333 | if (c == '%') { |
jurica238814 | 0:e61e7fc7cfe1 | 334 | // |
jurica238814 | 0:e61e7fc7cfe1 | 335 | // Filter out flags |
jurica238814 | 0:e61e7fc7cfe1 | 336 | // |
jurica238814 | 0:e61e7fc7cfe1 | 337 | FormatFlags = 0u; |
jurica238814 | 0:e61e7fc7cfe1 | 338 | v = 1; |
jurica238814 | 0:e61e7fc7cfe1 | 339 | do { |
jurica238814 | 0:e61e7fc7cfe1 | 340 | c = *sFormat; |
jurica238814 | 0:e61e7fc7cfe1 | 341 | switch (c) { |
jurica238814 | 0:e61e7fc7cfe1 | 342 | case '-': FormatFlags |= FORMAT_FLAG_LEFT_JUSTIFY; sFormat++; break; |
jurica238814 | 0:e61e7fc7cfe1 | 343 | case '0': FormatFlags |= FORMAT_FLAG_PAD_ZERO; sFormat++; break; |
jurica238814 | 0:e61e7fc7cfe1 | 344 | case '+': FormatFlags |= FORMAT_FLAG_PRINT_SIGN; sFormat++; break; |
jurica238814 | 0:e61e7fc7cfe1 | 345 | case '#': FormatFlags |= FORMAT_FLAG_ALTERNATE; sFormat++; break; |
jurica238814 | 0:e61e7fc7cfe1 | 346 | default: v = 0; break; |
jurica238814 | 0:e61e7fc7cfe1 | 347 | } |
jurica238814 | 0:e61e7fc7cfe1 | 348 | } while (v); |
jurica238814 | 0:e61e7fc7cfe1 | 349 | // |
jurica238814 | 0:e61e7fc7cfe1 | 350 | // filter out field with |
jurica238814 | 0:e61e7fc7cfe1 | 351 | // |
jurica238814 | 0:e61e7fc7cfe1 | 352 | FieldWidth = 0u; |
jurica238814 | 0:e61e7fc7cfe1 | 353 | do { |
jurica238814 | 0:e61e7fc7cfe1 | 354 | c = *sFormat; |
jurica238814 | 0:e61e7fc7cfe1 | 355 | if ((c < '0') || (c > '9')) { |
jurica238814 | 0:e61e7fc7cfe1 | 356 | break; |
jurica238814 | 0:e61e7fc7cfe1 | 357 | } |
jurica238814 | 0:e61e7fc7cfe1 | 358 | sFormat++; |
jurica238814 | 0:e61e7fc7cfe1 | 359 | FieldWidth = (FieldWidth * 10u) + ((unsigned)c - '0'); |
jurica238814 | 0:e61e7fc7cfe1 | 360 | } while (1); |
jurica238814 | 0:e61e7fc7cfe1 | 361 | |
jurica238814 | 0:e61e7fc7cfe1 | 362 | // |
jurica238814 | 0:e61e7fc7cfe1 | 363 | // Filter out precision (number of digits to display) |
jurica238814 | 0:e61e7fc7cfe1 | 364 | // |
jurica238814 | 0:e61e7fc7cfe1 | 365 | NumDigits = 0u; |
jurica238814 | 0:e61e7fc7cfe1 | 366 | c = *sFormat; |
jurica238814 | 0:e61e7fc7cfe1 | 367 | if (c == '.') { |
jurica238814 | 0:e61e7fc7cfe1 | 368 | sFormat++; |
jurica238814 | 0:e61e7fc7cfe1 | 369 | do { |
jurica238814 | 0:e61e7fc7cfe1 | 370 | c = *sFormat; |
jurica238814 | 0:e61e7fc7cfe1 | 371 | if ((c < '0') || (c > '9')) { |
jurica238814 | 0:e61e7fc7cfe1 | 372 | break; |
jurica238814 | 0:e61e7fc7cfe1 | 373 | } |
jurica238814 | 0:e61e7fc7cfe1 | 374 | sFormat++; |
jurica238814 | 0:e61e7fc7cfe1 | 375 | NumDigits = NumDigits * 10u + ((unsigned)c - '0'); |
jurica238814 | 0:e61e7fc7cfe1 | 376 | } while (1); |
jurica238814 | 0:e61e7fc7cfe1 | 377 | } |
jurica238814 | 0:e61e7fc7cfe1 | 378 | // |
jurica238814 | 0:e61e7fc7cfe1 | 379 | // Filter out length modifier |
jurica238814 | 0:e61e7fc7cfe1 | 380 | // |
jurica238814 | 0:e61e7fc7cfe1 | 381 | c = *sFormat; |
jurica238814 | 0:e61e7fc7cfe1 | 382 | do { |
jurica238814 | 0:e61e7fc7cfe1 | 383 | if ((c == 'l') || (c == 'h')) { |
jurica238814 | 0:e61e7fc7cfe1 | 384 | c = *sFormat; |
jurica238814 | 0:e61e7fc7cfe1 | 385 | sFormat++; |
jurica238814 | 0:e61e7fc7cfe1 | 386 | } else { |
jurica238814 | 0:e61e7fc7cfe1 | 387 | break; |
jurica238814 | 0:e61e7fc7cfe1 | 388 | } |
jurica238814 | 0:e61e7fc7cfe1 | 389 | } while (1); |
jurica238814 | 0:e61e7fc7cfe1 | 390 | // |
jurica238814 | 0:e61e7fc7cfe1 | 391 | // Handle specifiers |
jurica238814 | 0:e61e7fc7cfe1 | 392 | // |
jurica238814 | 0:e61e7fc7cfe1 | 393 | switch (c) { |
jurica238814 | 0:e61e7fc7cfe1 | 394 | case 'c': { |
jurica238814 | 0:e61e7fc7cfe1 | 395 | char c0; |
jurica238814 | 0:e61e7fc7cfe1 | 396 | v = va_arg(*pParamList, int); |
jurica238814 | 0:e61e7fc7cfe1 | 397 | c0 = (char)v; |
jurica238814 | 0:e61e7fc7cfe1 | 398 | _StoreChar(&BufferDesc, c0); |
jurica238814 | 0:e61e7fc7cfe1 | 399 | break; |
jurica238814 | 0:e61e7fc7cfe1 | 400 | } |
jurica238814 | 0:e61e7fc7cfe1 | 401 | case 'd': |
jurica238814 | 0:e61e7fc7cfe1 | 402 | v = va_arg(*pParamList, int); |
jurica238814 | 0:e61e7fc7cfe1 | 403 | _PrintInt(&BufferDesc, v, 10u, NumDigits, FieldWidth, FormatFlags); |
jurica238814 | 0:e61e7fc7cfe1 | 404 | break; |
jurica238814 | 0:e61e7fc7cfe1 | 405 | case 'u': |
jurica238814 | 0:e61e7fc7cfe1 | 406 | v = va_arg(*pParamList, int); |
jurica238814 | 0:e61e7fc7cfe1 | 407 | _PrintUnsigned(&BufferDesc, (unsigned)v, 10u, NumDigits, FieldWidth, FormatFlags); |
jurica238814 | 0:e61e7fc7cfe1 | 408 | break; |
jurica238814 | 0:e61e7fc7cfe1 | 409 | case 'x': |
jurica238814 | 0:e61e7fc7cfe1 | 410 | case 'X': |
jurica238814 | 0:e61e7fc7cfe1 | 411 | v = va_arg(*pParamList, int); |
jurica238814 | 0:e61e7fc7cfe1 | 412 | _PrintUnsigned(&BufferDesc, (unsigned)v, 16u, NumDigits, FieldWidth, FormatFlags); |
jurica238814 | 0:e61e7fc7cfe1 | 413 | break; |
jurica238814 | 0:e61e7fc7cfe1 | 414 | case 's': |
jurica238814 | 0:e61e7fc7cfe1 | 415 | { |
jurica238814 | 0:e61e7fc7cfe1 | 416 | const char * s = va_arg(*pParamList, const char *); |
jurica238814 | 0:e61e7fc7cfe1 | 417 | do { |
jurica238814 | 0:e61e7fc7cfe1 | 418 | c = *s; |
jurica238814 | 0:e61e7fc7cfe1 | 419 | s++; |
jurica238814 | 0:e61e7fc7cfe1 | 420 | if (c == '\0') { |
jurica238814 | 0:e61e7fc7cfe1 | 421 | break; |
jurica238814 | 0:e61e7fc7cfe1 | 422 | } |
jurica238814 | 0:e61e7fc7cfe1 | 423 | _StoreChar(&BufferDesc, c); |
jurica238814 | 0:e61e7fc7cfe1 | 424 | } while (BufferDesc.ReturnValue >= 0); |
jurica238814 | 0:e61e7fc7cfe1 | 425 | } |
jurica238814 | 0:e61e7fc7cfe1 | 426 | break; |
jurica238814 | 0:e61e7fc7cfe1 | 427 | case 'p': |
jurica238814 | 0:e61e7fc7cfe1 | 428 | v = va_arg(*pParamList, int); |
jurica238814 | 0:e61e7fc7cfe1 | 429 | _PrintUnsigned(&BufferDesc, (unsigned)v, 16u, 8u, 8u, 0u); |
jurica238814 | 0:e61e7fc7cfe1 | 430 | break; |
jurica238814 | 0:e61e7fc7cfe1 | 431 | case '%': |
jurica238814 | 0:e61e7fc7cfe1 | 432 | _StoreChar(&BufferDesc, '%'); |
jurica238814 | 0:e61e7fc7cfe1 | 433 | break; |
jurica238814 | 0:e61e7fc7cfe1 | 434 | default: |
jurica238814 | 0:e61e7fc7cfe1 | 435 | break; |
jurica238814 | 0:e61e7fc7cfe1 | 436 | } |
jurica238814 | 0:e61e7fc7cfe1 | 437 | sFormat++; |
jurica238814 | 0:e61e7fc7cfe1 | 438 | } else { |
jurica238814 | 0:e61e7fc7cfe1 | 439 | _StoreChar(&BufferDesc, c); |
jurica238814 | 0:e61e7fc7cfe1 | 440 | } |
jurica238814 | 0:e61e7fc7cfe1 | 441 | } while (BufferDesc.ReturnValue >= 0); |
jurica238814 | 0:e61e7fc7cfe1 | 442 | |
jurica238814 | 0:e61e7fc7cfe1 | 443 | if (BufferDesc.ReturnValue > 0) { |
jurica238814 | 0:e61e7fc7cfe1 | 444 | // |
jurica238814 | 0:e61e7fc7cfe1 | 445 | // Write remaining data, if any |
jurica238814 | 0:e61e7fc7cfe1 | 446 | // |
jurica238814 | 0:e61e7fc7cfe1 | 447 | if (BufferDesc.Cnt != 0u) { |
jurica238814 | 0:e61e7fc7cfe1 | 448 | SEGGER_RTT_Write(BufferIndex, acBuffer, BufferDesc.Cnt); |
jurica238814 | 0:e61e7fc7cfe1 | 449 | } |
jurica238814 | 0:e61e7fc7cfe1 | 450 | BufferDesc.ReturnValue += (int)BufferDesc.Cnt; |
jurica238814 | 0:e61e7fc7cfe1 | 451 | } |
jurica238814 | 0:e61e7fc7cfe1 | 452 | return BufferDesc.ReturnValue; |
jurica238814 | 0:e61e7fc7cfe1 | 453 | } |
jurica238814 | 0:e61e7fc7cfe1 | 454 | |
jurica238814 | 0:e61e7fc7cfe1 | 455 | /********************************************************************* |
jurica238814 | 0:e61e7fc7cfe1 | 456 | * |
jurica238814 | 0:e61e7fc7cfe1 | 457 | * SEGGER_RTT_printf |
jurica238814 | 0:e61e7fc7cfe1 | 458 | * |
jurica238814 | 0:e61e7fc7cfe1 | 459 | * Function description |
jurica238814 | 0:e61e7fc7cfe1 | 460 | * Stores a formatted string in SEGGER RTT control block. |
jurica238814 | 0:e61e7fc7cfe1 | 461 | * This data is read by the host. |
jurica238814 | 0:e61e7fc7cfe1 | 462 | * |
jurica238814 | 0:e61e7fc7cfe1 | 463 | * Parameters |
jurica238814 | 0:e61e7fc7cfe1 | 464 | * BufferIndex Index of "Up"-buffer to be used. (e.g. 0 for "Terminal") |
jurica238814 | 0:e61e7fc7cfe1 | 465 | * sFormat Pointer to format string, followed by the arguments for conversion |
jurica238814 | 0:e61e7fc7cfe1 | 466 | * |
jurica238814 | 0:e61e7fc7cfe1 | 467 | * Return values |
jurica238814 | 0:e61e7fc7cfe1 | 468 | * >= 0: Number of bytes which have been stored in the "Up"-buffer. |
jurica238814 | 0:e61e7fc7cfe1 | 469 | * < 0: Error |
jurica238814 | 0:e61e7fc7cfe1 | 470 | * |
jurica238814 | 0:e61e7fc7cfe1 | 471 | * Notes |
jurica238814 | 0:e61e7fc7cfe1 | 472 | * (1) Conversion specifications have following syntax: |
jurica238814 | 0:e61e7fc7cfe1 | 473 | * %[flags][FieldWidth][.Precision]ConversionSpecifier |
jurica238814 | 0:e61e7fc7cfe1 | 474 | * (2) Supported flags: |
jurica238814 | 0:e61e7fc7cfe1 | 475 | * -: Left justify within the field width |
jurica238814 | 0:e61e7fc7cfe1 | 476 | * +: Always print sign extension for signed conversions |
jurica238814 | 0:e61e7fc7cfe1 | 477 | * 0: Pad with 0 instead of spaces. Ignored when using '-'-flag or precision |
jurica238814 | 0:e61e7fc7cfe1 | 478 | * Supported conversion specifiers: |
jurica238814 | 0:e61e7fc7cfe1 | 479 | * c: Print the argument as one char |
jurica238814 | 0:e61e7fc7cfe1 | 480 | * d: Print the argument as a signed integer |
jurica238814 | 0:e61e7fc7cfe1 | 481 | * u: Print the argument as an unsigned integer |
jurica238814 | 0:e61e7fc7cfe1 | 482 | * x: Print the argument as an hexadecimal integer |
jurica238814 | 0:e61e7fc7cfe1 | 483 | * s: Print the string pointed to by the argument |
jurica238814 | 0:e61e7fc7cfe1 | 484 | * p: Print the argument as an 8-digit hexadecimal integer. (Argument shall be a pointer to void.) |
jurica238814 | 0:e61e7fc7cfe1 | 485 | */ |
jurica238814 | 0:e61e7fc7cfe1 | 486 | int SEGGER_RTT_printf(unsigned BufferIndex, const char * sFormat, ...) { |
jurica238814 | 0:e61e7fc7cfe1 | 487 | va_list ParamList; |
jurica238814 | 0:e61e7fc7cfe1 | 488 | |
jurica238814 | 0:e61e7fc7cfe1 | 489 | va_start(ParamList, sFormat); |
jurica238814 | 0:e61e7fc7cfe1 | 490 | return SEGGER_RTT_vprintf(BufferIndex, sFormat, &ParamList); |
jurica238814 | 0:e61e7fc7cfe1 | 491 | } |
jurica238814 | 0:e61e7fc7cfe1 | 492 | /*************************** End of file ****************************/ |
jurica238814 | 0:e61e7fc7cfe1 | 493 |