summaryrefslogtreecommitdiff
path: root/bsp/ht32/libraries/HT32_STD_5xxxx_FWLib/library/HT32F5xxxx_Driver/src/printf.c
blob: 4f3515330c23a35e9193ede66512fcbad020f93c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*********************************************************************************************************//**
 * @file    printf.c
 * @version $Rev:: 93           $
 * @date    $Date:: 2015-11-24 #$
 * @brief   Print functions.
 *************************************************************************************************************
 * @attention
 *
 * Firmware Disclaimer Information
 *
 * 1. The customer hereby acknowledges and agrees that the program technical documentation, including the
 *    code, which is supplied by Holtek Semiconductor Inc., (hereinafter referred to as "HOLTEK") is the
 *    proprietary and confidential intellectual property of HOLTEK, and is protected by copyright law and
 *    other intellectual property laws.
 *
 * 2. The customer hereby acknowledges and agrees that the program technical documentation, including the
 *    code, is confidential information belonging to HOLTEK, and must not be disclosed to any third parties
 *    other than HOLTEK and the customer.
 *
 * 3. The program technical documentation, including the code, is provided "as is" and for customer reference
 *    only. After delivery by HOLTEK, the customer shall use the program technical documentation, including
 *    the code, at their own risk. HOLTEK disclaims any expressed, implied or statutory warranties, including
 *    the warranties of merchantability, satisfactory quality and fitness for a particular purpose.
 *
 * <h2><center>Copyright (C) Holtek Semiconductor Inc. All rights reserved</center></h2>
 ************************************************************************************************************/

/* Includes ------------------------------------------------------------------------------------------------*/
#include "ht32.h"
#include <string.h>

/** @addtogroup HT32_Peripheral_Driver HT32 Peripheral Driver
  * @{
  */

/** @defgroup PRINTF printf re-implementation
  * @brief printf related functions
  * @{
  */


/* Private macro -------------------------------------------------------------------------------------------*/
/** @defgroup PRINTF_Private_Macro printf private macros
  * @{
  */
#define vaStart(list, param) list = (char*)((int)&param + sizeof(param))
#define vaArg(list, type) ((type *)(list += sizeof(type)))[-1]
/**
  * @}
  */

/* Private function prototypes -----------------------------------------------------------------------------*/
static const char *FormatItem(const char *f, int a);
static void PutRepChar(const char c, int count);
static int PutString(const char *pString);
static int PutStringReverse(const char *pString, int index);
static void PutNumber(int value, int radix, int width, char fill);

/* Global functions ----------------------------------------------------------------------------------------*/
/** @defgroup PRINTF_Exported_Functions printf exported functions
  * @{
  */
/*********************************************************************************************************//**
 * @brief Print function.
 * @param f: Format string.
 * @retval String length.
 ************************************************************************************************************/
signed int printf(const char *f, ...)
{
  char *argP;
  int i = 0;

  vaStart(argP, f);
  while (*f)
  {
    if (*f == '%')
    {
      f = FormatItem(f + 1, vaArg(argP, int));
    }
    else
    {
      fputc(*f++, (FILE *)1);
    }
    i++;
  }
  return i;
}

/*********************************************************************************************************//**
 * @brief Put string.
 * @param pString: String.
 * @retval String length.
 ************************************************************************************************************/
signed int puts(const char *pString)
{
  int i;
  i = PutString(pString);
  fputc('\n', (FILE *)1);
  return i;
}
/**
  * @}
  */

/* Private functions ---------------------------------------------------------------------------------------*/
/** @defgroup PRINTF_Private_Function printf private functions
  * @{
  */
/*********************************************************************************************************//**
 * @brief Format item for print function.
 * @param f: Format string.
 * @param a: Length of format string.
 * @retval Point of string.
 ************************************************************************************************************/
