source: rtems/c/src/lib/libbsp/arm/shared/io/printk.c @ 08330bf

4.104.114.84.95
Last change on this file since 08330bf was 08330bf, checked in by Joel Sherrill <joel.sherrill@…>, on 07/27/00 at 01:04:11

Port of RTEMS to the ARM processor family by Eric Valette
<valette@…> and Emmanuel Raguet <raguet@…>
of Canon CRF - Communication Dept. This port includes a
basic BSP that is sufficient to link hello world.

  • Property mode set to 100644
File size: 3.1 KB
Line 
1/*-------------------------------------------------------------------------+
2| printk.c - ARM BSP
3+--------------------------------------------------------------------------+
4|
5|   COPYRIGHT (c) 2000 Canon Research France SA.
6|   Emmanuel Raguet, mailto:raguet@crf.canon.fr
7|
8|   The license and distribution terms for this file may be
9|   found in found in the file LICENSE in this distribution or at
10|   http://www.OARcorp.com/rtems/license.html.
11|
12|  $Id$
13+--------------------------------------------------------------------------*/
14
15
16#include <stdarg.h>
17#include <stdio.h>
18#include <bspio.h>
19
20/*-------------------------------------------------------------------------+
21|         Function: printNum
22|      Description: print number in a given base.
23| Global Variables: None.
24|        Arguments: num - number to print, base - base used to print the number.
25|          Returns: Nothing.
26+--------------------------------------------------------------------------*/
27static void
28printNum(long unsigned int num, int base, int sign)
29{
30  long unsigned int n;
31  int count;
32  char toPrint[20];
33
34  if ( (sign == 1) && ((long)num <  0) ) {
35    BSP_output_char('-');
36    num = -num;
37  }
38 
39  count = 0;
40  while ((n = num / base) > 0) {
41    toPrint[count++] = (num - (n*base));
42    num = n ;
43  }
44  toPrint[count++] = num;
45
46  for (n = 0; n < count; n++){
47    BSP_output_char("0123456789ABCDEF"[(int)(toPrint[count-(n+1)])]);
48  }
49} /* printNum */
50
51
52/*-------------------------------------------------------------------------+
53|         Function: printk
54|      Description: a simplified version of printf intended for use when the
55                    console is not yet initialized or in ISR's.
56| Global Variables: None.
57|        Arguments: as in printf: fmt - format string, ... - unnamed arguments.
58|          Returns: Nothing.
59+--------------------------------------------------------------------------*/
60void
61printk(char *fmt, ...)
62{
63  va_list  ap;      /* points to each unnamed argument in turn */
64  char     c, *str;
65  int      lflag, base, sign;
66
67  _CPU_ISR_Disable(level);
68 
69  va_start(ap, fmt); /* make ap point to 1st unnamed arg */
70  for (; *fmt != '\0'; fmt++)
71  {
72    lflag = 0;
73    base  = 0;
74    sign = 0;
75    if (*fmt == '%')
76    {
77      if ((c = *++fmt) == 'l')
78      {
79        lflag = 1;
80        c = *++fmt;
81      }
82      switch (c)
83      {
84        case 'o': case 'O': base = 8; sign = 0; break;
85        case 'd': case 'D': base = 10; sign = 1; break;
86        case 'u': case 'U': base = 10; sign = 0; break;
87        case 'x': case 'X': base = 16; sign = 0; break;
88        case 's':
89          for (str = va_arg(ap, char *); *str; str++)
90            BSP_output_char(*str);
91          break;
92        case 'c':
93          BSP_output_char(va_arg(ap, char));
94          break;
95        default:
96          BSP_output_char(c);
97          break;
98      } /* switch*/
99
100      if (base)
101        printNum(lflag ? va_arg(ap, long int) : (long int)va_arg(ap, int),
102                 base, sign);
103    }
104    else
105    {
106      BSP_output_char(*fmt);
107    }
108  }
109  va_end(ap); /* clean up when done */
110  _CPU_ISR_Enable(level);
111
112} /* printk */
113
Note: See TracBrowser for help on using the repository browser.