Software SPI allows non-standard SPI pins to be used for interfacing with SPI devices

Dependencies:   SWSPI

Fork of SWSPI by Dave Van Wagner

Files at this revision

API Documentation at this revision

Comitter:
Throwbot
Date:
Thu May 08 19:10:37 2014 +0000
Parent:
0:6a500a08c7fd
Child:
2:4612564007c0
Commit message:
good version, just intergrated mouse sensor, has shell, has rc

Changed in this revision

ADNS5090.cpp Show annotated file Show diff for this revision Revisions of this file
ADNS5090.h Show annotated file Show diff for this revision Revisions of this file
ADNS5090.lib Show annotated file Show diff for this revision Revisions of this file
SWSPI.cpp Show diff for this revision Revisions of this file
SWSPI.h Show diff for this revision Revisions of this file
SWSPI.lib Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ADNS5090.cpp	Thu May 08 19:10:37 2014 +0000
@@ -0,0 +1,78 @@
+#include "ADNS5090.h"
+
+ADNS5090::ADNS5090(PinName mosi_pin, PinName miso_pin, PinName sclk_pin, PinName ncs_pin, float pxPerMM_) :
+    spi(mosi_pin, miso_pin, sclk_pin),
+    ncs(ncs_pin),
+    dx_px(0), dy_px(0),
+    pxPerMM(pxPerMM_)
+{
+    ncs = 1;
+    spi.format(8,3);
+    spi.frequency(1000000);    
+    reset();
+}
+
+
+bool ADNS5090::updateMotion()
+{
+    bool ret = false;    
+    dx_px = 0;
+    dy_px = 0;    
+    
+    ncs = 0;    
+    spi.write(ADNS5090_MOTION_ST_REG);    
+    
+    if(0x80 & spi.write(0))
+    {
+        spi.write(ADNS5090_MOTION_BURST_REG);
+        dx_px = (int8_t)spi.write(0);
+        dy_px = (int8_t)spi.write(0);
+        sq = (uint8_t)spi.write(0);
+        ret = true;
+    }
+    ncs = 1;    
+    return ret;
+}
+
+float ADNS5090::dx()
+{
+    return dx_px/pxPerMM;
+}
+
+float ADNS5090::dy()
+{
+    return dy_px/pxPerMM;
+}
+
+void ADNS5090::reset()
+{
+    ncs = 0;
+    spi.write(ADNS5090_WRITE_VAL |
+        ADNS5090_RESET_REG);
+    spi.write(ADNS5090_RESET_VAL);
+    ncs = 1;
+}
+
+void ADNS5090::powerDown()
+{
+    ncs = 0;
+    spi.write(ADNS5090_WRITE_VAL |
+        ADNS5090_MOUSE_CTRL_REG);
+    spi.write(ADNS5090_POWERDOWN_VAL);
+    ncs = 1;
+}
+
+void ADNS5090::setDPI()
+{    
+    ncs = 0;
+    spi.write(ADNS5090_WRITE_VAL|
+        ADNS5090_MOUSE_CTRL_REG);
+    spi.write(0x24);
+    ncs = 1;
+    
+    ncs = 0;
+    spi.write(ADNS5090_WRITE_VAL|
+        0x21);        
+    spi.write(0x10);
+    ncs = 1; 
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ADNS5090.h	Thu May 08 19:10:37 2014 +0000
@@ -0,0 +1,41 @@
+#ifndef ADNS5090_H
+#define ADNS5090_H
+
+#include "stdint.h"
+#include "mbed.h"
+#include "SWSPI.h"
+
+#define ADNS5090_WRITE_VAL          (0x80)
+#define ADNS5090_MOTION_ST_REG      (0x02)
+#define ADNS5090_MOTION_BURST_REG   (0x63)
+#define ADNS5090_RESET_REG          (0x3a)
+#define ADNS5090_RESET_VAL          (0x5a)
+#define ADNS5090_MOUSE_CTRL_REG     (0x0d)
+#define ADNS5090_POWERDOWN_VAL      (0x02)
+
+
+class ADNS5090
+{
+private:
+
+    SWSPI spi;
+    DigitalOut ncs;
+    
+public:
+
+    ADNS5090(PinName mosi_pin, PinName miso_pin, PinName sclk_pin, PinName ncs_pin, float pxPerMM_);
+    
+    int8_t dx_px, dy_px;
+    uint8_t sq;  //surface quality, higher better, ~30 is good, 0= no surface
+    float pxPerMM;
+    
+    float dx();
+    float dy();
+    
+    bool updateMotion();
+    void reset();
+    void powerDown();
+    void setDPI();
+};
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ADNS5090.lib	Thu May 08 19:10:37 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/davervw/code/SWSPI/#6a500a08c7fd
--- a/SWSPI.cpp	Tue Feb 04 06:54:01 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,86 +0,0 @@
-/* SWSPI, Software SPI library
- * Copyright (c) 2012-2014, David R. Van Wagner, http://techwithdave.blogspot.com
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
-
-#include <mbed.h>
-#include "SWSPI.h"
-
-SWSPI::SWSPI(PinName mosi_pin, PinName miso_pin, PinName sclk_pin)
-{
-    mosi = new DigitalOut(mosi_pin);
-    miso = new DigitalIn(miso_pin);
-    sclk = new DigitalOut(sclk_pin);
-    format(8);
-    frequency();
-}
-
-SWSPI::~SWSPI()
-{
-    delete mosi;
-    delete miso;
-    delete sclk;
-}
-
-void SWSPI::format(int bits, int mode)
-{
-    this->bits = bits;
-    this->mode = mode;
-    polarity = (mode >> 1) & 1;
-    phase = mode & 1;
-    sclk->write(polarity);
-}
-
-void SWSPI::frequency(int hz)
-{
-    this->freq = hz;
-}
-
-int SWSPI::write(int value)
-{
-    int read = 0;
-    for (int bit = bits-1; bit >= 0; --bit)
-    {
-        mosi->write(((value >> bit) & 0x01) != 0);
-
-        if (phase == 0)
-        {
-            if (miso->read())
-                read |= (1 << bit);
-        }
-
-        sclk->write(!polarity);
-
-        wait(1.0/freq/2);
-
-        if (phase == 1)
-        {
-            if (miso->read())
-                read |= (1 << bit);
-        }
-
-        sclk->write(polarity);
-
-        wait(1.0/freq/2);
-    }
-    
-    return read;
-}
-
--- a/SWSPI.h	Tue Feb 04 06:54:01 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,95 +0,0 @@
-/* SWSPI, Software SPI library
- * Copyright (c) 2012-2014, David R. Van Wagner, http://techwithdave.blogspot.com
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
-
-#ifndef SWSPI_H
-#define SWSPI_H
-
-/** A software implemented SPI that can use any digital pins
- *
- * Useful when don't want to share a single SPI hardware among attached devices
- * or when pinout doesn't match exactly to the target's SPI pins
- *
- * @code
- * #include "mbed.h"
- * #include "SWSPI.h"
- * 
- * SWSPI spi(p5, p6, p7); // mosi, miso, sclk
- * 
- * int main() 
- * {
- *     DigitalOut cs(p8);
- *     spi.format(8, 0);
- *     spi.frequency(10000000);
- *     cs.write(0);
- *     spi.write(0x9f);
- *     int jedecid = (spi.write(0) << 16) | (spi.write(0) << 8) | spi.write(0);
- *     cs.write(1);
- * }
- * @endcode
- */
-class SWSPI
-{
-private:
-    DigitalOut* mosi;
-    DigitalIn* miso;
-    DigitalOut* sclk;
-    int port;
-    int bits;
-    int mode;
-    int polarity; // idle clock value
-    int phase; // 0=sample on leading (first) clock edge, 1=trailing (second)
-    int freq;
-    
-public:
-    /** Create SWSPI object
-     *
-     *  @param mosi_pin
-     *  @param miso_pin
-     *  @param sclk_pin
-     */
-    SWSPI(PinName mosi_pin, PinName miso_pin, PinName sclk_pin);
-    
-    /** Destructor */
-    ~SWSPI();
-    
-    /** Specify SPI format
-     *
-     *  @param bits  8 or 16 are typical values
-     *  @param mode  0, 1, 2, or 3 phase (bit1) and idle clock (bit0)
-     */
-    void format(int bits, int mode = 0);
-    
-    /** Specify SPI clock frequency
-     *
-     *  @param hz  frequency (optional, defaults to 10000000)
-     */
-    void frequency(int hz = 10000000);
-    
-    /** Write data and read result
-     *
-     *  @param value  data to write (see format for bit size)
-     *  returns value read from device
-     */
-    int write(int value);
-};
-
-#endif // SWSPI_H
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SWSPI.lib	Thu May 08 19:10:37 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/davervw/code/SWSPI/#6a500a08c7fd