Index: trunk/kernel/kern/printk.c
===================================================================
--- trunk/kernel/kern/printk.c	(revision 669)
+++ trunk/kernel/kern/printk.c	(revision 683)
@@ -41,13 +41,15 @@
 //////////////////////////////////////////////////////////////////////////////////////
 // This static function is called by printk(), nolock_printk(), and snprintk(),
-// functions to build a string from a printf-like format, and stores it 
-// in the buffer defined by the <string> and <length> arguments.
-// It does NOT add a terminating NUL character in the <string> buffer.
-// If success, it returns the number of bytes actually copied in the string buffer.
+// functions to build a string from a printf-like <format>, and stores it 
+// in the buffer defined by the <string> and <size> arguments.
+// The <format> itself is supposed to be a NUL terminated string. The <string>
+// buffer <size> must be large enough to contains also the NUL terminating character.
+// If success, it returns the number of bytes actually copied in the <string> buffer,
+// but this length does NOT include the terminating NUL character.
 // It returns -2 in case of illegal format, it returns -1 if the formated string 
-// exceeds the length argument. 
+// exceeds the <size> argument. 
 //////////////////////////////////////////////////////////////////////////////////////
 // @ string    : buffer allocated by caller.
-// @ length    : buffer size in bytes
+// @ size      : buffer size in bytes
 // @ format    : printf like format.
 // @ args      : va_list of arguments.
@@ -55,10 +57,10 @@
 //////////////////////////////////////////////////////////////////////////////////////
 static int32_t format_to_string( char       * string,
-                                 uint32_t     length,
+                                 uint32_t     size,
                                  const char * format, 
                                  va_list    * args ) 
 {
 
-#define TO_STRING(x) do { string[ps] = (x); ps++; if(ps==length) return -1; } while(0);
+#define TO_STRING(x) do { string[ps] = (x); ps++; if(ps==size) return -1; } while(0);
 
     uint32_t   ps = 0;        // index in string buffer
@@ -74,5 +76,5 @@
             goto format_to_string_arguments;
         }
-        else                  // copy one char to string
+        else                  // copy one char of format to string
         {
             TO_STRING( *format );
@@ -81,6 +83,6 @@
     }
 
-    TO_STRING( 0 );
-    return ps;
+    TO_STRING( 0 );      // NUL character written in buffer
+    return (ps - 1);     // but not counted in length
 
 format_to_string_arguments:
