From c40d959ed0b5880f73809836eadddd72a7c277d6 Mon Sep 17 00:00:00 2001 From: "R. Steve McKown" Date: Sun, 14 Oct 2012 18:59:23 -0600 Subject: [PATCH] Add debug library; use with tmimsp platforms --- tos/lib/debug/Debug.h | 67 +++++++++++++++++++++ tos/lib/debug/Debug.nc | 23 ++++++++ tos/lib/debug/DebugC.nc | 37 ++++++++++++ tos/lib/debug/DebugP.nc | 109 +++++++++++++++++++++++++++++++++++ tos/lib/debug/Debug_undef.h | 23 ++++++++ tos/lib/debug/NoDebugP.nc | 23 ++++++++ tos/platforms/tmimsp/.family | 1 + 7 files changed, 283 insertions(+) create mode 100644 tos/lib/debug/Debug.h create mode 100644 tos/lib/debug/Debug.nc create mode 100644 tos/lib/debug/DebugC.nc create mode 100644 tos/lib/debug/DebugP.nc create mode 100644 tos/lib/debug/Debug_undef.h create mode 100644 tos/lib/debug/NoDebugP.nc diff --git a/tos/lib/debug/Debug.h b/tos/lib/debug/Debug.h new file mode 100644 index 00000000..e9be7cf0 --- /dev/null +++ b/tos/lib/debug/Debug.h @@ -0,0 +1,67 @@ +/* Copyright (c) 2006-2009 by Sporian Microsystems, Inc. + * All Rights Reserved. + * + * This document is the proprietary and confidential property of Sporian + * Microsystems, Inc. All use, distribution, reproduction or re-distribution + * is disallowed without the prior express written consent of Sporian + * Microsystems, Inc. + */ + +/** + * TinyOS debug layer offering printf-style functionality. + * + * @author R. Steve McKown + * + * For a module to use the debug functions it must: + * + * 1. use the Debug interface, which is then wired to DebugC. + * 2. include "Debug.h" to set the macros appropriately. + * + * Optionally, a module may implement module specific debug, where a different + * global define can be used to individually select debug on or off for each + * module. This feature requires the following lines of code in the module, + * after Debug.h has been included. MODULENAME_DEBUG should be changed to a + * more meaningful, and module-unique, name, such as MYMODULE_DEBUG. + * + * #ifndef SERIAL_DEBUG + * # undef MODULENAME_DEBUG + * #else + * # ifndef MODULENAME_DEBUG + * # include "Debug_undef.h" + * # endif + * #endif + * + * To activate debug for a program, add this line to the Makefile: + * CFLAGS += -DSERIAL_DEBUG + * + * To activate debug for a module with selectable debug, one must additionally + * define the modules debug identifier in the Makefile, such as: + * CFLAGS += -DMODULENAME_DEBUG + * + * Undefining SERIAL_DEBUG deactivates all program debug regardless of the state + * of any module-specific debug identifiers. + * + * Debug.h must no include the traditional multiple-inclusion guard so that each + * module including Debug.h has the opportunity to either set or clear (i.e. + * make a noop) the debug macros, according to the operating state defined for + * that module. + */ + +#undef dbg_printf +#undef dbg_assert + +#ifdef SERIAL_DEBUG + +#include +#include + +#define dbg_printf(args...) uprintf(call Debug.uptr(), args) +#define dbg_assert(condition, args...) \ + do { if (condition) uprintf(call Debug.uptr(), args); } while(0) + +#else + +#define dbg_printf(args...) ((void)0) +#define dbg_assert(condition, args...) ((void)0) + +#endif diff --git a/tos/lib/debug/Debug.nc b/tos/lib/debug/Debug.nc new file mode 100644 index 00000000..3fc68804 --- /dev/null +++ b/tos/lib/debug/Debug.nc @@ -0,0 +1,23 @@ +/* Copyright (c) 2006-2009 by Sporian Microsystems, Inc. + * All Rights Reserved. + * + * This document is the proprietary and confidential property of Sporian + * Microsystems, Inc. All use, distribution, reproduction or re-distribution + * is disallowed without the prior express written consent of Sporian + * Microsystems, Inc. + */ + +/* A simple debug component. + */ + +#include +#include "Uprint.h" + +interface Debug { + async command void raw(const void* ptr, uint16_t len); + async command void byteAsBits(uint8_t byte); + async command void hexdump(const void* ptr, uint16_t len); + + /* Used for dbgprintf; see Debug.h */ + async command uprint_ptr_t uptr(); +} diff --git a/tos/lib/debug/DebugC.nc b/tos/lib/debug/DebugC.nc new file mode 100644 index 00000000..4e83774e --- /dev/null +++ b/tos/lib/debug/DebugC.nc @@ -0,0 +1,37 @@ +/* Copyright (c) 2006-2009 by Sporian Microsystems, Inc. + * All Rights Reserved. + * + * This document is the proprietary and confidential property of Sporian + * Microsystems, Inc. All use, distribution, reproduction or re-distribution + * is disallowed without the prior express written consent of Sporian + * Microsystems, Inc. + */ + +/* A simple debug component. The best use would be to have a PlatformDebugC + * component that either wires to this component or one called NoDebugC + * depending upon compilation options. + */ + +configuration DebugC { + provides interface Debug; +} +implementation +{ +#ifdef SERIAL_DEBUG +#warning "SERIAL DEBUG ON" + + components DebugP; + Debug = DebugP; + + components MainC; + MainC.SoftwareInit -> DebugP; + + components DebugUartC; + DebugP.UartResource -> DebugUartC; + DebugP.UartByte -> DebugUartC; +#else + + components NoDebugP; + Debug = NoDebugP; +#endif +} diff --git a/tos/lib/debug/DebugP.nc b/tos/lib/debug/DebugP.nc new file mode 100644 index 00000000..d420f9d7 --- /dev/null +++ b/tos/lib/debug/DebugP.nc @@ -0,0 +1,109 @@ +/* Copyright (c) 2006-2007 by Sporian Microsystems, Inc. + * All Rights Reserved. + * + * This document is the proprietary and confidential property of Sporian + * Microsystems, Inc. All use, distribution, reproduction or re-distribution + * is disallowed without the prior express written consent of Sporian + * Microsystems, Inc. + */ + +/* A simple debug component. + */ + +#include +#include +#include "Uprint.h" + +module DebugP { + provides { + interface Init; + interface Debug; + } + uses { + interface Resource as UartResource; + interface UartByte; + } +} +implementation { + enum { COLUMNS = 16 }; + + bool m_on; + + int debug_putc(int c) + { + call UartByte.send((uint8_t)c); + return c; + } + + void uartWrite(const char* buf) + { + if (buf) { + uprintf(debug_putc, buf); + } + } + + command error_t Init.init() + { + if (call UartResource.immediateRequest() != SUCCESS) + call UartResource.request(); + return SUCCESS; + } + + event void UartResource.granted() {} + + async command uprint_ptr_t Debug.uptr() + { + return debug_putc; + } + + async command void Debug.raw(const void* string, uint16_t len) + { + uint8_t* buf = (uint8_t*)string; + atomic { + if (buf) { + atomic { + while (len--) { + char c = *buf; + + if (isprint(c)) + debug_putc(c); + else + uprintf(debug_putc, "{%02x}", c); + buf++; + } + } + } + } + } + + async command void Debug.byteAsBits(uint8_t byte) + { + atomic { + unsigned mask = 1 << 7; + uprintf(debug_putc, "0b"); + while (mask) { + debug_putc((byte & mask) ? '1' : '0'); + mask >>= 1; + } + } + } + + async command void Debug.hexdump(const void* addr, uint16_t len) + { + atomic { + uint8_t* ptr = (void*)addr; + uint16_t i = 0; + + /* FIXME: ptr as 16 bit value is platform specific */ + uprintf(debug_putc, "Hexdump for location 0x%04x", (uint16_t)ptr); + while (i < len) { + char* prefix = (i % COLUMNS) ? " " : "\r\n"; + + uprintf(debug_putc, "%s%02x", prefix, *ptr++); + i++; + } + if (i % COLUMNS) + uprintf(debug_putc, "\r\n"); + } + } +} diff --git a/tos/lib/debug/Debug_undef.h b/tos/lib/debug/Debug_undef.h new file mode 100644 index 00000000..3d6b373f --- /dev/null +++ b/tos/lib/debug/Debug_undef.h @@ -0,0 +1,23 @@ +/* Copyright (c) 2006-2009 by Sporian Microsystems, Inc. + * All Rights Reserved. + * + * This document is the proprietary and confidential property of Sporian + * Microsystems, Inc. All use, distribution, reproduction or re-distribution + * is disallowed without the prior express written consent of Sporian + * Microsystems, Inc. + */ + +/** + * TinyOS debug layer offering printf-style functionality. + * + * This module sets the debug function macros to no-op. See Debug.h for more + * information. + * + * @author R. Steve McKown + */ + +#undef dbg_printf +#undef dbg_assert + +#define dbg_printf(args...) ((void)0) +#define dbg_assert(condition, args...) ((void)0) diff --git a/tos/lib/debug/NoDebugP.nc b/tos/lib/debug/NoDebugP.nc new file mode 100644 index 00000000..e85fe172 --- /dev/null +++ b/tos/lib/debug/NoDebugP.nc @@ -0,0 +1,23 @@ +/* Copyright (c) 2006-2007 by Sporian Microsystems, Inc. + * All Rights Reserved. + * + * This document is the proprietary and confidential property of Sporian + * Microsystems, Inc. All use, distribution, reproduction or re-distribution + * is disallowed without the prior express written consent of Sporian + * Microsystems, Inc. + */ + +/* A simple debug component. + */ + +#include + +module NoDebugP { + provides interface Debug; +} +implementation { + async command uprint_ptr_t Debug.uptr() { return NULL; } + async command void Debug.raw(const void* string, uint16_t len) {} + async command void Debug.byteAsBits(uint8_t byte) {} + async command void Debug.hexdump(const void* addr, uint16_t len) {} +} diff --git a/tos/platforms/tmimsp/.family b/tos/platforms/tmimsp/.family index 71576bc3..313b32dc 100644 --- a/tos/platforms/tmimsp/.family +++ b/tos/platforms/tmimsp/.family @@ -23,6 +23,7 @@ push( @includes, qw( %T/chips/cp210x %T/chips/stm25p %T/lib/adc + %T/lib/debug %T/lib/misc %T/lib/power %T/lib/serial -- 2.39.2