nucho / ADNS6010

Files at this revision

API Documentation at this revision

Comitter:
nucho
Date:
Sun Nov 20 13:30:42 2011 +0000
Child:
1:ac87e825b0d7
Commit message:

Changed in this revision

ADNS6010.cpp Show annotated file Show diff for this revision Revisions of this file
ADNS6010.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ADNS6010.cpp	Sun Nov 20 13:30:42 2011 +0000
@@ -0,0 +1,144 @@
+#include "ADNS6010.h"
+
+ADNS6010::ADNS6010(PinName mosi, PinName miso, PinName sclk, PinName ncs, PinName reset,PinName npd)
+        : spi_(mosi, miso, sclk), npd_(npd),reset_(reset), ncs_(ncs) {
+    spi_.format(8,3);
+    spi_.frequency(500000);
+
+    // Chip Reset
+    creset();
+    start();
+   
+    npd_ =1;
+    wait_ms(1);
+
+    int prod_id = cread(REGISTER_PRODUCTID); // read Product ID register
+    int rev_id = cread(REGISTER_REVISONID); // read Revision ID register
+
+    if ((prod_id!=ADNS6010_PRODUCTID)||(rev_id!=ADNS6010_REVISIONID)) {
+        error("The connection with the ADNS-6010 is not established\n");
+    } else {
+        printf("productID:%d\trevisionID:%d\n", prod_id,rev_id);
+    }
+    
+}
+
+// write data to a register
+void ADNS6010::cwrite(unsigned char caddress, unsigned char cdata) {
+    ncs_=0;
+    // send the command to read the register
+    caddress |= 0x80;
+    spi_.write(caddress);
+    // send dummy byte
+    spi_.write(cdata);
+    ncs_=1;
+    
+    wait_us(10); // more than the minimum for safety
+}
+
+
+// mouse chip - change resolurion
+void ADNS6010::changeCPI(int cpi) {
+    int config = 0x49;//resolution reset(=default=400)
+    
+    switch (cpi) {
+        case 400:
+            //config = config & 0x49
+            break;
+            
+        case 800: // Change cpi to 800
+            config |= 0x10;
+            break;
+
+        case 1600: // Change cpi to 1600
+            config |= 0x04;
+            break;
+
+        case 2000: // Change cpi to 2000
+            config |= 0x14;
+            break;
+
+        default:
+            error("setting fault\n cpi setting is only 400 or 800 or 1600 or 2000\n\r");
+            break;
+    }
+    
+    cwrite(REGISTER_CONFIGURATION_BITS,config);
+}
+
+
+void ADNS6010::end()
+{
+    npd_=0;
+}
+
+void ADNS6010::start()
+{
+    npd_=1;
+}
+
+void ADNS6010::creset()
+{
+    reset_ = 1;
+    wait_us(10);
+    reset_ = 0;
+}
+
+void ADNS6010::set_freq(int freq)
+{
+    if(freq>=500000&&freq<=2000000)
+    {
+        spi_.frequency(freq);
+    }
+}
+
+int ADNS6010::read_squal()
+{
+    return cread(REGISTER_SQUAL)*4;
+}
+
+// read the delta_X and delta_Y of the chip
+void ADNS6010::read_deltas(int* a_dx, int* a_dy) {
+
+    int * dx, * dy;
+    dx=(int*)a_dx;
+    dy=(int*)a_dy;
+    *dx = 0;
+    *dy = 0;
+
+    int motion = cread(REGISTER_MOTION); // read Motion register
+    
+    if ((motion&0x80)==0x80) {
+        *dx = MChipMotion(cread(REGISTER_DELTAX));
+        *dy = MChipMotion(cread(REGISTER_DELTAY));
+    }
+}
+
+// read a register
+int ADNS6010::cread(unsigned char cregister) {
+
+    ncs_=0;
+    // send the command to read the register
+    spi_.write(cregister);
+    
+    if(cregister==REGISTER_MOTION)  wait_us(DELAY_TRAD_MOT);
+    else                            wait_us(DELAY_TRAD);
+    
+    // send dummy byte
+    int reply = spi_.write(0x00);
+    ncs_=1;
+    
+    return reply;
+}
+
+// Transform the reading of the mouse Delta register to actual motion
+int ADNS6010::MChipMotion(int reading) {
+
+    int displacement;
+    if (reading <= 0x7f) {
+        displacement = reading;
+    } else {
+        displacement = reading - 256;
+    }
+    return displacement;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ADNS6010.h	Sun Nov 20 13:30:42 2011 +0000
@@ -0,0 +1,87 @@
+/**************************
+This class referred to ADNS5020EN library
+http://mbed.org/users/IPAB/programs/ADNS5020EN/5zwdp
+**************************/
+
+#ifndef _ADNS6010_H_
+#define _ADNS6010_H_
+
+#define REGISTER_PRODUCTID 0x00
+#define REGISTER_REVISONID 0x01
+#define REGISTER_MOTION 0x02
+#define REGISTER_DELTAX 0x03
+#define REGISTER_DELTAY 0x04
+#define REGISTER_SQUAL  0x05
+#define REGISTER_CONFIGURATION_BITS  0x0a
+
+#define ADNS6010_PRODUCTID  0x1C
+#define ADNS6010_REVISIONID 0x20
+
+#define DELAY_TRAD    50
+#define DELAY_TRAD_MOT  75
+#define DELAY_RESET_PULSE 10
+
+#include"mbed.h"
+
+ /** Interface to control a ADNS-6010 Avago mouse 
+  * chip using a SPI and 3 DigitalOuts
+  */
+class ADNS6010 {
+public:
+   /** Create the mouse chip control interface. Returns Error if the connection is not established.
+    *
+    * @param mosi A SPI(mosi) pin, for the spi interface of the mouse chip (SDIO)
+    * @param miso A SPI(miso) pin, for the spi interface of the mouse chip (SDIO)
+    * @param sclk A SPI(sclk) pin, for the spi interface of the mouse chip (SCLK)
+    * @param ncs A DigitalOut, set low for chip select
+    * @param reset A DigitalOut, set high for chip reset
+    * @param npd A DigitalOut, set high for chip powerdown
+    */
+    ADNS6010(PinName mosi, PinName miso, PinName sclk, PinName ncs, PinName reset,PinName npd);
+    
+   /** Read the deltaX and deltaY registers and assign to the input variable the counts for the previous read.
+    * 
+    * @param a_dx A pointer to an integer, to return the value of the deltaX register in reading counts
+    * @param a_dy A pointer to an integer, to return the value of the deltaY register in reading counts
+    */
+    void read_deltas(int* a_dx, int* a_dy);
+    
+   /** Read the SQUAL registers
+    * 
+    * @return  SQUAL register value an integer
+    */
+    int read_squal();
+    
+   /** Change the resolution of the mouse chip.
+    * 
+    * @param cpi A int value  400 or 800 or 1600 or 2000 
+    */      
+    void changeCPI(int cpi);
+    
+   /** Set new operating frequency to the mouse chip.
+    * 
+    * @param freq A int value between 500000 and 2000000 for the SPI frequency(in MHz)
+    */
+    void set_freq(int freq);
+    
+   /** Chip Power Down.
+    */
+    void end();
+    
+   /** Chip Power Up.
+    */
+    void start();
+    
+private:
+    int cread(unsigned char cregister);
+    void cwrite(unsigned char caddress, unsigned char cdata);
+    int MChipMotion(int reading);
+    void creset();
+    
+    SPI spi_;
+    DigitalOut npd_;
+    DigitalOut reset_;
+    DigitalOut ncs_;
+};
+
+#endif