Device driver

Committer:
sam_grove
Date:
Wed Apr 10 04:41:21 2013 +0000
Revision:
0:e9a81060a092
Child:
1:18fe8829aa69
Initial commit - working in a basic way

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 */
sam_grove 0:e9a81060a092 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;
sam_grove 0:e9a81060a092 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);
sam_grove 0:e9a81060a092 48
sam_grove 0:e9a81060a092 49 _spi->format(16,3);
sam_grove 0:e9a81060a092 50 _spi->frequency(10000000);
sam_grove 0:e9a81060a092 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");
sam_grove 0:e9a81060a092 58
sam_grove 0:e9a81060a092 59 _rst->write(1);
sam_grove 0:e9a81060a092 60 wait(1.0);
sam_grove 0:e9a81060a092 61
sam_grove 0:e9a81060a092 62 writeRegister(0x8000);
sam_grove 0:e9a81060a092 63 writeRegister(0x7e00);
sam_grove 0:e9a81060a092 64 uint16_t id;
sam_grove 0:e9a81060a092 65 readRegister(0x7e00, id);
sam_grove 0:e9a81060a092 66 if(id != 0x4068)
sam_grove 0:e9a81060a092 67 {
sam_grove 0:e9a81060a092 68 ERROR("Product ID doesn't match, %04X\n", id);
sam_grove 0:e9a81060a092 69 }
sam_grove 0:e9a81060a092 70 LOG("Product ID is %04X\n", id);
sam_grove 0:e9a81060a092 71
sam_grove 0:e9a81060a092 72 //this result correctly returns 0x4068 as per the ADIS16488 specification
sam_grove 0:e9a81060a092 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);
sam_grove 0:e9a81060a092 79
sam_grove 0:e9a81060a092 80 writeRegister(0x8003); //change to page 3
sam_grove 0:e9a81060a092 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);
sam_grove 0:e9a81060a092 85
sam_grove 0:e9a81060a092 86 writeRegister(0x7A00); //get FIRMWARE_DM
sam_grove 0:e9a81060a092 87 uint16_t rev_dm;
sam_grove 0:e9a81060a092 88 readRegister(0x7A00, rev_dm);
sam_grove 0:e9a81060a092 89
sam_grove 0:e9a81060a092 90 writeRegister(0x7C00); //get FIRMWARE_YR
sam_grove 0:e9a81060a092 91 uint16_t rev_yr;
sam_grove 0:e9a81060a092 92 readRegister(0x7C00, rev_yr);
sam_grove 0:e9a81060a092 93 LOG("Frimware date/month/year is %02X/%02X/%04X\n", ((rev_dm>>8)&0xff), (rev_dm&0xff), rev_yr);
sam_grove 0:e9a81060a092 94
sam_grove 0:e9a81060a092 95 //change the DECRATE to 98.4 Hz (this is also in page 3)
sam_grove 0:e9a81060a092 96 writeRegister(0x8C17); //write high byte (only page number can be written in a single byte)
sam_grove 0:e9a81060a092 97 writeRegister(0x8D00); //write the low byte of DECRATE
sam_grove 0:e9a81060a092 98
sam_grove 0:e9a81060a092 99 // using varf...
sam_grove 0:e9a81060a092 100 writeRegister(0x86CD); //write high byte to register 0x06
sam_grove 0:e9a81060a092 101 writeRegister(0x8700); //write the low byte of 00 to registed 0x07
sam_grove 0:e9a81060a092 102 writeRegister(0x8000); //change to page 0
sam_grove 0:e9a81060a092 103
sam_grove 0:e9a81060a092 104 // configred so IRQ is allowed
sam_grove 0:e9a81060a092 105 _dr->rise(this, &Adis16488::drHandler);
sam_grove 0:e9a81060a092 106
sam_grove 0:e9a81060a092 107 return;
sam_grove 0:e9a81060a092 108 }
sam_grove 0:e9a81060a092 109
sam_grove 0:e9a81060a092 110 void Adis16488::disable(void)
sam_grove 0:e9a81060a092 111 {
sam_grove 0:e9a81060a092 112 _dr->rise(NULL);
sam_grove 0:e9a81060a092 113 _rst->write(0);
sam_grove 0:e9a81060a092 114 }
sam_grove 0:e9a81060a092 115
sam_grove 0:e9a81060a092 116 void Adis16488::readRegister(uint16_t const reg, uint16_t &data)
sam_grove 0:e9a81060a092 117 {
sam_grove 0:e9a81060a092 118 _cs->write(0);
sam_grove 0:e9a81060a092 119 _spi->write(reg);
sam_grove 0:e9a81060a092 120 data = _spi->write(reg);
sam_grove 0:e9a81060a092 121 _cs->write(1);
sam_grove 0:e9a81060a092 122
sam_grove 0:e9a81060a092 123 return;
sam_grove 0:e9a81060a092 124 }
sam_grove 0:e9a81060a092 125
sam_grove 0:e9a81060a092 126 void Adis16488::writeRegister(uint16_t const reg)
sam_grove 0:e9a81060a092 127 {
sam_grove 0:e9a81060a092 128 _cs->write(0);
sam_grove 0:e9a81060a092 129 _spi->write(reg);
sam_grove 0:e9a81060a092 130 _cs->write(1);
sam_grove 0:e9a81060a092 131
sam_grove 0:e9a81060a092 132 return;
sam_grove 0:e9a81060a092 133 }
sam_grove 0:e9a81060a092 134
sam_grove 0:e9a81060a092 135 void Adis16488::drHandler(void)
sam_grove 0:e9a81060a092 136 {
sam_grove 0:e9a81060a092 137 led_1 = !led_1;
sam_grove 0:e9a81060a092 138 // read gyro
sam_grove 0:e9a81060a092 139 for(int i=0; i<6; ++i)
sam_grove 0:e9a81060a092 140 {
sam_grove 0:e9a81060a092 141 readRegister(GYRO_REGS[i], gyro.data[i]);
sam_grove 0:e9a81060a092 142 }
sam_grove 0:e9a81060a092 143 // read accel data
sam_grove 0:e9a81060a092 144 for(int i=0; i<6; ++i)
sam_grove 0:e9a81060a092 145 {
sam_grove 0:e9a81060a092 146 readRegister(ACCL_REGS[i], accel.data[i]);
sam_grove 0:e9a81060a092 147 }
sam_grove 0:e9a81060a092 148 // read mag data
sam_grove 0:e9a81060a092 149 for(int i=0; i<3; ++i)
sam_grove 0:e9a81060a092 150 {
sam_grove 0:e9a81060a092 151 readRegister(MAGN_REGS[i], magn.data[i]);
sam_grove 0:e9a81060a092 152 }
sam_grove 0:e9a81060a092 153 // read delta angles
sam_grove 0:e9a81060a092 154 for(int i=0; i<6; ++i)
sam_grove 0:e9a81060a092 155 {
sam_grove 0:e9a81060a092 156 readRegister(DELTANG_REGS[i], deltang.data[i]);
sam_grove 0:e9a81060a092 157 }
sam_grove 0:e9a81060a092 158 // read delta velocity
sam_grove 0:e9a81060a092 159 for(int i=0; i<6; ++i)
sam_grove 0:e9a81060a092 160 {
sam_grove 0:e9a81060a092 161 readRegister(DELTVEL_REGS[i], deltvel.data[i]);
sam_grove 0:e9a81060a092 162 }
sam_grove 0:e9a81060a092 163
sam_grove 0:e9a81060a092 164 return;
sam_grove 0:e9a81060a092 165 }
sam_grove 0:e9a81060a092 166