Save some more RAM on AVR.
authorbernie <bernie@38d2e660-2303-0410-9eaa-f027e97ec537>
Wed, 8 Dec 2004 08:52:00 +0000 (08:52 +0000)
committerbernie <bernie@38d2e660-2303-0410-9eaa-f027e97ec537>
Wed, 8 Dec 2004 08:52:00 +0000 (08:52 +0000)
git-svn-id: https://src.develer.com/svnoss/bertos/trunk@302 38d2e660-2303-0410-9eaa-f027e97ec537

debug.h
drv/kdebug.c

diff --git a/debug.h b/debug.h
index 86d6820c1eda69f2b1ddfd6e8378ac8f2a695b6c..66967a7b7ff0773c65a61ff3cd03d9264e35aef1 100755 (executable)
--- a/debug.h
+++ b/debug.h
@@ -17,6 +17,9 @@
 
 /*#*
  *#* $Log$
+ *#* Revision 1.6  2004/12/08 08:52:00  bernie
+ *#* Save some more RAM on AVR.
+ *#*
  *#* Revision 1.5  2004/12/08 08:04:13  bernie
  *#* Doxygen fixes.
  *#*
@@ -73,6 +76,7 @@
        #define UNUSED(type,name) type
        #endif
 #else /* !OS_HOSTED */
+       #include <config.h>
        #include <compiler.h>
 #endif /* !OS_HOSTED */
 
                void kdbg_init(void);
                void kputchar(char c);
                void kdump(const void *buf, size_t len);
+               void __init_wall(long *wall, int size);
 
                #ifdef __AVR__
                        #include <avr/pgmspace.h>
                        void kprintf_P(const char *PROGMEM fmt, ...) FORMAT(__printf__, 1, 2);
                        int __assert_P(const char *PROGMEM cond, const char *PROGMEM file, int line);
                        int __invalid_ptr_P(void *p, const char *PROGMEM name, const char *PROGMEM file, int line);
+                       int __check_wall_P(long *wall, int size, const char * PGM_ATTR name, const char * PGM_ATTR file, int line);
                        #define kputs(str)  kputs_P(PSTR(str))
                        #define kprintf(fmt, ...)  kprintf_P(PSTR(fmt) ,## __VA_ARGS__)
                        #define __assert(cond, file, line)  __assert_P(PSTR(cond), PSTR(file), (line))
                        #define __invalid_ptr(p, name, file, line)  __invalid_ptr_P((p), PSTR(name), PSTR(file), (line))
+                       #define __check_wall(wall, size, name, file, line)  __check_wall_P(wall, size, PSTR(name), PSTR(file), (line))
                #else /* !__AVR__ */
                        void kputs(const char *str);
                        void kprintf(const char * fmt, ...) FORMAT(__printf__, 1, 2);
                        int __assert(const char *cond, const char *file, int line);
                        int __invalid_ptr(void *p, const char *name, const char *file, int line);
+                       int __check_wall(long *wall, int size, const char *name, const char *file, int line);
                #endif /* !__AVR__ */
 
-               void __init_wall(long *wall, int size);
-               int __check_wall(long *wall, int size, const char *name, const char *file, int line);
-
                #ifndef CONFIG_KDEBUG_ASSERT_NO_TEXT
                        #define ASSERT(x)         ((void)(LIKELY(x) ? 0 : __assert(#x, THIS_FILE, __LINE__)))
                        #define ASSERT2(x, help)  ((void)(LIKELY(x) ? 0 : __assert(help " (" #x ")", THIS_FILE, __LINE__)))
index 88554e9571faedaa94bb374540140b7e809c9956..03a86430787ad181e27902deecde4337b79800a0 100755 (executable)
@@ -15,6 +15,9 @@
 
 /*#*
  *#* $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.
  *#*
@@ -285,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 */
@@ -297,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 */
 }
 
 
@@ -315,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;
 }
 
@@ -342,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;
 
@@ -350,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;
                }
        }
@@ -360,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
  */
@@ -372,4 +428,6 @@ void kdump(const void *_buf, size_t len)
        kputchar('\n');
 }
 
+#endif /* CONFIG_PRINTF */
+
 #endif /* _DEBUG */