An improved version of mbed's I2C class.
Dependents: acd52832_Humidity_Temp_Example BB acnSensa_LIS aconnoCellularGnss ... more
aconno_i2c.cpp@0:bfefd65ef71d, 2017-09-22 (annotated)
- Committer:
- jurica238814
- Date:
- Fri Sep 22 12:46:17 2017 +0000
- Revision:
- 0:bfefd65ef71d
- Child:
- 1:5ae1807e3902
Init commit. Basic methods added.
Who changed what in which revision?
User | Revision | Line number | New 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, ®Addr, 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 | 0:bfefd65ef71d | 35 | /* |
jurica238814 | 0:bfefd65ef71d | 36 | * This method is used to send commands to I2C Device. |
jurica238814 | 0:bfefd65ef71d | 37 | * Ex. send 2B command to Si7006 to get Device ID. |
jurica238814 | 0:bfefd65ef71d | 38 | */ |
jurica238814 | 0:bfefd65ef71d | 39 | uint8_t aconno_i2c::sendCommand(char *command, uint8_t len, char *response, uint8_t responseLen){ |
jurica238814 | 0:bfefd65ef71d | 40 | uint8_t success; /* 0 on success (ack), non-0 on failure (nack) */ |
jurica238814 | 0:bfefd65ef71d | 41 | |
jurica238814 | 0:bfefd65ef71d | 42 | write(i2cAddress & 0xFE, command, len); // R/W bit is set low for a write command |
jurica238814 | 0:bfefd65ef71d | 43 | success = read(i2cAddress | 0x01, response, responseLen); // R/W bit is set high for a read command |
jurica238814 | 0:bfefd65ef71d | 44 | return success; |
jurica238814 | 0:bfefd65ef71d | 45 | } |
jurica238814 | 0:bfefd65ef71d | 46 | |
jurica238814 | 0:bfefd65ef71d | 47 |