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

Committer:
nucho
Date:
Mon Nov 21 16:00:51 2011 +0000
Revision:
1:ac87e825b0d7
Parent:
0:efc16a3c5ce4

        

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