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 MPU6050 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 // Changelog: 00004 // 2013-01-08 - first release 00005 00006 #include "I2Cdev.h" 00007 #include "MODI2C.h" 00008 00009 00010 //#define useDebugSerial 00011 00012 I2Cdev::I2Cdev(): debugSerial(USBTX, USBRX), i2c(I2C_SDA,I2C_SCL) 00013 { 00014 00015 } 00016 00017 I2Cdev::I2Cdev(PinName i2cSda, PinName i2cScl): debugSerial(USBTX, USBRX), i2c(i2cSda,i2cScl) 00018 { 00019 00020 } 00021 00022 /** Read a single bit from an 8-bit device register. 00023 * @param devAddr I2C slave device address 00024 * @param regAddr Register regAddr to read from 00025 * @param bitNum Bit position to read (0-7) 00026 * @param data Container for single bit value 00027 * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout) 00028 * @return Status of read operation (true = success) 00029 */ 00030 int8_t I2Cdev::readBit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t *data, uint16_t timeout) { 00031 uint8_t b; 00032 uint8_t count = readByte(devAddr, regAddr, &b, timeout); 00033 *data = b & (1 << bitNum); 00034 return count; 00035 } 00036 00037 /** Read a single bit from a 16-bit device register. 00038 * @param devAddr I2C slave device address 00039 * @param regAddr Register regAddr to read from 00040 * @param bitNum Bit position to read (0-15) 00041 * @param data Container for single bit value 00042 * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout) 00043 * @return Status of read operation (true = success) 00044 */ 00045 int8_t I2Cdev::readBitW(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint16_t *data, uint16_t timeout) { 00046 uint16_t b; 00047 uint8_t count = readWord(devAddr, regAddr, &b, timeout); 00048 *data = b & (1 << bitNum); 00049 return count; 00050 } 00051 00052 /** Read multiple bits from an 8-bit device register. 00053 * @param devAddr I2C slave device address 00054 * @param regAddr Register regAddr to read from 00055 * @param bitStart First bit position to read (0-7) 00056 * @param length Number of bits to read (not more than 8) 00057 * @param data Container for right-aligned value (i.e. '101' read from any bitStart position will equal 0x05) 00058 * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout) 00059 * @return Status of read operation (true = success) 00060 */ 00061 int8_t I2Cdev::readBits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t *data, uint16_t timeout) { 00062 // 01101001 read byte 00063 // 76543210 bit numbers 00064 // xxx args: bitStart=4, length=3 00065 // 010 masked 00066 // -> 010 shifted 00067 uint8_t count, b; 00068 if ((count = readByte(devAddr, regAddr, &b, timeout)) != 0) { 00069 uint8_t mask = ((1 << length) - 1) << (bitStart - length + 1); 00070 b &= mask; 00071 b >>= (bitStart - length + 1); 00072 *data = b; 00073 } 00074 return count; 00075 } 00076 00077 /** Read multiple bits from a 16-bit device register. 00078 * @param devAddr I2C slave device address 00079 * @param regAddr Register regAddr to read from 00080 * @param bitStart First bit position to read (0-15) 00081 * @param length Number of bits to read (not more than 16) 00082 * @param data Container for right-aligned value (i.e. '101' read from any bitStart position will equal 0x05) 00083 * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout) 00084 * @return Status of read operation (1 = success, 0 = failure, -1 = timeout) 00085 */ 00086 int8_t I2Cdev::readBitsW(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint16_t *data, uint16_t timeout) { 00087 // 1101011001101001 read byte 00088 // fedcba9876543210 bit numbers 00089 // xxx args: bitStart=12, length=3 00090 // 010 masked 00091 // -> 010 shifted 00092 uint8_t count; 00093 uint16_t w; 00094 if ((count = readWord(devAddr, regAddr, &w, timeout)) != 0) { 00095 uint16_t mask = ((1 << length) - 1) << (bitStart - length + 1); 00096 w &= mask; 00097 w >>= (bitStart - length + 1); 00098 *data = w; 00099 } 00100 return count; 00101 } 00102 /** Read single byte from an 8-bit device register. 00103 * @param devAddr I2C slave device address 00104 * @param regAddr Register regAddr to read from 00105 * @param data Container for byte value read from device 00106 * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout) 00107 * @return Status of read operation (true = success) 00108 */ 00109 int8_t I2Cdev::readByte(uint8_t devAddr, uint8_t regAddr, uint8_t *data, uint16_t timeout) { 00110 return readBytes(devAddr, regAddr, 1, data, timeout); 00111 } 00112 00113 /** Read single word from a 16-bit device register. 00114 * @param devAddr I2C slave device address 00115 * @param regAddr Register regAddr to read from 00116 * @param data Container for word value read from device 00117 * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout) 00118 * @return Status of read operation (true = success) 00119 */ 00120 int8_t I2Cdev::readWord(uint8_t devAddr, uint8_t regAddr, uint16_t *data, uint16_t timeout) { 00121 return readWords(devAddr, regAddr, 1, data, timeout); 00122 } 00123 00124 /** Read multiple bytes from an 8-bit device register. 00125 * @param devAddr I2C slave device address 00126 * @param regAddr First register regAddr to read from 00127 * @param length Number of bytes to read 00128 * @param data Buffer to store read data in 00129 * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout) 00130 * @return Number of bytes read (-1 indicates failure) 00131 */ 00132 int8_t I2Cdev::readBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data, uint16_t timeout) 00133 { 00134 char command[1]; 00135 command[0] = regAddr; 00136 i2c.write(devAddr, command, 1, true); 00137 i2c.read(devAddr, (char*)data, length); 00138 return length; 00139 } 00140 00141 void I2Cdev::readBytes_nb(uint8_t devAddr, char *command, uint8_t length, uint8_t *data, uint32_t (*function)(uint32_t), void* param) 00142 { 00143 i2c.write(devAddr, command, 1, true); 00144 i2c.read_nb(devAddr, (char*)data, length, function, param); 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 uint32_t writefinhdl(uint32_t param){ 00259 //free((void*)param); 00260 ((I2Cdev*)param)->freebuffer(); 00261 return 0; 00262 } 00263 00264 bool I2Cdev::writeBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data) 00265 { 00266 char* mem = NULL; 00267 mem = allocbuffer(); 00268 *mem = regAddr; 00269 memcpy(mem+1, data, length); 00270 00271 i2c.write(devAddr, mem, length+1, &writefinhdl, this); 00272 return true; 00273 } 00274 00275 char *I2Cdev::allocbuffer(){ 00276 char *buff = (char *)pool.alloc(); 00277 while (buff==NULL) buff = (char *)pool.alloc(); 00278 queue.put(buff); 00279 return buff; 00280 } 00281 00282 void I2Cdev::freebuffer(){ 00283 osEvent evt = queue.get(0); 00284 pool.free((char(*)[14])evt.value.p); 00285 } 00286 00287 bool I2Cdev::writeWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint16_t *data) 00288 { 00289 return true; 00290 } 00291 00292 uint16_t I2Cdev::readTimeout(void) 00293 { 00294 return 0; 00295 }
Generated on Tue Jul 12 2022 21:34:30 by
1.7.2
