[1bce637] | 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 | |
---|
[9c49db4] | 21 | #if HAVE_CONFIG_H |
---|
| 22 | #include "config.h" |
---|
| 23 | #endif |
---|
[1bce637] | 24 | |
---|
| 25 | #include <stdarg.h> |
---|
| 26 | #include <stdio.h> |
---|
[29e1c006] | 27 | #include <rtems/bspIo.h> |
---|
[1bce637] | 28 | |
---|
| 29 | /*-------------------------------------------------------------------------+ |
---|
| 30 | | Function: printNum |
---|
| 31 | | Description: print number in a given base. |
---|
| 32 | | Global Variables: None. |
---|
| 33 | | Arguments: num - number to print, base - base used to print the number. |
---|
[50f32b11] | 34 | | Returns: Nothing. |
---|
[1bce637] | 35 | +--------------------------------------------------------------------------*/ |
---|
| 36 | static void |
---|
| 37 | printNum(long unsigned int num, int base, int sign, int maxwidth, int lead) |
---|
| 38 | { |
---|
| 39 | long unsigned int n; |
---|
| 40 | int count; |
---|
| 41 | char toPrint[20]; |
---|
| 42 | |
---|
| 43 | if ( (sign == 1) && ((long)num < 0) ) { |
---|
| 44 | BSP_output_char('-'); |
---|
| 45 | num = -num; |
---|
[ceead58] | 46 | if (maxwidth) maxwidth--; |
---|
[1bce637] | 47 | } |
---|
[50f32b11] | 48 | |
---|
[1bce637] | 49 | count = 0; |
---|
| 50 | while ((n = num / base) > 0) { |
---|
| 51 | toPrint[count++] = (num - (n*base)); |
---|
| 52 | num = n ; |
---|
| 53 | } |
---|
| 54 | toPrint[count++] = num; |
---|
| 55 | |
---|
| 56 | if (maxwidth) { |
---|
| 57 | for (n=maxwidth-count ; n ; n-- ) |
---|
| 58 | BSP_output_char(lead); |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | for (n = 0; n < count; n++){ |
---|
| 62 | BSP_output_char("0123456789ABCDEF"[(int)(toPrint[count-(n+1)])]); |
---|
| 63 | } |
---|
| 64 | } /* printNum */ |
---|
| 65 | |
---|
| 66 | |
---|
| 67 | /*-------------------------------------------------------------------------+ |
---|
| 68 | | Function: printk |
---|
| 69 | | Description: a simplified version of printf intended for use when the |
---|
| 70 | console is not yet initialized or in ISR's. |
---|
| 71 | | Global Variables: None. |
---|
| 72 | | Arguments: as in printf: fmt - format string, ... - unnamed arguments. |
---|
[50f32b11] | 73 | | Returns: Nothing. |
---|
[1bce637] | 74 | +--------------------------------------------------------------------------*/ |
---|
| 75 | void |
---|
[108bab3] | 76 | vprintk(char *fmt, va_list ap) |
---|
[1bce637] | 77 | { |
---|
| 78 | char c, *str; |
---|
| 79 | int lflag, base, sign, width, lead; |
---|
| 80 | /* unsigned int level; */ |
---|
| 81 | |
---|
| 82 | /* _CPU_ISR_Disable(level); */ |
---|
[50f32b11] | 83 | |
---|
[1bce637] | 84 | for (; *fmt != '\0'; fmt++) |
---|
| 85 | { |
---|
| 86 | lflag = 0; |
---|
| 87 | base = 0; |
---|
| 88 | sign = 0; |
---|
| 89 | width = 0; |
---|
| 90 | lead = ' '; |
---|
| 91 | if (*fmt == '%') |
---|
| 92 | { |
---|
| 93 | fmt++; |
---|
| 94 | if (*fmt == '0' ) { |
---|
| 95 | lead = '0'; |
---|
| 96 | fmt++; |
---|
| 97 | } |
---|
| 98 | while (*fmt >= '0' && *fmt <= '9' ) { |
---|
| 99 | width *= 10; |
---|
| 100 | width += (*fmt - '0'); |
---|
| 101 | fmt++; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | if ((c = *fmt) == 'l') |
---|
| 105 | { |
---|
| 106 | lflag = 1; |
---|
| 107 | c = *++fmt; |
---|
| 108 | } |
---|
| 109 | switch (c) |
---|
| 110 | { |
---|
| 111 | case 'o': case 'O': base = 8; sign = 0; break; |
---|
[1f5a1afe] | 112 | case 'i': case 'I': |
---|
[1bce637] | 113 | case 'd': case 'D': base = 10; sign = 1; break; |
---|
| 114 | case 'u': case 'U': base = 10; sign = 0; break; |
---|
| 115 | case 'x': case 'X': base = 16; sign = 0; break; |
---|
| 116 | case 's': |
---|
[50f32b11] | 117 | for (str = va_arg(ap, char *); *str; str++) |
---|
[1bce637] | 118 | BSP_output_char(*str); |
---|
| 119 | break; |
---|
| 120 | case 'c': |
---|
[a3726b1] | 121 | #if 0 |
---|
[1bce637] | 122 | #if defined(_TMS320C3x) || defined(_TMS320C4x) |
---|
| 123 | BSP_output_char(va_arg(ap, int)); |
---|
| 124 | #else |
---|
| 125 | BSP_output_char(va_arg(ap, char)); |
---|
[a3726b1] | 126 | #endif |
---|
| 127 | #else |
---|
| 128 | BSP_output_char(va_arg(ap, int)); |
---|
[1bce637] | 129 | #endif |
---|
| 130 | break; |
---|
| 131 | default: |
---|
| 132 | BSP_output_char(c); |
---|
| 133 | break; |
---|
| 134 | } /* switch*/ |
---|
| 135 | |
---|
| 136 | if (base) |
---|
| 137 | printNum(lflag ? va_arg(ap, long int) : (long int)va_arg(ap, int), |
---|
| 138 | base, sign, width, lead); |
---|
| 139 | } |
---|
| 140 | else |
---|
| 141 | { |
---|
| 142 | BSP_output_char(*fmt); |
---|
| 143 | } |
---|
| 144 | } |
---|
| 145 | /* _CPU_ISR_Enable(level); */ |
---|
| 146 | |
---|
[108bab3] | 147 | } /* vprintk */ |
---|
| 148 | |
---|
| 149 | void |
---|
| 150 | printk(char *fmt, ...) |
---|
| 151 | { |
---|
| 152 | va_list ap; /* points to each unnamed argument in turn */ |
---|
[50f32b11] | 153 | |
---|
[108bab3] | 154 | va_start(ap, fmt); /* make ap point to 1st unnamed arg */ |
---|
| 155 | vprintk(fmt, ap); |
---|
| 156 | va_end(ap); /* clean up when done */ |
---|
[1bce637] | 157 | } /* printk */ |
---|