An improved version of mbed's I2C class.
Dependents: acd52832_Humidity_Temp_Example BB acnSensa_LIS aconnoCellularGnss ... more
aconno_i2c.cpp
- Committer:
- jurica238814
- Date:
- 2017-09-22
- Revision:
- 1:5ae1807e3902
- Parent:
- 0:bfefd65ef71d
- Child:
- 2:3c0eab894a4b
File content as of revision 1:5ae1807e3902:
/* * I2C library made by Jurica Resetar @ aconno * 2017 * More info @ aconno.de * jurica_resetar@yahoo.com * All right reserved * */ #include "aconno_i2c.h" aconno_i2c::aconno_i2c(PinName data, PinName clock, char address) : I2C(data, clock){ i2cAddress = address; } uint8_t aconno_i2c::writeToReg(char regAddress, char *data, int len){ uint8_t success; /* '0' - NAK was received '1' - ACK was received, '2' - timeout */ char dataToSend[2]; dataToSend[0] = regAddress; dataToSend[1] = *data; success = write(i2cAddress & 0xFE, dataToSend, len + 1); // R/W bit is set low for a write command return success; } uint8_t aconno_i2c::readFromReg(char regAddress, char *dataBuffer, int len){ uint8_t success; /* 0 on success (ack), non-0 on failure (nack) */ char regAddr = regAddress; write(i2cAddress & 0xFE, ®Addr, 1); // R/W bit is set low for a write command success = read(i2cAddress | 0x01, dataBuffer, len); // R/W bit is set high for a read command return success; } /* * This method is used with sendCommand(char *command, uint8_t len) method when delay between command * and response from the slave is required */ uint8_t aconno_i2c::readBus(char *dataBuffer, int len){ uint8_t success; /* 0 on success (ack), non-0 on failure (nack) */ success = read(i2cAddress | 0x01, dataBuffer, len); // R/W bit is set high for a read command return success; } /* * This method is used to send commands to I2C Device. * Ex. send 2B command to Si7006 to get Device ID. */ uint8_t aconno_i2c::sendCommand(char *command, uint8_t len, char *response, uint8_t responseLen){ uint8_t success; /* 0 on success (ack), non-0 on failure (nack) */ write(i2cAddress & 0xFE, command, len); // R/W bit is set low for a write command success = read(i2cAddress | 0x01, response, responseLen); // R/W bit is set high for a read command return success; } uint8_t aconno_i2c::sendCommand(char *command, uint8_t len){ uint8_t success; /* 0 on success (ack), non-0 on failure (nack) */ success = write(i2cAddress & 0xFE, command, len); return success; }