static const char *FormatItem(const char *f, int a)
{
  char c;
  int fieldwidth = 0;
  int leftjust = FALSE;
  int radix = 0;
  char fill = ' ';
  int i;

  if (*f == '0')
  {
    fill = '0';
  }

  while ((c = *f++) != 0)
  {
    if (c >= '0' && c <= '9')
    {
      fieldwidth = (fieldwidth * 10) + (c - '0');
    }
    else
    {
      switch (c)
      {
        case '\000':
        {
          return (--f);
        }
        case '%':
        {
          fputc('%', (FILE *)1);
          return (f);
        }
        case '-':
        {
          leftjust = TRUE;
          break;
        }
        case 'c':
        {
          if (leftjust)
          {
            fputc(a & 0x7f, (FILE *)f);
          }
          if (fieldwidth > 0)
          {
            PutRepChar(fill, fieldwidth - 1);
          }
          if (!leftjust)
          {
            fputc(a & 0x7f, (FILE *)f);
            return (f);
          }
        }
        case 's':
        {
          i = 0;
          while (*((char *)(a + i)) !='\0' )
          {
            i++;
          }

          if (leftjust)
          {
            PutString((char *)a);
          }

          if (fieldwidth > i )
          {
            PutRepChar(fill, fieldwidth - i);
          }

          if (!leftjust)
          {
            PutString((char *)a);
          }
          return (f);
        }
         case 'l':
        {
          radix = -10;
          f++;
          break;
        }
        case 'd':
        case 'i':
        {
          radix = -10;
          break;
        }
        case 'u':
        {
          radix = 10;
          break;
        }
        case 'x':
        case 'X':
        {
          radix = 16;
          break;
        }
        case 'o':
        {
          radix = 8;
          break;
        }
        default:
        {
          radix = 3;
          break;
        }
      }
    }
    if (radix)
    {
      break;
    }
  }

  if (leftjust)
  {
    fieldwidth = -fieldwidth;
  }

  PutNumber(a, radix, fieldwidth, fill);

  return (f);
}

/*********************************************************************************************************//**
 * @brief Put repeat character.
 * @param c: Character.
 * @param count: Repeat count
 ************************************************************************************************************/
static void PutRepChar(const char c, int count)
{
  while (count--)
  {
    fputc(c, (FILE *)1);
  }
}

/*********************************************************************************************************//**
 * @brief Put string.
 * @param pString: String.
 * @retval String length.
 ************************************************************************************************************/
static int PutString(const char *pString)
{
    int i = 0;
    while (*pString != '\0')
    {
        fputc(*pString, (FILE *)1);
        pString++;
        i++;
    }

    return i;
}

/*********************************************************************************************************//**
 * @brief Put string in reversed order.
 * @param pString: String.
 * @param index: String length
 * @retval String length.
 ************************************************************************************************************/
static int PutStringReverse(const char *pString, int index)
{
    int i = 0;
    while ((index--) > 0)
    {
        fputc(pString[index], (FILE *)1);
        i++;
    }
    return i;
}

/*********************************************************************************************************//**
 * @brief Put number.
 * @param value: Value of number.
 * @param radix: Radix of number.
 * @param width: Width of number.
 * @param fill: fill character.
 ************************************************************************************************************/
static void PutNumber(int value, int radix, int width, char fill)
{
  char buffer[8];
  int bi = 0;
  unsigned int uvalue;
  unsigned short digit;
  unsigned short left = FALSE;
  unsigned short negative = FALSE;

  if (fill == 0)
  {
    fill = ' ';
  }

  if (width < 0)
  {
   width = -width;
   left = TRUE;
  }

  if (width < 0 || width > 80)
  {
    width = 0;
  }

  if (radix < 0)
  {
    radix = -radix;
    if (value < 0)
    {
      negative = TRUE;
      value = -value;
    }
  }

  uvalue = value;

  do
  {
    if (radix != 16)
    {
      digit = uvalue % radix;
      uvalue = uvalue / radix;
    }
    else
    {
      digit = uvalue & 0xf;
      uvalue = uvalue >> 4;
    }
    buffer[bi] = digit + ((digit <= 9) ? '0' : ('A' - 10));
    bi++;
  }
  while (uvalue != 0);

  if (negative)
  {
    buffer[bi] = '-';
    bi += 1;
  }

  if (width <= bi)
  {
    PutStringReverse(buffer, bi);
  }
  else
  {
    width -= bi;
    if (!left)
    {
      PutRepChar(fill, width);
    }

    PutStringReverse(buffer, bi);

    if (left)
    {
      PutRepChar(fill, width);
    }
  }
}
/**
  * @}
  */


/**
  * @}
  */

/**
  * @}
  */