An improved version of mbed's I2C class.

Dependents:   acd52832_Humidity_Temp_Example BB acnSensa_LIS aconnoCellularGnss ... more

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