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:
Mon Jun 16 10:45:10 2014 +0000
Parent:
1:98a9d6fc60ae
Commit message:
klhkl

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
diff -r 98a9d6fc60ae -r 4612564007c0 ADNS5090.cpp
--- a/ADNS5090.cpp	Thu May 08 19:10:37 2014 +0000
+++ b/ADNS5090.cpp	Mon Jun 16 10:45:10 2014 +0000
@@ -1,14 +1,17 @@
 #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),
+ADNS5090::ADNS5090(SWSPI& spi_hook, Mutex& mutex_hook, PinName ncs_pin, float pxPerMM_) :
+    spi(spi_hook),
+    spi_mutex(mutex_hook),
     ncs(ncs_pin),
     dx_px(0), dy_px(0),
     pxPerMM(pxPerMM_)
 {
     ncs = 1;
+    spi_mutex.lock();
     spi.format(8,3);
-    spi.frequency(1000000);    
+    spi.frequency(1000000);
+    spi_mutex.unlock();
     reset();
 }
 
@@ -19,7 +22,9 @@
     dx_px = 0;
     dy_px = 0;    
     
-    ncs = 0;    
+    spi_mutex.lock();
+    
+    ncs = 0;
     spi.write(ADNS5090_MOTION_ST_REG);    
     
     if(0x80 & spi.write(0))
@@ -30,7 +35,10 @@
         sq = (uint8_t)spi.write(0);
         ret = true;
     }
-    ncs = 1;    
+    ncs = 1;
+    
+    spi_mutex.unlock();
+    
     return ret;
 }
 
@@ -46,24 +54,30 @@
 
 void ADNS5090::reset()
 {
+    spi_mutex.lock();
     ncs = 0;
     spi.write(ADNS5090_WRITE_VAL |
         ADNS5090_RESET_REG);
     spi.write(ADNS5090_RESET_VAL);
     ncs = 1;
+    spi_mutex.unlock();
 }
 
 void ADNS5090::powerDown()
 {
+    spi_mutex.lock();
     ncs = 0;
     spi.write(ADNS5090_WRITE_VAL |
         ADNS5090_MOUSE_CTRL_REG);
     spi.write(ADNS5090_POWERDOWN_VAL);
     ncs = 1;
+    spi_mutex.unlock();
 }
 
 void ADNS5090::setDPI()
 {    
+    spi_mutex.lock();
+    
     ncs = 0;
     spi.write(ADNS5090_WRITE_VAL|
         ADNS5090_MOUSE_CTRL_REG);
@@ -75,4 +89,6 @@
         0x21);        
     spi.write(0x10);
     ncs = 1; 
+    
+    spi_mutex.unlock();
 }
\ No newline at end of file
diff -r 98a9d6fc60ae -r 4612564007c0 ADNS5090.h
--- a/ADNS5090.h	Thu May 08 19:10:37 2014 +0000
+++ b/ADNS5090.h	Mon Jun 16 10:45:10 2014 +0000
@@ -4,6 +4,7 @@
 #include "stdint.h"
 #include "mbed.h"
 #include "SWSPI.h"
+#include "rtos.h"
 
 #define ADNS5090_WRITE_VAL          (0x80)
 #define ADNS5090_MOTION_ST_REG      (0x02)
@@ -18,12 +19,13 @@
 {
 private:
 
-    SWSPI spi;
+    SWSPI& spi;
+    Mutex& spi_mutex;
     DigitalOut ncs;
     
 public:
 
-    ADNS5090(PinName mosi_pin, PinName miso_pin, PinName sclk_pin, PinName ncs_pin, float pxPerMM_);
+    ADNS5090(SWSPI&, Mutex&, PinName ncs_pin, float pxPerMM_);
     
     int8_t dx_px, dy_px;
     uint8_t sq;  //surface quality, higher better, ~30 is good, 0= no surface