AK8963

Dependents:   FreeIMU

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers I2CdevAK8963.cpp Source File

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