Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
imu-spi.cpp
00001 // 00002 // imu-spi.cpp 00003 // 00004 // copyright 2010 Hugh Shane 00005 // 00006 #include "mbed.h" 00007 #include "imu-spi.h" 00008 00009 ImuSpi::ImuSpi(void) : 00010 spi(p5, p6, p7), // mosi, miso, sclk 00011 cs(p8), // IMU chip select 00012 reset(p8), // IMU reset. CAUTION! This also resets the altimeter. 00013 diag(p21), // diagnostic output 00014 imuDataReady(p11) // interrupt on IMU data-ready 00015 { 00016 // Setup the spi for 16 bit data, high steady state clock, 00017 // second edge capture, with a 1 MHz clock rate 00018 spi.format(16,3); 00019 spi.frequency(1000000); 00020 00021 // init the pingpong buffer 00022 pingpong = 0; 00023 00024 // init the interrupt semaphore 00025 dataReady = false; 00026 00027 // interrupt on the falling edge of the IMU data-ready signal 00028 diag = 0; 00029 imuDataReady.fall(this, &ImuSpi::DataReadyISR); 00030 00031 // init the IMU 00032 InitImu(); 00033 00034 } 00035 00036 void ImuSpi::reinitInterrupts(void) { 00037 imuDataReady.fall(this, &ImuSpi::DataReadyISR); 00038 } 00039 00040 // initialize the IMU 00041 void ImuSpi::InitImu(void) { 00042 // deselect the IMU 00043 cs = 1; 00044 reset = 1; 00045 // perform a hard reset 00046 reset = 0; wait(.1); reset = 1; 00047 // send initialization commands 00048 Write(0x34, 0x04); // enable active low data ready on DIO1 00049 Write(0x38, 0x00); // Set the FIR filter to its widest possible bandwidth 00050 } 00051 00052 // read the IMU accelerometer and gyro registers into the pingpong buffer 00053 void ImuSpi::BurstRead(void) { 00054 static int16_t imuRegs[] = {0x04,0x06,0x08,0x0A,0x0C,0x0E,0x3C}; 00055 int16_t* wp = GetBufferWritePtr(); 00056 Read(imuRegs[0]); // set up the first register read operation 00057 wait(0.000024); // required to meet IMU t(datarate) spec 00058 00059 // send register read commands & read the registers 00060 for (int i = 1; i <= 6; i++) { 00061 *wp++ = Read(imuRegs[i]); 00062 wait(0.000024); 00063 } 00064 *wp = Read(0); // read the last register output 00065 } 00066 00067 int16_t ImuSpi::Read(char adr) { 00068 int16_t cmd = 0x3f00 & (adr << 8); 00069 int16_t response; 00070 cs = 0; 00071 response = spi.write(cmd); 00072 cs = 1; 00073 return response; 00074 } 00075 00076 00077 void ImuSpi::Write(char adr, char data) { 00078 int16_t cmd = 0x8000 | (adr << 8) | data; 00079 cs = 0; 00080 spi.write(cmd); 00081 cs = 1; 00082 } 00083 00084 bool ImuSpi::IsDataReady(void) { 00085 if (dataReady == true) { 00086 dataReady = false; 00087 return true; 00088 } else { 00089 return false; 00090 } 00091 } 00092 00093 void ImuSpi::DataReadyISR(void) { 00094 diag = 1; 00095 TogglePingpong(); 00096 dataReady = true; 00097 diag = 0; 00098 }
Generated on Sat Jul 16 2022 14:16:22 by
1.7.2