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