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