Chris LU / LSM9DS0_self_riding_bike

Fork of LSM9DS0 by Chris LU

Committer:
benson516
Date:
Wed Dec 28 16:44:09 2016 +0000
Revision:
10:60a176bd72b3
Parent:
3:bc0db184f092
Add a set of functions for vector-output capability.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
YCTung 0:0dbf7ee73651 1 //Original author
YCTung 0:0dbf7ee73651 2 /******************************************************************************
YCTung 0:0dbf7ee73651 3 SFE_LSM9DS0.h
YCTung 0:0dbf7ee73651 4 SFE_LSM9DS0 Library Header File
YCTung 0:0dbf7ee73651 5 Jim Lindblom @ SparkFun Electronics
YCTung 0:0dbf7ee73651 6 Original Creation Date: February 14, 2014 (Happy Valentines Day!)
YCTung 0:0dbf7ee73651 7 https://github.com/sparkfun/LSM9DS0_Breakout
YCTung 0:0dbf7ee73651 8 This file prototypes the LSM9DS0 class, implemented in SFE_LSM9DS0.cpp. In
YCTung 0:0dbf7ee73651 9 addition, it defines every register in the LSM9DS0 (both the Gyro and Accel/
YCTung 0:0dbf7ee73651 10 Magnetometer registers).
YCTung 0:0dbf7ee73651 11 Development environment specifics:
YCTung 0:0dbf7ee73651 12 IDE: Arduino 1.0.5
YCTung 0:0dbf7ee73651 13 Hardware Platform: Arduino Pro 3.3V/8MHz
YCTung 0:0dbf7ee73651 14 LSM9DS0 Breakout Version: 1.0
YCTung 0:0dbf7ee73651 15 This code is beerware; if you see me (or any other SparkFun employee) at the
YCTung 0:0dbf7ee73651 16 local, and you've found our code helpful, please buy us a round!
YCTung 0:0dbf7ee73651 17 Distributed as-is; no warranty is given.
YCTung 0:0dbf7ee73651 18 ******************************************************************************/
YCTung 0:0dbf7ee73651 19 #ifndef __SFE_LSM9DS0_H__
YCTung 0:0dbf7ee73651 20 #define __SFE_LSM9DS0_H__
YCTung 0:0dbf7ee73651 21
YCTung 0:0dbf7ee73651 22 #include "mbed.h"
benson516 10:60a176bd72b3 23 #include <vector>
benson516 10:60a176bd72b3 24 using std::vector;
YCTung 0:0dbf7ee73651 25
adam_z 2:48eb33afd0fa 26
adam_z 3:bc0db184f092 27 #define PI 3.14159
YCTung 0:0dbf7ee73651 28 ////////////////////////////
YCTung 0:0dbf7ee73651 29 // LSM9DS0 Gyro Registers //
YCTung 0:0dbf7ee73651 30 ////////////////////////////
YCTung 0:0dbf7ee73651 31 #define WHO_AM_I_G 0x0F
YCTung 0:0dbf7ee73651 32 #define CTRL_REG1_G 0x20
YCTung 0:0dbf7ee73651 33 #define CTRL_REG2_G 0x21
YCTung 0:0dbf7ee73651 34 #define CTRL_REG3_G 0x22
YCTung 0:0dbf7ee73651 35 #define CTRL_REG4_G 0x23
YCTung 0:0dbf7ee73651 36 #define CTRL_REG5_G 0x24
YCTung 0:0dbf7ee73651 37 #define REFERENCE_G 0x25
YCTung 0:0dbf7ee73651 38 #define STATUS_REG_G 0x27
YCTung 0:0dbf7ee73651 39 #define OUT_X_L_G 0x28
YCTung 0:0dbf7ee73651 40 #define OUT_X_H_G 0x29
YCTung 0:0dbf7ee73651 41 #define OUT_Y_L_G 0x2A
YCTung 0:0dbf7ee73651 42 #define OUT_Y_H_G 0x2B
YCTung 0:0dbf7ee73651 43 #define OUT_Z_L_G 0x2C
YCTung 0:0dbf7ee73651 44 #define OUT_Z_H_G 0x2D
YCTung 0:0dbf7ee73651 45 #define FIFO_CTRL_REG_G 0x2E
YCTung 0:0dbf7ee73651 46 #define FIFO_SRC_REG_G 0x2F
YCTung 0:0dbf7ee73651 47 #define INT1_CFG_G 0x30
YCTung 0:0dbf7ee73651 48 #define INT1_SRC_G 0x31
YCTung 0:0dbf7ee73651 49 #define INT1_THS_XH_G 0x32
YCTung 0:0dbf7ee73651 50 #define INT1_THS_XL_G 0x33
YCTung 0:0dbf7ee73651 51 #define INT1_THS_YH_G 0x34
YCTung 0:0dbf7ee73651 52 #define INT1_THS_YL_G 0x35
YCTung 0:0dbf7ee73651 53 #define INT1_THS_ZH_G 0x36
YCTung 0:0dbf7ee73651 54 #define INT1_THS_ZL_G 0x37
YCTung 0:0dbf7ee73651 55 #define INT1_DURATION_G 0x38
YCTung 0:0dbf7ee73651 56
YCTung 0:0dbf7ee73651 57 //////////////////////////////////////////
YCTung 0:0dbf7ee73651 58 // LSM9DS0 Accel/Magneto (XM) Registers //
YCTung 0:0dbf7ee73651 59 //////////////////////////////////////////
YCTung 0:0dbf7ee73651 60 #define OUT_TEMP_L_XM 0x05
YCTung 0:0dbf7ee73651 61 #define OUT_TEMP_H_XM 0x06
YCTung 0:0dbf7ee73651 62 #define STATUS_REG_M 0x07
YCTung 0:0dbf7ee73651 63 #define OUT_X_L_M 0x08
YCTung 0:0dbf7ee73651 64 #define OUT_X_H_M 0x09
YCTung 0:0dbf7ee73651 65 #define OUT_Y_L_M 0x0A
YCTung 0:0dbf7ee73651 66 #define OUT_Y_H_M 0x0B
YCTung 0:0dbf7ee73651 67 #define OUT_Z_L_M 0x0C
YCTung 0:0dbf7ee73651 68 #define OUT_Z_H_M 0x0D
YCTung 0:0dbf7ee73651 69 #define WHO_AM_I_XM 0x0F
YCTung 0:0dbf7ee73651 70 #define INT_CTRL_REG_M 0x12
YCTung 0:0dbf7ee73651 71 #define INT_SRC_REG_M 0x13
YCTung 0:0dbf7ee73651 72 #define INT_THS_L_M 0x14
YCTung 0:0dbf7ee73651 73 #define INT_THS_H_M 0x15
YCTung 0:0dbf7ee73651 74 #define OFFSET_X_L_M 0x16
YCTung 0:0dbf7ee73651 75 #define OFFSET_X_H_M 0x17
YCTung 0:0dbf7ee73651 76 #define OFFSET_Y_L_M 0x18
YCTung 0:0dbf7ee73651 77 #define OFFSET_Y_H_M 0x19
YCTung 0:0dbf7ee73651 78 #define OFFSET_Z_L_M 0x1A
YCTung 0:0dbf7ee73651 79 #define OFFSET_Z_H_M 0x1B
YCTung 0:0dbf7ee73651 80 #define REFERENCE_X 0x1C
YCTung 0:0dbf7ee73651 81 #define REFERENCE_Y 0x1D
YCTung 0:0dbf7ee73651 82 #define REFERENCE_Z 0x1E
YCTung 0:0dbf7ee73651 83 #define CTRL_REG0_XM 0x1F
YCTung 0:0dbf7ee73651 84 #define CTRL_REG1_XM 0x20
YCTung 0:0dbf7ee73651 85 #define CTRL_REG2_XM 0x21
YCTung 0:0dbf7ee73651 86 #define CTRL_REG3_XM 0x22
YCTung 0:0dbf7ee73651 87 #define CTRL_REG4_XM 0x23
YCTung 0:0dbf7ee73651 88 #define CTRL_REG5_XM 0x24
YCTung 0:0dbf7ee73651 89 #define CTRL_REG6_XM 0x25
YCTung 0:0dbf7ee73651 90 #define CTRL_REG7_XM 0x26
YCTung 0:0dbf7ee73651 91 #define STATUS_REG_A 0x27
YCTung 0:0dbf7ee73651 92 #define OUT_X_L_A 0x28
YCTung 0:0dbf7ee73651 93 #define OUT_X_H_A 0x29
YCTung 0:0dbf7ee73651 94 #define OUT_Y_L_A 0x2A
YCTung 0:0dbf7ee73651 95 #define OUT_Y_H_A 0x2B
YCTung 0:0dbf7ee73651 96 #define OUT_Z_L_A 0x2C
YCTung 0:0dbf7ee73651 97 #define OUT_Z_H_A 0x2D
YCTung 0:0dbf7ee73651 98 #define FIFO_CTRL_REG 0x2E
YCTung 0:0dbf7ee73651 99 #define FIFO_SRC_REG 0x2F
YCTung 0:0dbf7ee73651 100 #define INT_GEN_1_REG 0x30
YCTung 0:0dbf7ee73651 101 #define INT_GEN_1_SRC 0x31
YCTung 0:0dbf7ee73651 102 #define INT_GEN_1_THS 0x32
YCTung 0:0dbf7ee73651 103 #define INT_GEN_1_DURATION 0x33
YCTung 0:0dbf7ee73651 104 #define INT_GEN_2_REG 0x34
YCTung 0:0dbf7ee73651 105 #define INT_GEN_2_SRC 0x35
YCTung 0:0dbf7ee73651 106 #define INT_GEN_2_THS 0x36
YCTung 0:0dbf7ee73651 107 #define INT_GEN_2_DURATION 0x37
YCTung 0:0dbf7ee73651 108 #define CLICK_CFG 0x38
YCTung 0:0dbf7ee73651 109 #define CLICK_SRC 0x39
YCTung 0:0dbf7ee73651 110 #define CLICK_THS 0x3A
YCTung 0:0dbf7ee73651 111 #define TIME_LIMIT 0x3B
YCTung 0:0dbf7ee73651 112 #define TIME_LATENCY 0x3C
YCTung 0:0dbf7ee73651 113 #define TIME_WINDOW 0x3D
YCTung 0:0dbf7ee73651 114 #define ACT_THS 0x3E
YCTung 0:0dbf7ee73651 115 #define ACT_DUR 0x3F
YCTung 0:0dbf7ee73651 116
YCTung 0:0dbf7ee73651 117 // The LSM9DS0 functions over both I2C or SPI. This library supports both.
YCTung 0:0dbf7ee73651 118 // But the interface mode used must be sent to the LSM9DS0 constructor. Use
YCTung 0:0dbf7ee73651 119 // one of these two as the first parameter of the constructor.
YCTung 0:0dbf7ee73651 120 enum interface_mode
YCTung 0:0dbf7ee73651 121 {
YCTung 0:0dbf7ee73651 122 SPI_MODE = 1,
YCTung 0:0dbf7ee73651 123 I2C_MODE = 0,
YCTung 0:0dbf7ee73651 124 };
YCTung 0:0dbf7ee73651 125
YCTung 0:0dbf7ee73651 126 class LSM9DS0
YCTung 0:0dbf7ee73651 127 {
YCTung 0:0dbf7ee73651 128 public:
YCTung 0:0dbf7ee73651 129 // gyro_scale defines the possible full-scale ranges of the gyroscope:
YCTung 0:0dbf7ee73651 130 enum gyro_scale
YCTung 0:0dbf7ee73651 131 {
YCTung 0:0dbf7ee73651 132 G_SCALE_245DPS = 0x0, // 00: 245 degrees per second
YCTung 0:0dbf7ee73651 133 G_SCALE_500DPS = 0x1, // 01: 500 dps
YCTung 0:0dbf7ee73651 134 G_SCALE_2000DPS = 0x2, // 10: 2000 dps
YCTung 0:0dbf7ee73651 135 };
YCTung 0:0dbf7ee73651 136 // accel_scale defines all possible FSR's of the accelerometer:
YCTung 0:0dbf7ee73651 137 enum accel_scale
YCTung 0:0dbf7ee73651 138 {
YCTung 0:0dbf7ee73651 139 A_SCALE_2G = 0x0, // 000: 2g
YCTung 0:0dbf7ee73651 140 A_SCALE_4G = 0x1, // 001: 4g
YCTung 0:0dbf7ee73651 141 A_SCALE_6G = 0x2, // 010: 6g
YCTung 0:0dbf7ee73651 142 A_SCALE_8G = 0x3, // 011: 8g
YCTung 0:0dbf7ee73651 143 A_SCALE_16G = 0x4, // 100: 16g
YCTung 0:0dbf7ee73651 144 };
YCTung 0:0dbf7ee73651 145 // mag_scale defines all possible FSR's of the magnetometer:
YCTung 0:0dbf7ee73651 146 enum mag_scale
YCTung 0:0dbf7ee73651 147 {
YCTung 0:0dbf7ee73651 148 M_SCALE_2GS = 0x0, // 00: 2Gs
YCTung 0:0dbf7ee73651 149 M_SCALE_4GS = 0x1, // 01: 4Gs
YCTung 0:0dbf7ee73651 150 M_SCALE_8GS = 0x2, // 10: 8Gs
YCTung 0:0dbf7ee73651 151 M_SCALE_12GS= 0x3, // 11: 12Gs
YCTung 0:0dbf7ee73651 152 };
YCTung 0:0dbf7ee73651 153 // gyro_odr defines all possible data rate/bandwidth combos of the gyro:
YCTung 0:0dbf7ee73651 154 enum gyro_odr
YCTung 0:0dbf7ee73651 155 { // ODR (Hz) --- Cutoff
YCTung 0:0dbf7ee73651 156 G_ODR_95_BW_125 = 0x0, // 95 12.5
YCTung 0:0dbf7ee73651 157 G_ODR_95_BW_25 = 0x1, // 95 25
YCTung 0:0dbf7ee73651 158 // 0x2 and 0x3 define the same data rate and bandwidth
YCTung 0:0dbf7ee73651 159 G_ODR_190_BW_125 = 0x4, // 190 12.5
YCTung 0:0dbf7ee73651 160 G_ODR_190_BW_25 = 0x5, // 190 25
YCTung 0:0dbf7ee73651 161 G_ODR_190_BW_50 = 0x6, // 190 50
YCTung 0:0dbf7ee73651 162 G_ODR_190_BW_70 = 0x7, // 190 70
YCTung 0:0dbf7ee73651 163 G_ODR_380_BW_20 = 0x8, // 380 20
YCTung 0:0dbf7ee73651 164 G_ODR_380_BW_25 = 0x9, // 380 25
YCTung 0:0dbf7ee73651 165 G_ODR_380_BW_50 = 0xA, // 380 50
YCTung 0:0dbf7ee73651 166 G_ODR_380_BW_100 = 0xB, // 380 100
YCTung 0:0dbf7ee73651 167 G_ODR_760_BW_30 = 0xC, // 760 30
YCTung 0:0dbf7ee73651 168 G_ODR_760_BW_35 = 0xD, // 760 35
YCTung 0:0dbf7ee73651 169 G_ODR_760_BW_50 = 0xE, // 760 50
YCTung 0:0dbf7ee73651 170 G_ODR_760_BW_100 = 0xF, // 760 100
YCTung 0:0dbf7ee73651 171 };
YCTung 0:0dbf7ee73651 172 // accel_oder defines all possible output data rates of the accelerometer:
YCTung 0:0dbf7ee73651 173 enum accel_odr
YCTung 0:0dbf7ee73651 174 {
YCTung 0:0dbf7ee73651 175 A_POWER_DOWN= 0x00, // Power-down mode (0x0)
YCTung 0:0dbf7ee73651 176 A_ODR_3125 = 0x01, // 3.125 Hz (0x1)
YCTung 0:0dbf7ee73651 177 A_ODR_625 = 0x02, // 6.25 Hz (0x2)
YCTung 0:0dbf7ee73651 178 A_ODR_125 = 0x03, // 12.5 Hz (0x3)
YCTung 0:0dbf7ee73651 179 A_ODR_25 = 0x04, // 25 Hz (0x4)
YCTung 0:0dbf7ee73651 180 A_ODR_50 = 0x05, // 50 Hz (0x5)
YCTung 0:0dbf7ee73651 181 A_ODR_100 = 0x06, // 100 Hz (0x6)
YCTung 0:0dbf7ee73651 182 A_ODR_200 = 0x07, // 200 Hz (0x7)
YCTung 0:0dbf7ee73651 183 A_ODR_400 = 0x08, // 400 Hz (0x8)
YCTung 0:0dbf7ee73651 184 A_ODR_800 = 0x09, // 800 Hz (9)
YCTung 0:0dbf7ee73651 185 A_ODR_1600 = 0x0A, // 1600 Hz (0xA)
YCTung 0:0dbf7ee73651 186 };
YCTung 0:0dbf7ee73651 187
YCTung 0:0dbf7ee73651 188 // accel_abw defines all possible anti-aliasing filter rates of the accelerometer:
YCTung 0:0dbf7ee73651 189 enum accel_abw
YCTung 0:0dbf7ee73651 190 {
YCTung 0:0dbf7ee73651 191 A_ABW_773 = 0x0, // 773 Hz (0x0)
YCTung 0:0dbf7ee73651 192 A_ABW_194 = 0x1, // 194 Hz (0x1)
YCTung 0:0dbf7ee73651 193 A_ABW_362 = 0x2, // 362 Hz (0x2)
YCTung 0:0dbf7ee73651 194 A_ABW_50 = 0x3, // 50 Hz (0x3)
YCTung 0:0dbf7ee73651 195 };
YCTung 0:0dbf7ee73651 196
YCTung 0:0dbf7ee73651 197
YCTung 0:0dbf7ee73651 198 // mag_oder defines all possible output data rates of the magnetometer:
YCTung 0:0dbf7ee73651 199 enum mag_odr
YCTung 0:0dbf7ee73651 200 {
YCTung 0:0dbf7ee73651 201 M_ODR_3125 = 0x00, // 3.125 Hz (0x00)
YCTung 0:0dbf7ee73651 202 M_ODR_625 = 0x01, // 6.25 Hz (0x01)
YCTung 0:0dbf7ee73651 203 M_ODR_125 = 0x02, // 12.5 Hz (0x02)
YCTung 0:0dbf7ee73651 204 M_ODR_25 = 0x03, // 25 Hz (0x03)
YCTung 0:0dbf7ee73651 205 M_ODR_50 = 0x04, // 50 (0x04)
YCTung 0:0dbf7ee73651 206 M_ODR_100 = 0x05, // 100 Hz (0x05)
YCTung 0:0dbf7ee73651 207 };
benson516 10:60a176bd72b3 208
benson516 10:60a176bd72b3 209 // Unit transformation
benson516 10:60a176bd72b3 210 float deg2rad; // = 3.1415926/180.0;
benson516 10:60a176bd72b3 211 float rad2deg; // = 180.0/3.1415926;
YCTung 0:0dbf7ee73651 212
YCTung 0:0dbf7ee73651 213 // We'll store the gyro, accel, and magnetometer readings in a series of
YCTung 0:0dbf7ee73651 214 // public class variables. Each sensor gets three variables -- one for each
YCTung 0:0dbf7ee73651 215 // axis. Call readGyro(), readAccel(), and readMag() first, before using
YCTung 0:0dbf7ee73651 216 // these variables!
YCTung 0:0dbf7ee73651 217 // These values are the RAW signed 16-bit readings from the sensors.
YCTung 0:0dbf7ee73651 218 int16_t gx, gy, gz; // x, y, and z axis readings of the gyroscope
YCTung 0:0dbf7ee73651 219 int16_t ax, ay, az; // x, y, and z axis readings of the accelerometer
YCTung 0:0dbf7ee73651 220 int16_t mx, my, mz; // x, y, and z axis readings of the magnetometer
YCTung 0:0dbf7ee73651 221 int16_t temperature;
YCTung 0:0dbf7ee73651 222 float abias[3];
YCTung 0:0dbf7ee73651 223 float gbias[3];
YCTung 1:2c34ccab5256 224
YCTung 1:2c34ccab5256 225 int16_t gyroOffset[3];
YCTung 1:2c34ccab5256 226 int16_t accelOffset[3];
YCTung 1:2c34ccab5256 227 int16_t magOffset[3];
YCTung 1:2c34ccab5256 228 void setGyroOffset(int16_t, int16_t, int16_t);
YCTung 1:2c34ccab5256 229 void setAccelOffset(int16_t, int16_t, int16_t);
YCTung 1:2c34ccab5256 230 void setMagOffset(int16_t, int16_t, int16_t);
YCTung 0:0dbf7ee73651 231
YCTung 0:0dbf7ee73651 232 // LSM9DS0 -- LSM9DS0 class constructor
YCTung 0:0dbf7ee73651 233 // The constructor will set up a handful of private variables, and set the
YCTung 0:0dbf7ee73651 234 // communication mode as well.
YCTung 0:0dbf7ee73651 235 // Input:
YCTung 0:0dbf7ee73651 236 // - interface = Either SPI_MODE or I2C_MODE, whichever you're using
YCTung 0:0dbf7ee73651 237 // to talk to the IC.
YCTung 0:0dbf7ee73651 238 // - gAddr = If I2C_MODE, this is the I2C address of the gyroscope.
YCTung 0:0dbf7ee73651 239 // If SPI_MODE, this is the chip select pin of the gyro (CSG)
YCTung 0:0dbf7ee73651 240 // - xmAddr = If I2C_MODE, this is the I2C address of the accel/mag.
YCTung 0:0dbf7ee73651 241 // If SPI_MODE, this is the cs pin of the accel/mag (CSXM)
YCTung 0:0dbf7ee73651 242 LSM9DS0(interface_mode interface, uint8_t gAddr, uint8_t xmAddr);
YCTung 0:0dbf7ee73651 243
YCTung 0:0dbf7ee73651 244 // begin() -- Initialize the gyro, accelerometer, and magnetometer.
YCTung 0:0dbf7ee73651 245 // This will set up the scale and output rate of each sensor. It'll also
YCTung 0:0dbf7ee73651 246 // "turn on" every sensor and every axis of every sensor.
YCTung 0:0dbf7ee73651 247 // Input:
YCTung 0:0dbf7ee73651 248 // - gScl = The scale of the gyroscope. This should be a gyro_scale value.
YCTung 0:0dbf7ee73651 249 // - aScl = The scale of the accelerometer. Should be a accel_scale value.
YCTung 0:0dbf7ee73651 250 // - mScl = The scale of the magnetometer. Should be a mag_scale value.
YCTung 0:0dbf7ee73651 251 // - gODR = Output data rate of the gyroscope. gyro_odr value.
YCTung 0:0dbf7ee73651 252 // - aODR = Output data rate of the accelerometer. accel_odr value.
YCTung 0:0dbf7ee73651 253 // - mODR = Output data rate of the magnetometer. mag_odr value.
YCTung 0:0dbf7ee73651 254 // Output: The function will return an unsigned 16-bit value. The most-sig
YCTung 0:0dbf7ee73651 255 // bytes of the output are the WHO_AM_I reading of the accel. The
YCTung 0:0dbf7ee73651 256 // least significant two bytes are the WHO_AM_I reading of the gyro.
YCTung 0:0dbf7ee73651 257 // All parameters have a defaulted value, so you can call just "begin()".
YCTung 0:0dbf7ee73651 258 // Default values are FSR's of: 2000DPS, 8g, 8Gs; ODRs of 760 Hz for
YCTung 0:0dbf7ee73651 259 // gyro, 800 Hz for accelerometer, 100 Hz for magnetometer.
YCTung 0:0dbf7ee73651 260 // Use the return value of this function to verify communication.
YCTung 0:0dbf7ee73651 261 uint16_t begin(gyro_scale gScl = G_SCALE_2000DPS,
YCTung 0:0dbf7ee73651 262 accel_scale aScl = A_SCALE_8G, mag_scale mScl = M_SCALE_8GS,
YCTung 0:0dbf7ee73651 263 gyro_odr gODR = G_ODR_760_BW_100, accel_odr aODR = A_ODR_800,
YCTung 0:0dbf7ee73651 264 mag_odr mODR = M_ODR_100);
YCTung 0:0dbf7ee73651 265
YCTung 0:0dbf7ee73651 266 // readGyro() -- Read the gyroscope output registers.
YCTung 0:0dbf7ee73651 267 // This function will read all six gyroscope output registers.
YCTung 0:0dbf7ee73651 268 // The readings are stored in the class' gx, gy, and gz variables. Read
YCTung 0:0dbf7ee73651 269 // those _after_ calling readGyro().
YCTung 0:0dbf7ee73651 270 void readGyro();
benson516 10:60a176bd72b3 271 void readGyroFloatVector_degPs(vector<float> &v_out); // Read to float array v_out[]
benson516 10:60a176bd72b3 272 void readGyroFloatVector_radPs(vector<float> &v_out); // Read to float array v_out[]
benson516 10:60a176bd72b3 273 void readGyroRawVector(vector<int16_t> &v_out); // Raw data in int16_t
YCTung 1:2c34ccab5256 274 int16_t readRawGyroX( void );
YCTung 1:2c34ccab5256 275 int16_t readRawGyroY( void );
YCTung 1:2c34ccab5256 276 int16_t readRawGyroZ( void );
YCTung 1:2c34ccab5256 277 float readFloatGyroX( void );
YCTung 1:2c34ccab5256 278 float readFloatGyroY( void );
YCTung 1:2c34ccab5256 279 float readFloatGyroZ( void );
YCTung 0:0dbf7ee73651 280
YCTung 0:0dbf7ee73651 281 // readAccel() -- Read the accelerometer output registers.
YCTung 0:0dbf7ee73651 282 // This function will read all six accelerometer output registers.
YCTung 0:0dbf7ee73651 283 // The readings are stored in the class' ax, ay, and az variables. Read
YCTung 0:0dbf7ee73651 284 // those _after_ calling readAccel().
YCTung 0:0dbf7ee73651 285 void readAccel();
benson516 10:60a176bd72b3 286 void readAccelFloatVector(vector<float> &v_out); // Read to float array v_out[]
benson516 10:60a176bd72b3 287 void readAccelRawVector(vector<int16_t> &v_out); // Raw data in int16_t
YCTung 1:2c34ccab5256 288 int16_t readRawAccelX( void );
YCTung 1:2c34ccab5256 289 int16_t readRawAccelY( void );
YCTung 1:2c34ccab5256 290 int16_t readRawAccelZ( void );
YCTung 1:2c34ccab5256 291 float readFloatAccelX( void );
YCTung 1:2c34ccab5256 292 float readFloatAccelY( void );
YCTung 1:2c34ccab5256 293 float readFloatAccelZ( void );
YCTung 0:0dbf7ee73651 294
YCTung 0:0dbf7ee73651 295 // readMag() -- Read the magnetometer output registers.
YCTung 0:0dbf7ee73651 296 // This function will read all six magnetometer output registers.
YCTung 0:0dbf7ee73651 297 // The readings are stored in the class' mx, my, and mz variables. Read
YCTung 0:0dbf7ee73651 298 // those _after_ calling readMag().
YCTung 0:0dbf7ee73651 299 void readMag();
benson516 10:60a176bd72b3 300 void readMagFloatVector(vector<float> &v_out); // Read to float array v_out[]
benson516 10:60a176bd72b3 301 void readMagRawVector(vector<int16_t> &v_out); // Raw data in int16_t
YCTung 1:2c34ccab5256 302 int16_t readRawMagX( void );
YCTung 1:2c34ccab5256 303 int16_t readRawMagY( void );
YCTung 1:2c34ccab5256 304 int16_t readRawMagZ( void );
YCTung 1:2c34ccab5256 305 float readFloatMagX( void );
YCTung 1:2c34ccab5256 306 float readFloatMagY( void );
YCTung 1:2c34ccab5256 307 float readFloatMagZ( void );
YCTung 0:0dbf7ee73651 308
YCTung 0:0dbf7ee73651 309 // readTemp() -- Read the temperature output register.
YCTung 0:0dbf7ee73651 310 // This function will read two temperature output registers.
YCTung 0:0dbf7ee73651 311 // The combined readings are stored in the class' temperature variables. Read
YCTung 0:0dbf7ee73651 312 // those _after_ calling readTemp().
YCTung 0:0dbf7ee73651 313 void readTemp();
YCTung 0:0dbf7ee73651 314
YCTung 0:0dbf7ee73651 315 // calcGyro() -- Convert from RAW signed 16-bit value to degrees per second
YCTung 0:0dbf7ee73651 316 // This function reads in a signed 16-bit value and returns the scaled
YCTung 0:0dbf7ee73651 317 // DPS. This function relies on gScale and gRes being correct.
YCTung 0:0dbf7ee73651 318 // Input:
YCTung 0:0dbf7ee73651 319 // - gyro = A signed 16-bit raw reading from the gyroscope.
YCTung 0:0dbf7ee73651 320 float calcGyro(int16_t gyro);
YCTung 0:0dbf7ee73651 321
YCTung 0:0dbf7ee73651 322 // calcAccel() -- Convert from RAW signed 16-bit value to gravity (g's).
YCTung 0:0dbf7ee73651 323 // This function reads in a signed 16-bit value and returns the scaled
YCTung 0:0dbf7ee73651 324 // g's. This function relies on aScale and aRes being correct.
YCTung 0:0dbf7ee73651 325 // Input:
YCTung 0:0dbf7ee73651 326 // - accel = A signed 16-bit raw reading from the accelerometer.
YCTung 0:0dbf7ee73651 327 float calcAccel(int16_t accel);
YCTung 0:0dbf7ee73651 328
YCTung 0:0dbf7ee73651 329 // calcMag() -- Convert from RAW signed 16-bit value to Gauss (Gs)
YCTung 0:0dbf7ee73651 330 // This function reads in a signed 16-bit value and returns the scaled
YCTung 0:0dbf7ee73651 331 // Gs. This function relies on mScale and mRes being correct.
YCTung 0:0dbf7ee73651 332 // Input:
YCTung 0:0dbf7ee73651 333 // - mag = A signed 16-bit raw reading from the magnetometer.
YCTung 0:0dbf7ee73651 334 float calcMag(int16_t mag);
YCTung 0:0dbf7ee73651 335
YCTung 0:0dbf7ee73651 336 // setGyroScale() -- Set the full-scale range of the gyroscope.
YCTung 0:0dbf7ee73651 337 // This function can be called to set the scale of the gyroscope to
YCTung 0:0dbf7ee73651 338 // 245, 500, or 200 degrees per second.
YCTung 0:0dbf7ee73651 339 // Input:
YCTung 0:0dbf7ee73651 340 // - gScl = The desired gyroscope scale. Must be one of three possible
YCTung 0:0dbf7ee73651 341 // values from the gyro_scale enum.
YCTung 0:0dbf7ee73651 342 void setGyroScale(gyro_scale gScl);
YCTung 0:0dbf7ee73651 343
YCTung 0:0dbf7ee73651 344 // setAccelScale() -- Set the full-scale range of the accelerometer.
YCTung 0:0dbf7ee73651 345 // This function can be called to set the scale of the accelerometer to
YCTung 0:0dbf7ee73651 346 // 2, 4, 6, 8, or 16 g's.
YCTung 0:0dbf7ee73651 347 // Input:
YCTung 0:0dbf7ee73651 348 // - aScl = The desired accelerometer scale. Must be one of five possible
YCTung 0:0dbf7ee73651 349 // values from the accel_scale enum.
YCTung 0:0dbf7ee73651 350 void setAccelScale(accel_scale aScl);
YCTung 0:0dbf7ee73651 351
YCTung 0:0dbf7ee73651 352 // setMagScale() -- Set the full-scale range of the magnetometer.
YCTung 0:0dbf7ee73651 353 // This function can be called to set the scale of the magnetometer to
YCTung 0:0dbf7ee73651 354 // 2, 4, 8, or 12 Gs.
YCTung 0:0dbf7ee73651 355 // Input:
YCTung 0:0dbf7ee73651 356 // - mScl = The desired magnetometer scale. Must be one of four possible
YCTung 0:0dbf7ee73651 357 // values from the mag_scale enum.
YCTung 0:0dbf7ee73651 358 void setMagScale(mag_scale mScl);
YCTung 0:0dbf7ee73651 359
YCTung 0:0dbf7ee73651 360 // setGyroODR() -- Set the output data rate and bandwidth of the gyroscope
YCTung 0:0dbf7ee73651 361 // Input:
YCTung 0:0dbf7ee73651 362 // - gRate = The desired output rate and cutoff frequency of the gyro.
YCTung 0:0dbf7ee73651 363 // Must be a value from the gyro_odr enum (check above, there're 14).
YCTung 0:0dbf7ee73651 364 void setGyroODR(gyro_odr gRate);
YCTung 0:0dbf7ee73651 365
YCTung 0:0dbf7ee73651 366 // setAccelODR() -- Set the output data rate of the accelerometer
YCTung 0:0dbf7ee73651 367 // Input:
YCTung 0:0dbf7ee73651 368 // - aRate = The desired output rate of the accel.
YCTung 0:0dbf7ee73651 369 // Must be a value from the accel_odr enum (check above, there're 11).
YCTung 0:0dbf7ee73651 370 void setAccelODR(accel_odr aRate);
YCTung 0:0dbf7ee73651 371
YCTung 1:2c34ccab5256 372 // setAccelABW() -- Set the anti-aliasing filter rate of the accelerometer
YCTung 0:0dbf7ee73651 373 // Input:
YCTung 0:0dbf7ee73651 374 // - abwRate = The desired anti-aliasing filter rate of the accel.
YCTung 0:0dbf7ee73651 375 // Must be a value from the accel_abw enum (check above, there're 4).
YCTung 0:0dbf7ee73651 376 void setAccelABW(accel_abw abwRate);
YCTung 0:0dbf7ee73651 377
YCTung 0:0dbf7ee73651 378 // setMagODR() -- Set the output data rate of the magnetometer
YCTung 0:0dbf7ee73651 379 // Input:
YCTung 0:0dbf7ee73651 380 // - mRate = The desired output rate of the mag.
YCTung 0:0dbf7ee73651 381 // Must be a value from the mag_odr enum (check above, there're 6).
YCTung 0:0dbf7ee73651 382 void setMagODR(mag_odr mRate);
YCTung 0:0dbf7ee73651 383
YCTung 0:0dbf7ee73651 384 // configGyroInt() -- Configure the gyro interrupt output.
YCTung 0:0dbf7ee73651 385 // Triggers can be set to either rising above or falling below a specified
YCTung 0:0dbf7ee73651 386 // threshold. This function helps setup the interrupt configuration and
YCTung 0:0dbf7ee73651 387 // threshold values for all axes.
YCTung 0:0dbf7ee73651 388 // Input:
YCTung 0:0dbf7ee73651 389 // - int1Cfg = A 8-bit value that is sent directly to the INT1_CFG_G
YCTung 0:0dbf7ee73651 390 // register. This sets AND/OR and high/low interrupt gen for each axis
YCTung 0:0dbf7ee73651 391 // - int1ThsX = 16-bit interrupt threshold value for x-axis
YCTung 0:0dbf7ee73651 392 // - int1ThsY = 16-bit interrupt threshold value for y-axis
YCTung 0:0dbf7ee73651 393 // - int1ThsZ = 16-bit interrupt threshold value for z-axis
YCTung 0:0dbf7ee73651 394 // - duration = Duration an interrupt holds after triggered. This value
YCTung 0:0dbf7ee73651 395 // is copied directly into the INT1_DURATION_G register.
YCTung 0:0dbf7ee73651 396 // Before using this function, read about the INT1_CFG_G register and
YCTung 0:0dbf7ee73651 397 // the related INT1* registers in the LMS9DS0 datasheet.
YCTung 0:0dbf7ee73651 398 void configGyroInt(uint8_t int1Cfg, uint16_t int1ThsX = 0,
YCTung 0:0dbf7ee73651 399 uint16_t int1ThsY = 0, uint16_t int1ThsZ = 0,
YCTung 0:0dbf7ee73651 400 uint8_t duration = 0);
YCTung 0:0dbf7ee73651 401
YCTung 0:0dbf7ee73651 402
YCTung 0:0dbf7ee73651 403 void calLSM9DS0(float gbias[3], float abias[3]);
YCTung 0:0dbf7ee73651 404
YCTung 0:0dbf7ee73651 405 SPI spi_;
YCTung 0:0dbf7ee73651 406 I2C i2c_;
YCTung 0:0dbf7ee73651 407 DigitalOut csG_;
YCTung 0:0dbf7ee73651 408 DigitalOut csXM_;
adam_z 2:48eb33afd0fa 409
adam_z 2:48eb33afd0fa 410 float pitch, roll;
adam_z 2:48eb33afd0fa 411 void complementaryFilter(float * data, float dt);
YCTung 0:0dbf7ee73651 412
YCTung 0:0dbf7ee73651 413 private:
YCTung 0:0dbf7ee73651 414 // xmAddress and gAddress store the I2C address or SPI chip select pin
YCTung 0:0dbf7ee73651 415 // for each sensor.
YCTung 0:0dbf7ee73651 416 uint8_t xmAddress, gAddress;
YCTung 0:0dbf7ee73651 417 // interfaceMode keeps track of whether we're using SPI or I2C to talk
YCTung 0:0dbf7ee73651 418 interface_mode interfaceMode;
YCTung 0:0dbf7ee73651 419
YCTung 0:0dbf7ee73651 420 // gScale, aScale, and mScale store the current scale range for each
YCTung 0:0dbf7ee73651 421 // sensor. Should be updated whenever that value changes.
YCTung 0:0dbf7ee73651 422 gyro_scale gScale;
YCTung 0:0dbf7ee73651 423 accel_scale aScale;
YCTung 0:0dbf7ee73651 424 mag_scale mScale;
YCTung 0:0dbf7ee73651 425
YCTung 0:0dbf7ee73651 426 // gRes, aRes, and mRes store the current resolution for each sensor.
YCTung 0:0dbf7ee73651 427 // Units of these values would be DPS (or g's or Gs's) per ADC tick.
YCTung 0:0dbf7ee73651 428 // This value is calculated as (sensor scale) / (2^15).
YCTung 0:0dbf7ee73651 429 float gRes, aRes, mRes;
YCTung 0:0dbf7ee73651 430
YCTung 0:0dbf7ee73651 431 // initGyro() -- Sets up the gyroscope to begin reading.
YCTung 0:0dbf7ee73651 432 // This function steps through all five gyroscope control registers.
YCTung 0:0dbf7ee73651 433 // Upon exit, the following parameters will be set:
YCTung 0:0dbf7ee73651 434 // - CTRL_REG1_G = 0x0F: Normal operation mode, all axes enabled.
YCTung 0:0dbf7ee73651 435 // 95 Hz ODR, 12.5 Hz cutoff frequency.
YCTung 0:0dbf7ee73651 436 // - CTRL_REG2_G = 0x00: HPF set to normal mode, cutoff frequency
YCTung 0:0dbf7ee73651 437 // set to 7.2 Hz (depends on ODR).
YCTung 0:0dbf7ee73651 438 // - CTRL_REG3_G = 0x88: Interrupt enabled on INT_G (set to push-pull and
YCTung 0:0dbf7ee73651 439 // active high). Data-ready output enabled on DRDY_G.
YCTung 0:0dbf7ee73651 440 // - CTRL_REG4_G = 0x00: Continuous update mode. Data LSB stored in lower
YCTung 0:0dbf7ee73651 441 // address. Scale set to 245 DPS. SPI mode set to 4-wire.
YCTung 0:0dbf7ee73651 442 // - CTRL_REG5_G = 0x00: FIFO disabled. HPF disabled.
YCTung 0:0dbf7ee73651 443 void initGyro();
YCTung 0:0dbf7ee73651 444
YCTung 0:0dbf7ee73651 445 // initAccel() -- Sets up the accelerometer to begin reading.
YCTung 0:0dbf7ee73651 446 // This function steps through all accelerometer related control registers.
YCTung 0:0dbf7ee73651 447 // Upon exit these registers will be set as:
YCTung 0:0dbf7ee73651 448 // - CTRL_REG0_XM = 0x00: FIFO disabled. HPF bypassed. Normal mode.
YCTung 0:0dbf7ee73651 449 // - CTRL_REG1_XM = 0x57: 100 Hz data rate. Continuous update.
YCTung 0:0dbf7ee73651 450 // all axes enabled.
YCTung 0:0dbf7ee73651 451 // - CTRL_REG2_XM = 0x00: 2g scale. 773 Hz anti-alias filter BW.
YCTung 0:0dbf7ee73651 452 // - CTRL_REG3_XM = 0x04: Accel data ready signal on INT1_XM pin.
YCTung 0:0dbf7ee73651 453 void initAccel();
YCTung 0:0dbf7ee73651 454
YCTung 0:0dbf7ee73651 455 // initMag() -- Sets up the magnetometer to begin reading.
YCTung 0:0dbf7ee73651 456 // This function steps through all magnetometer-related control registers.
YCTung 0:0dbf7ee73651 457 // Upon exit these registers will be set as:
YCTung 0:0dbf7ee73651 458 // - CTRL_REG4_XM = 0x04: Mag data ready signal on INT2_XM pin.
YCTung 0:0dbf7ee73651 459 // - CTRL_REG5_XM = 0x14: 100 Hz update rate. Low resolution. Interrupt
YCTung 0:0dbf7ee73651 460 // requests don't latch. Temperature sensor disabled.
YCTung 0:0dbf7ee73651 461 // - CTRL_REG6_XM = 0x00: 2 Gs scale.
YCTung 0:0dbf7ee73651 462 // - CTRL_REG7_XM = 0x00: Continuous conversion mode. Normal HPF mode.
YCTung 0:0dbf7ee73651 463 // - INT_CTRL_REG_M = 0x09: Interrupt active-high. Enable interrupts.
YCTung 0:0dbf7ee73651 464 void initMag();
YCTung 0:0dbf7ee73651 465
YCTung 0:0dbf7ee73651 466 // gReadByte() -- Reads a byte from a specified gyroscope register.
YCTung 0:0dbf7ee73651 467 // Input:
YCTung 0:0dbf7ee73651 468 // - subAddress = Register to be read from.
YCTung 0:0dbf7ee73651 469 // Output:
YCTung 0:0dbf7ee73651 470 // - An 8-bit value read from the requested address.
YCTung 0:0dbf7ee73651 471 uint8_t gReadByte(uint8_t subAddress);
YCTung 0:0dbf7ee73651 472
YCTung 0:0dbf7ee73651 473 // gReadBytes() -- Reads a number of bytes -- beginning at an address
YCTung 0:0dbf7ee73651 474 // and incrementing from there -- from the gyroscope.
YCTung 0:0dbf7ee73651 475 // Input:
YCTung 0:0dbf7ee73651 476 // - subAddress = Register to be read from.
YCTung 0:0dbf7ee73651 477 // - * dest = A pointer to an array of uint8_t's. Values read will be
YCTung 0:0dbf7ee73651 478 // stored in here on return.
YCTung 0:0dbf7ee73651 479 // - count = The number of bytes to be read.
YCTung 0:0dbf7ee73651 480 // Output: No value is returned, but the `dest` array will store
YCTung 0:0dbf7ee73651 481 // the data read upon exit.
YCTung 0:0dbf7ee73651 482 void gReadBytes(uint8_t subAddress, uint8_t * dest, uint8_t count);
YCTung 0:0dbf7ee73651 483
YCTung 0:0dbf7ee73651 484 // gWriteByte() -- Write a byte to a register in the gyroscope.
YCTung 0:0dbf7ee73651 485 // Input:
YCTung 0:0dbf7ee73651 486 // - subAddress = Register to be written to.
YCTung 0:0dbf7ee73651 487 // - data = data to be written to the register.
YCTung 0:0dbf7ee73651 488 void gWriteByte(uint8_t subAddress, uint8_t data);
YCTung 0:0dbf7ee73651 489
YCTung 0:0dbf7ee73651 490 // xmReadByte() -- Read a byte from a register in the accel/mag sensor
YCTung 0:0dbf7ee73651 491 // Input:
YCTung 0:0dbf7ee73651 492 // - subAddress = Register to be read from.
YCTung 0:0dbf7ee73651 493 // Output:
YCTung 0:0dbf7ee73651 494 // - An 8-bit value read from the requested register.
YCTung 0:0dbf7ee73651 495 uint8_t xmReadByte(uint8_t subAddress);
YCTung 0:0dbf7ee73651 496
YCTung 0:0dbf7ee73651 497 // xmReadBytes() -- Reads a number of bytes -- beginning at an address
YCTung 0:0dbf7ee73651 498 // and incrementing from there -- from the accelerometer/magnetometer.
YCTung 0:0dbf7ee73651 499 // Input:
YCTung 0:0dbf7ee73651 500 // - subAddress = Register to be read from.
YCTung 0:0dbf7ee73651 501 // - * dest = A pointer to an array of uint8_t's. Values read will be
YCTung 0:0dbf7ee73651 502 // stored in here on return.
YCTung 0:0dbf7ee73651 503 // - count = The number of bytes to be read.
YCTung 0:0dbf7ee73651 504 // Output: No value is returned, but the `dest` array will store
YCTung 0:0dbf7ee73651 505 // the data read upon exit.
YCTung 0:0dbf7ee73651 506 void xmReadBytes(uint8_t subAddress, uint8_t * dest, uint8_t count);
YCTung 0:0dbf7ee73651 507
YCTung 0:0dbf7ee73651 508 // xmWriteByte() -- Write a byte to a register in the accel/mag sensor.
YCTung 0:0dbf7ee73651 509 // Input:
YCTung 0:0dbf7ee73651 510 // - subAddress = Register to be written to.
YCTung 0:0dbf7ee73651 511 // - data = data to be written to the register.
YCTung 0:0dbf7ee73651 512 void xmWriteByte(uint8_t subAddress, uint8_t data);
YCTung 0:0dbf7ee73651 513
YCTung 0:0dbf7ee73651 514 // calcgRes() -- Calculate the resolution of the gyroscope.
YCTung 0:0dbf7ee73651 515 // This function will set the value of the gRes variable. gScale must
YCTung 0:0dbf7ee73651 516 // be set prior to calling this function.
YCTung 0:0dbf7ee73651 517 void calcgRes();
YCTung 0:0dbf7ee73651 518
YCTung 0:0dbf7ee73651 519 // calcmRes() -- Calculate the resolution of the magnetometer.
YCTung 0:0dbf7ee73651 520 // This function will set the value of the mRes variable. mScale must
YCTung 0:0dbf7ee73651 521 // be set prior to calling this function.
YCTung 0:0dbf7ee73651 522 void calcmRes();
YCTung 0:0dbf7ee73651 523
YCTung 0:0dbf7ee73651 524 // calcaRes() -- Calculate the resolution of the accelerometer.
YCTung 0:0dbf7ee73651 525 // This function will set the value of the aRes variable. aScale must
YCTung 0:0dbf7ee73651 526 // be set prior to calling this function.
YCTung 0:0dbf7ee73651 527 void calcaRes();
YCTung 0:0dbf7ee73651 528
YCTung 0:0dbf7ee73651 529 ///////////////////
YCTung 0:0dbf7ee73651 530 // SPI Functions //
YCTung 0:0dbf7ee73651 531 ///////////////////
YCTung 0:0dbf7ee73651 532 // initSPI() -- Initialize the SPI hardware.
YCTung 0:0dbf7ee73651 533 // This function will setup all SPI pins and related hardware.
YCTung 0:0dbf7ee73651 534 void initSPI();
YCTung 0:0dbf7ee73651 535
YCTung 0:0dbf7ee73651 536 // SPIwriteByte() -- Write a byte out of SPI to a register in the device
YCTung 0:0dbf7ee73651 537 // Input:
YCTung 0:0dbf7ee73651 538 // - csPin = The chip select pin of the slave device.
YCTung 0:0dbf7ee73651 539 // - subAddress = The register to be written to.
YCTung 0:0dbf7ee73651 540 // - data = Byte to be written to the register.
YCTung 0:0dbf7ee73651 541 void SPIwriteByte(uint8_t csPin, uint8_t subAddress, uint8_t data);
YCTung 0:0dbf7ee73651 542
YCTung 0:0dbf7ee73651 543 // SPIreadByte() -- Read a single byte from a register over SPI.
YCTung 0:0dbf7ee73651 544 // Input:
YCTung 0:0dbf7ee73651 545 // - csPin = The chip select pin of the slave device.
YCTung 0:0dbf7ee73651 546 // - subAddress = The register to be read from.
YCTung 0:0dbf7ee73651 547 // Output:
YCTung 0:0dbf7ee73651 548 // - The byte read from the requested address.
YCTung 0:0dbf7ee73651 549 uint8_t SPIreadByte(uint8_t csPin, uint8_t subAddress);
YCTung 0:0dbf7ee73651 550
YCTung 0:0dbf7ee73651 551 // SPIreadBytes() -- Read a series of bytes, starting at a register via SPI
YCTung 0:0dbf7ee73651 552 // Input:
YCTung 0:0dbf7ee73651 553 // - csPin = The chip select pin of a slave device.
YCTung 0:0dbf7ee73651 554 // - subAddress = The register to begin reading.
YCTung 0:0dbf7ee73651 555 // - * dest = Pointer to an array where we'll store the readings.
YCTung 0:0dbf7ee73651 556 // - count = Number of registers to be read.
YCTung 0:0dbf7ee73651 557 // Output: No value is returned by the function, but the registers read are
YCTung 0:0dbf7ee73651 558 // all stored in the *dest array given.
YCTung 0:0dbf7ee73651 559 void SPIreadBytes(uint8_t csPin, uint8_t subAddress,
YCTung 0:0dbf7ee73651 560 uint8_t * dest, uint8_t count);
YCTung 0:0dbf7ee73651 561
YCTung 0:0dbf7ee73651 562 ///////////////////
YCTung 0:0dbf7ee73651 563 // I2C Functions //
YCTung 0:0dbf7ee73651 564 ///////////////////
YCTung 0:0dbf7ee73651 565 // initI2C() -- Initialize the I2C hardware.
YCTung 0:0dbf7ee73651 566 // This function will setup all I2C pins and related hardware.
YCTung 0:0dbf7ee73651 567 void initI2C();
YCTung 0:0dbf7ee73651 568
YCTung 0:0dbf7ee73651 569 // I2CwriteByte() -- Write a byte out of I2C to a register in the device
YCTung 0:0dbf7ee73651 570 // Input:
YCTung 0:0dbf7ee73651 571 // - address = The 7-bit I2C address of the slave device.
YCTung 0:0dbf7ee73651 572 // - subAddress = The register to be written to.
YCTung 0:0dbf7ee73651 573 // - data = Byte to be written to the register.
YCTung 0:0dbf7ee73651 574 void I2CwriteByte(uint8_t address, uint8_t subAddress, uint8_t data);
YCTung 0:0dbf7ee73651 575
YCTung 0:0dbf7ee73651 576 // I2CreadByte() -- Read a single byte from a register over I2C.
YCTung 0:0dbf7ee73651 577 // Input:
YCTung 0:0dbf7ee73651 578 // - address = The 7-bit I2C address of the slave device.
YCTung 0:0dbf7ee73651 579 // - subAddress = The register to be read from.
YCTung 0:0dbf7ee73651 580 // Output:
YCTung 0:0dbf7ee73651 581 // - The byte read from the requested address.
YCTung 0:0dbf7ee73651 582 uint8_t I2CreadByte(uint8_t address, uint8_t subAddress);
YCTung 0:0dbf7ee73651 583
YCTung 0:0dbf7ee73651 584 // I2CreadBytes() -- Read a series of bytes, starting at a register via SPI
YCTung 0:0dbf7ee73651 585 // Input:
YCTung 0:0dbf7ee73651 586 // - address = The 7-bit I2C address of the slave device.
YCTung 0:0dbf7ee73651 587 // - subAddress = The register to begin reading.
YCTung 0:0dbf7ee73651 588 // - * dest = Pointer to an array where we'll store the readings.
YCTung 0:0dbf7ee73651 589 // - count = Number of registers to be read.
YCTung 0:0dbf7ee73651 590 // Output: No value is returned by the function, but the registers read are
YCTung 0:0dbf7ee73651 591 // all stored in the *dest array given.
YCTung 0:0dbf7ee73651 592 void I2CreadBytes(uint8_t address, uint8_t subAddress, uint8_t * dest, uint8_t count);
adam_z 2:48eb33afd0fa 593
adam_z 2:48eb33afd0fa 594
YCTung 0:0dbf7ee73651 595 };
YCTung 0:0dbf7ee73651 596
YCTung 0:0dbf7ee73651 597 #endif // SFE_LSM9DS0_H //