DISABLE_INTS/ENABLE_INTS: Convert to IRQ_DISABLE/IRQ_ENABLE.
[bertos.git] / drv / kdebug.c
index c64ab13921d4495630f099cae979d5992eeabc21..03a86430787ad181e27902deecde4337b79800a0 100755 (executable)
@@ -1,21 +1,29 @@
 /*!
  * \file
  * <!--
- * Copyright 2003,2004 Develer S.r.l. (http://www.develer.com/)
- * Copyright 2000,2001,2002 Bernardo Innocenti <bernie@codewiz.org>
+ * Copyright 2003, 2004 Develer S.r.l. (http://www.develer.com/)
+ * Copyright 2000, 2001, 2002 Bernardo Innocenti <bernie@codewiz.org>
  * This file is part of DevLib - See devlib/README for information.
  * -->
  *
- * \brief General pourpose debug functions.
+ * \brief General pourpose debug support for embedded systems (implementation).
  *
  * \version $Id$
- *
  * \author Bernardo Innocenti <bernie@develer.com>
  * \author Stefano Fedrigo <aleph@develer.com>
  */
 
 /*#*
  *#* $Log$
+ *#* Revision 1.18  2004/12/08 08:52:00  bernie
+ *#* Save some more RAM on AVR.
+ *#*
+ *#* Revision 1.17  2004/10/03 18:41:28  bernie
+ *#* Restore \version header killed by mistake in previous commit.
+ *#*
+ *#* Revision 1.16  2004/10/03 18:40:50  bernie
+ *#* Use new CPU macros.
+ *#*
  *#* Revision 1.15  2004/09/14 21:03:46  bernie
  *#* Use debug.h instead of kdebug.h.
  *#*
                #define KDBG_UART0_BUS_TX    do {} while (0)
                #endif
 
-               #if defined(__AVR_ATmega64__)
+               #if CPU_AVR_ATMEGA64
                        #define UCR UCSR0B
                        #define UDR UDR0
                        #define USR UCSR0A
-               #elif defined(__AVR_ATmega8__)
+               #elif CPU_AVR_ATMEGA8
                        #define UCR UCSRB
                        #define USR UCSRA
+               #else
+                       #error Unknown CPU
                #endif
 
                #define KDBG_WAIT_READY()     do { loop_until_bit_is_set(USR, UDRE); } while(0)
@@ -216,7 +226,7 @@ void kdbg_init(void)
        /* Compute the baud rate */
        uint16_t period = (((CLOCK_FREQ / 16UL) + (CONFIG_KDEBUG_BAUDRATE / 2)) / CONFIG_KDEBUG_BAUDRATE) - 1;
 
-       #if defined(__AVR_ATmega64__)
+       #if CPU_AVR_ATMEGA64
                #if CONFIG_KDEBUG_PORT == 0
                        UBRR0H = (uint8_t)(period>>8);
                        UBRR0L = (uint8_t)period;
@@ -228,14 +238,14 @@ void kdbg_init(void)
                #else
                        #error CONFIG_KDEBUG_PORT must be either 0 or 1
                #endif
-       #elif defined(__AVR_ATmega8__)
+       #elif CPU_AVR_ATMEGA8
                UBRRH = (uint8_t)(period>>8);
                UBRRL = (uint8_t)period;
-       #elif defined(__AVR_ATmega103__)
+       #elif CPU_AVR_ATMEGA103
                UBRR = (uint8_t)period;
                KDBG_UART0_BUS_INIT;
        #else
-               #error Unknown arch
+               #error Unknown CPU
        #endif
 
 #endif /* !CPU_I196 && !CPU_AVR */
@@ -278,6 +288,8 @@ void kputchar(char c)
 
 void PGM_FUNC(kprintf)(const char * PGM_ATTR fmt, ...)
 {
+
+#if CONFIG_PRINTF
        va_list ap;
 
        /* Mask serial TX intr */
@@ -290,6 +302,10 @@ void PGM_FUNC(kprintf)(const char * PGM_ATTR fmt, ...)
 
        /* Restore serial TX intr */
        KDBG_RESTORE_IRQ(irqsave);
+#else
+       /* A better than nothing printf() surrogate. */
+       PGM_FUNC(kputs)(fmt);
+#endif /* CONFIG_PRINTF */
 }
 
 
