An improved version of mbed's I2C class.

Dependents:   acd52832_Humidity_Temp_Example BB acnSensa_LIS aconnoCellularGnss ... more

Committer:
jurica238814
Date:
Thu Jul 19 12:48:11 2018 +0200
Revision:
5:54ba0e4f13ae
Parent:
4:db46d6bb60f0
Child:
6:4d1b387c12c3
Write2Reg fixed

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