source: rtems/c/src/lib/libbsp/powerpc/mvme2307/console/printk.c @ 19ca797

4.104.114.84.95
Last change on this file since 19ca797 was 19ca797, checked in by Joel Sherrill <joel.sherrill@…>, on 10/04/99 at 20:41:28

Motorola MVME2307 BSP submitted by Jay Kulpinski <jskulpin@…>.
No modifications made.

  • Property mode set to 100644
File size: 5.8 KB
Line 
1#include <bsp.h>
2
3#if 1
4
5static void outbyte(char c) {
6    volatile char * const pc16550 = (char *) IO_TO_LOCAL(0x03f8);
7#   define THR  0
8#   define LSR  5
9#   define THRE 0x20
10    while ((pc16550[LSR] & THRE) == 0) {
11        ;
12    }
13    pc16550[THR] = c;
14}
15
16#else
17
18/* printk to memory for debugging */
19static void outbyte(char c) {
20    static char *memory_log = (char *) 0x01F00000;
21
22    *memory_log++ = c;
23    if (memory_log >= (char *) 0x01F80000) {
24        memory_log--;
25    }
26}
27
28#endif
29
30/*-------------------------------------------------------------------------+
31| printk.c v1.1 - PC386 BSP - 1997/08/07
32+--------------------------------------------------------------------------+
33| (C) Copyright 1997 -
34| - NavIST Group - Real-Time Distributed Systems and Industrial Automation
35|
36| http://pandora.ist.utl.pt
37|
38| Instituto Superior Tecnico * Lisboa * PORTUGAL
39+--------------------------------------------------------------------------+
40| Disclaimer:
41|
42| This file is provided "AS IS" without warranty of any kind, either
43| expressed or implied.
44+--------------------------------------------------------------------------+
45| This code is based on code by: Jose Rufino - IST
46|
47|  $Id$
48+--------------------------------------------------------------------------*/
49
50
51#include <stdarg.h>
52
53/*-------------------------------------------------------------------------+
54|         Function: printNum
55|      Description: print number in a given base.
56| Global Variables: None.
57|        Arguments: num - number to print, base - base used to print the number.
58|          Returns: Nothing.
59+--------------------------------------------------------------------------*/
60static void printNum(long unsigned int num, int base, int sign, int width,
61                     int zerofill) {
62    long unsigned int n;
63    int count, negative = 0;
64    char toPrint[80];
65    char *digits = "0123456789ABCDEF";
66
67    if (width > sizeof(toPrint)) {
68        width = sizeof(toPrint);
69    }
70    if ((sign == 1) && ((long) num < 0)) {
71        negative = 1;
72        num = -num;
73    }
74
75    count = 0;
76    while ((n = num / base) > 0) {
77        toPrint[count++] = digits[num - (n * base)];
78        num = n;
79    }
80    toPrint[count++] = digits[num];
81    if (count + negative < width) {
82        /* this needs to be padded out to width */
83        if (zerofill) {
84            while (count + negative < width) {
85                toPrint[count++] = '0';
86            }
87            if (negative) {
88                toPrint[count++] = '-';
89            }
90        } else {
91            if (negative) {
92                toPrint[count++] = '-';
93            }
94            while (count < width) {
95                toPrint[count++] = ' ';
96            }
97        }
98    } else if (negative) {
99        toPrint[count++] = '-';
100    }
101
102    for (n = 0; n < count; n++) {
103        outbyte(toPrint[count - (n + 1)]);
104    }
105}
106
107
108/*-------------------------------------------------------------------------+
109|         Function: printk
110|      Description: a simplified version of printf intended for use when the
111                    console is not yet initialized or in ISR's.
112| Global Variables: None.
113|        Arguments: as in printf: fmt - format string, ... - unnamed arguments.
114|          Returns: Nothing.
115+--------------------------------------------------------------------------*/
116int printk_enabled = 1;
117
118void printk(char *fmt, ...) {
119    va_list ap;                 /* points to each unnamed argument in turn */
120    char c, *str;
121    int lflag, base, sign, width, zero;
122
123    /* disable interrupts???  */
124
125    if (printk_enabled) {
126        va_start(ap, fmt);              /* make ap point to 1st unnamed arg */
127        for (; *fmt != '\0'; fmt++) {
128            lflag = 0;
129            base = 0;
130            sign = 0;
131            width = 0;
132            if (*fmt == '\n') {
133                outbyte('\r');
134            }
135            if (*fmt == '%') {
136                c = *++fmt;
137                if (c == '0') {
138                    zero = 1;
139                    c = *++fmt;
140                } else {
141                    zero = 0;
142                }
143                for (; c >= '0' && c <= '9'; c = *++fmt) {
144                    width = width * 10 + (c - '0');
145                }
146                if (c == 'l') {
147                    lflag = 1;
148                    c = *++fmt;
149                }
150                switch (c) {
151                    case 'o':
152                    case 'O':
153                        base = 8;
154                        sign = 0;
155                        break;
156                    case 'd':
157                    case 'D':
158                        base = 10;
159                        sign = 1;
160                        break;
161                    case 'u':
162                    case 'U':
163                        base = 10;
164                        sign = 0;
165                        break;
166                    case 'x':
167                    case 'X':
168                        base = 16;
169                        sign = 0;
170                        break;
171                    case 's':
172                        for (str = va_arg(ap, char *); *str; str++) {
173                            outbyte(*str);
174                            width--;
175                        }
176                        while (width-- > 0) {
177                            outbyte(' ');
178                        }
179                        break;
180                    case 'c':
181                        outbyte(va_arg(ap, int));
182                        break;
183                    default:
184                        outbyte(c);
185                        break;
186                }
187
188                if (base) {
189                    printNum(lflag ? va_arg(ap, long int) :
190                             (long int) va_arg(ap, int), base, sign, width, zero);
191                }
192            } else {
193                outbyte(*fmt);
194            }
195        }
196        va_end(ap);                     /* clean up when done */
197        /* enable interrupts???  */
198    }
199}
Note: See TracBrowser for help on using the repository browser.