@@ -308,22 +324,61 @@ void PGM_FUNC(kputs)(const char * PGM_ATTR str)
 }
 
 
-int PGM_FUNC(__assert)(const char * PGM_ATTR cond, const char * PGM_ATTR file, int line)
+/*!
+ * Cheap function to print small integers without using printf().
+ */
+static int kputnum(int num)
+{
+       int output_len = 0;
+       int divisor = 10000;
+       int digit;
+
+       do
+       {
+               digit = num / divisor;
+               num %= divisor;
+
+               if (digit || output_len || divisor == 1)
+               {
+                       kputchar(digit + '0');
+                       ++output_len;
+               }
+       }
+       while (divisor /= 10);
+
+       return output_len;
+}
+
+
+static void klocation(const char * PGM_ATTR file, int line)
 {
        PGM_FUNC(kputs)(file);
-       PGM_FUNC(kprintf)(PSTR(":%d: Assertion failed: "), line);
+       kputchar(':');
+       kputnum(line);
+       PGM_FUNC(kputs)(PSTR(": "));
+}
+
+int PGM_FUNC(__assert)(const char * PGM_ATTR cond, const char * PGM_ATTR file, int line)
+{
+       klocation(file, line);
+       PGM_FUNC(kputs)(PSTR("Assertion failed: "));
        PGM_FUNC(kputs)(cond);
-       PGM_FUNC(kputs)(PSTR("\n"));
+       kputchar('\n');
        return 1;
 }
 
 
 int PGM_FUNC(__invalid_ptr)(void *value, const char * PGM_ATTR name, const char * PGM_ATTR file, int line)
 {
-       PGM_FUNC(kputs)(file);
-       PGM_FUNC(kprintf)(PSTR(":%d: Invalid pointer: "), line);
+       klocation(file, line);
+       PGM_FUNC(kputs)(PSTR("Invalid ptr: "));
        PGM_FUNC(kputs)(name);
-       PGM_FUNC(kprintf)(PSTR(" = 0x%x\n"), value);
+       #if CONFIG_PRINTF
+               PGM_FUNC(kprintf)(PSTR(" = 0x%x\n"), value);
+       #else
+               (void)value;
+               kputchar('\n');
+       #endif
        return 1;
 }
 
@@ -335,7 +390,7 @@ void __init_wall(long *wall, int size)
 }
 
 
-int __check_wall(long *wall, int size, const char *name, const char *file, int line)
+int PGM_FUNC(__check_wall)(long *wall, int size, const char * PGM_ATTR name, const char * PGM_ATTR file, int line)
 {
        int i, fail = 0;
 
@@ -343,8 +398,14 @@ int __check_wall(long *wall, int size, const char *name, const char *file, int l
        {
                if (wall[i] != WALL_VALUE)
                {
-                       kprintf("%s:%d: Wall broken: %s[%d] (0x%p) = 0x%lx\n",
-                               file, line, name, i, wall + i, wall[i]);
+                       klocation(file, line);
+                       PGM_FUNC(kputs)(PSTR("Wall broken: "));
+                       PGM_FUNC(kputs)(name);
+                       #if CONFIG_PRINTF
+                               PGM_FUNC(kprintf)(PSTR("[%d] (0x%p) = 0x%lx\n"), i, wall + i, wall[i]);
+                       #else
+                               kputchar('\n');
+                       #endif
                        fail = 1;
                }
        }
@@ -353,6 +414,8 @@ int __check_wall(long *wall, int size, const char *name, const char *file, int l
 }
 
 
+#if CONFIG_PRINTF
+
 /*!
  * Dump binary data in hex
  */
@@ -365,4 +428,6 @@ void kdump(const void *_buf, size_t len)
        kputchar('\n');
 }
 
+#endif /* CONFIG_PRINTF */
+
 #endif /* _DEBUG */