Pin usage: MOSI(D4), MISO(D5), SCK(D3), CS(D6), format(8, 3) Test OK.
Fork of LSM6DS3 by
LSM6DS3.h@5:3bc3507470bd, 2016-02-27 (annotated)
- Committer:
- YCTung
- Date:
- Sat Feb 27 07:41:02 2016 +0000
- Revision:
- 5:3bc3507470bd
- Parent:
- 2:c19d384b2896
Change line 133 in LSM6DS3.h to SPI_MODE, pin D6
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
adam_z | 0:301b2cb34ead | 1 | #ifndef __LSM6DS3IMU_H__ |
adam_z | 0:301b2cb34ead | 2 | #define __LSM6DS3IMU_H__ |
adam_z | 0:301b2cb34ead | 3 | |
adam_z | 0:301b2cb34ead | 4 | |
adam_z | 0:301b2cb34ead | 5 | #include "mbed.h" |
adam_z | 0:301b2cb34ead | 6 | #include "stdint.h" |
adam_z | 0:301b2cb34ead | 7 | #include "LSM6DS3_Types.h" |
adam_z | 0:301b2cb34ead | 8 | #include "LSM6DS3_Registers.h" |
adam_z | 0:301b2cb34ead | 9 | |
adam_z | 0:301b2cb34ead | 10 | #include "math.h" |
adam_z | 0:301b2cb34ead | 11 | |
adam_z | 0:301b2cb34ead | 12 | #define I2C_MODE 0 |
adam_z | 0:301b2cb34ead | 13 | #define SPI_MODE 1 |
adam_z | 0:301b2cb34ead | 14 | |
adam_z | 0:301b2cb34ead | 15 | // Return values |
adam_z | 0:301b2cb34ead | 16 | typedef enum |
adam_z | 0:301b2cb34ead | 17 | { |
adam_z | 0:301b2cb34ead | 18 | IMU_SUCCESS, |
adam_z | 0:301b2cb34ead | 19 | IMU_HW_ERROR, |
adam_z | 0:301b2cb34ead | 20 | IMU_NOT_SUPPORTED, |
adam_z | 0:301b2cb34ead | 21 | IMU_GENERIC_ERROR, |
adam_z | 0:301b2cb34ead | 22 | IMU_OUT_OF_BOUNDS, |
adam_z | 0:301b2cb34ead | 23 | IMU_ALL_ONES_WARNING, |
adam_z | 0:301b2cb34ead | 24 | //... |
adam_z | 0:301b2cb34ead | 25 | } status_t; |
adam_z | 0:301b2cb34ead | 26 | |
adam_z | 0:301b2cb34ead | 27 | //This is the core operational class of the driver. |
adam_z | 0:301b2cb34ead | 28 | // LSM6DS3Core contains only read and write operations towards the IMU. |
adam_z | 0:301b2cb34ead | 29 | // To use the higher level functions, use the class LSM6DS3 which inherits |
adam_z | 0:301b2cb34ead | 30 | // this class. |
adam_z | 0:301b2cb34ead | 31 | //========================self define================= |
adam_z | 0:301b2cb34ead | 32 | |
adam_z | 0:301b2cb34ead | 33 | //==================================================== |
adam_z | 0:301b2cb34ead | 34 | |
adam_z | 0:301b2cb34ead | 35 | class LSM6DS3Core |
adam_z | 0:301b2cb34ead | 36 | { |
adam_z | 0:301b2cb34ead | 37 | public: |
adam_z | 0:301b2cb34ead | 38 | LSM6DS3Core( uint8_t ); |
adam_z | 0:301b2cb34ead | 39 | LSM6DS3Core( uint8_t, uint8_t ); |
adam_z | 0:301b2cb34ead | 40 | // ~LSM6DS3Core() = default; |
adam_z | 0:301b2cb34ead | 41 | |
adam_z | 0:301b2cb34ead | 42 | status_t beginCore( void ); |
adam_z | 0:301b2cb34ead | 43 | |
adam_z | 0:301b2cb34ead | 44 | //The following utilities read and write to the IMU |
adam_z | 0:301b2cb34ead | 45 | |
adam_z | 0:301b2cb34ead | 46 | //ReadRegisterRegion takes a uint8 array address as input and reads |
adam_z | 0:301b2cb34ead | 47 | // a chunk of memory into that array. |
adam_z | 0:301b2cb34ead | 48 | status_t readRegisterRegion(uint8_t*, uint8_t, uint8_t ); |
adam_z | 0:301b2cb34ead | 49 | |
adam_z | 0:301b2cb34ead | 50 | //readRegister reads one 8-bit register |
adam_z | 0:301b2cb34ead | 51 | status_t readRegister(uint8_t*, uint8_t); |
adam_z | 0:301b2cb34ead | 52 | |
adam_z | 0:301b2cb34ead | 53 | //Reads two 8-bit regs, LSByte then MSByte order, and concatenates them. |
adam_z | 0:301b2cb34ead | 54 | // Acts as a 16-bit read operation |
adam_z | 0:301b2cb34ead | 55 | status_t readRegisterInt16(int16_t*, uint8_t offset ); |
adam_z | 0:301b2cb34ead | 56 | |
adam_z | 0:301b2cb34ead | 57 | //Writes an 8-bit byte; |
adam_z | 0:301b2cb34ead | 58 | status_t writeRegister(uint8_t, uint8_t); |
adam_z | 0:301b2cb34ead | 59 | |
adam_z | 0:301b2cb34ead | 60 | //Change to embedded page |
adam_z | 0:301b2cb34ead | 61 | status_t embeddedPage( void ); |
adam_z | 0:301b2cb34ead | 62 | |
adam_z | 0:301b2cb34ead | 63 | //Change to base page |
adam_z | 0:301b2cb34ead | 64 | status_t basePage( void ); |
adam_z | 2:c19d384b2896 | 65 | SPI spi_; |
adam_z | 2:c19d384b2896 | 66 | I2C i2c_; |
adam_z | 2:c19d384b2896 | 67 | DigitalOut cs_; |
adam_z | 1:cc2caaf5536c | 68 | |
adam_z | 0:301b2cb34ead | 69 | |
adam_z | 0:301b2cb34ead | 70 | private: |
adam_z | 0:301b2cb34ead | 71 | |
adam_z | 0:301b2cb34ead | 72 | //Communication stuff |
adam_z | 0:301b2cb34ead | 73 | uint8_t commInterface; |
adam_z | 0:301b2cb34ead | 74 | uint8_t I2CAddress; |
adam_z | 0:301b2cb34ead | 75 | uint8_t chipSelectPin; |
adam_z | 0:301b2cb34ead | 76 | |
adam_z | 0:301b2cb34ead | 77 | }; |
adam_z | 0:301b2cb34ead | 78 | |
adam_z | 0:301b2cb34ead | 79 | //This struct holds the settings the driver uses to do calculations |
adam_z | 0:301b2cb34ead | 80 | struct SensorSettings { |
adam_z | 0:301b2cb34ead | 81 | public: |
adam_z | 0:301b2cb34ead | 82 | //Gyro settings |
adam_z | 0:301b2cb34ead | 83 | uint8_t gyroEnabled; |
adam_z | 0:301b2cb34ead | 84 | uint16_t gyroRange; |
adam_z | 0:301b2cb34ead | 85 | uint16_t gyroSampleRate; |
adam_z | 0:301b2cb34ead | 86 | uint16_t gyroBandWidth; |
adam_z | 0:301b2cb34ead | 87 | |
adam_z | 0:301b2cb34ead | 88 | uint8_t gyroFifoEnabled; |
adam_z | 0:301b2cb34ead | 89 | uint8_t gyroFifoDecimation; |
adam_z | 0:301b2cb34ead | 90 | |
adam_z | 0:301b2cb34ead | 91 | //Accelerometer settings |
adam_z | 0:301b2cb34ead | 92 | uint8_t accelEnabled; |
adam_z | 0:301b2cb34ead | 93 | uint8_t accelODROff; |
adam_z | 0:301b2cb34ead | 94 | uint16_t accelRange; |
adam_z | 0:301b2cb34ead | 95 | uint16_t accelSampleRate; |
adam_z | 0:301b2cb34ead | 96 | uint16_t accelBandWidth; |
adam_z | 0:301b2cb34ead | 97 | |
adam_z | 0:301b2cb34ead | 98 | uint8_t accelFifoEnabled; |
adam_z | 0:301b2cb34ead | 99 | uint8_t accelFifoDecimation; |
adam_z | 0:301b2cb34ead | 100 | |
adam_z | 0:301b2cb34ead | 101 | //Temperature settings |
adam_z | 0:301b2cb34ead | 102 | uint8_t tempEnabled; |
adam_z | 0:301b2cb34ead | 103 | |
adam_z | 0:301b2cb34ead | 104 | //Non-basic mode settings |
adam_z | 0:301b2cb34ead | 105 | uint8_t commMode; |
adam_z | 0:301b2cb34ead | 106 | |
adam_z | 0:301b2cb34ead | 107 | //FIFO control data |
adam_z | 0:301b2cb34ead | 108 | uint16_t fifoThreshold; |
adam_z | 0:301b2cb34ead | 109 | int16_t fifoSampleRate; |
adam_z | 0:301b2cb34ead | 110 | uint8_t fifoModeWord; |
adam_z | 0:301b2cb34ead | 111 | |
adam_z | 0:301b2cb34ead | 112 | }; |
adam_z | 0:301b2cb34ead | 113 | |
adam_z | 0:301b2cb34ead | 114 | |
adam_z | 0:301b2cb34ead | 115 | //This is the highest level class of the driver. |
adam_z | 0:301b2cb34ead | 116 | // |
adam_z | 0:301b2cb34ead | 117 | // class LSM6DS3 inherits the core and makes use of the beginCore() |
adam_z | 0:301b2cb34ead | 118 | //method through it's own begin() method. It also contains the |
adam_z | 0:301b2cb34ead | 119 | //settings struct to hold user settings. |
adam_z | 0:301b2cb34ead | 120 | |
adam_z | 0:301b2cb34ead | 121 | class LSM6DS3 : public LSM6DS3Core |
adam_z | 0:301b2cb34ead | 122 | { |
adam_z | 0:301b2cb34ead | 123 | public: |
adam_z | 0:301b2cb34ead | 124 | //IMU settings |
adam_z | 0:301b2cb34ead | 125 | SensorSettings settings; |
adam_z | 0:301b2cb34ead | 126 | |
adam_z | 0:301b2cb34ead | 127 | //Error checking |
adam_z | 0:301b2cb34ead | 128 | uint16_t allOnesCounter; |
adam_z | 0:301b2cb34ead | 129 | uint16_t nonSuccessCounter; |
adam_z | 0:301b2cb34ead | 130 | |
adam_z | 0:301b2cb34ead | 131 | //Constructor generates default SensorSettings. |
adam_z | 0:301b2cb34ead | 132 | //(over-ride after construction if desired) |
YCTung | 5:3bc3507470bd | 133 | LSM6DS3( uint8_t busType = SPI_MODE, uint8_t inputArg = D6 ); |
adam_z | 0:301b2cb34ead | 134 | // ~LSM6DS3() = default; |
adam_z | 0:301b2cb34ead | 135 | |
adam_z | 0:301b2cb34ead | 136 | //Call to apply SensorSettings |
adam_z | 0:301b2cb34ead | 137 | status_t begin(void); |
adam_z | 0:301b2cb34ead | 138 | |
adam_z | 0:301b2cb34ead | 139 | //Returns the raw bits from the sensor cast as 16-bit signed integers |
adam_z | 0:301b2cb34ead | 140 | int16_t readRawAccelX( void ); |
adam_z | 0:301b2cb34ead | 141 | int16_t readRawAccelY( void ); |
adam_z | 0:301b2cb34ead | 142 | int16_t readRawAccelZ( void ); |
adam_z | 0:301b2cb34ead | 143 | int16_t readRawGyroX( void ); |
adam_z | 0:301b2cb34ead | 144 | int16_t readRawGyroY( void ); |
adam_z | 0:301b2cb34ead | 145 | int16_t readRawGyroZ( void ); |
adam_z | 0:301b2cb34ead | 146 | |
adam_z | 0:301b2cb34ead | 147 | int16_t accelOffset[3]; |
adam_z | 0:301b2cb34ead | 148 | int16_t gyroOffset[3]; |
adam_z | 0:301b2cb34ead | 149 | void setOffset(int16_t, int16_t, int16_t, int16_t, int16_t, int16_t); |
adam_z | 0:301b2cb34ead | 150 | |
adam_z | 0:301b2cb34ead | 151 | |
adam_z | 0:301b2cb34ead | 152 | |
adam_z | 0:301b2cb34ead | 153 | //Returns the values as floats. Inside, this calls readRaw___(); |
adam_z | 0:301b2cb34ead | 154 | float readFloatAccelX( void ); |
adam_z | 0:301b2cb34ead | 155 | float readFloatAccelY( void ); |
adam_z | 0:301b2cb34ead | 156 | float readFloatAccelZ( void ); |
adam_z | 0:301b2cb34ead | 157 | float readFloatGyroX( void ); |
adam_z | 0:301b2cb34ead | 158 | float readFloatGyroY( void ); |
adam_z | 0:301b2cb34ead | 159 | float readFloatGyroZ( void ); |
adam_z | 0:301b2cb34ead | 160 | |
adam_z | 0:301b2cb34ead | 161 | //Temperature related methods |
adam_z | 0:301b2cb34ead | 162 | int16_t readRawTemp( void ); |
adam_z | 0:301b2cb34ead | 163 | float readTempC( void ); |
adam_z | 0:301b2cb34ead | 164 | float readTempF( void ); |
adam_z | 0:301b2cb34ead | 165 | |
adam_z | 0:301b2cb34ead | 166 | //FIFO stuff |
adam_z | 0:301b2cb34ead | 167 | void fifoBegin( void ); |
adam_z | 0:301b2cb34ead | 168 | void fifoClear( void ); |
adam_z | 0:301b2cb34ead | 169 | int16_t fifoRead( void ); |
adam_z | 0:301b2cb34ead | 170 | uint16_t fifoGetStatus( void ); |
adam_z | 0:301b2cb34ead | 171 | void fifoEnd( void ); |
adam_z | 0:301b2cb34ead | 172 | |
adam_z | 0:301b2cb34ead | 173 | float calcGyro( int32_t ); |
adam_z | 0:301b2cb34ead | 174 | float calcAccel( int32_t ); |
adam_z | 0:301b2cb34ead | 175 | |
adam_z | 0:301b2cb34ead | 176 | private: |
adam_z | 0:301b2cb34ead | 177 | |
adam_z | 0:301b2cb34ead | 178 | }; |
adam_z | 0:301b2cb34ead | 179 | |
adam_z | 0:301b2cb34ead | 180 | #endif |