this version has all of Jim's fixes for reading the GPS and IMU data synchronously

Dependencies:   MODSERIAL SDFileSystem mbed SDShell CRC CommHandler FP LinkedList LogUtil

Revision:
0:432b860b6ff7
Child:
6:71da5b99de97
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ADIS16488.h	Mon Apr 22 21:26:04 2013 +0000
@@ -0,0 +1,122 @@
+
+
+
+//set up the SPI on pins 5, 6, 7 to read from the ADIS16488
+SPI spi(p5, p6, p7); // mosi (DIN), miso (DOUT), sclk (CLK)
+DigitalOut ADIS_CS(p8);     //Chip Select for the ADIS SPI
+InterruptIn ADIS_DR(p28);   //DataReady interrupt connected to DIO2 for ADIS
+DigitalOut ADIS_RST(p20);   //ADIS reset pin
+
+bool IMUDataReady = false;
+int IMURecordCounter = 0;
+//see Table 9 from page 11 of the ADIS16488 spec 
+//see fig 15 of spec -- note the low byte of the regsiter word is always zero
+// X_DELTANG_LOW, Y_DELTANG_LOW, X_DETANG_LOW, X_DELTVEL_LOW, Y_DELTVEL_LOW, Z_DELTVEL_LOW
+unsigned short LOW_REGISTER[]  = {0x4000, 0x4400, 0x4800, 0x4C00, 0x5000, 0x5400};   
+// X_DELTANG_HIGH, Y_DELTANG_HIGH, X_DETANG_HIGH, X_DELTVEL_HIGH, Y_DELTVEL_HIGH, Z_DELTVEL_HIGH
+unsigned short HIGH_REGISTER[] = {0x4200, 0x4600, 0x4A00, 0x4E00, 0x5200, 0x5600}; 
+
+volatile unsigned long IMUtimeFrom1PPS = 0;
+volatile int IMUClockCounter = 0;            //counter for IMU samples per sec
+
+union WD { long dataWord; unsigned short pt[2];} wd;
+
+//IMU records are buffered in the IMUDataReady ISR
+const unsigned char IMUrecArraySize = 15;
+
+#pragma pack(1)
+struct IMUREC
+{
+    unsigned long synch;
+    unsigned short msgID;
+    unsigned long GPSTime;
+    long dataWord[6];
+    //  4 + 2 + 4 + 24 = 34
+};
+
+IMUREC imuPing[IMUrecArraySize];
+IMUREC imuPong[IMUrecArraySize];
+IMUREC tempRec;
+volatile bool fillingPingWritingPong = true;
+
+
+unsigned long maxDelIMUmsecs = 0;
+unsigned long delIMUmsecs = 0;      
+unsigned long lastIMUmsecs = 0; 
+
+void IMUDataReadyISR(void)
+{   
+    IMUtimeFrom1PPS = timeFromPPS.read_us();        
+    tempRec.GPSTime = GPSTimemsecs + PPSTimeOffset*1000 + IMUtimeFrom1PPS/1000.0;
+    
+    if (IMUClockCounter == IMUrecArraySize ) 
+    {
+        IMUDataReady = true;
+        fillingPingWritingPong = !fillingPingWritingPong;
+        IMUClockCounter = 0;
+    }
+  
+    spi.write((int) HIGH_REGISTER[0]); //next read will return results from HIGH_REGITER[0]
+    for (int i=0; i<6; i++)  //read the 6 rate and accel variables
+    {
+        wd.pt[1] = (unsigned short)spi.write((int) LOW_REGISTER[i]); 
+        if (i<5)  // dont this on the last because this was pre-called
+        {   wd.pt[0] = (unsigned short)spi.write((int) HIGH_REGISTER[i+1]); }
+        
+        if ( fillingPingWritingPong) tempRec.dataWord[i] = wd.dataWord; //data word is a signed long
+        else                         tempRec.dataWord[i] = wd.dataWord; //data word is a signed long
+    }           
+            
+    if (fillingPingWritingPong)  imuPing[IMUClockCounter] = tempRec;
+    else                         imuPong[IMUClockCounter] = tempRec;
+    
+    IMUClockCounter++;
+    
+    return;
+} 
+
+void setupADIS(void)
+{
+    ADIS_DR.mode(PullDown);
+    ADIS_RST = 0;
+    
+    //  set the IMU dataReady ISR
+    ADIS_DR.rise(&IMUDataReadyISR);
+    
+    // Setup the mbed SPI for 16 bit data, high steady state clock,
+    // second edge capture, with a 1MHz clock rate
+    spi.format(16,3);
+    spi.frequency(5000000);   
+    
+    ADIS_CS = 1;  //CS must be set high before it goes low cause the enable is the transition
+    ADIS_RST = 1;
+    wait(0.5);
+    ADIS_CS = 0; //set the Chip select low to enable the IMU SPI access
+    
+    spi.write((int)0x8003);  //change to page 3
+    
+    //change the DECRATE to 98.4 Hz (this is also in page 3)
+    //the 8 sets the high bit to 1 indicating a write to a register
+    // The C abd D designate the registers for the DECRATE of Page 3
+    // The 0x17 sets the rate to:  2460/(23+1) = 102.5Hz
+    // The 0x18 sets the rate to:  2460/(24+1) =  98.4Hz
+    spi.write((int)0x8C17);    //write high byte  (only page number can be written in a single byte)
+    spi.write((int)0x8D00);    //write the low byte of DECRATE 
+    
+    //to set the GPS VARF clock as the input synch clock for the IMU
+    //the high byte is CD indicating the synch is enabled on the rising edge of the input clock  
+    //spi.write((int)0x86CD);    //write high byte to register 0x06
+    //spi.write((int)0x8700);    //write the low byte of 00 to registed 0x07
+    
+    //change the page to 0 to get the data
+    spi.write((int)0x8000);  //change to page 0
+    
+    toPC.printf(" setting the default values\n");
+    
+    //set the IMU synch and message ID
+    tempRec.synch = 0x1C1244AA;  //same as the GPS synch words
+    tempRec.msgID = 111;  //IMU record ID
+    
+    toPC.printf(" finished setting the default values\n");
+
+}