An improved version of mbed's I2C class.

Dependents:   acd52832_Humidity_Temp_Example BB acnSensa_LIS aconnoCellularGnss ... more

Committer:
jurica238814
Date:
Thu Jul 19 15:17:28 2018 +0200
Revision:
7:b2f0c302ba6d
Parent:
6:4d1b387c12c3
Method description added

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jurica238814 0:bfefd65ef71d 1 /*
jurica238814 0:bfefd65ef71d 2 * I2C library made by Jurica Resetar @ aconno
jurica238814 0:bfefd65ef71d 3 * 2017
jurica238814 0:bfefd65ef71d 4 * More info @ aconno.de
jurica238814 0:bfefd65ef71d 5 * jurica_resetar@yahoo.com
jurica238814 0:bfefd65ef71d 6 * All right reserved
jurica238814 0:bfefd65ef71d 7 *
jurica238814 0:bfefd65ef71d 8 */
jurica238814 0:bfefd65ef71d 9
jurica238814 0:bfefd65ef71d 10 #include "aconno_i2c.h"
jurica238814 0:bfefd65ef71d 11
jurica238814 4:db46d6bb60f0 12 /**
jurica238814 4:db46d6bb60f0 13 * [aconno_i2c::aconno_i2c description]
jurica238814 4:db46d6bb60f0 14 * @param i2c [description]
jurica238814 4:db46d6bb60f0 15 * @param address [description]
jurica238814 4:db46d6bb60f0 16 */
jurica238814 2:3c0eab894a4b 17 aconno_i2c::aconno_i2c(I2C *i2c, char address){
jurica238814 4:db46d6bb60f0 18 this->i2c = i2c;
jurica238814 0:bfefd65ef71d 19 i2cAddress = address;
jurica238814 2:3c0eab894a4b 20 }
jurica238814 0:bfefd65ef71d 21
jurica238814 7:b2f0c302ba6d 22 /**
jurica238814 7:b2f0c302ba6d 23 * Method for changing specific bits within register
jurica238814 7:b2f0c302ba6d 24 * @param regAddress: 8bit address
jurica238814 7:b2f0c302ba6d 25 * @param regSize: register size in B
jurica238814 7:b2f0c302ba6d 26 * @param newValue new value for [numOfBits] bits within the register
jurica238814 7:b2f0c302ba6d 27 * @param numOfBits sizeof[newValue] in b
jurica238814 7:b2f0c302ba6d 28 * @param offset newValue offset within the register
jurica238814 7:b2f0c302ba6d 29 * @return returns new value of the register
jurica238814 7:b2f0c302ba6d 30 */
jurica238814 6:4d1b387c12c3 31 uint16_t aconno_i2c::changeRegBits(char regAddress, uint8_t regSize,
jurica238814 6:4d1b387c12c3 32 uint16_t newValue, uint16_t numOfBits, uint16_t offset)
jurica238814 6:4d1b387c12c3 33 {
jurica238814 6:4d1b387c12c3 34 uint16_t mask;
jurica238814 6:4d1b387c12c3 35 uint16_t regData = 0;
jurica238814 6:4d1b387c12c3 36 // Read old configuration
jurica238814 6:4d1b387c12c3 37 readFromReg(regAddress, (char*)&regData, regSize);
jurica238814 6:4d1b387c12c3 38 mask = 0 | newValue << offset;
jurica238814 6:4d1b387c12c3 39 // Clear bits
jurica238814 6:4d1b387c12c3 40 regData &= ~(((1 << numOfBits)-1) << offset);
jurica238814 6:4d1b387c12c3 41 regData |= mask; // Set/clear bits as required
jurica238814 6:4d1b387c12c3 42 writeToReg(regAddress, (char*)&regData, regSize);
jurica238814 6:4d1b387c12c3 43 return regData;
jurica238814 6:4d1b387c12c3 44 }
jurica238814 6:4d1b387c12c3 45
jurica238814 4:db46d6bb60f0 46 /**
jurica238814 4:db46d6bb60f0 47 * [aconno_i2c::writeToReg description]
jurica238814 4:db46d6bb60f0 48 * @param regAddress [description]
jurica238814 4:db46d6bb60f0 49 * @param data [description]
jurica238814 4:db46d6bb60f0 50 * @param len [description]
jurica238814 4:db46d6bb60f0 51 * @return [description]
jurica238814 4:db46d6bb60f0 52 */
jurica238814 0:bfefd65ef71d 53 uint8_t aconno_i2c::writeToReg(char regAddress, char *data, int len){
jurica238814 3:b9f3eef1fad9 54 uint8_t success; /* 0 on success (ack), non-0 on failure (nack) */
jurica238814 5:54ba0e4f13ae 55 char dataToSend[len+1];
jurica238814 4:db46d6bb60f0 56
jurica238814 0:bfefd65ef71d 57 dataToSend[0] = regAddress;
jurica238814 5:54ba0e4f13ae 58 memcpy(dataToSend+1, data, len);
jurica238814 5:54ba0e4f13ae 59 // R/W bit is set low for a write command
jurica238814 5:54ba0e4f13ae 60 success = i2c->write(i2cAddress & 0xFE, dataToSend, len + 1);
jurica238814 0:bfefd65ef71d 61 return success;
jurica238814 0:bfefd65ef71d 62 }
jurica238814 0:bfefd65ef71d 63
jurica238814 4:db46d6bb60f0 64 /**
jurica238814 4:db46d6bb60f0 65 * [aconno_i2c::readFromReg description]
jurica238814 4:db46d6bb60f0 66 * @param regAddress [description]
jurica238814 4:db46d6bb60f0 67 * @param dataBuffer [description]
jurica238814 4:db46d6bb60f0 68 * @param len [description]
jurica238814 4:db46d6bb60f0 69 * @return [description]
jurica238814 4:db46d6bb60f0 70 */
jurica238814 0:bfefd65ef71d 71 uint8_t aconno_i2c::readFromReg(char regAddress, char *dataBuffer, int len){
jurica238814 0:bfefd65ef71d 72 uint8_t success; /* 0 on success (ack), non-0 on failure (nack) */
jurica238814 0:bfefd65ef71d 73 char regAddr = regAddress;
jurica238814 4:db46d6bb60f0 74
jurica238814 2:3c0eab894a4b 75 i2c->write(i2cAddress & 0xFE, &regAddr, 1); // R/W bit is set low for a write command
jurica238814 2:3c0eab894a4b 76 success = i2c->read(i2cAddress | 0x01, dataBuffer, len); // R/W bit is set high for a read command
jurica238814 0:bfefd65ef71d 77 return success;
jurica238814 0:bfefd65ef71d 78 }
jurica238814 0:bfefd65ef71d 79
jurica238814 4:db46d6bb60f0 80 /**
jurica238814 4:db46d6bb60f0 81 * This method is used with sendCommand(char *command, uint8_t len) method when delay between command
jurica238814 4:db46d6bb60f0 82 * and response from the slave is required
jurica238814 4:db46d6bb60f0 83 * @param dataBuffer [description]
jurica238814 4:db46d6bb60f0 84 * @param len [description]
jurica238814 4:db46d6bb60f0 85 * @return [description]
jurica238814 1:5ae1807e3902 86 */
jurica238814 1:5ae1807e3902 87 uint8_t aconno_i2c::readBus(char *dataBuffer, int len){
jurica238814 1:5ae1807e3902 88 uint8_t success; /* 0 on success (ack), non-0 on failure (nack) */
jurica238814 4:db46d6bb60f0 89
jurica238814 2:3c0eab894a4b 90 success = i2c->read(i2cAddress | 0x01, dataBuffer, len); // R/W bit is set high for a read command
jurica238814 1:5ae1807e3902 91 return success;
jurica238814 1:5ae1807e3902 92 }
jurica238814 1:5ae1807e3902 93
jurica238814 4:db46d6bb60f0 94 /**
jurica238814 4:db46d6bb60f0 95 * This method is used to send commands to I2C Device.
jurica238814 0:bfefd65ef71d 96 * Ex. send 2B command to Si7006 to get Device ID.
jurica238814 4:db46d6bb60f0 97 * @param command [description]
jurica238814 4:db46d6bb60f0 98 * @param len [description]
jurica238814 4:db46d6bb60f0 99 * @param response [description]
jurica238814 4:db46d6bb60f0 100 * @param responseLen [description]
jurica238814 4:db46d6bb60f0 101 * @param repeated [description]
jurica238814 4:db46d6bb60f0 102 * @return 0 on success (ack), non-0 on failure (nack)
jurica238814 0:bfefd65ef71d 103 */
jurica238814 4:db46d6bb60f0 104 uint8_t aconno_i2c::sendCommand(char *command, uint8_t len, char *response,
jurica238814 4:db46d6bb60f0 105 uint8_t responseLen, bool repeated)
jurica238814 4:db46d6bb60f0 106 {
jurica238814 0:bfefd65ef71d 107 uint8_t success; /* 0 on success (ack), non-0 on failure (nack) */
jurica238814 4:db46d6bb60f0 108
jurica238814 4:db46d6bb60f0 109 // R/W bit is set low for a write command
jurica238814 4:db46d6bb60f0 110 i2c->write(i2cAddress & 0xFE, command, len, repeated);
jurica238814 4:db46d6bb60f0 111 // R/W bit is set high for a read command
jurica238814 4:db46d6bb60f0 112 success = i2c->read(i2cAddress | 0x01, response, responseLen);
jurica238814 0:bfefd65ef71d 113 return success;
jurica238814 0:bfefd65ef71d 114 }
jurica238814 0:bfefd65ef71d 115
jurica238814 4:db46d6bb60f0 116 /**
jurica238814 4:db46d6bb60f0 117 * [aconno_i2c::sendCommand description]
jurica238814 4:db46d6bb60f0 118 * @param command [description]
jurica238814 4:db46d6bb60f0 119 * @param len [description]
jurica238814 4:db46d6bb60f0 120 * @return [description]
jurica238814 4:db46d6bb60f0 121 */
jurica238814 1:5ae1807e3902 122 uint8_t aconno_i2c::sendCommand(char *command, uint8_t len){
jurica238814 1:5ae1807e3902 123 uint8_t success; /* 0 on success (ack), non-0 on failure (nack) */
jurica238814 4:db46d6bb60f0 124
jurica238814 2:3c0eab894a4b 125 success = i2c->write(i2cAddress & 0xFE, command, len);
jurica238814 1:5ae1807e3902 126 return success;
jurica238814 1:5ae1807e3902 127 }