From 1bce63758768e6ef94426fd11d63478f2cec1306 Mon Sep 17 00:00:00 2001 From: Joel Sherrill Date: Mon, 12 Jun 2000 16:37:04 +0000 Subject: Added printk as a generic, shared routine. --- c/src/exec/include/rtems/bspIo.h | 37 +++++++++ c/src/exec/libcsupport/src/Makefile.am | 3 +- c/src/exec/libcsupport/src/printk.c | 144 +++++++++++++++++++++++++++++++++ c/src/lib/include/Makefile.am | 2 +- c/src/lib/include/bspIo.h | 37 +++++++++ c/src/lib/libc/Makefile.am | 3 +- c/src/lib/libc/printk.c | 144 +++++++++++++++++++++++++++++++++ 7 files changed, 367 insertions(+), 3 deletions(-) create mode 100644 c/src/exec/include/rtems/bspIo.h create mode 100644 c/src/exec/libcsupport/src/printk.c create mode 100644 c/src/lib/include/bspIo.h create mode 100644 c/src/lib/libc/printk.c (limited to 'c/src') diff --git a/c/src/exec/include/rtems/bspIo.h b/c/src/exec/include/rtems/bspIo.h new file mode 100644 index 0000000000..0ff070c4c5 --- /dev/null +++ b/c/src/exec/include/rtems/bspIo.h @@ -0,0 +1,37 @@ +/* bspIo.h + * + * This include file contains declaration of interface that + * will be provided by the file contained in this directory. + * + * + * COPYRIGHT (c) 1998 valette@crf.canon.fr + * + * The license and distribution terms for this file may be + * found in found in the file LICENSE in this distribution or at + * http://www.OARcorp.com/rtems/license.html. + * + * $Id$ + */ +#ifndef _LIBBSP_I386_SHARED_IO_BSP_IO_H +#define _LIBBSP_I386_SHARED_IO_BSP_IO_H + +/* + * All the functions declared as extern after this comment + * MUST be implemented in each BSP. Using this function, + * this directory contains shared code that export higher level + * functionnality described after the next command. + */ +typedef void (*BSP_output_char_function_type) (char c); +typedef char (*BSP_polling_getchar_function_type) (void); + +extern BSP_output_char_function_type BSP_output_char; +extern BSP_polling_getchar_function_type BSP_poll_char; +/* + * All the function declared as extern after this comment + * are available for each ix86 BSP by compiling and linking + * the files contained in this directory PROVIDED definition + * and initialisation of the previous variable are done. + */ +void printk(char *fmt, ...); + +#endif diff --git a/c/src/exec/libcsupport/src/Makefile.am b/c/src/exec/libcsupport/src/Makefile.am index 54b674b2f7..0f4a4cef2e 100644 --- a/c/src/exec/libcsupport/src/Makefile.am +++ b/c/src/exec/libcsupport/src/Makefile.am @@ -48,7 +48,8 @@ LIBC_GLUE_C_FILES = __getpid.c __gettod.c __times.c truncate.c access.c \ UNIX_LIBC_C_FILES = unixlibc.c hosterr.c -COMMON_C_FILES = gxx_wrappers.c $(BASE_FS_C_FILES) $(MALLOC_C_FILES) $(TERMIOS_C_FILES) \ +COMMON_C_FILES = gxx_wrappers.c printk.c $(BASE_FS_C_FILES) \ + $(MALLOC_C_FILES) $(TERMIOS_C_FILES) \ $(ERROR_C_FILES) $(ASSOCIATION_C_FILES) UNIX_C_FILES = $(UNIX_LIBC_C_FILES) imfs_unixstub.c diff --git a/c/src/exec/libcsupport/src/printk.c b/c/src/exec/libcsupport/src/printk.c new file mode 100644 index 0000000000..253e5f1abc --- /dev/null +++ b/c/src/exec/libcsupport/src/printk.c @@ -0,0 +1,144 @@ +/*-------------------------------------------------------------------------+ +| printk.c v1.1 - PC386 BSP - 1997/08/07 ++--------------------------------------------------------------------------+ +| (C) Copyright 1997 - +| - NavIST Group - Real-Time Distributed Systems and Industrial Automation +| +| http://pandora.ist.utl.pt +| +| Instituto Superior Tecnico * Lisboa * PORTUGAL ++--------------------------------------------------------------------------+ +| Disclaimer: +| +| This file is provided "AS IS" without warranty of any kind, either +| expressed or implied. ++--------------------------------------------------------------------------+ +| This code is based on code by: Jose Rufino - IST +| +| $Id$ ++--------------------------------------------------------------------------*/ + + +#include +#include +#include +/* #include */ + +/*-------------------------------------------------------------------------+ +| Function: printNum +| Description: print number in a given base. +| Global Variables: None. +| Arguments: num - number to print, base - base used to print the number. +| Returns: Nothing. ++--------------------------------------------------------------------------*/ +static void +printNum(long unsigned int num, int base, int sign, int maxwidth, int lead) +{ + long unsigned int n; + int count; + char toPrint[20]; + + if ( (sign == 1) && ((long)num < 0) ) { + BSP_output_char('-'); + num = -num; + maxwidth--; + } + + count = 0; + while ((n = num / base) > 0) { + toPrint[count++] = (num - (n*base)); + num = n ; + } + toPrint[count++] = num; + + if (maxwidth) { + for (n=maxwidth-count ; n ; n-- ) + BSP_output_char(lead); + } + + for (n = 0; n < count; n++){ + BSP_output_char("0123456789ABCDEF"[(int)(toPrint[count-(n+1)])]); + } +} /* printNum */ + + +/*-------------------------------------------------------------------------+ +| Function: printk +| Description: a simplified version of printf intended for use when the + console is not yet initialized or in ISR's. +| Global Variables: None. +| Arguments: as in printf: fmt - format string, ... - unnamed arguments. +| Returns: Nothing. ++--------------------------------------------------------------------------*/ +void +printk(char *fmt, ...) +{ + va_list ap; /* points to each unnamed argument in turn */ + char c, *str; + int lflag, base, sign, width, lead; + /* unsigned int level; */ + + /* _CPU_ISR_Disable(level); */ + + va_start(ap, fmt); /* make ap point to 1st unnamed arg */ + for (; *fmt != '\0'; fmt++) + { + lflag = 0; + base = 0; + sign = 0; + width = 0; + lead = ' '; + if (*fmt == '%') + { + fmt++; + if (*fmt == '0' ) { + lead = '0'; + fmt++; + } + while (*fmt >= '0' && *fmt <= '9' ) { + width *= 10; + width += (*fmt - '0'); + fmt++; + } + + if ((c = *fmt) == 'l') + { + lflag = 1; + c = *++fmt; + } + switch (c) + { + case 'o': case 'O': base = 8; sign = 0; break; + case 'd': case 'D': base = 10; sign = 1; break; + case 'u': case 'U': base = 10; sign = 0; break; + case 'x': case 'X': base = 16; sign = 0; break; + case 's': + for (str = va_arg(ap, char *); *str; str++) + BSP_output_char(*str); + break; + case 'c': +#if defined(_TMS320C3x) || defined(_TMS320C4x) + BSP_output_char(va_arg(ap, int)); +#else + BSP_output_char(va_arg(ap, char)); +#endif + break; + default: + BSP_output_char(c); + break; + } /* switch*/ + + if (base) + printNum(lflag ? va_arg(ap, long int) : (long int)va_arg(ap, int), + base, sign, width, lead); + } + else + { + BSP_output_char(*fmt); + } + } + va_end(ap); /* clean up when done */ + /* _CPU_ISR_Enable(level); */ + +} /* printk */ + diff --git a/c/src/lib/include/Makefile.am b/c/src/lib/include/Makefile.am index 4a5e8d14c4..3988f9918d 100644 --- a/c/src/lib/include/Makefile.am +++ b/c/src/lib/include/Makefile.am @@ -4,7 +4,7 @@ AUTOMAKE_OPTIONS = foreign 1.4 -H_FILES = chain.h console.h clockdrv.h iosupp.h ringbuf.h spurious.h \ +H_FILES = bspIo.h chain.h console.h clockdrv.h iosupp.h ringbuf.h spurious.h \ timerdrv.h vmeintr.h noinst_HEADERS = $(H_FILES) diff --git a/c/src/lib/include/bspIo.h b/c/src/lib/include/bspIo.h new file mode 100644 index 0000000000..0ff070c4c5 --- /dev/null +++ b/c/src/lib/include/bspIo.h @@ -0,0 +1,37 @@ +/* bspIo.h + * + * This include file contains declaration of interface that + * will be provided by the file contained in this directory. + * + * + * COPYRIGHT (c) 1998 valette@crf.canon.fr + * + * The license and distribution terms for this file may be + * found in found in the file LICENSE in this distribution or at + * http://www.OARcorp.com/rtems/license.html. + * + * $Id$ + */ +#ifndef _LIBBSP_I386_SHARED_IO_BSP_IO_H +#define _LIBBSP_I386_SHARED_IO_BSP_IO_H + +/* + * All the functions declared as extern after this comment + * MUST be implemented in each BSP. Using this function, + * this directory contains shared code that export higher level + * functionnality described after the next command. + */ +typedef void (*BSP_output_char_function_type) (char c); +typedef char (*BSP_polling_getchar_function_type) (void); + +extern BSP_output_char_function_type BSP_output_char; +extern BSP_polling_getchar_function_type BSP_poll_char; +/* + * All the function declared as extern after this comment + * are available for each ix86 BSP by compiling and linking + * the files contained in this directory PROVIDED definition + * and initialisation of the previous variable are done. + */ +void printk(char *fmt, ...); + +#endif diff --git a/c/src/lib/libc/Makefile.am b/c/src/lib/libc/Makefile.am index 54b674b2f7..0f4a4cef2e 100644 --- a/c/src/lib/libc/Makefile.am +++ b/c/src/lib/libc/Makefile.am @@ -48,7 +48,8 @@ LIBC_GLUE_C_FILES = __getpid.c __gettod.c __times.c truncate.c access.c \ UNIX_LIBC_C_FILES = unixlibc.c hosterr.c -COMMON_C_FILES = gxx_wrappers.c $(BASE_FS_C_FILES) $(MALLOC_C_FILES) $(TERMIOS_C_FILES) \ +COMMON_C_FILES = gxx_wrappers.c printk.c $(BASE_FS_C_FILES) \ + $(MALLOC_C_FILES) $(TERMIOS_C_FILES) \ $(ERROR_C_FILES) $(ASSOCIATION_C_FILES) UNIX_C_FILES = $(UNIX_LIBC_C_FILES) imfs_unixstub.c diff --git a/c/src/lib/libc/printk.c b/c/src/lib/libc/printk.c new file mode 100644 index 0000000000..253e5f1abc --- /dev/null +++ b/c/src/lib/libc/printk.c @@ -0,0 +1,144 @@ +/*-------------------------------------------------------------------------+ +| printk.c v1.1 - PC386 BSP - 1997/08/07 ++--------------------------------------------------------------------------+ +| (C) Copyright 1997 - +| - NavIST Group - Real-Time Distributed Systems and Industrial Automation +| +| http://pandora.ist.utl.pt +| +| Instituto Superior Tecnico * Lisboa * PORTUGAL ++--------------------------------------------------------------------------+ +| Disclaimer: +| +| This file is provided "AS IS" without warranty of any kind, either +| expressed or implied. ++--------------------------------------------------------------------------+ +| This code is based on code by: Jose Rufino - IST +| +| $Id$ ++--------------------------------------------------------------------------*/ + + +#include +#include +#include +/* #include */ + +/*-------------------------------------------------------------------------+ +| Function: printNum +| Description: print number in a given base. +| Global Variables: None. +| Arguments: num - number to print, base - base used to print the number. +| Returns: Nothing. ++--------------------------------------------------------------------------*/ +static void +printNum(long unsigned int num, int base, int sign, int maxwidth, int lead) +{ + long unsigned int n; + int count; + char toPrint[20]; + + if ( (sign == 1) && ((long)num < 0) ) { + BSP_output_char('-'); + num = -num; + maxwidth--; + } + + count = 0; + while ((n = num / base) > 0) { + toPrint[count++] = (num - (n*base)); + num = n ; + } + toPrint[count++] = num; + + if (maxwidth) { + for (n=maxwidth-count ; n ; n-- ) + BSP_output_char(lead); + } + + for (n = 0; n < count; n++){ + BSP_output_char("0123456789ABCDEF"[(int)(toPrint[count-(n+1)])]); + } +} /* printNum */ + + +/*-------------------------------------------------------------------------+ +| Function: printk +| Description: a simplified version of printf intended for use when the + console is not yet initialized or in ISR's. +| Global Variables: None. +| Arguments: as in printf: fmt - format string, ... - unnamed arguments. +| Returns: Nothing. ++--------------------------------------------------------------------------*/ +void +printk(char *fmt, ...) +{ + va_list ap; /* points to each unnamed argument in turn */ + char c, *str; + int lflag, base, sign, width, lead; + /* unsigned int level; */ + + /* _CPU_ISR_Disable(level); */ + + va_start(ap, fmt); /* make ap point to 1st unnamed arg */ + for (; *fmt != '\0'; fmt++) + { + lflag = 0; + base = 0; + sign = 0; + width = 0; + lead = ' '; + if (*fmt == '%') + { + fmt++; + if (*fmt == '0' ) { + lead = '0'; + fmt++; + } + while (*fmt >= '0' && *fmt <= '9' ) { + width *= 10; + width += (*fmt - '0'); + fmt++; + } + + if ((c = *fmt) == 'l') + { + lflag = 1; + c = *++fmt; + } + switch (c) + { + case 'o': case 'O': base = 8; sign = 0; break; + case 'd': case 'D': base = 10; sign = 1; break; + case 'u': case 'U': base = 10; sign = 0; break; + case 'x': case 'X': base = 16; sign = 0; break; + case 's': + for (str = va_arg(ap, char *); *str; str++) + BSP_output_char(*str); + break; + case 'c': +#if defined(_TMS320C3x) || defined(_TMS320C4x) + BSP_output_char(va_arg(ap, int)); +#else + BSP_output_char(va_arg(ap, char)); +#endif + break; + default: + BSP_output_char(c); + break; + } /* switch*/ + + if (base) + printNum(lflag ? va_arg(ap, long int) : (long int)va_arg(ap, int), + base, sign, width, lead); + } + else + { + BSP_output_char(*fmt); + } + } + va_end(ap); /* clean up when done */ + /* _CPU_ISR_Enable(level); */ + +} /* printk */ + -- cgit v1.2.3