Allows for reading accelerometer, gyroscope, and magnetometer data from an LSM9DS0 IMU device
Dependents: uVGA_4180 uLCD_4180_mini ECE4781_Project
I2Cdev.cpp
00001 // ported from arduino library: https://github.com/jrowberg/i2cdevlib 00002 // written by szymon gaertig (email: szymon@gaertig.com.pl, website: szymongaertig.pl) 00003 // Changelog: 00004 // 2013-01-08 - first release 00005 00006 #include "I2Cdev.h" 00007 00008 #define useDebugSerial 00009 00010 I2Cdev::I2Cdev(): i2c(I2C_SDA,I2C_SCL), debugSerial(USBTX, USBRX) 00011 { 00012 00013 } 00014 00015 I2Cdev::I2Cdev(PinName i2cSda, PinName i2cScl): i2c(i2cSda,i2cScl), debugSerial(USBTX, USBRX) 00016 { 00017 i2c.frequency(100000); 00018 } 00019 00020 /** Read a single bit from an 8-bit device register. 00021 * @param devAddr I2C slave device address 00022 * @param regAddr Register regAddr to read from 00023 * @param bitNum Bit position to read (0-7) 00024 * @param data Container for single bit value 00025 * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout) 00026 * @return Status of read operation (true = success) 00027 */ 00028 int8_t I2Cdev::readBit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t *data, uint16_t timeout) { 00029 uint8_t b; 00030 uint8_t count = readByte(devAddr, regAddr, &b, timeout); 00031 *data = b & (1 << bitNum); 00032 return count; 00033 } 00034 00035 /** Read a single bit from a 16-bit device register. 00036 * @param devAddr I2C slave device address 00037 * @param regAddr Register regAddr to read from 00038 * @param bitNum Bit position to read (0-15) 00039 * @param data Container for single bit value 00040 * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout) 00041 * @return Status of read operation (true = success) 00042 */ 00043 int8_t I2Cdev::readBitW(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint16_t *data, uint16_t timeout) { 00044 uint16_t b; 00045 uint8_t count = readWord(devAddr, regAddr, &b, timeout); 00046 *data = b & (1 << bitNum); 00047 return count; 00048 } 00049 00050 /** Read multiple bits from an 8-bit device register. 00051 * @param devAddr I2C slave device address 00052 * @param regAddr Register regAddr to read from 00053 * @param bitStart First bit position to read (0-7) 00054 * @param length Number of bits to read (not more than 8) 00055 * @param data Container for right-aligned value (i.e. '101' read from any bitStart position will equal 0x05) 00056 * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout) 00057 * @return Status of read operation (true = success) 00058 */ 00059 int8_t I2Cdev::readBits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t *data, uint16_t timeout) { 00060 // 01101001 read byte 00061 // 76543210 bit numbers 00062 // xxx args: bitStart=4, length=3 00063 // 010 masked 00064 // -> 010 shifted 00065 uint8_t count, b; 00066 if ((count = readByte(devAddr, regAddr, &b, timeout)) != 0) { 00067 uint8_t mask = ((1 << length) - 1) << (bitStart - length + 1); 00068 b &= mask; 00069 b >>= (bitStart - length + 1); 00070 *data = b; 00071 } 00072 return count; 00073 } 00074 00075 /** Read multiple bits from a 16-bit device register. 00076 * @param devAddr I2C slave device address 00077 * @param regAddr Register regAddr to read from 00078 * @param bitStart First bit position to read (0-15) 00079 * @param length Number of bits to read (not more than 16) 00080 * @param data Container for right-aligned value (i.e. '101' read from any bitStart position will equal 0x05) 00081 * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout) 00082 * @return Status of read operation (1 = success, 0 = failure, -1 = timeout) 00083 */ 00084 int8_t I2Cdev::readBitsW(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint16_t *data, uint16_t timeout) { 00085 // 1101011001101001 read byte 00086 // fedcba9876543210 bit numbers 00087 // xxx args: bitStart=12, length=3 00088 // 010 masked 00089 // -> 010 shifted 00090 uint8_t count; 00091 uint16_t w; 00092 if ((count = readWord(devAddr, regAddr, &w, timeout)) != 0) { 00093 uint16_t mask = ((1 << length) - 1) << (bitStart - length + 1); 00094 w &= mask; 00095 w >>= (bitStart - length + 1); 00096 *data = w; 00097 } 00098 return count; 00099 } 00100 /** Read single byte from an 8-bit device register. 00101 * @param devAddr I2C slave device address 00102 * @param regAddr Register regAddr to read from 00103 * @param data Container for byte value read from device 00104 * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout) 00105 * @return Status of read operation (true = success) 00106 */ 00107 int8_t I2Cdev::readByte(uint8_t devAddr, uint8_t regAddr, uint8_t *data, uint16_t timeout) { 00108 00109 return readBytes(devAddr, regAddr, 1, data, timeout); 00110 } 00111 00112 /** Read single word from a 16-bit device register. 00113 * @param devAddr I2C slave device address 00114 * @param regAddr Register regAddr to read from 00115 * @param data Container for word value read from device 00116 * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout) 00117 * @return Status of read operation (true = success) 00118 */ 00119 int8_t I2Cdev::readWord(uint8_t devAddr, uint8_t regAddr, uint16_t *data, uint16_t timeout) { 00120 return readWords(devAddr, regAddr, 1, data, timeout); 00121 } 00122 00123 /** Read multiple bytes from an 8-bit device register. 00124 * @param devAddr I2C slave device address 00125 * @param regAddr First register regAddr to read from 00126 * @param length Number of bytes to read 00127 * @param data Buffer to store read data in 00128 * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout) 00129 * @return Number of bytes read (-1 indicates failure) 00130 */ 00131 int8_t I2Cdev::readBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data, uint16_t timeout) 00132 { 00133 char command[1]; 00134 command[0] = regAddr; 00135 char *redData = (char*)malloc(length); 00136 i2c.write(devAddr<<1, command, 1, true); 00137 i2c.read(devAddr<<1, redData, length); 00138 for(int i =0; i < length; i++) { 00139 data[i] = redData[i]; 00140 //debugSerial.printf("We read %x\n" , redData[i]); 00141 00142 } 00143 free (redData); 00144 return length; 00145 } 00146 00147 int8_t I2Cdev::readWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint16_t *data, uint16_t timeout) 00148 { 00149 return 0; 00150 } 00151 00152 /** write a single bit in an 8-bit device register. 00153 * @param devAddr I2C slave device address 00154 * @param regAddr Register regAddr to write to 00155 * @param bitNum Bit position to write (0-7) 00156 * @param value New bit value to write 00157 * @return Status of operation (true = success) 00158 */ 00159 bool I2Cdev::writeBit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t data) { 00160 uint8_t b; 00161 readByte(devAddr, regAddr, &b); 00162 b = (data != 0) ? (b | (1 << bitNum)) : (b & ~(1 << bitNum)); 00163 return writeByte(devAddr, regAddr, b); 00164 } 00165 00166 /** write a single bit in a 16-bit device register. 00167 * @param devAddr I2C slave device address 00168 * @param regAddr Register regAddr to write to 00169 * @param bitNum Bit position to write (0-15) 00170 * @param value New bit value to write 00171 * @return Status of operation (true = success) 00172 */ 00173 bool I2Cdev::writeBitW(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint16_t data) { 00174 uint16_t w; 00175 readWord(devAddr, regAddr, &w); 00176 w = (data != 0) ? (w | (1 << bitNum)) : (w & ~(1 << bitNum)); 00177 return writeWord(devAddr, regAddr, w); 00178 } 00179 00180 /** Write multiple bits in an 8-bit device register. 00181 * @param devAddr I2C slave device address 00182 * @param regAddr Register regAddr to write to 00183 * @param bitStart First bit position to write (0-7) 00184 * @param length Number of bits to write (not more than 8) 00185 * @param data Right-aligned value to write 00186 * @return Status of operation (true = success) 00187 */ 00188 bool I2Cdev::writeBits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t data) { 00189 // 010 value to write 00190 // 76543210 bit numbers 00191 // xxx args: bitStart=4, length=3 00192 // 00011100 mask byte 00193 // 10101111 original value (sample) 00194 // 10100011 original & ~mask 00195 // 10101011 masked | value 00196 uint8_t b; 00197 if (readByte(devAddr, regAddr, &b) != 0) { 00198 uint8_t mask = ((1 << length) - 1) << (bitStart - length + 1); 00199 data <<= (bitStart - length + 1); // shift data into correct position 00200 data &= mask; // zero all non-important bits in data 00201 b &= ~(mask); // zero all important bits in existing byte 00202 b |= data; // combine data with existing byte 00203 return writeByte(devAddr, regAddr, b); 00204 } else { 00205 return false; 00206 } 00207 } 00208 00209 /** Write multiple bits in a 16-bit device register. 00210 * @param devAddr I2C slave device address 00211 * @param regAddr Register regAddr to write to 00212 * @param bitStart First bit position to write (0-15) 00213 * @param length Number of bits to write (not more than 16) 00214 * @param data Right-aligned value to write 00215 * @return Status of operation (true = success) 00216 */ 00217 bool I2Cdev::writeBitsW(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint16_t data) { 00218 // 010 value to write 00219 // fedcba9876543210 bit numbers 00220 // xxx args: bitStart=12, length=3 00221 // 0001110000000000 mask byte 00222 // 1010111110010110 original value (sample) 00223 // 1010001110010110 original & ~mask 00224 // 1010101110010110 masked | value 00225 uint16_t w; 00226 if (readWord(devAddr, regAddr, &w) != 0) { 00227 uint8_t mask = ((1 << length) - 1) << (bitStart - length + 1); 00228 data <<= (bitStart - length + 1); // shift data into correct position 00229 data &= mask; // zero all non-important bits in data 00230 w &= ~(mask); // zero all important bits in existing word 00231 w |= data; // combine data with existing word 00232 return writeWord(devAddr, regAddr, w); 00233 } else { 00234 return false; 00235 } 00236 } 00237 00238 /** Write single byte to an 8-bit device register. 00239 * @param devAddr I2C slave device address 00240 * @param regAddr Register address to write to 00241 * @param data New byte value to write 00242 * @return Status of operation (true = success) 00243 */ 00244 bool I2Cdev::writeByte(uint8_t devAddr, uint8_t regAddr, uint8_t data) { 00245 return writeBytes(devAddr, regAddr, 1, &data); 00246 } 00247 00248 /** Write single word to a 16-bit device register. 00249 * @param devAddr I2C slave device address 00250 * @param regAddr Register address to write to 00251 * @param data New word value to write 00252 * @return Status of operation (true = success) 00253 */ 00254 bool I2Cdev::writeWord(uint8_t devAddr, uint8_t regAddr, uint16_t data) { 00255 return writeWords(devAddr, regAddr, 1, &data); 00256 } 00257 00258 bool I2Cdev::writeBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data) 00259 { 00260 i2c.start(); 00261 i2c.write(devAddr<<1); 00262 i2c.write(regAddr); 00263 for(int i = 0; i < length; i++) { 00264 i2c.write(data[i]); 00265 } 00266 i2c.stop(); 00267 return true; 00268 } 00269 00270 bool I2Cdev::writeWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint16_t *data) 00271 { 00272 return true; 00273 } 00274 00275 uint16_t I2Cdev::readTimeout(void) 00276 { 00277 return 0; 00278 }
Generated on Sat Jul 16 2022 09:58:21 by 1.7.2