うおーるぼっとLEDのワークショップ用

Dependencies:   mbed

Committer:
jksoft
Date:
Sat Feb 04 03:33:08 2017 +0000
Revision:
3:b707c09b4433
Parent:
0:9fcc79b3f00e
????

Who changed what in which revision?

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