@@ -95,13 +97,21 @@
         switch (*format) 
         {
-            case ('c'):             // char conversion 
-            {
-                int val = va_arg( *args , int );
-                buf[0] = (char)val;
+            case ('c'):             // one printable character 
+            {
+                buf[0] = (char)va_arg( *args , uint32_t );
                 pbuf   = buf;
                 len    = 1;
                 break;
             }
-            case ('d'):             // up to 10 digits decimal signed integer
+            case ('b'):             // one ASCII code value (2 hexadecimal digits)
+            {
+                uint8_t val = (uint8_t)va_arg( *args , uint32_t ); 
+                buf[1] = HexaTab[val & 0xF];
+                buf[0] = HexaTab[(val >> 4) & 0xF];
+                pbuf   = buf;
+                len    = 2;
+                break;
+            }            
+            case ('d'):            // one int32_t (up to 10 decimal digits after sign) 
             {
                 int32_t val = va_arg( *args , int32_t );
@@ -120,5 +130,5 @@
                 break;
             }
-            case ('u'):             // up to 10 digits decimal unsigned integer
+            case ('u'):           // one uint32_t (up to 10 decimal digits)
             {
                 uint32_t val = va_arg( *args , uint32_t );
@@ -132,6 +142,6 @@
                 break;
             }
-            case ('x'):             // up to 8 digits hexad after "0x"
-            case ('X'):             // exactly 8 digits hexa after "0x"
+            case ('x'):           // one uint32_t (up to 8 hexa digits after "0x")
+
             {
                 uint32_t val = va_arg( *args , uint32_t );
@@ -141,6 +151,6 @@
                 {
                     buf[7 - i] = HexaTab[val & 0xF];
-                    if( (*format == 'x') && ((val >> 4) == 0) )  break;
                     val = val >> 4;
+                    if(val == 0)  break;
                 }
                 len =  i + 1;
@@ -148,6 +158,19 @@
                 break;
             }
-            case ('l'):             // up to 16 digits hexa after "0x"
-            case ('L'):             // exactly 16 digits hexa after "0x"
+            case ('X'):         // one uint32_t (exactly 8 hexa digits after "0x")
+            {
+                uint32_t val = va_arg( *args , uint32_t );
+                TO_STRING( '0' );
+                TO_STRING( 'x' );
+                for(i = 0 ; i < 8 ; i++) 
+                {
+                    buf[7 - i] = (val != 0) ? HexaTab[val & 0xF] : '0';
+                    val = val >> 4;
+                }
+                len = 8;
+                pbuf = &buf[0];
+                break;
+            }
+            case ('l'):          // one uint64_t (up to 16 digits hexa after "0x")
             {
                 uint64_t val = (((uint64_t)va_arg( *args, uint32_t)) << 32) |
@@ -158,6 +181,6 @@
                 {
                     buf[15 - i] = HexaTab[val & 0xF];
-                    if( (*format == 'l') && ((val >> 4) == 0) )  break;
                     val = val >> 4;
+                    if( val == 0)  break;
                 }
                 len =  i + 1;
@@ -165,5 +188,20 @@
                 break;
             }
-            case ('s'):             /* string */
+            case ('L'):          // one uint64_t (exactly 16 digits hexa after "0x")
+            {
+                uint64_t val = (((uint64_t)va_arg( *args, uint32_t)) << 32) |
+                               ((uint64_t)va_arg( *args, uint32_t));
+                TO_STRING( '0' );
+                TO_STRING( 'x' );
+                for(i = 0 ; i < 16 ; i++) 
+                {
+                    buf[15 - i] = (val != 0) ? HexaTab[val & 0xF] : '0';
+                    val = val >> 4;
+                }
+                len =  16;
+                pbuf = &buf[0];
+                break;
+            }
+            case ('s'):             /* one characters string */
             {
                 char* str = va_arg( *args , char* );
@@ -213,7 +251,7 @@
     // build a string from format
     length = format_to_string( buffer,
-                      CONFIG_PRINTK_BUF_SIZE,
-                      format,
-                      &args );
+                               CONFIG_PRINTK_BUF_SIZE,
+                               format,
+                               &args );
     va_end( args );
 
@@ -258,7 +296,7 @@
     // build a string from format
     length = format_to_string( buffer,
-                      CONFIG_PRINTK_BUF_SIZE,
-                      format,
-                      &args );
+                               CONFIG_PRINTK_BUF_SIZE,
+                               format,
+                               &args );
     va_end( args );
 
@@ -315,5 +353,5 @@
         if( length > 0  )  // display panic message on TXT0, including formated string
         {
-            printk("\n[ASSERT] in %s / core[%x,%d] / thread[%x,%x] / cycle %d\n       %s\n",
+            printk("\n[ASSERT] in %s / core[%x,%d] / thread[%x,%x] / cycle %d\n   <%s>\n",
             func_name, local_cxy, lid, pid, trdid, cycle, buffer );
         }
@@ -332,5 +370,5 @@
 {
     va_list       args;
-    int32_t       string_length;
+    int32_t       length;
 
     // build args va_list
@@ -338,18 +376,12 @@
 
     // build a string from format
-    string_length = format_to_string( buffer , size , format , &args );
+    length = format_to_string( buffer , size , format , &args );
+
+    // release args list
     va_end( args );
 
-    if( (string_length < 0) || (string_length == (int32_t)size) )  // failure
-    {
-        return -1;
-    }
-    else                                                           // success
-    {
-        // add NUL character
-        buffer[string_length] = 0;
-
-        return string_length;
-    }
+    if( length < 0 )   return -1;
+    else               return length;
+
 }   // end snprintk()
 
