<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Generator" CONTENT="MS Exchange Server version 5.5.2653.12">
<TITLE>FYI - updated printk()</TITLE>
</HEAD>
<BODY>
<P><FONT SIZE=2>All,</FONT>
</P>
<BR>
<P><FONT SIZE=2>For anyone that needs it - like me - I have added a binary format to printk()</FONT>
</P>
<P><FONT SIZE=2>I use it like this -</FONT>
</P>
<P><FONT SIZE=2> printk(" %08B \n", 0xAA ); prints 10101010</FONT>
</P>
<P><FONT SIZE=2> printk(" %016B \n", 0x5555 ); prints 0101010101010101</FONT>
<BR><FONT SIZE=2> </FONT>
<BR><FONT SIZE=2> printk(" %032B \n", 0x12345678 ); prints 00010011011101000101011001111000</FONT>
</P>
<P><FONT SIZE=2>The 0 after the % is used as the leading fill character and the 8/16/32 is used</FONT>
<BR><FONT SIZE=2>as the output field width just like with other numbers.</FONT>
</P>
<P><FONT SIZE=2>Regards,</FONT>
<BR><FONT SIZE=2>Nick</FONT>
</P>
<BR>
<BR>
<P><FONT SIZE=2>/*-------------------------------------------------------------------------+</FONT>
<BR><FONT SIZE=2>| printk.c v1.1 - PC386 BSP - 1997/08/07</FONT>
<BR><FONT SIZE=2>+--------------------------------------------------------------------------+</FONT>
<BR><FONT SIZE=2>| (C) Copyright 1997 -</FONT>
<BR><FONT SIZE=2>| - NavIST Group - Real-Time Distributed Systems and Industrial Automation</FONT>
<BR><FONT SIZE=2>|</FONT>
<BR><FONT SIZE=2>| <A HREF="http://pandora.ist.utl.pt" TARGET="_blank">http://pandora.ist.utl.pt</A></FONT>
<BR><FONT SIZE=2>|</FONT>
<BR><FONT SIZE=2>| Instituto Superior Tecnico * Lisboa * PORTUGAL</FONT>
<BR><FONT SIZE=2>+--------------------------------------------------------------------------+</FONT>
<BR><FONT SIZE=2>| Disclaimer:</FONT>
<BR><FONT SIZE=2>|</FONT>
<BR><FONT SIZE=2>| This file is provided "AS IS" without warranty of any kind, either</FONT>
<BR><FONT SIZE=2>| expressed or implied.</FONT>
<BR><FONT SIZE=2>+--------------------------------------------------------------------------+</FONT>
<BR><FONT SIZE=2>| This code is based on code by: Jose Rufino - IST</FONT>
<BR><FONT SIZE=2>|</FONT>
<BR><FONT SIZE=2>| $Id: printk.c,v 1.2 2001/10/10 00:35:06 hennenfen Exp $</FONT>
<BR><FONT SIZE=2>+--------------------------------------------------------------------------*/</FONT>
</P>
<P><FONT SIZE=2>#if HAVE_CONFIG_H</FONT>
<BR><FONT SIZE=2>#include "config.h"</FONT>
<BR><FONT SIZE=2>#endif</FONT>
</P>
<P><FONT SIZE=2>#include <stdarg.h></FONT>
<BR><FONT SIZE=2>#include <stdio.h></FONT>
<BR><FONT SIZE=2>#include <bspIo.h></FONT>
<BR><FONT SIZE=2>/* #include <libcpu/cpu.h> */</FONT>
</P>
<P><FONT SIZE=2>/*-------------------------------------------------------------------------+</FONT>
<BR><FONT SIZE=2>| Function: printNum</FONT>
<BR><FONT SIZE=2>| Description: print number in a given base.</FONT>
<BR><FONT SIZE=2>| Global Variables: None.</FONT>
<BR><FONT SIZE=2>| Arguments: num - number to print, base - base used to print the number.</FONT>
<BR><FONT SIZE=2>| Returns: Nothing. </FONT>
<BR><FONT SIZE=2>+--------------------------------------------------------------------------*/</FONT>
<BR><FONT SIZE=2>static void</FONT>
<BR><FONT SIZE=2>printNum(long unsigned int num, int base, int sign, int maxwidth, int lead)</FONT>
<BR><FONT SIZE=2>{</FONT>
<BR><FONT SIZE=2> long unsigned int n;</FONT>
<BR><FONT SIZE=2> int count;</FONT>
<BR><FONT SIZE=2> char toPrint[64];</FONT>
</P>
<P><FONT SIZE=2> if ( (sign == 1) && ((long)num < 0) ) {</FONT>
<BR><FONT SIZE=2> BSP_output_char('-');</FONT>
<BR><FONT SIZE=2> num = -num;</FONT>
<BR><FONT SIZE=2> if (maxwidth) maxwidth--;</FONT>
<BR><FONT SIZE=2> }</FONT>
<BR><FONT SIZE=2> </FONT>
<BR><FONT SIZE=2> count = 0;</FONT>
<BR><FONT SIZE=2> while ((n = num / base) > 0) {</FONT>
<BR><FONT SIZE=2> toPrint[count++] = (num - (n*base));</FONT>
<BR><FONT SIZE=2> num = n ;</FONT>
<BR><FONT SIZE=2> }</FONT>
<BR><FONT SIZE=2> toPrint[count++] = num;</FONT>
</P>
<P><FONT SIZE=2> if (maxwidth && count < maxwidth) {</FONT>
<BR><FONT SIZE=2> for (n=maxwidth-count ; n ; n-- )</FONT>
<BR><FONT SIZE=2> BSP_output_char(lead);</FONT>
<BR><FONT SIZE=2> }</FONT>
</P>
<P><FONT SIZE=2> for (n = 0; n < count; n++){</FONT>
<BR><FONT SIZE=2> BSP_output_char("0123456789ABCDEF"[(int)(toPrint[count-(n+1)])]);</FONT>
<BR><FONT SIZE=2> }</FONT>
<BR><FONT SIZE=2>} /* printNum */</FONT>
</P>
<BR>
<P><FONT SIZE=2>/*-------------------------------------------------------------------------+</FONT>
<BR><FONT SIZE=2>| Function: printk</FONT>
<BR><FONT SIZE=2>| Description: a simplified version of printf intended for use when the</FONT>
<BR><FONT SIZE=2> console is not yet initialized or in ISR's.</FONT>
<BR><FONT SIZE=2>| Global Variables: None.</FONT>
<BR><FONT SIZE=2>| Arguments: as in printf: fmt - format string, ... - unnamed arguments.</FONT>
<BR><FONT SIZE=2>| Returns: Nothing. </FONT>
<BR><FONT SIZE=2>+--------------------------------------------------------------------------*/</FONT>
<BR><FONT SIZE=2>void</FONT>
<BR><FONT SIZE=2>printk(char *fmt, ...)</FONT>
<BR><FONT SIZE=2>{</FONT>
<BR><FONT SIZE=2> va_list ap; /* points to each unnamed argument in turn */</FONT>
<BR><FONT SIZE=2> char c, *str;</FONT>
<BR><FONT SIZE=2> int lflag, base, sign, width, lead;</FONT>
<BR><FONT SIZE=2> /* unsigned int level; */</FONT>
</P>
<P><FONT SIZE=2> /* _CPU_ISR_Disable(level); */</FONT>
<BR><FONT SIZE=2> </FONT>
<BR><FONT SIZE=2> va_start(ap, fmt); /* make ap point to 1st unnamed arg */</FONT>
<BR><FONT SIZE=2> for (; *fmt != '\0'; fmt++)</FONT>
<BR><FONT SIZE=2> {</FONT>
<BR><FONT SIZE=2> lflag = 0;</FONT>
<BR><FONT SIZE=2> base = 0;</FONT>
<BR><FONT SIZE=2> sign = 0;</FONT>
<BR><FONT SIZE=2> width = 0;</FONT>
<BR><FONT SIZE=2> lead = ' ';</FONT>
<BR><FONT SIZE=2> if (*fmt == '%')</FONT>
<BR><FONT SIZE=2> {</FONT>
<BR><FONT SIZE=2> fmt++;</FONT>
<BR><FONT SIZE=2> if (*fmt == '0' ) {</FONT>
<BR><FONT SIZE=2> lead = '0';</FONT>
<BR><FONT SIZE=2> fmt++;</FONT>
<BR><FONT SIZE=2> }</FONT>
<BR><FONT SIZE=2> while (*fmt >= '0' && *fmt <= '9' ) {</FONT>
<BR><FONT SIZE=2> width *= 10;</FONT>
<BR><FONT SIZE=2> width += (*fmt - '0');</FONT>
<BR><FONT SIZE=2> fmt++;</FONT>
<BR><FONT SIZE=2> }</FONT>
</P>
<P><FONT SIZE=2> if ((c = *fmt) == 'l')</FONT>
<BR><FONT SIZE=2> {</FONT>
<BR><FONT SIZE=2> lflag = 1;</FONT>
<BR><FONT SIZE=2> c = *++fmt;</FONT>
<BR><FONT SIZE=2> }</FONT>
<BR><FONT SIZE=2> switch (c)</FONT>
<BR><FONT SIZE=2> {</FONT>
<BR><FONT SIZE=2> case 'b': case 'B': base = 2; sign = 0; break;</FONT>
<BR><FONT SIZE=2> case 'o': case 'O': base = 8; sign = 0; break;</FONT>
<BR><FONT SIZE=2> case 'd': case 'D': base = 10; sign = 1; break;</FONT>
<BR><FONT SIZE=2> case 'u': case 'U': base = 10; sign = 0; break;</FONT>
<BR><FONT SIZE=2> case 'x': case 'X': base = 16; sign = 0; break;</FONT>
<BR><FONT SIZE=2> case 's':</FONT>
<BR><FONT SIZE=2> for (str = va_arg(ap, char *); *str; str++) </FONT>
<BR><FONT SIZE=2> BSP_output_char(*str);</FONT>
<BR><FONT SIZE=2> break;</FONT>
<BR><FONT SIZE=2> case 'c':</FONT>
<BR><FONT SIZE=2>#if 0</FONT>
<BR><FONT SIZE=2>#if defined(_TMS320C3x) || defined(_TMS320C4x)</FONT>
<BR><FONT SIZE=2> BSP_output_char(va_arg(ap, int));</FONT>
<BR><FONT SIZE=2>#else</FONT>
<BR><FONT SIZE=2> BSP_output_char(va_arg(ap, char));</FONT>
<BR><FONT SIZE=2>#endif</FONT>
<BR><FONT SIZE=2>#else</FONT>
<BR><FONT SIZE=2> BSP_output_char(va_arg(ap, int));</FONT>
<BR><FONT SIZE=2>#endif</FONT>
<BR><FONT SIZE=2> break;</FONT>
<BR><FONT SIZE=2> default:</FONT>
<BR><FONT SIZE=2> BSP_output_char(c);</FONT>
<BR><FONT SIZE=2> break;</FONT>
<BR><FONT SIZE=2> } /* switch*/</FONT>
</P>
<P><FONT SIZE=2> if (base)</FONT>
<BR><FONT SIZE=2> printNum(lflag ? va_arg(ap, long int) : (long int)va_arg(ap, int),</FONT>
<BR><FONT SIZE=2> base, sign, width, lead);</FONT>
<BR><FONT SIZE=2> }</FONT>
<BR><FONT SIZE=2> else</FONT>
<BR><FONT SIZE=2> {</FONT>
<BR><FONT SIZE=2> BSP_output_char(*fmt);</FONT>
<BR><FONT SIZE=2> }</FONT>
<BR><FONT SIZE=2> }</FONT>
<BR><FONT SIZE=2> va_end(ap); /* clean up when done */</FONT>
<BR><FONT SIZE=2> /* _CPU_ISR_Enable(level); */</FONT>
<BR><FONT SIZE=2>} /* printk */</FONT>
</P>
<BR>
<BR>
<BR>
<BR>
<BR>
<BR>
<BR>
<BR>
<BR>
<BR>
<BR>
<BR>
<BR>
<BR>
<BR>
<P><FONT FACE="Arial" SIZE=2 COLOR="#000000"></FONT>
</BODY>
</HTML>