テスト用です。

Dependencies:   mbed

Committer:
jksoft
Date:
Tue Oct 11 11:09:42 2016 +0000
Revision:
0:8468a4403fea
SB??ver;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jksoft 0:8468a4403fea 1 // ported from arduino library: https://github.com/jrowberg/i2cdevlib
jksoft 0:8468a4403fea 2 // written by szymon gaertig (email: szymon@gaertig.com.pl, website: szymongaertig.pl)
jksoft 0:8468a4403fea 3 // Changelog:
jksoft 0:8468a4403fea 4 // 2013-01-08 - first release
jksoft 0:8468a4403fea 5
jksoft 0:8468a4403fea 6 #include "I2Cdev.h"
jksoft 0:8468a4403fea 7
jksoft 0:8468a4403fea 8 #define useDebugSerial
jksoft 0:8468a4403fea 9
jksoft 0:8468a4403fea 10 I2Cdev::I2Cdev(): debugSerial(USBTX, USBRX), i2c(I2C_SDA,I2C_SCL)
jksoft 0:8468a4403fea 11 {
jksoft 0:8468a4403fea 12
jksoft 0:8468a4403fea 13 }
jksoft 0:8468a4403fea 14
jksoft 0:8468a4403fea 15 I2Cdev::I2Cdev(PinName i2cSda, PinName i2cScl): debugSerial(USBTX, USBRX), i2c(i2cSda,i2cScl)
jksoft 0:8468a4403fea 16 {
jksoft 0:8468a4403fea 17
jksoft 0:8468a4403fea 18 }
jksoft 0:8468a4403fea 19
jksoft 0:8468a4403fea 20 /** Read a single bit from an 8-bit device register.
jksoft 0:8468a4403fea 21 * @param devAddr I2C slave device address
jksoft 0:8468a4403fea 22 * @param regAddr Register regAddr to read from
jksoft 0:8468a4403fea 23 * @param bitNum Bit position to read (0-7)
jksoft 0:8468a4403fea 24 * @param data Container for single bit value
jksoft 0:8468a4403fea 25 * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout)
jksoft 0:8468a4403fea 26 * @return Status of read operation (true = success)
jksoft 0:8468a4403fea 27 */
jksoft 0:8468a4403fea 28 int8_t I2Cdev::readBit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t *data, uint16_t timeout) {
jksoft 0:8468a4403fea 29 uint8_t b;
jksoft 0:8468a4403fea 30 uint8_t count = readByte(devAddr, regAddr, &b, timeout);
jksoft 0:8468a4403fea 31 *data = b & (1 << bitNum);
jksoft 0:8468a4403fea 32 return count;
jksoft 0:8468a4403fea 33 }
jksoft 0:8468a4403fea 34
jksoft 0:8468a4403fea 35 /** Read a single bit from a 16-bit device register.
jksoft 0:8468a4403fea 36 * @param devAddr I2C slave device address
jksoft 0:8468a4403fea 37 * @param regAddr Register regAddr to read from
jksoft 0:8468a4403fea 38 * @param bitNum Bit position to read (0-15)
jksoft 0:8468a4403fea 39 * @param data Container for single bit value
jksoft 0:8468a4403fea 40 * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout)
jksoft 0:8468a4403fea 41 * @return Status of read operation (true = success)
jksoft 0:8468a4403fea 42 */
jksoft 0:8468a4403fea 43 int8_t I2Cdev::readBitW(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint16_t *data, uint16_t timeout) {
jksoft 0:8468a4403fea 44 uint16_t b;
jksoft 0:8468a4403fea 45 uint8_t count = readWord(devAddr, regAddr, &b, timeout);
jksoft 0:8468a4403fea 46 *data = b & (1 << bitNum);
jksoft 0:8468a4403fea 47 return count;
jksoft 0:8468a4403fea 48 }
jksoft 0:8468a4403fea 49
jksoft 0:8468a4403fea 50 /** Read multiple bits from an 8-bit device register.
jksoft 0:8468a4403fea 51 * @param devAddr I2C slave device address
jksoft 0:8468a4403fea 52 * @param regAddr Register regAddr to read from
jksoft 0:8468a4403fea 53 * @param bitStart First bit position to read (0-7)
jksoft 0:8468a4403fea 54 * @param length Number of bits to read (not more than 8)
jksoft 0:8468a4403fea 55 * @param data Container for right-aligned value (i.e. '101' read from any bitStart position will equal 0x05)
jksoft 0:8468a4403fea 56 * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout)
jksoft 0:8468a4403fea 57 * @return Status of read operation (true = success)
jksoft 0:8468a4403fea 58 */
jksoft 0:8468a4403fea 59 int8_t I2Cdev::readBits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t *data, uint16_t timeout) {
jksoft 0:8468a4403fea 60 // 01101001 read byte
jksoft 0:8468a4403fea 61 // 76543210 bit numbers
jksoft 0:8468a4403fea 62 // xxx args: bitStart=4, length=3
jksoft 0:8468a4403fea 63 // 010 masked
jksoft 0:8468a4403fea 64 // -> 010 shifted
jksoft 0:8468a4403fea 65 uint8_t count, b;
jksoft 0:8468a4403fea 66 if ((count = readByte(devAddr, regAddr, &b, timeout)) != 0) {
jksoft 0:8468a4403fea 67 uint8_t mask = ((1 << length) - 1) << (bitStart - length + 1);
jksoft 0:8468a4403fea 68 b &= mask;
jksoft 0:8468a4403fea 69 b >>= (bitStart - length + 1);
jksoft 0:8468a4403fea 70 *data = b;
jksoft 0:8468a4403fea 71 }
jksoft 0:8468a4403fea 72 return count;
jksoft 0:8468a4403fea 73 }
jksoft 0:8468a4403fea 74
jksoft 0:8468a4403fea 75 /** Read multiple bits from a 16-bit device register.
jksoft 0:8468a4403fea 76 * @param devAddr I2C slave device address
jksoft 0:8468a4403fea 77 * @param regAddr Register regAddr to read from
jksoft 0:8468a4403fea 78 * @param bitStart First bit position to read (0-15)
jksoft 0:8468a4403fea 79 * @param length Number of bits to read (not more than 16)
jksoft 0:8468a4403fea 80 * @param data Container for right-aligned value (i.e. '101' read from any bitStart position will equal 0x05)
jksoft 0:8468a4403fea 81 * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout)
jksoft 0:8468a4403fea 82 * @return Status of read operation (1 = success, 0 = failure, -1 = timeout)
jksoft 0:8468a4403fea 83 */
jksoft 0:8468a4403fea 84 int8_t I2Cdev::readBitsW(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint16_t *data, uint16_t timeout) {
jksoft 0:8468a4403fea 85 // 1101011001101001 read byte
jksoft 0:8468a4403fea 86 // fedcba9876543210 bit numbers
jksoft 0:8468a4403fea 87 // xxx args: bitStart=12, length=3
jksoft 0:8468a4403fea 88 // 010 masked
jksoft 0:8468a4403fea 89 // -> 010 shifted
jksoft 0:8468a4403fea 90 uint8_t count;
jksoft 0:8468a4403fea 91 uint16_t w;
jksoft 0:8468a4403fea 92 if ((count = readWord(devAddr, regAddr, &w, timeout)) != 0) {
jksoft 0:8468a4403fea 93 uint16_t mask = ((1 << length) - 1) << (bitStart - length + 1);
jksoft 0:8468a4403fea 94 w &= mask;
jksoft 0:8468a4403fea 95 w >>= (bitStart - length + 1);
jksoft 0:8468a4403fea 96 *data = w;
jksoft 0:8468a4403fea 97 }
jksoft 0:8468a4403fea 98 return count;
jksoft 0:8468a4403fea 99 }
jksoft 0:8468a4403fea 100 /** Read single byte from an 8-bit device register.
jksoft 0:8468a4403fea 101 * @param devAddr I2C slave device address
jksoft 0:8468a4403fea 102 * @param regAddr Register regAddr to read from
jksoft 0:8468a4403fea 103 * @param data Container for byte value read from device
jksoft 0:8468a4403fea 104 * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout)
jksoft 0:8468a4403fea 105 * @return Status of read operation (true = success)
jksoft 0:8468a4403fea 106 */
jksoft 0:8468a4403fea 107 int8_t I2Cdev::readByte(uint8_t devAddr, uint8_t regAddr, uint8_t *data, uint16_t timeout) {
jksoft 0:8468a4403fea 108 return readBytes(devAddr, regAddr, 1, data, timeout);
jksoft 0:8468a4403fea 109 }
jksoft 0:8468a4403fea 110
jksoft 0:8468a4403fea 111 /** Read single word from a 16-bit device register.
jksoft 0:8468a4403fea 112 * @param devAddr I2C slave device address
jksoft 0:8468a4403fea 113 * @param regAddr Register regAddr to read from
jksoft 0:8468a4403fea 114 * @param data Container for word value read from device
jksoft 0:8468a4403fea 115 * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout)
jksoft 0:8468a4403fea 116 * @return Status of read operation (true = success)
jksoft 0:8468a4403fea 117 */
jksoft 0:8468a4403fea 118 int8_t I2Cdev::readWord(uint8_t devAddr, uint8_t regAddr, uint16_t *data, uint16_t timeout) {
jksoft 0:8468a4403fea 119 return readWords(devAddr, regAddr, 1, data, timeout);
jksoft 0:8468a4403fea 120 }
jksoft 0:8468a4403fea 121
jksoft 0:8468a4403fea 122 /** Read multiple bytes from an 8-bit device register.
jksoft 0:8468a4403fea 123 * @param devAddr I2C slave device address
jksoft 0:8468a4403fea 124 * @param regAddr First register regAddr to read from
jksoft 0:8468a4403fea 125 * @param length Number of bytes to read
jksoft 0:8468a4403fea 126 * @param data Buffer to store read data in
jksoft 0:8468a4403fea 127 * @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout)
jksoft 0:8468a4403fea 128 * @return Number of bytes read (-1 indicates failure)
jksoft 0:8468a4403fea 129 */
jksoft 0:8468a4403fea 130 int8_t I2Cdev::readBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data, uint16_t timeout)
jksoft 0:8468a4403fea 131 {
jksoft 0:8468a4403fea 132 char command[1];
jksoft 0:8468a4403fea 133 command[0] = regAddr;
jksoft 0:8468a4403fea 134 char *redData = (char*)malloc(length);
jksoft 0:8468a4403fea 135 i2c.write(devAddr<<1, command, 1, true);
jksoft 0:8468a4403fea 136 i2c.read(devAddr<<1, redData, length);
jksoft 0:8468a4403fea 137 for(int i =0; i < length; i++) {
jksoft 0:8468a4403fea 138 data[i] = redData[i];
jksoft 0:8468a4403fea 139 }
jksoft 0:8468a4403fea 140 free (redData);
jksoft 0:8468a4403fea 141 return length;
jksoft 0:8468a4403fea 142 }
jksoft 0:8468a4403fea 143
jksoft 0:8468a4403fea 144 int8_t I2Cdev::readWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint16_t *data, uint16_t timeout)
jksoft 0:8468a4403fea 145 {
jksoft 0:8468a4403fea 146 return 0;
jksoft 0:8468a4403fea 147 }
jksoft 0:8468a4403fea 148
jksoft 0:8468a4403fea 149 /** write a single bit in an 8-bit device register.
jksoft 0:8468a4403fea 150 * @param devAddr I2C slave device address
jksoft 0:8468a4403fea 151 * @param regAddr Register regAddr to write to
jksoft 0:8468a4403fea 152 * @param bitNum Bit position to write (0-7)
jksoft 0:8468a4403fea 153 * @param value New bit value to write
jksoft 0:8468a4403fea 154 * @return Status of operation (true = success)
jksoft 0:8468a4403fea 155 */
jksoft 0:8468a4403fea 156 bool I2Cdev::writeBit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t data) {
jksoft 0:8468a4403fea 157 uint8_t b;
jksoft 0:8468a4403fea 158 readByte(devAddr, regAddr, &b);
jksoft 0:8468a4403fea 159 b = (data != 0) ? (b | (1 << bitNum)) : (b & ~(1 << bitNum));
jksoft 0:8468a4403fea 160 return writeByte(devAddr, regAddr, b);
jksoft 0:8468a4403fea 161 }
jksoft 0:8468a4403fea 162
jksoft 0:8468a4403fea 163 /** write a single bit in a 16-bit device register.
jksoft 0:8468a4403fea 164 * @param devAddr I2C slave device address
jksoft 0:8468a4403fea 165 * @param regAddr Register regAddr to write to
jksoft 0:8468a4403fea 166 * @param bitNum Bit position to write (0-15)
jksoft 0:8468a4403fea 167 * @param value New bit value to write
jksoft 0:8468a4403fea 168 * @return Status of operation (true = success)
jksoft 0:8468a4403fea 169 */
jksoft 0:8468a4403fea 170 bool I2Cdev::writeBitW(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint16_t data) {
jksoft 0:8468a4403fea 171 uint16_t w;
jksoft 0:8468a4403fea 172 readWord(devAddr, regAddr, &w);
jksoft 0:8468a4403fea 173 w = (data != 0) ? (w | (1 << bitNum)) : (w & ~(1 << bitNum));
jksoft 0:8468a4403fea 174 return writeWord(devAddr, regAddr, w);
jksoft 0:8468a4403fea 175 }
jksoft 0:8468a4403fea 176
jksoft 0:8468a4403fea 177 /** Write multiple bits in an 8-bit device register.
jksoft 0:8468a4403fea 178 * @param devAddr I2C slave device address
jksoft 0:8468a4403fea 179 * @param regAddr Register regAddr to write to
jksoft 0:8468a4403fea 180 * @param bitStart First bit position to write (0-7)
jksoft 0:8468a4403fea 181 * @param length Number of bits to write (not more than 8)
jksoft 0:8468a4403fea 182 * @param data Right-aligned value to write
jksoft 0:8468a4403fea 183 * @return Status of operation (true = success)
jksoft 0:8468a4403fea 184 */
jksoft 0:8468a4403fea 185 bool I2Cdev::writeBits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t data) {
jksoft 0:8468a4403fea 186 // 010 value to write
jksoft 0:8468a4403fea 187 // 76543210 bit numbers
jksoft 0:8468a4403fea 188 // xxx args: bitStart=4, length=3
jksoft 0:8468a4403fea 189 // 00011100 mask byte
jksoft 0:8468a4403fea 190 // 10101111 original value (sample)
jksoft 0:8468a4403fea 191 // 10100011 original & ~mask
jksoft 0:8468a4403fea 192 // 10101011 masked | value
jksoft 0:8468a4403fea 193 uint8_t b;
jksoft 0:8468a4403fea 194 if (readByte(devAddr, regAddr, &b) != 0) {
jksoft 0:8468a4403fea 195 uint8_t mask = ((1 << length) - 1) << (bitStart - length + 1);
jksoft 0:8468a4403fea 196 data <<= (bitStart - length + 1); // shift data into correct position
jksoft 0:8468a4403fea 197 data &= mask; // zero all non-important bits in data
jksoft 0:8468a4403fea 198 b &= ~(mask); // zero all important bits in existing byte
jksoft 0:8468a4403fea 199 b |= data; // combine data with existing byte
jksoft 0:8468a4403fea 200 return writeByte(devAddr, regAddr, b);
jksoft 0:8468a4403fea 201 } else {
jksoft 0:8468a4403fea 202 return false;
jksoft 0:8468a4403fea 203 }
jksoft 0:8468a4403fea 204 }
jksoft 0:8468a4403fea 205
jksoft 0:8468a4403fea 206 /** Write multiple bits in a 16-bit device register.
jksoft 0:8468a4403fea 207 * @param devAddr I2C slave device address
jksoft 0:8468a4403fea 208 * @param regAddr Register regAddr to write to
jksoft 0:8468a4403fea 209 * @param bitStart First bit position to write (0-15)
jksoft 0:8468a4403fea 210 * @param length Number of bits to write (not more than 16)
jksoft 0:8468a4403fea 211 * @param data Right-aligned value to write
jksoft 0:8468a4403fea 212 * @return Status of operation (true = success)
jksoft 0:8468a4403fea 213 */
jksoft 0:8468a4403fea 214 bool I2Cdev::writeBitsW(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint16_t data) {
jksoft 0:8468a4403fea 215 // 010 value to write
jksoft 0:8468a4403fea 216 // fedcba9876543210 bit numbers
jksoft 0:8468a4403fea 217 // xxx args: bitStart=12, length=3
jksoft 0:8468a4403fea 218 // 0001110000000000 mask byte
jksoft 0:8468a4403fea 219 // 1010111110010110 original value (sample)
jksoft 0:8468a4403fea 220 // 1010001110010110 original & ~mask
jksoft 0:8468a4403fea 221 // 1010101110010110 masked | value
jksoft 0:8468a4403fea 222 uint16_t w;
jksoft 0:8468a4403fea 223 if (readWord(devAddr, regAddr, &w) != 0) {
jksoft 0:8468a4403fea 224 uint8_t mask = ((1 << length) - 1) << (bitStart - length + 1);
jksoft 0:8468a4403fea 225 data <<= (bitStart - length + 1); // shift data into correct position
jksoft 0:8468a4403fea 226 data &= mask; // zero all non-important bits in data
jksoft 0:8468a4403fea 227 w &= ~(mask); // zero all important bits in existing word
jksoft 0:8468a4403fea 228 w |= data; // combine data with existing word
jksoft 0:8468a4403fea 229 return writeWord(devAddr, regAddr, w);
jksoft 0:8468a4403fea 230 } else {
jksoft 0:8468a4403fea 231 return false;
jksoft 0:8468a4403fea 232 }
jksoft 0:8468a4403fea 233 }
jksoft 0:8468a4403fea 234
jksoft 0:8468a4403fea 235 /** Write single byte to an 8-bit device register.
jksoft 0:8468a4403fea 236 * @param devAddr I2C slave device address
jksoft 0:8468a4403fea 237 * @param regAddr Register address to write to
jksoft 0:8468a4403fea 238 * @param data New byte value to write
jksoft 0:8468a4403fea 239 * @return Status of operation (true = success)
jksoft 0:8468a4403fea 240 */
jksoft 0:8468a4403fea 241 bool I2Cdev::writeByte(uint8_t devAddr, uint8_t regAddr, uint8_t data) {
jksoft 0:8468a4403fea 242 return writeBytes(devAddr, regAddr, 1, &data);
jksoft 0:8468a4403fea 243 }
jksoft 0:8468a4403fea 244
jksoft 0:8468a4403fea 245 /** Write single word to a 16-bit device register.
jksoft 0:8468a4403fea 246 * @param devAddr I2C slave device address
jksoft 0:8468a4403fea 247 * @param regAddr Register address to write to
jksoft 0:8468a4403fea 248 * @param data New word value to write
jksoft 0:8468a4403fea 249 * @return Status of operation (true = success)
jksoft 0:8468a4403fea 250 */
jksoft 0:8468a4403fea 251 bool I2Cdev::writeWord(uint8_t devAddr, uint8_t regAddr, uint16_t data) {
jksoft 0:8468a4403fea 252 return writeWords(devAddr, regAddr, 1, &data);
jksoft 0:8468a4403fea 253 }
jksoft 0:8468a4403fea 254
jksoft 0:8468a4403fea 255 bool I2Cdev::writeBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data)
jksoft 0:8468a4403fea 256 {
jksoft 0:8468a4403fea 257 i2c.start();
jksoft 0:8468a4403fea 258 i2c.write(devAddr<<1);
jksoft 0:8468a4403fea 259 i2c.write(regAddr);
jksoft 0:8468a4403fea 260 for(int i = 0; i < length; i++) {
jksoft 0:8468a4403fea 261 i2c.write(data[i]);
jksoft 0:8468a4403fea 262 }
jksoft 0:8468a4403fea 263 i2c.stop();
jksoft 0:8468a4403fea 264 return true;
jksoft 0:8468a4403fea 265 }
jksoft 0:8468a4403fea 266
jksoft 0:8468a4403fea 267 bool I2Cdev::writeWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint16_t *data)
jksoft 0:8468a4403fea 268 {
jksoft 0:8468a4403fea 269 return true;
jksoft 0:8468a4403fea 270 }
jksoft 0:8468a4403fea 271
jksoft 0:8468a4403fea 272 uint16_t I2Cdev::readTimeout(void)
jksoft 0:8468a4403fea 273 {
jksoft 0:8468a4403fea 274 return 0;
jksoft 0:8468a4403fea 275 }