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