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