A library for the Invensense MPU9150
Revision 2:581fad93a809, committed 2014-06-09
- Comitter:
- ethanharstad
- Date:
- Mon Jun 09 21:17:40 2014 +0000
- Parent:
- 1:1b0ada1695a7
- Commit message:
- Untested implementation
Changed in this revision
diff -r 1b0ada1695a7 -r 581fad93a809 I2CHelper.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/I2CHelper.lib Mon Jun 09 21:17:40 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/ethanharstad/code/I2CHelper/#51de41e0e0c9
diff -r 1b0ada1695a7 -r 581fad93a809 MPU9150.cpp --- a/MPU9150.cpp Wed Jun 04 05:56:54 2014 +0000 +++ b/MPU9150.cpp Mon Jun 09 21:17:40 2014 +0000 @@ -1,8 +1,12 @@ +// Check methods that write bit fields #include "MPU9150.h" #include "mbed.h" MPU9150::MPU9150() : i2c_(I2C_SDA, I2C_SCL) { address_ = MPU9150_ADDRESS_DEFAULT; + + accelRange_ = 0; + gyroRange_ = 0; } MPU9150::MPU9150(const bool AD0) : i2c_(I2C_SDA, I2C_SCL) { @@ -11,6 +15,9 @@ } else { address_ = MPU9150_ADDRESS_AD0_LOW; } + + accelRange_ = 0; + gyroRange_ = 0; } MPU9150::MPU9150(const PinName sda, const PinName scl, const bool AD0) : i2c_(sda, scl) { @@ -19,4 +26,236 @@ } else { address_ = MPU9150_ADDRESS_AD0_LOW; } + + accelRange_ = 0; + gyroRange_ = 0; } + +void MPU9150::initialize() { + i2c_.setFrequency(100000); // Set frequency to 400 kHz + setClockSource(MPU9150_PWR_MGMT_CLOCK_GYRO_X); + setGyroFullScaleRange(MPU9150_GYRO_FS_250); + setAccelFullScaleRange(MPU9150_ACCEL_FS_2); + setSleepEnabled(false); +} + +bool MPU9150::test() { + return getDeviceID() == 0x34; +} + +uint8_t MPU9150::getSampleRateDivider() { + uint8_t buf = 0xFF; + i2c_.readByte(address_, MPU9150_RA_SMPRT_DIV, &buf); + return buf; +} +void MPU9150::setSampleRateDivider(const uint8_t divider) { + i2c_.writeByte(address_, MPU9150_RA_SMPRT_DIV, divider); +} + +uint8_t MPU9150::getExternalFrameSync() { + uint8_t buf = 0xFF; + i2c_.readBits(address_, MPU9150_RA_CONFIG, 5, 3, &buf); + return buf; +} +void MPU9150::setExternalFrameSync(const uint8_t sync) { + i2c_.writeBits(address_, MPU9150_RA_CONFIG, 5, 3, sync); +} +uint8_t MPU9150::getDLPFBandwidth() { + uint8_t buf = 0xFF; + i2c_.readBits(address_, MPU9150_RA_CONFIG, 2, 3, &buf); + return buf; +} +void MPU9150::setDLPFBandwidth(const uint8_t bandwidth) { + i2c_.writeBits(address_, MPU9150_RA_CONFIG, 2, 3, bandwidth); +} + +uint8_t MPU9150::getGyroFullScaleRange() { + uint8_t buf = 0xFF; + i2c_.readBits(address_, MPU9150_RA_GYRO_CONFIG, 4, 2, &buf); + return buf; +} +void MPU9150::setGyroFullScaleRange(const uint8_t range) { + i2c_.writeBits(address_, MPU9150_RA_GYRO_CONFIG, 4, 2, range); +} + +uint8_t MPU9150::getAccelFullScaleRange() { + uint8_t buf = 0xFF; + i2c_.readBits(address_, MPU9150_RA_ACCEL_CONFIG, 4, 2, &buf); + return buf; +} +void MPU9150::setAccelFullScaleRange(const uint8_t range) { + i2c_.writeBits(address_, MPU9150_RA_ACCEL_CONFIG, 4, 2, range); +} + +int16_t MPU9150::getAccelX() { + uint16_t buf = 0xFFFF; + i2c_.readWord(address_, MPU9150_RA_ACCEL_XOUT_H, &buf); + return (int16_t)buf; +} +int16_t MPU9150::getAccelY() { + uint16_t buf = 0xFFFF; + i2c_.readWord(address_, MPU9150_RA_ACCEL_YOUT_H, &buf); + return (int16_t)buf; +} +int16_t MPU9150::getAccelZ() { + uint16_t buf = 0xFFFF; + i2c_.readWord(address_, MPU9150_RA_ACCEL_ZOUT_H, &buf); + return (int16_t)buf; +} + +int16_t MPU9150::getTemp() { + uint16_t buf = 0xFFFF; + i2c_.readWord(address_, MPU9150_RA_TEMP_OUT_H, &buf); + return (int16_t)buf; +} + +int16_t MPU9150::getGyroX() { + uint16_t buf = 0xFFFF; + i2c_.readWord(address_, MPU9150_RA_GYRO_XOUT_H, &buf); + return (int16_t)buf; +} +int16_t MPU9150::getGyroY() { + uint16_t buf = 0xFFFF; + i2c_.readWord(address_, MPU9150_RA_GYRO_YOUT_H, &buf); + return (int16_t)buf; +} +int16_t MPU9150::getGyroZ() { + uint16_t buf = 0xFFFF; + i2c_.readWord(address_, MPU9150_RA_GYRO_ZOUT_H, &buf); + return (int16_t)buf; +} + +void MPU9150::resetGyroPath() { + i2c_.writeBit(address_, MPU9150_RA_SIGNAL_PATH_RESET, 2, 0x01); +} +void MPU9150::resetAccelPath() { + i2c_.writeBit(address_, MPU9150_RA_SIGNAL_PATH_RESET, 1, 0x01); +} +void MPU9150::resetTempPath() { + i2c_.writeBit(address_, MPU9150_RA_SIGNAL_PATH_RESET, 0, 0x01); +} + +bool MPU9150::getFifoEnabled() { + uint8_t buf = 0xFF; + i2c_.readBit(address_, MPU9150_RA_USER_CTRL, 6, &buf); + return (bool)buf; +} +void MPU9150::setFifoEnabled(const bool fifo) { + i2c_.writeBit(address_, MPU9150_RA_USER_CTRL, 6, fifo); +} +bool MPU9150::getI2CMasterEnabled() { + uint8_t buf = 0xFF; + i2c_.readBit(address_, MPU9150_RA_USER_CTRL, 5, &buf); + return (bool)buf; +} +void MPU9150::setI2CMasterEnabled(const bool master) { + i2c_.writeBit(address_, MPU9150_RA_USER_CTRL, 5, master); +} +void MPU9150::resetFifo() { + i2c_.writeBit(address_, MPU9150_RA_USER_CTRL, 2, 0x01); +} +void MPU9150::resetI2CMaster() { + i2c_.writeBit(address_, MPU9150_RA_USER_CTRL, 1, 0x01); +} +void MPU9150::resetSensors() { + i2c_.writeBit(address_, MPU9150_RA_USER_CTRL, 0, 0x01); +} + +void MPU9150::reset() { + i2c_.writeBit(address_, MPU9150_RA_PWR_MGMT_1, 7, 0x01); +} +bool MPU9150::getSleepEnabled() { + uint8_t buf = 0xFF; + i2c_.readBit(address_, MPU9150_RA_PWR_MGMT_1, 6, &buf); + return (bool)buf; +} +void MPU9150::setSleepEnabled(const bool sleep) { + i2c_.writeBit(address_, MPU9150_RA_PWR_MGMT_1, 6, sleep); +} +bool MPU9150::getCycleEnabled() { + uint8_t buf = 0xFF; + i2c_.readBit(address_, MPU9150_RA_PWR_MGMT_1, 5, &buf); + return (bool)buf; +} +void MPU9150::setCycleEnabled(const bool cycle) { + i2c_.writeBit(address_, MPU9150_RA_PWR_MGMT_1, 5, cycle); +} +bool MPU9150::getTempEnabled() { + uint8_t buf = 0xFF; + i2c_.readBit(address_, MPU9150_RA_PWR_MGMT_1, 3, &buf); + return (bool)buf; +} +void MPU9150::setTempEnabled(const bool temp) { + i2c_.writeBit(address_, MPU9150_RA_PWR_MGMT_1, 3, temp); +} +uint8_t MPU9150::getClockSource() { + uint8_t buf = 0xFF; + i2c_.readBits(address_, MPU9150_RA_PWR_MGMT_1, 2, 3, &buf); + return buf; +} +void MPU9150::setClockSource(const uint8_t source) { + i2c_.writeBits(address_, MPU9150_RA_PWR_MGMT_1, 2, 3, source); +} + +uint8_t MPU9150::getCycleFrequency() { + uint8_t buf = 0xFF; + i2c_.readBits(address_, MPU9150_RA_PWR_MGMT_2, 7, 2, &buf); + return buf; +} +void MPU9150::setCycleFrequency(const uint8_t frequency) { + i2c_.writeBits(address_, MPU9150_RA_PWR_MGMT_2, 7, 2, frequency); +} +bool MPU9150::getStandbyAccelXEnabled() { + uint8_t buf = 0xFF; + i2c_.readBit(address_, MPU9150_RA_PWR_MGMT_2, 5, &buf); + return (bool)buf; +} +void MPU9150::setStandbyAccelXEnabled(const bool enabled) { + i2c_.writeBit(address_, MPU9150_RA_PWR_MGMT_2, 5, enabled); +} +bool MPU9150::getStandbyAccelYEnabled() { + uint8_t buf = 0xFF; + i2c_.readBit(address_, MPU9150_RA_PWR_MGMT_2, 4, &buf); + return (bool)buf; +} +void MPU9150::setStandbyAccelYEnabled(const bool enabled) { + i2c_.writeBit(address_, MPU9150_RA_PWR_MGMT_2, 4, enabled); +} +bool MPU9150::getStandbyAccelZEnabled() { + uint8_t buf = 0xFF; + i2c_.readBit(address_, MPU9150_RA_PWR_MGMT_2, 3, &buf); + return (bool)buf; +} +void MPU9150::setStandbyAccelZEnabled(const bool enabled) { + i2c_.writeBit(address_, MPU9150_RA_PWR_MGMT_2, 3, enabled); +} +bool MPU9150::getStandbyGyroXEnabled() { + uint8_t buf = 0xFF; + i2c_.readBit(address_, MPU9150_RA_PWR_MGMT_2, 2, &buf); + return (bool)buf; +} +void MPU9150::setStandbyGyroXEnabled(const bool enabled) { + i2c_.writeBit(address_, MPU9150_RA_PWR_MGMT_2, 2, enabled); +} +bool MPU9150::getStandbyGyroYEnabled() { + uint8_t buf = 0xFF; + i2c_.readBit(address_, MPU9150_RA_PWR_MGMT_2, 1, &buf); + return (bool)buf; +} +void MPU9150::setStandbyGyroYEnabled(const bool enabled) { + i2c_.writeBit(address_, MPU9150_RA_PWR_MGMT_2, 1, enabled); +} +bool MPU9150::getStandbyGyroZEnabled() { + uint8_t buf = 0xFF; + i2c_.readBit(address_, MPU9150_RA_PWR_MGMT_2, 0, &buf); + return (bool)buf; +} +void MPU9150::setStandbyGyroZEnabled(const bool enabled) { + i2c_.writeBit(address_, MPU9150_RA_PWR_MGMT_2, 0, enabled); +} + +uint8_t MPU9150::getDeviceID() { + uint8_t buf = 0xFF; + i2c_.readBits(address_, MPU9150_RA_WHO_AM_I, 6, 6, &buf); + return buf; +}
diff -r 1b0ada1695a7 -r 581fad93a809 MPU9150.h --- a/MPU9150.h Wed Jun 04 05:56:54 2014 +0000 +++ b/MPU9150.h Mon Jun 09 21:17:40 2014 +0000 @@ -2,6 +2,7 @@ #define MPU9150_H_ #include "mbed.h" +#include "I2CHelper.h" #define MPU9150_ADDRESS_AD0_LOW 0x68 #define MPU9150_ADDRESS_AD0_HIGH 0x69 @@ -11,7 +12,7 @@ #define MPU9150_RA_SELF_TEST_Y 0x0E #define MPU9150_RA_SELF_TEST_Z 0x0F #define MPU9150_RA_SELF_TEST_A 0x10 -#define MPU9150_RA_SMPLRT_DIV 0x19 +#define MPU9150_RA_SMPRT_DIV 0x19 #define MPU9150_RA_CONFIG 0x1A #define MPU9150_RA_GYRO_CONFIG 0x1B #define MPU9150_RA_ACCEL_CONFIG 0x1C @@ -90,7 +91,7 @@ #define MPU9150_RA_FIFO_R_W 0x74 #define MPU9150_RA_WHO_AM_I 0x75 -// CONFIG - EXT_SYNC_SET +// CONFIG Register #define MPU9150_EXT_SYNC_DISABLED 0x00 #define MPU9150_EXT_SYNC_TEMP_OUT_L 0x01 #define MPU9150_EXT_SYNC_GYRO_XOUT_L 0x02 @@ -99,8 +100,6 @@ #define MPU9150_EXT_SYNC_ACCEL_XOUT_L 0x05 #define MPU9150_EXT_SYNC_ACCEL_YOUT_L 0x06 #define MPU9150_EXT_SYNC_ACCEL_ZOUT_L 0x07 - -// CONFIG - DLPF_CFG #define MPU9150_DLPF_BW_256 0x00 #define MPU9150_DLPF_BW_188 0x01 #define MPU9150_DLPF_BW_98 0x02 @@ -109,18 +108,55 @@ #define MPU9150_DLPF_BW_10 0x05 #define MPU9150_DLPF_BW_5 0x06 -// GYRO_CONFIG +// GYRO_CONFIG Register #define MPU9150_GYRO_FS_250 0x00 #define MPU9150_GYRO_FS_500 0x01 #define MPU9150_GYRO_FS_1000 0x02 #define MPU9150_GYRO_FS_2000 0x03 -// ACCEL_CONFIG +// ACCEL_CONFIG Register #define MPU9150_ACCEL_FS_2 0x00 #define MPU9150_ACCEL_FS_4 0x01 #define MPU9150_ACCEL_FS_8 0x02 #define MPU9150_ACCEL_FS_16 0x03 +// SIGNAL_PATH_RESET Register +#define MPU9150_SIGNAL_PATH_GYRO_RESET 0x04 +#define MPU9150_SIGNAL_PATH_ACCEL_RESET 0x02 +#define MPU9150_SIGNAL_PATH_TEMP_RESET 0x01 + +// USER_CTRL Register +#define MPU9150_USER_CTRL_FIFO_EN 0x40 +#define MPU9150_USER_CTRL_I2C_MST_EN 0x20 +#define MPU9150_USER_CTRL_FIFO_RESET 0x04 +#define MPU9150_USER_CTRL_I2C_MST_RESET 0x02 +#define MPU9150_USER_CTRL_SIG_RESET 0x01 + +// PWR_MGMT_1 Register +#define MPU9150_PWR_MGMT_DEVICE_RESET 0x80 +#define MPU9150_PWR_MGMT_SLEEP 0x40 +#define MPU9150_PWR_MGMT_CYCLE 0x20 +#define MPU9150_PWR_MGMT_TEMP_DISABLE 0x08 +#define MPU9150_PWR_MGMT_CLOCK_INTERNAL 0x00 +#define MPU9150_PWR_MGMT_CLOCK_GYRO_X 0x01 +#define MPU9150_PWR_MGMT_CLOCK_GYRO_Y 0x02 +#define MPU9150_PWR_MGMT_CLOCK_GYRO_Z 0x03 +#define MPU9150_PWR_MGMT_CLOCK_EXT_32k 0x04 +#define MPU9150_PWR_MGMT_CLOCK_EXT_19M 0x05 +#define MPU9150_PWR_MGMT_CLOCK_DISABLE 0x07 + +// PWR_MGMT_2 Register +#define MPU9150_PWR_MGMT_STBY_GYRO_Z 0x01 +#define MPU9150_PWR_MGMT_STBY_GYRO_Y 0x02 +#define MPU9150_PWR_MGMT_STBY_GYRO_X 0x04 +#define MPU9150_PWR_MGMT_STBY_ACCEL_Z 0x08 +#define MPU9150_PWR_MGMT_STBY_ACCEL_Y 0x10 +#define MPU9150_PWR_MGMT_STBY_ACCEL_X 0x20 +#define MPU9150_PWR_MGMT_CYCLE_1_25HZ 0x00 +#define MPU9150_PWR_MGMT_CYCLE_5HZ 0x01 +#define MPU9150_PWR_MGMT_CYCLE_20HZ 0x02 +#define MPU9150_PWR_MGMT_CYCLE_40HZ 0x03 + class MPU9150 { public: MPU9150(); @@ -138,7 +174,7 @@ uint8_t getExternalFrameSync(); void setExternalFrameSync(const uint8_t sync); uint8_t getDLPFBandwidth(); - void setDLFPBandwidth(const uint8_t bandwidth); + void setDLPFBandwidth(const uint8_t bandwidth); // GYRO_CONFIG Register uint8_t getGyroFullScaleRange(); @@ -206,11 +242,10 @@ uint8_t getDeviceID(); private: - uint8_t readByte(const uint8_t deviceAddress, const uint8_t registerAddress, uint8_t *data); - bool writeByte(const uint8_t deviceAddress, const uint8_t registerAddress, const uint8_t *data); - - I2C i2c_; - uint8_t address_; + I2CHelper i2c_; + uint8_t address_; + uint8_t accelRange_; + uint8_t gyroRange_; }; #endif \ No newline at end of file