1 | /*-------------------------------------------------------------------------+ |
---|
2 | | printk.c v1.1 - PC386 BSP - 1997/08/07 |
---|
3 | +--------------------------------------------------------------------------+ |
---|
4 | | (C) Copyright 1997 - |
---|
5 | | - NavIST Group - Real-Time Distributed Systems and Industrial Automation |
---|
6 | | |
---|
7 | | http://pandora.ist.utl.pt |
---|
8 | | |
---|
9 | | Instituto Superior Tecnico * Lisboa * PORTUGAL |
---|
10 | +--------------------------------------------------------------------------+ |
---|
11 | | Disclaimer: |
---|
12 | | |
---|
13 | | This file is provided "AS IS" without warranty of any kind, either |
---|
14 | | expressed or implied. |
---|
15 | +--------------------------------------------------------------------------+ |
---|
16 | | This code is based on code by: Jose Rufino - IST |
---|
17 | | |
---|
18 | | $Id$ |
---|
19 | +--------------------------------------------------------------------------*/ |
---|
20 | |
---|
21 | #if HAVE_CONFIG_H |
---|
22 | #include "config.h" |
---|
23 | #endif |
---|
24 | |
---|
25 | #include <stdarg.h> |
---|
26 | #include <stdio.h> |
---|
27 | #include <bspIo.h> |
---|
28 | /* #include <libcpu/cpu.h> */ |
---|
29 | |
---|
30 | /*-------------------------------------------------------------------------+ |
---|
31 | | Function: printNum |
---|
32 | | Description: print number in a given base. |
---|
33 | | Global Variables: None. |
---|
34 | | Arguments: num - number to print, base - base used to print the number. |
---|
35 | | Returns: Nothing. |
---|
36 | +--------------------------------------------------------------------------*/ |
---|
37 | static void |
---|
38 | printNum(long unsigned int num, int base, int sign, int maxwidth, int lead) |
---|
39 | { |
---|
40 | long unsigned int n; |
---|
41 | int count; |
---|
42 | char toPrint[20]; |
---|
43 | |
---|
44 | if ( (sign == 1) && ((long)num < 0) ) { |
---|
45 | BSP_output_char('-'); |
---|
46 | num = -num; |
---|
47 | if (maxwidth) maxwidth--; |
---|
48 | } |
---|
49 | |
---|
50 | count = 0; |
---|
51 | while ((n = num / base) > 0) { |
---|
52 | toPrint[count++] = (num - (n*base)); |
---|
53 | num = n ; |
---|
54 | } |
---|
55 | toPrint[count++] = num; |
---|
56 | |
---|
57 | if (maxwidth) { |
---|
58 | for (n=maxwidth-count ; n ; n-- ) |
---|
59 | BSP_output_char(lead); |
---|
60 | } |
---|
61 | |
---|
62 | for (n = 0; n < count; n++){ |
---|
63 | BSP_output_char("0123456789ABCDEF"[(int)(toPrint[count-(n+1)])]); |
---|
64 | } |
---|
65 | } /* printNum */ |
---|
66 | |
---|
67 | |
---|
68 | /*-------------------------------------------------------------------------+ |
---|
69 | | Function: printk |
---|
70 | | Description: a simplified version of printf intended for use when the |
---|
71 | console is not yet initialized or in ISR's. |
---|
72 | | Global Variables: None. |
---|
73 | | Arguments: as in printf: fmt - format string, ... - unnamed arguments. |
---|
74 | | Returns: Nothing. |
---|
75 | +--------------------------------------------------------------------------*/ |
---|
76 | void |
---|
77 | printk(char *fmt, ...) |
---|
78 | { |
---|
79 | va_list ap; /* points to each unnamed argument in turn */ |
---|
80 | char c, *str; |
---|
81 | int lflag, base, sign, width, lead; |
---|
82 | /* unsigned int level; */ |
---|
83 | |
---|
84 | /* _CPU_ISR_Disable(level); */ |
---|
85 | |
---|
86 | va_start(ap, fmt); /* make ap point to 1st unnamed arg */ |
---|
87 | for (; *fmt != '\0'; fmt++) |
---|
88 | { |
---|
89 | lflag = 0; |
---|
90 | base = 0; |
---|
91 | sign = 0; |
---|
92 | width = 0; |
---|
93 | lead = ' '; |
---|
94 | if (*fmt == '%') |
---|
95 | { |
---|
96 | fmt++; |
---|
97 | if (*fmt == '0' ) { |
---|
98 | lead = '0'; |
---|
99 | fmt++; |
---|
100 | } |
---|
101 | while (*fmt >= '0' && *fmt <= '9' ) { |
---|
102 | width *= 10; |
---|
103 | width += (*fmt - '0'); |
---|
104 | fmt++; |
---|
105 | } |
---|
106 | |
---|
107 | if ((c = *fmt) == 'l') |
---|
108 | { |
---|
109 | lflag = 1; |
---|
110 | c = *++fmt; |
---|
111 | } |
---|
112 | switch (c) |
---|
113 | { |
---|
114 | case 'o': case 'O': base = 8; sign = 0; break; |
---|
115 | case 'd': case 'D': base = 10; sign = 1; break; |
---|
116 | case 'u': case 'U': base = 10; sign = 0; break; |
---|
117 | case 'x': case 'X': base = 16; sign = 0; break; |
---|
118 | case 's': |
---|
119 | for (str = va_arg(ap, char *); *str; str++) |
---|
120 | BSP_output_char(*str); |
---|
121 | break; |
---|
122 | case 'c': |
---|
123 | #if 0 |
---|
124 | #if defined(_TMS320C3x) || defined(_TMS320C4x) |
---|
125 | BSP_output_char(va_arg(ap, int)); |
---|
126 | #else |
---|
127 | BSP_output_char(va_arg(ap, char)); |
---|
128 | #endif |
---|
129 | #else |
---|
130 | BSP_output_char(va_arg(ap, int)); |
---|
131 | #endif |
---|
132 | break; |
---|
133 | default: |
---|
134 | BSP_output_char(c); |
---|
135 | break; |
---|
136 | } /* switch*/ |
---|
137 | |
---|
138 | if (base) |
---|
139 | printNum(lflag ? va_arg(ap, long int) : (long int)va_arg(ap, int), |
---|
140 | base, sign, width, lead); |
---|
141 | } |
---|
142 | else |
---|
143 | { |
---|
144 | BSP_output_char(*fmt); |
---|
145 | } |
---|
146 | } |
---|
147 | va_end(ap); /* clean up when done */ |
---|
148 | /* _CPU_ISR_Enable(level); */ |
---|
149 | |
---|
150 | } /* printk */ |
---|
151 | |
---|