Read data from an ADIS16355 IMU and write it to the USB port.

Dependencies:   mbed

Committer:
yahugh
Date:
Wed Aug 10 21:50:36 2011 +0000
Revision:
0:c6ee363ac724
initial release, still has some problems but might be of interest to others regardless

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yahugh 0:c6ee363ac724 1 //
yahugh 0:c6ee363ac724 2 // imu-spi.cpp
yahugh 0:c6ee363ac724 3 //
yahugh 0:c6ee363ac724 4 // copyright 2010 Hugh Shane
yahugh 0:c6ee363ac724 5 //
yahugh 0:c6ee363ac724 6 #include "mbed.h"
yahugh 0:c6ee363ac724 7 #include "imu-spi.h"
yahugh 0:c6ee363ac724 8
yahugh 0:c6ee363ac724 9 ImuSpi::ImuSpi(void) :
yahugh 0:c6ee363ac724 10 spi(p5, p6, p7), // mosi, miso, sclk
yahugh 0:c6ee363ac724 11 cs(p8), // IMU chip select
yahugh 0:c6ee363ac724 12 reset(p8), // IMU reset. CAUTION! This also resets the altimeter.
yahugh 0:c6ee363ac724 13 diag(p21), // diagnostic output
yahugh 0:c6ee363ac724 14 imuDataReady(p11) // interrupt on IMU data-ready
yahugh 0:c6ee363ac724 15 {
yahugh 0:c6ee363ac724 16 // Setup the spi for 16 bit data, high steady state clock,
yahugh 0:c6ee363ac724 17 // second edge capture, with a 1 MHz clock rate
yahugh 0:c6ee363ac724 18 spi.format(16,3);
yahugh 0:c6ee363ac724 19 spi.frequency(1000000);
yahugh 0:c6ee363ac724 20
yahugh 0:c6ee363ac724 21 // init the pingpong buffer
yahugh 0:c6ee363ac724 22 pingpong = 0;
yahugh 0:c6ee363ac724 23
yahugh 0:c6ee363ac724 24 // init the interrupt semaphore
yahugh 0:c6ee363ac724 25 dataReady = false;
yahugh 0:c6ee363ac724 26
yahugh 0:c6ee363ac724 27 // interrupt on the falling edge of the IMU data-ready signal
yahugh 0:c6ee363ac724 28 diag = 0;
yahugh 0:c6ee363ac724 29 imuDataReady.fall(this, &ImuSpi::DataReadyISR);
yahugh 0:c6ee363ac724 30
yahugh 0:c6ee363ac724 31 // init the IMU
yahugh 0:c6ee363ac724 32 InitImu();
yahugh 0:c6ee363ac724 33
yahugh 0:c6ee363ac724 34 }
yahugh 0:c6ee363ac724 35
yahugh 0:c6ee363ac724 36 void ImuSpi::reinitInterrupts(void) {
yahugh 0:c6ee363ac724 37 imuDataReady.fall(this, &ImuSpi::DataReadyISR);
yahugh 0:c6ee363ac724 38 }
yahugh 0:c6ee363ac724 39
yahugh 0:c6ee363ac724 40 // initialize the IMU
yahugh 0:c6ee363ac724 41 void ImuSpi::InitImu(void) {
yahugh 0:c6ee363ac724 42 // deselect the IMU
yahugh 0:c6ee363ac724 43 cs = 1;
yahugh 0:c6ee363ac724 44 reset = 1;
yahugh 0:c6ee363ac724 45 // perform a hard reset
yahugh 0:c6ee363ac724 46 reset = 0; wait(.1); reset = 1;
yahugh 0:c6ee363ac724 47 // send initialization commands
yahugh 0:c6ee363ac724 48 Write(0x34, 0x04); // enable active low data ready on DIO1
yahugh 0:c6ee363ac724 49 Write(0x38, 0x00); // Set the FIR filter to its widest possible bandwidth
yahugh 0:c6ee363ac724 50 }
yahugh 0:c6ee363ac724 51
yahugh 0:c6ee363ac724 52 // read the IMU accelerometer and gyro registers into the pingpong buffer
yahugh 0:c6ee363ac724 53 void ImuSpi::BurstRead(void) {
yahugh 0:c6ee363ac724 54 static int16_t imuRegs[] = {0x04,0x06,0x08,0x0A,0x0C,0x0E,0x3C};
yahugh 0:c6ee363ac724 55 int16_t* wp = GetBufferWritePtr();
yahugh 0:c6ee363ac724 56 Read(imuRegs[0]); // set up the first register read operation
yahugh 0:c6ee363ac724 57 wait(0.000024); // required to meet IMU t(datarate) spec
yahugh 0:c6ee363ac724 58
yahugh 0:c6ee363ac724 59 // send register read commands & read the registers
yahugh 0:c6ee363ac724 60 for (int i = 1; i <= 6; i++) {
yahugh 0:c6ee363ac724 61 *wp++ = Read(imuRegs[i]);
yahugh 0:c6ee363ac724 62 wait(0.000024);
yahugh 0:c6ee363ac724 63 }
yahugh 0:c6ee363ac724 64 *wp = Read(0); // read the last register output
yahugh 0:c6ee363ac724 65 }
yahugh 0:c6ee363ac724 66
yahugh 0:c6ee363ac724 67 int16_t ImuSpi::Read(char adr) {
yahugh 0:c6ee363ac724 68 int16_t cmd = 0x3f00 & (adr << 8);
yahugh 0:c6ee363ac724 69 int16_t response;
yahugh 0:c6ee363ac724 70 cs = 0;
yahugh 0:c6ee363ac724 71 response = spi.write(cmd);
yahugh 0:c6ee363ac724 72 cs = 1;
yahugh 0:c6ee363ac724 73 return response;
yahugh 0:c6ee363ac724 74 }
yahugh 0:c6ee363ac724 75
yahugh 0:c6ee363ac724 76
yahugh 0:c6ee363ac724 77 void ImuSpi::Write(char adr, char data) {
yahugh 0:c6ee363ac724 78 int16_t cmd = 0x8000 | (adr << 8) | data;
yahugh 0:c6ee363ac724 79 cs = 0;
yahugh 0:c6ee363ac724 80 spi.write(cmd);
yahugh 0:c6ee363ac724 81 cs = 1;
yahugh 0:c6ee363ac724 82 }
yahugh 0:c6ee363ac724 83
yahugh 0:c6ee363ac724 84 bool ImuSpi::IsDataReady(void) {
yahugh 0:c6ee363ac724 85 if (dataReady == true) {
yahugh 0:c6ee363ac724 86 dataReady = false;
yahugh 0:c6ee363ac724 87 return true;
yahugh 0:c6ee363ac724 88 } else {
yahugh 0:c6ee363ac724 89 return false;
yahugh 0:c6ee363ac724 90 }
yahugh 0:c6ee363ac724 91 }
yahugh 0:c6ee363ac724 92
yahugh 0:c6ee363ac724 93 void ImuSpi::DataReadyISR(void) {
yahugh 0:c6ee363ac724 94 diag = 1;
yahugh 0:c6ee363ac724 95 TogglePingpong();
yahugh 0:c6ee363ac724 96 dataReady = true;
yahugh 0:c6ee363ac724 97 diag = 0;
yahugh 0:c6ee363ac724 98 }