+/* 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 <ctype.h>
+#include <stdarg.h>
+#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");
+ }
+ }
+}