An improved version of mbed's I2C class.

Dependents:   acd52832_Humidity_Temp_Example BB acnSensa_LIS aconnoCellularGnss ... more

Committer:
jurica238814
Date:
Tue Sep 26 12:03:09 2017 +0000
Revision:
2:3c0eab894a4b
Parent:
1:5ae1807e3902
Child:
3:b9f3eef1fad9
I2C inheritance removed.

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 2:3c0eab894a4b 12 aconno_i2c::aconno_i2c(I2C *i2c, char address){
jurica238814 2:3c0eab894a4b 13 this->i2c = i2c;
jurica238814 0:bfefd65ef71d 14 i2cAddress = address;
jurica238814 2:3c0eab894a4b 15 }
jurica238814 0:bfefd65ef71d 16
jurica238814 0:bfefd65ef71d 17 uint8_t aconno_i2c::writeToReg(char regAddress, char *data, int len){
jurica238814 0:bfefd65ef71d 18 uint8_t success; /* '0' - NAK was received '1' - ACK was received, '2' - timeout */
jurica238814 0:bfefd65ef71d 19 char dataToSend[2];
jurica238814 0:bfefd65ef71d 20
jurica238814 0:bfefd65ef71d 21 dataToSend[0] = regAddress;
jurica238814 0:bfefd65ef71d 22 dataToSend[1] = *data;
jurica238814 2:3c0eab894a4b 23 success = i2c->write(i2cAddress & 0xFE, dataToSend, len + 1); // R/W bit is set low for a write command
jurica238814 0:bfefd65ef71d 24 return success;
jurica238814 0:bfefd65ef71d 25 }
jurica238814 0:bfefd65ef71d 26
jurica238814 0:bfefd65ef71d 27 uint8_t aconno_i2c::readFromReg(char regAddress, char *dataBuffer, int len){
jurica238814 0:bfefd65ef71d 28 uint8_t success; /* 0 on success (ack), non-0 on failure (nack) */
jurica238814 0:bfefd65ef71d 29 char regAddr = regAddress;
jurica238814 0:bfefd65ef71d 30
jurica238814 2:3c0eab894a4b 31 i2c->write(i2cAddress & 0xFE, &regAddr, 1); // R/W bit is set low for a write command
jurica238814 2:3c0eab894a4b 32 success = i2c->read(i2cAddress | 0x01, dataBuffer, len); // R/W bit is set high for a read command
jurica238814 0:bfefd65ef71d 33 return success;
jurica238814 0:bfefd65ef71d 34 }
jurica238814 0:bfefd65ef71d 35
jurica238814 1:5ae1807e3902 36 /*
jurica238814 1:5ae1807e3902 37 * This method is used with sendCommand(char *command, uint8_t len) method when delay between command
jurica238814 1:5ae1807e3902 38 * and response from the slave is required
jurica238814 1:5ae1807e3902 39 */
jurica238814 1:5ae1807e3902 40 uint8_t aconno_i2c::readBus(char *dataBuffer, int len){
jurica238814 1:5ae1807e3902 41 uint8_t success; /* 0 on success (ack), non-0 on failure (nack) */
jurica238814 1:5ae1807e3902 42
jurica238814 2:3c0eab894a4b 43 success = i2c->read(i2cAddress | 0x01, dataBuffer, len); // R/W bit is set high for a read command
jurica238814 1:5ae1807e3902 44 return success;
jurica238814 1:5ae1807e3902 45 }
jurica238814 1:5ae1807e3902 46
jurica238814 0:bfefd65ef71d 47 /*
jurica238814 0:bfefd65ef71d 48 * This method is used to send commands to I2C Device.
jurica238814 0:bfefd65ef71d 49 * Ex. send 2B command to Si7006 to get Device ID.
jurica238814 0:bfefd65ef71d 50 */
jurica238814 0:bfefd65ef71d 51 uint8_t aconno_i2c::sendCommand(char *command, uint8_t len, char *response, uint8_t responseLen){
jurica238814 0:bfefd65ef71d 52 uint8_t success; /* 0 on success (ack), non-0 on failure (nack) */
jurica238814 0:bfefd65ef71d 53
jurica238814 2:3c0eab894a4b 54 i2c->write(i2cAddress & 0xFE, command, len); // R/W bit is set low for a write command
jurica238814 2:3c0eab894a4b 55 success = i2c->read(i2cAddress | 0x01, response, responseLen); // R/W bit is set high for a read command
jurica238814 0:bfefd65ef71d 56 return success;
jurica238814 0:bfefd65ef71d 57 }
jurica238814 0:bfefd65ef71d 58
jurica238814 1:5ae1807e3902 59 uint8_t aconno_i2c::sendCommand(char *command, uint8_t len){
jurica238814 1:5ae1807e3902 60 uint8_t success; /* 0 on success (ack), non-0 on failure (nack) */
jurica238814 1:5ae1807e3902 61
jurica238814 2:3c0eab894a4b 62 success = i2c->write(i2cAddress & 0xFE, command, len);
jurica238814 1:5ae1807e3902 63 return success;
jurica238814 1:5ae1807e3902 64 }
jurica238814 0:bfefd65ef71d 65