Avago ADNS6010 mouse chip library This class referred to ADNS5020EN library http://mbed.org/users/IPAB/programs/ADNS5020EN/5zwdp

Committer:
nucho
Date:
Sun Nov 20 13:30:42 2011 +0000
Revision:
0:efc16a3c5ce4
Child:
1:ac87e825b0d7

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nucho 0:efc16a3c5ce4 1 #include "ADNS6010.h"
nucho 0:efc16a3c5ce4 2
nucho 0:efc16a3c5ce4 3 ADNS6010::ADNS6010(PinName mosi, PinName miso, PinName sclk, PinName ncs, PinName reset,PinName npd)
nucho 0:efc16a3c5ce4 4 : spi_(mosi, miso, sclk), npd_(npd),reset_(reset), ncs_(ncs) {
nucho 0:efc16a3c5ce4 5 spi_.format(8,3);
nucho 0:efc16a3c5ce4 6 spi_.frequency(500000);
nucho 0:efc16a3c5ce4 7
nucho 0:efc16a3c5ce4 8 // Chip Reset
nucho 0:efc16a3c5ce4 9 creset();
nucho 0:efc16a3c5ce4 10 start();
nucho 0:efc16a3c5ce4 11
nucho 0:efc16a3c5ce4 12 npd_ =1;
nucho 0:efc16a3c5ce4 13 wait_ms(1);
nucho 0:efc16a3c5ce4 14
nucho 0:efc16a3c5ce4 15 int prod_id = cread(REGISTER_PRODUCTID); // read Product ID register
nucho 0:efc16a3c5ce4 16 int rev_id = cread(REGISTER_REVISONID); // read Revision ID register
nucho 0:efc16a3c5ce4 17
nucho 0:efc16a3c5ce4 18 if ((prod_id!=ADNS6010_PRODUCTID)||(rev_id!=ADNS6010_REVISIONID)) {
nucho 0:efc16a3c5ce4 19 error("The connection with the ADNS-6010 is not established\n");
nucho 0:efc16a3c5ce4 20 } else {
nucho 0:efc16a3c5ce4 21 printf("productID:%d\trevisionID:%d\n", prod_id,rev_id);
nucho 0:efc16a3c5ce4 22 }
nucho 0:efc16a3c5ce4 23
nucho 0:efc16a3c5ce4 24 }
nucho 0:efc16a3c5ce4 25
nucho 0:efc16a3c5ce4 26 // write data to a register
nucho 0:efc16a3c5ce4 27 void ADNS6010::cwrite(unsigned char caddress, unsigned char cdata) {
nucho 0:efc16a3c5ce4 28 ncs_=0;
nucho 0:efc16a3c5ce4 29 // send the command to read the register
nucho 0:efc16a3c5ce4 30 caddress |= 0x80;
nucho 0:efc16a3c5ce4 31 spi_.write(caddress);
nucho 0:efc16a3c5ce4 32 // send dummy byte
nucho 0:efc16a3c5ce4 33 spi_.write(cdata);
nucho 0:efc16a3c5ce4 34 ncs_=1;
nucho 0:efc16a3c5ce4 35
nucho 0:efc16a3c5ce4 36 wait_us(10); // more than the minimum for safety
nucho 0:efc16a3c5ce4 37 }
nucho 0:efc16a3c5ce4 38
nucho 0:efc16a3c5ce4 39
nucho 0:efc16a3c5ce4 40 // mouse chip - change resolurion
nucho 0:efc16a3c5ce4 41 void ADNS6010::changeCPI(int cpi) {
nucho 0:efc16a3c5ce4 42 int config = 0x49;//resolution reset(=default=400)
nucho 0:efc16a3c5ce4 43
nucho 0:efc16a3c5ce4 44 switch (cpi) {
nucho 0:efc16a3c5ce4 45 case 400:
nucho 0:efc16a3c5ce4 46 //config = config & 0x49
nucho 0:efc16a3c5ce4 47 break;
nucho 0:efc16a3c5ce4 48
nucho 0:efc16a3c5ce4 49 case 800: // Change cpi to 800
nucho 0:efc16a3c5ce4 50 config |= 0x10;
nucho 0:efc16a3c5ce4 51 break;
nucho 0:efc16a3c5ce4 52
nucho 0:efc16a3c5ce4 53 case 1600: // Change cpi to 1600
nucho 0:efc16a3c5ce4 54 config |= 0x04;
nucho 0:efc16a3c5ce4 55 break;
nucho 0:efc16a3c5ce4 56
nucho 0:efc16a3c5ce4 57 case 2000: // Change cpi to 2000
nucho 0:efc16a3c5ce4 58 config |= 0x14;
nucho 0:efc16a3c5ce4 59 break;
nucho 0:efc16a3c5ce4 60
nucho 0:efc16a3c5ce4 61 default:
nucho 0:efc16a3c5ce4 62 error("setting fault\n cpi setting is only 400 or 800 or 1600 or 2000\n\r");
nucho 0:efc16a3c5ce4 63 break;
nucho 0:efc16a3c5ce4 64 }
nucho 0:efc16a3c5ce4 65
nucho 0:efc16a3c5ce4 66 cwrite(REGISTER_CONFIGURATION_BITS,config);
nucho 0:efc16a3c5ce4 67 }
nucho 0:efc16a3c5ce4 68
nucho 0:efc16a3c5ce4 69
nucho 0:efc16a3c5ce4 70 void ADNS6010::end()
nucho 0:efc16a3c5ce4 71 {
nucho 0:efc16a3c5ce4 72 npd_=0;
nucho 0:efc16a3c5ce4 73 }
nucho 0:efc16a3c5ce4 74
nucho 0:efc16a3c5ce4 75 void ADNS6010::start()
nucho 0:efc16a3c5ce4 76 {
nucho 0:efc16a3c5ce4 77 npd_=1;
nucho 0:efc16a3c5ce4 78 }
nucho 0:efc16a3c5ce4 79
nucho 0:efc16a3c5ce4 80 void ADNS6010::creset()
nucho 0:efc16a3c5ce4 81 {
nucho 0:efc16a3c5ce4 82 reset_ = 1;
nucho 0:efc16a3c5ce4 83 wait_us(10);
nucho 0:efc16a3c5ce4 84 reset_ = 0;
nucho 0:efc16a3c5ce4 85 }
nucho 0:efc16a3c5ce4 86
nucho 0:efc16a3c5ce4 87 void ADNS6010::set_freq(int freq)
nucho 0:efc16a3c5ce4 88 {
nucho 0:efc16a3c5ce4 89 if(freq>=500000&&freq<=2000000)
nucho 0:efc16a3c5ce4 90 {
nucho 0:efc16a3c5ce4 91 spi_.frequency(freq);
nucho 0:efc16a3c5ce4 92 }
nucho 0:efc16a3c5ce4 93 }
nucho 0:efc16a3c5ce4 94
nucho 0:efc16a3c5ce4 95 int ADNS6010::read_squal()
nucho 0:efc16a3c5ce4 96 {
nucho 0:efc16a3c5ce4 97 return cread(REGISTER_SQUAL)*4;
nucho 0:efc16a3c5ce4 98 }
nucho 0:efc16a3c5ce4 99
nucho 0:efc16a3c5ce4 100 // read the delta_X and delta_Y of the chip
nucho 0:efc16a3c5ce4 101 void ADNS6010::read_deltas(int* a_dx, int* a_dy) {
nucho 0:efc16a3c5ce4 102
nucho 0:efc16a3c5ce4 103 int * dx, * dy;
nucho 0:efc16a3c5ce4 104 dx=(int*)a_dx;
nucho 0:efc16a3c5ce4 105 dy=(int*)a_dy;
nucho 0:efc16a3c5ce4 106 *dx = 0;
nucho 0:efc16a3c5ce4 107 *dy = 0;
nucho 0:efc16a3c5ce4 108
nucho 0:efc16a3c5ce4 109 int motion = cread(REGISTER_MOTION); // read Motion register
nucho 0:efc16a3c5ce4 110
nucho 0:efc16a3c5ce4 111 if ((motion&0x80)==0x80) {
nucho 0:efc16a3c5ce4 112 *dx = MChipMotion(cread(REGISTER_DELTAX));
nucho 0:efc16a3c5ce4 113 *dy = MChipMotion(cread(REGISTER_DELTAY));
nucho 0:efc16a3c5ce4 114 }
nucho 0:efc16a3c5ce4 115 }
nucho 0:efc16a3c5ce4 116
nucho 0:efc16a3c5ce4 117 // read a register
nucho 0:efc16a3c5ce4 118 int ADNS6010::cread(unsigned char cregister) {
nucho 0:efc16a3c5ce4 119
nucho 0:efc16a3c5ce4 120 ncs_=0;
nucho 0:efc16a3c5ce4 121 // send the command to read the register
nucho 0:efc16a3c5ce4 122 spi_.write(cregister);
nucho 0:efc16a3c5ce4 123
nucho 0:efc16a3c5ce4 124 if(cregister==REGISTER_MOTION) wait_us(DELAY_TRAD_MOT);
nucho 0:efc16a3c5ce4 125 else wait_us(DELAY_TRAD);
nucho 0:efc16a3c5ce4 126
nucho 0:efc16a3c5ce4 127 // send dummy byte
nucho 0:efc16a3c5ce4 128 int reply = spi_.write(0x00);
nucho 0:efc16a3c5ce4 129 ncs_=1;
nucho 0:efc16a3c5ce4 130
nucho 0:efc16a3c5ce4 131 return reply;
nucho 0:efc16a3c5ce4 132 }
nucho 0:efc16a3c5ce4 133
nucho 0:efc16a3c5ce4 134 // Transform the reading of the mouse Delta register to actual motion
nucho 0:efc16a3c5ce4 135 int ADNS6010::MChipMotion(int reading) {
nucho 0:efc16a3c5ce4 136
nucho 0:efc16a3c5ce4 137 int displacement;
nucho 0:efc16a3c5ce4 138 if (reading <= 0x7f) {
nucho 0:efc16a3c5ce4 139 displacement = reading;
nucho 0:efc16a3c5ce4 140 } else {
nucho 0:efc16a3c5ce4 141 displacement = reading - 256;
nucho 0:efc16a3c5ce4 142 }
nucho 0:efc16a3c5ce4 143 return displacement;
nucho 0:efc16a3c5ce4 144 }