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

Dependencies:   SWSPI

Fork of SWSPI by Dave Van Wagner

Committer:
Throwbot
Date:
Mon Jun 16 10:45:10 2014 +0000
Revision:
2:4612564007c0
Parent:
1:98a9d6fc60ae
klhkl

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Throwbot 1:98a9d6fc60ae 1 #include "ADNS5090.h"
davervw 0:6a500a08c7fd 2
Throwbot 2:4612564007c0 3 ADNS5090::ADNS5090(SWSPI& spi_hook, Mutex& mutex_hook, PinName ncs_pin, float pxPerMM_) :
Throwbot 2:4612564007c0 4 spi(spi_hook),
Throwbot 2:4612564007c0 5 spi_mutex(mutex_hook),
Throwbot 1:98a9d6fc60ae 6 ncs(ncs_pin),
Throwbot 1:98a9d6fc60ae 7 dx_px(0), dy_px(0),
Throwbot 1:98a9d6fc60ae 8 pxPerMM(pxPerMM_)
davervw 0:6a500a08c7fd 9 {
Throwbot 1:98a9d6fc60ae 10 ncs = 1;
Throwbot 2:4612564007c0 11 spi_mutex.lock();
Throwbot 1:98a9d6fc60ae 12 spi.format(8,3);
Throwbot 2:4612564007c0 13 spi.frequency(1000000);
Throwbot 2:4612564007c0 14 spi_mutex.unlock();
Throwbot 1:98a9d6fc60ae 15 reset();
davervw 0:6a500a08c7fd 16 }
davervw 0:6a500a08c7fd 17
Throwbot 1:98a9d6fc60ae 18
Throwbot 1:98a9d6fc60ae 19 bool ADNS5090::updateMotion()
davervw 0:6a500a08c7fd 20 {
Throwbot 1:98a9d6fc60ae 21 bool ret = false;
Throwbot 1:98a9d6fc60ae 22 dx_px = 0;
Throwbot 1:98a9d6fc60ae 23 dy_px = 0;
Throwbot 1:98a9d6fc60ae 24
Throwbot 2:4612564007c0 25 spi_mutex.lock();
Throwbot 2:4612564007c0 26
Throwbot 2:4612564007c0 27 ncs = 0;
Throwbot 1:98a9d6fc60ae 28 spi.write(ADNS5090_MOTION_ST_REG);
Throwbot 1:98a9d6fc60ae 29
Throwbot 1:98a9d6fc60ae 30 if(0x80 & spi.write(0))
Throwbot 1:98a9d6fc60ae 31 {
Throwbot 1:98a9d6fc60ae 32 spi.write(ADNS5090_MOTION_BURST_REG);
Throwbot 1:98a9d6fc60ae 33 dx_px = (int8_t)spi.write(0);
Throwbot 1:98a9d6fc60ae 34 dy_px = (int8_t)spi.write(0);
Throwbot 1:98a9d6fc60ae 35 sq = (uint8_t)spi.write(0);
Throwbot 1:98a9d6fc60ae 36 ret = true;
Throwbot 1:98a9d6fc60ae 37 }
Throwbot 2:4612564007c0 38 ncs = 1;
Throwbot 2:4612564007c0 39
Throwbot 2:4612564007c0 40 spi_mutex.unlock();
Throwbot 2:4612564007c0 41
Throwbot 1:98a9d6fc60ae 42 return ret;
davervw 0:6a500a08c7fd 43 }
davervw 0:6a500a08c7fd 44
Throwbot 1:98a9d6fc60ae 45 float ADNS5090::dx()
davervw 0:6a500a08c7fd 46 {
Throwbot 1:98a9d6fc60ae 47 return dx_px/pxPerMM;
davervw 0:6a500a08c7fd 48 }
davervw 0:6a500a08c7fd 49
Throwbot 1:98a9d6fc60ae 50 float ADNS5090::dy()
davervw 0:6a500a08c7fd 51 {
Throwbot 1:98a9d6fc60ae 52 return dy_px/pxPerMM;
Throwbot 1:98a9d6fc60ae 53 }
Throwbot 1:98a9d6fc60ae 54
Throwbot 1:98a9d6fc60ae 55 void ADNS5090::reset()
Throwbot 1:98a9d6fc60ae 56 {
Throwbot 2:4612564007c0 57 spi_mutex.lock();
Throwbot 1:98a9d6fc60ae 58 ncs = 0;
Throwbot 1:98a9d6fc60ae 59 spi.write(ADNS5090_WRITE_VAL |
Throwbot 1:98a9d6fc60ae 60 ADNS5090_RESET_REG);
Throwbot 1:98a9d6fc60ae 61 spi.write(ADNS5090_RESET_VAL);
Throwbot 1:98a9d6fc60ae 62 ncs = 1;
Throwbot 2:4612564007c0 63 spi_mutex.unlock();
davervw 0:6a500a08c7fd 64 }
davervw 0:6a500a08c7fd 65
Throwbot 1:98a9d6fc60ae 66 void ADNS5090::powerDown()
davervw 0:6a500a08c7fd 67 {
Throwbot 2:4612564007c0 68 spi_mutex.lock();
Throwbot 1:98a9d6fc60ae 69 ncs = 0;
Throwbot 1:98a9d6fc60ae 70 spi.write(ADNS5090_WRITE_VAL |
Throwbot 1:98a9d6fc60ae 71 ADNS5090_MOUSE_CTRL_REG);
Throwbot 1:98a9d6fc60ae 72 spi.write(ADNS5090_POWERDOWN_VAL);
Throwbot 1:98a9d6fc60ae 73 ncs = 1;
Throwbot 2:4612564007c0 74 spi_mutex.unlock();
davervw 0:6a500a08c7fd 75 }
davervw 0:6a500a08c7fd 76
Throwbot 1:98a9d6fc60ae 77 void ADNS5090::setDPI()
Throwbot 1:98a9d6fc60ae 78 {
Throwbot 2:4612564007c0 79 spi_mutex.lock();
Throwbot 2:4612564007c0 80
Throwbot 1:98a9d6fc60ae 81 ncs = 0;
Throwbot 1:98a9d6fc60ae 82 spi.write(ADNS5090_WRITE_VAL|
Throwbot 1:98a9d6fc60ae 83 ADNS5090_MOUSE_CTRL_REG);
Throwbot 1:98a9d6fc60ae 84 spi.write(0x24);
Throwbot 1:98a9d6fc60ae 85 ncs = 1;
Throwbot 1:98a9d6fc60ae 86
Throwbot 1:98a9d6fc60ae 87 ncs = 0;
Throwbot 1:98a9d6fc60ae 88 spi.write(ADNS5090_WRITE_VAL|
Throwbot 1:98a9d6fc60ae 89 0x21);
Throwbot 1:98a9d6fc60ae 90 spi.write(0x10);
Throwbot 1:98a9d6fc60ae 91 ncs = 1;
Throwbot 2:4612564007c0 92
Throwbot 2:4612564007c0 93 spi_mutex.unlock();
Throwbot 1:98a9d6fc60ae 94 }