Shows SPI activity. Replace SPI with SPIDebug, and replace chip select's DigitalOutput with CSDebug. Outputs debug info on stdout. Currently only outputs 8-bit debug info. Easy enough to change that if your SPI debug uses a larger data width.

Dependents:   SST25VF064C

Files at this revision

API Documentation at this revision

Comitter:
davervw
Date:
Tue Apr 10 04:42:12 2012 +0000
Commit message:
SPIDebug library initial commit

Changed in this revision

SPIDebug.cpp Show annotated file Show diff for this revision Revisions of this file
SPIDebug.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 0f32ac84dca5 SPIDebug.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SPIDebug.cpp	Tue Apr 10 04:42:12 2012 +0000
@@ -0,0 +1,71 @@
+///////////////////////////////////////////////////////////////////////////////
+// SPIDebug.h
+//
+// COPYRIGHT (c) 2012 by David Van Wagner
+//
+// dave@vanwagner.org
+// http://techwithdave.blogspot.com
+//
+// License: Creative Commons Attribution-ShareAlike 3.0 Unported License
+// http://creativecommons.org/licenses/by-sa/3.0/
+///////////////////////////////////////////////////////////////////////////////
+
+#include "SPIDebug.h"
+
+bool SPIDebug::debug = true; // default on
+
+SPIDebug::SPIDebug(PinName mosi, PinName miso, PinName sclk, const char *name)
+{
+    if (debug)
+        printf("SPI(%08x, %08x, %08x, %s)\n", mosi, miso, sclk, name);
+    spi = new SPI(mosi, miso, sclk, name);
+}
+
+SPIDebug::~SPIDebug()
+{
+    delete spi;
+}
+
+void SPIDebug::format(int bits, int mode)
+{
+    if (debug)
+        printf("SPI.format(%d, %d)\n", bits, mode);
+    spi->format(bits, mode);
+}
+
+void SPIDebug::frequency(int hz)
+{
+    if (debug)
+        printf("SPI.frequency(%d)\n", hz);
+    spi->frequency(hz);
+}
+
+int SPIDebug::write(int value)
+{
+    int result = spi->write(value);
+    if (debug)
+        printf(">%02x <%02x ", value, result);
+    return result;
+}
+
+CSDebug::CSDebug(PinName pin)
+{
+    cs = new DigitalOut(pin);
+}
+
+CSDebug::~CSDebug()
+{
+    delete cs;
+}
+
+void CSDebug::write(bool state)
+{
+    cs->write(state);
+    if (SPIDebug::debug)
+    {
+        if (state == false)
+            printf("SPI CS\n");
+        else
+            printf("\n");
+    }
+}
diff -r 000000000000 -r 0f32ac84dca5 SPIDebug.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SPIDebug.h	Tue Apr 10 04:42:12 2012 +0000
@@ -0,0 +1,38 @@
+///////////////////////////////////////////////////////////////////////////////
+// SPIDebug.h
+//
+// COPYRIGHT (c) 2012 by David Van Wagner
+//
+// dave@vanwagner.org
+// http://techwithdave.blogspot.com
+//
+// License: Creative Commons Attribution-ShareAlike 3.0 Unported License
+// http://creativecommons.org/licenses/by-sa/3.0/
+///////////////////////////////////////////////////////////////////////////////
+
+#include <mbed.h>
+
+class SPIDebug
+{
+private:
+    SPI* spi;
+    
+public:
+    SPIDebug(PinName mosi, PinName miso, PinName sclk, const char *name = NULL);
+    virtual ~SPIDebug();
+    void format(int bits, int mode = 0);
+    void frequency(int hz = 10000000);
+    virtual int write(int value);
+    static bool debug; // allow to be changed to control whether there are debug messages
+};
+
+class CSDebug
+{
+private:
+    DigitalOut* cs;
+
+public:
+    CSDebug(PinName pin);
+    ~CSDebug();
+    void write(bool state);
+};