fork of Sam Grove's library

Fork of ADIS16488 by Sam Grove

Committer:
raprism
Date:
Tue Jan 10 22:22:51 2017 +0100
Revision:
3:4420cf0e0b74
Parent:
1:18fe8829aa69
- added ADIS16485 as correct model type
- removed redundant SPI writes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sam_grove 0:e9a81060a092 1 /**
sam_grove 0:e9a81060a092 2 * @file Adis16488.cpp
sam_grove 0:e9a81060a092 3 * @brief Device driver - ADIS16488 IMU
sam_grove 0:e9a81060a092 4 * @author sam grove
sam_grove 0:e9a81060a092 5 * @version 1.0
sam_grove 0:e9a81060a092 6 * @see http://www.analog.com/static/imported-files/data_sheets/ADIS16488.pdf
sam_grove 0:e9a81060a092 7 *
sam_grove 0:e9a81060a092 8 * Copyright (c) 2013
sam_grove 0:e9a81060a092 9 *
sam_grove 0:e9a81060a092 10 * Licensed under the Apache License, Version 2.0 (the "License");
sam_grove 0:e9a81060a092 11 * you may not use this file except in compliance with the License.
sam_grove 0:e9a81060a092 12 * You may obtain a copy of the License at
sam_grove 0:e9a81060a092 13 *
sam_grove 0:e9a81060a092 14 * http://www.apache.org/licenses/LICENSE-2.0
sam_grove 0:e9a81060a092 15 *
sam_grove 0:e9a81060a092 16 * Unless required by applicable law or agreed to in writing, software
sam_grove 0:e9a81060a092 17 * distributed under the License is distributed on an "AS IS" BASIS,
sam_grove 0:e9a81060a092 18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
sam_grove 0:e9a81060a092 19 * See the License for the specific language governing permissions and
sam_grove 0:e9a81060a092 20 * limitations under the License.
sam_grove 0:e9a81060a092 21 */
raprism 3:4420cf0e0b74 22
sam_grove 0:e9a81060a092 23 #include "Adis16488.h"
sam_grove 0:e9a81060a092 24
sam_grove 0:e9a81060a092 25 uint16_t const GYRO_REGS[] = {0x1000, 0x1200, 0x1400, 0x1600, 0x1800, 0x1A00};
sam_grove 0:e9a81060a092 26 uint16_t const ACCL_REGS[] = {0x1C00, 0x1E00, 0x2000, 0x2200, 0x2400, 0x2600};
sam_grove 0:e9a81060a092 27 uint16_t const MAGN_REGS[] = {0x2800, 0x2A00, 0x2C00};
sam_grove 0:e9a81060a092 28 uint16_t const DELTANG_REGS[] = {0x4000, 0x4200, 0x4400, 0x4600, 0x4800, 0x4A00};
sam_grove 0:e9a81060a092 29 uint16_t const DELTVEL_REGS[] = {0x4C00, 0x4E00, 0x5000, 0x5200, 0x5400, 0x5600};
sam_grove 0:e9a81060a092 30
sam_grove 0:e9a81060a092 31 DigitalOut led_1(LED1);
sam_grove 0:e9a81060a092 32
sam_grove 0:e9a81060a092 33 Adis16488::Adis16488(SPI &spi, DigitalOut &cs, DigitalOut &rst, InterruptIn &dr)
sam_grove 0:e9a81060a092 34 {
sam_grove 0:e9a81060a092 35 _spi = &spi;
sam_grove 0:e9a81060a092 36 _cs = &cs;
sam_grove 0:e9a81060a092 37 _rst = &rst;
sam_grove 0:e9a81060a092 38 _dr = &dr;
raprism 3:4420cf0e0b74 39
sam_grove 0:e9a81060a092 40 return;
sam_grove 0:e9a81060a092 41 }
sam_grove 0:e9a81060a092 42
sam_grove 0:e9a81060a092 43 void Adis16488::init(void)
sam_grove 0:e9a81060a092 44 {
sam_grove 0:e9a81060a092 45 _dr->mode(PullDown);
sam_grove 0:e9a81060a092 46 _rst->write(0);
sam_grove 0:e9a81060a092 47 _cs->write(0);
raprism 3:4420cf0e0b74 48
raprism 3:4420cf0e0b74 49 //_spi->format(16, 3); // needs to be done with spi.format()!
raprism 3:4420cf0e0b74 50 _spi->frequency(2000000);
raprism 3:4420cf0e0b74 51
sam_grove 0:e9a81060a092 52 return;
sam_grove 0:e9a81060a092 53 }
sam_grove 0:e9a81060a092 54
sam_grove 0:e9a81060a092 55 void Adis16488::enable(void)
sam_grove 0:e9a81060a092 56 {
sam_grove 0:e9a81060a092 57 LOG("Preparing the ADIS16488 IMU\n");
raprism 3:4420cf0e0b74 58
sam_grove 0:e9a81060a092 59 _rst->write(1);
sam_grove 0:e9a81060a092 60 wait(1.0);
raprism 3:4420cf0e0b74 61
sam_grove 0:e9a81060a092 62 writeRegister(0x8000);
raprism 3:4420cf0e0b74 63 //writeRegister(0x7e00);
sam_grove 0:e9a81060a092 64 uint16_t id;
raprism 3:4420cf0e0b74 65 readRegister(0x7e00, id); // actually writes also to register!
raprism 3:4420cf0e0b74 66 if(id != 0x4068 && id != 0x4065)
sam_grove 0:e9a81060a092 67 {
sam_grove 0:e9a81060a092 68 ERROR("Product ID doesn't match, %04X\n", id);
sam_grove 0:e9a81060a092 69 }
raprism 3:4420cf0e0b74 70 LOG("Product ID is %05u (%04X).\n", id, id);
sam_grove 0:e9a81060a092 71 //this result correctly returns 0x4068 as per the ADIS16488 specification
raprism 3:4420cf0e0b74 72
raprism 3:4420cf0e0b74 73 //get the SERIAL_NUM (page 4, reg: 0x20)
sam_grove 0:e9a81060a092 74 writeRegister(0x8004); //first change the page to page 4
sam_grove 0:e9a81060a092 75 writeRegister(0x2000); //send the register to get on the next write
sam_grove 0:e9a81060a092 76 uint16_t serial_num;
sam_grove 0:e9a81060a092 77 readRegister(0x2000, serial_num);
sam_grove 0:e9a81060a092 78 LOG("IMU serial number is %04X\n", serial_num);
raprism 3:4420cf0e0b74 79
sam_grove 0:e9a81060a092 80 writeRegister(0x8003); //change to page 3
raprism 3:4420cf0e0b74 81 //writeRegister(0x7800); //get FIRMWARE_REV
sam_grove 0:e9a81060a092 82 uint16_t rev;
sam_grove 0:e9a81060a092 83 readRegister(0x7800, rev);
sam_grove 0:e9a81060a092 84 LOG("Firmware revision %04X\n", rev);
raprism 3:4420cf0e0b74 85
raprism 3:4420cf0e0b74 86 //writeRegister(0x7A00); //get FIRMWARE_DM
sam_grove 0:e9a81060a092 87 uint16_t rev_dm;
sam_grove 0:e9a81060a092 88 readRegister(0x7A00, rev_dm);
raprism 3:4420cf0e0b74 89
raprism 3:4420cf0e0b74 90 //writeRegister(0x7C00); //get FIRMWARE_YR
sam_grove 0:e9a81060a092 91 uint16_t rev_yr;
sam_grove 0:e9a81060a092 92 readRegister(0x7C00, rev_yr);
raprism 3:4420cf0e0b74 93 LOG("Firmware date.month.year is %02X/%02X/%04X\n", (rev_dm&0xff), ((rev_dm>>8)&0xff), rev_yr);
raprism 3:4420cf0e0b74 94
raprism 3:4420cf0e0b74 95 // change the DEC_RATE (this is also in page 3)
raprism 3:4420cf0e0b74 96 // sampling rate is 2460/(DEC_RATE+1)Hz
raprism 3:4420cf0e0b74 97 // so for 0x17: 102.5 Hz
sam_grove 0:e9a81060a092 98 writeRegister(0x8C17); //write high byte (only page number can be written in a single byte)
raprism 3:4420cf0e0b74 99 writeRegister(0x8D00); //write the low byte of DEC_RATE
raprism 3:4420cf0e0b74 100
sam_grove 0:e9a81060a092 101 // using varf...
sam_grove 1:18fe8829aa69 102 // writeRegister(0x86CD); //write high byte to register 0x06
sam_grove 1:18fe8829aa69 103 // writeRegister(0x8700); //write the low byte of 00 to registed 0x07
sam_grove 0:e9a81060a092 104 writeRegister(0x8000); //change to page 0
raprism 3:4420cf0e0b74 105
raprism 3:4420cf0e0b74 106 // configured so IRQ is allowed
sam_grove 0:e9a81060a092 107 _dr->rise(this, &Adis16488::drHandler);
raprism 3:4420cf0e0b74 108
sam_grove 0:e9a81060a092 109 return;
sam_grove 0:e9a81060a092 110 }
sam_grove 0:e9a81060a092 111
sam_grove 0:e9a81060a092 112 void Adis16488::disable(void)
sam_grove 0:e9a81060a092 113 {
sam_grove 0:e9a81060a092 114 _dr->rise(NULL);
sam_grove 0:e9a81060a092 115 _rst->write(0);
sam_grove 0:e9a81060a092 116 }
sam_grove 0:e9a81060a092 117
sam_grove 0:e9a81060a092 118 void Adis16488::readRegister(uint16_t const reg, uint16_t &data)
raprism 3:4420cf0e0b74 119 {
sam_grove 0:e9a81060a092 120 _cs->write(0);
sam_grove 0:e9a81060a092 121 _spi->write(reg);
sam_grove 0:e9a81060a092 122 data = _spi->write(reg);
sam_grove 0:e9a81060a092 123 _cs->write(1);
raprism 3:4420cf0e0b74 124
sam_grove 0:e9a81060a092 125 return;
sam_grove 0:e9a81060a092 126 }
sam_grove 0:e9a81060a092 127
sam_grove 0:e9a81060a092 128 void Adis16488::writeRegister(uint16_t const reg)
sam_grove 0:e9a81060a092 129 {
sam_grove 0:e9a81060a092 130 _cs->write(0);
sam_grove 0:e9a81060a092 131 _spi->write(reg);
sam_grove 0:e9a81060a092 132 _cs->write(1);
raprism 3:4420cf0e0b74 133
sam_grove 0:e9a81060a092 134 return;
sam_grove 0:e9a81060a092 135 }
sam_grove 0:e9a81060a092 136
sam_grove 0:e9a81060a092 137 void Adis16488::drHandler(void)
sam_grove 0:e9a81060a092 138 {
sam_grove 0:e9a81060a092 139 led_1 = !led_1;
sam_grove 0:e9a81060a092 140 // read gyro
sam_grove 0:e9a81060a092 141 for(int i=0; i<6; ++i)
sam_grove 0:e9a81060a092 142 {
sam_grove 0:e9a81060a092 143 readRegister(GYRO_REGS[i], gyro.data[i]);
sam_grove 0:e9a81060a092 144 }
sam_grove 0:e9a81060a092 145 // read accel data
sam_grove 0:e9a81060a092 146 for(int i=0; i<6; ++i)
sam_grove 0:e9a81060a092 147 {
sam_grove 0:e9a81060a092 148 readRegister(ACCL_REGS[i], accel.data[i]);
sam_grove 0:e9a81060a092 149 }
sam_grove 0:e9a81060a092 150 // read mag data
sam_grove 0:e9a81060a092 151 for(int i=0; i<3; ++i)
sam_grove 0:e9a81060a092 152 {
sam_grove 0:e9a81060a092 153 readRegister(MAGN_REGS[i], magn.data[i]);
sam_grove 0:e9a81060a092 154 }
sam_grove 0:e9a81060a092 155 // read delta angles
sam_grove 0:e9a81060a092 156 for(int i=0; i<6; ++i)
sam_grove 0:e9a81060a092 157 {
sam_grove 0:e9a81060a092 158 readRegister(DELTANG_REGS[i], deltang.data[i]);
sam_grove 0:e9a81060a092 159 }
sam_grove 0:e9a81060a092 160 // read delta velocity
sam_grove 0:e9a81060a092 161 for(int i=0; i<6; ++i)
sam_grove 0:e9a81060a092 162 {
sam_grove 0:e9a81060a092 163 readRegister(DELTVEL_REGS[i], deltvel.data[i]);
sam_grove 0:e9a81060a092 164 }
raprism 3:4420cf0e0b74 165
sam_grove 0:e9a81060a092 166 return;
sam_grove 0:e9a81060a092 167 }