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:07:28 2018 +0200
Revision:
6:4d1b387c12c3
Parent:
5:54ba0e4f13ae
Child:
7:b2f0c302ba6d
SetBits in reg method 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 6:4d1b387c12c3 22 uint16_t aconno_i2c::changeRegBits(char regAddress, uint8_t regSize,
jurica238814 6:4d1b387c12c3 23 uint16_t newValue, uint16_t numOfBits, uint16_t offset)
jurica238814 6:4d1b387c12c3 24 {
jurica238814 6:4d1b387c12c3 25 uint16_t mask;
jurica238814 6:4d1b387c12c3 26 uint16_t regData = 0;
jurica238814 6:4d1b387c12c3 27 // Read old configuration
jurica238814 6:4d1b387c12c3 28 readFromReg(regAddress, (char*)&regData, regSize);
jurica238814 6:4d1b387c12c3 29 mask = 0 | newValue << offset;
jurica238814 6:4d1b387c12c3 30 // Clear bits
jurica238814 6:4d1b387c12c3 31 regData &= ~(((1 << numOfBits)-1) << offset);
jurica238814 6:4d1b387c12c3 32 regData |= mask; // Set/clear bits as required
jurica238814 6:4d1b387c12c3 33 writeToReg(regAddress, (char*)&regData, regSize);
jurica238814 6:4d1b387c12c3 34 return regData;
jurica238814 6:4d1b387c12c3 35 }
jurica238814 6:4d1b387c12c3 36
jurica238814 4:db46d6bb60f0 37 /**
jurica238814 4:db46d6bb60f0 38 * [aconno_i2c::writeToReg description]
jurica238814 4:db46d6bb60f0 39 * @param regAddress [description]
jurica238814 4:db46d6bb60f0 40 * @param data [description]
jurica238814 4:db46d6bb60f0 41 * @param len [description]
jurica238814 4:db46d6bb60f0 42 * @return [description]
jurica238814 4:db46d6bb60f0 43 */
jurica238814 0:bfefd65ef71d 44 uint8_t aconno_i2c::writeToReg(char regAddress, char *data, int len){
jurica238814 3:b9f3eef1fad9 45 uint8_t success; /* 0 on success (ack), non-0 on failure (nack) */
jurica238814 5:54ba0e4f13ae 46 char dataToSend[len+1];
jurica238814 4:db46d6bb60f0 47
jurica238814 0:bfefd65ef71d 48 dataToSend[0] = regAddress;
jurica238814 5:54ba0e4f13ae 49 memcpy(dataToSend+1, data, len);
jurica238814 5:54ba0e4f13ae 50 // R/W bit is set low for a write command
jurica238814 5:54ba0e4f13ae 51 success = i2c->write(i2cAddress & 0xFE, dataToSend, len + 1);
jurica238814 0:bfefd65ef71d 52 return success;
jurica238814 0:bfefd65ef71d 53 }
jurica238814 0:bfefd65ef71d 54
jurica238814 4:db46d6bb60f0 55 /**
jurica238814 4:db46d6bb60f0 56 * [aconno_i2c::readFromReg description]
jurica238814 4:db46d6bb60f0 57 * @param regAddress [description]
jurica238814 4:db46d6bb60f0 58 * @param dataBuffer [description]
jurica238814 4:db46d6bb60f0 59 * @param len [description]
jurica238814 4:db46d6bb60f0 60 * @return [description]
jurica238814 4:db46d6bb60f0 61 */
jurica238814 0:bfefd65ef71d 62 uint8_t aconno_i2c::readFromReg(char regAddress, char *dataBuffer, int len){
jurica238814 0:bfefd65ef71d 63 uint8_t success; /* 0 on success (ack), non-0 on failure (nack) */
jurica238814 0:bfefd65ef71d 64 char regAddr = regAddress;
jurica238814 4:db46d6bb60f0 65
jurica238814 2:3c0eab894a4b 66 i2c->write(i2cAddress & 0xFE, &regAddr, 1); // R/W bit is set low for a write command
jurica238814 2:3c0eab894a4b 67 success = i2c->read(i2cAddress | 0x01, dataBuffer, len); // R/W bit is set high for a read command
jurica238814 0:bfefd65ef71d 68 return success;
jurica238814 0:bfefd65ef71d 69 }
jurica238814 0:bfefd65ef71d 70
jurica238814 4:db46d6bb60f0 71 /**
jurica238814 4:db46d6bb60f0 72 * This method is used with sendCommand(char *command, uint8_t len) method when delay between command
jurica238814 4:db46d6bb60f0 73 * and response from the slave is required
jurica238814 4:db46d6bb60f0 74 * @param dataBuffer [description]
jurica238814 4:db46d6bb60f0 75 * @param len [description]
jurica238814 4:db46d6bb60f0 76 * @return [description]
jurica238814 1:5ae1807e3902 77 */
jurica238814 1:5ae1807e3902 78 uint8_t aconno_i2c::readBus(char *dataBuffer, int len){
jurica238814 1:5ae1807e3902 79 uint8_t success; /* 0 on success (ack), non-0 on failure (nack) */
jurica238814 4:db46d6bb60f0 80
jurica238814 2:3c0eab894a4b 81 success = i2c->read(i2cAddress | 0x01, dataBuffer, len); // R/W bit is set high for a read command
jurica238814 1:5ae1807e3902 82 return success;
jurica238814 1:5ae1807e3902 83 }
jurica238814 1:5ae1807e3902 84
jurica238814 4:db46d6bb60f0 85 /**
jurica238814 4:db46d6bb60f0 86 * This method is used to send commands to I2C Device.
jurica238814 0:bfefd65ef71d 87 * Ex. send 2B command to Si7006 to get Device ID.
jurica238814 4:db46d6bb60f0 88 * @param command [description]
jurica238814 4:db46d6bb60f0 89 * @param len [description]
jurica238814 4:db46d6bb60f0 90 * @param response [description]
jurica238814 4:db46d6bb60f0 91 * @param responseLen [description]
jurica238814 4:db46d6bb60f0 92 * @param repeated [description]
jurica238814 4:db46d6bb60f0 93 * @return 0 on success (ack), non-0 on failure (nack)
jurica238814 0:bfefd65ef71d 94 */
jurica238814 4:db46d6bb60f0 95 uint8_t aconno_i2c::sendCommand(char *command, uint8_t len, char *response,
jurica238814 4:db46d6bb60f0 96 uint8_t responseLen, bool repeated)
jurica238814 4:db46d6bb60f0 97 {
jurica238814 0:bfefd65ef71d 98 uint8_t success; /* 0 on success (ack), non-0 on failure (nack) */
jurica238814 4:db46d6bb60f0 99
jurica238814 4:db46d6bb60f0 100 // R/W bit is set low for a write command
jurica238814 4:db46d6bb60f0 101 i2c->write(i2cAddress & 0xFE, command, len, repeated);
jurica238814 4:db46d6bb60f0 102 // R/W bit is set high for a read command
jurica238814 4:db46d6bb60f0 103 success = i2c->read(i2cAddress | 0x01, response, responseLen);
jurica238814 0:bfefd65ef71d 104 return success;
jurica238814 0:bfefd65ef71d 105 }
jurica238814 0:bfefd65ef71d 106
jurica238814 4:db46d6bb60f0 107 /**
jurica238814 4:db46d6bb60f0 108 * [aconno_i2c::sendCommand description]
jurica238814 4:db46d6bb60f0 109 * @param command [description]
jurica238814 4:db46d6bb60f0 110 * @param len [description]
jurica238814 4:db46d6bb60f0 111 * @return [description]
jurica238814 4:db46d6bb60f0 112 */
jurica238814 1:5ae1807e3902 113 uint8_t aconno_i2c::sendCommand(char *command, uint8_t len){
jurica238814 1:5ae1807e3902 114 uint8_t success; /* 0 on success (ack), non-0 on failure (nack) */
jurica238814 4:db46d6bb60f0 115
jurica238814 2:3c0eab894a4b 116 success = i2c->write(i2cAddress & 0xFE, command, len);
jurica238814 1:5ae1807e3902 117 return success;
jurica238814 1:5ae1807e3902 118 }