![](/media/cache/profiles/eb1feb5ab28dec83652dd1e5c673f256.jpg.50x50_q85.jpg)
LTC2945 ported
LT_I2C.cpp@1:6d4265aa4184, 2017-08-23 (annotated)
- Committer:
- odtulumbedder
- Date:
- Wed Aug 23 11:05:46 2017 +0000
- Revision:
- 1:6d4265aa4184
- Parent:
- 0:6be57f391716
- Child:
- 2:7e390bcce297
DEBUGGING STUFF
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
odtulumbedder | 0:6be57f391716 | 1 | #include "LT_I2C.h" |
odtulumbedder | 0:6be57f391716 | 2 | |
odtulumbedder | 0:6be57f391716 | 3 | /* mbed headers */ |
odtulumbedder | 0:6be57f391716 | 4 | #include <mbed.h> |
odtulumbedder | 0:6be57f391716 | 5 | #include <I2C.h> |
odtulumbedder | 0:6be57f391716 | 6 | |
odtulumbedder | 1:6d4265aa4184 | 7 | /* for debugging */ |
odtulumbedder | 1:6d4265aa4184 | 8 | #ifdef LT_I2C_DEBUG |
odtulumbedder | 1:6d4265aa4184 | 9 | #define ASSERT(x) if (! (x)) { printf("%s:%d %s failed!\n", __FILE__, __LINE__, #x); } |
odtulumbedder | 1:6d4265aa4184 | 10 | #define DEBUG_MSG(x) printf("debugging: %s:%d %s\n", __FILE__, __LINE__, x); |
odtulumbedder | 1:6d4265aa4184 | 11 | #else |
odtulumbedder | 1:6d4265aa4184 | 12 | #define ASSERT(x) |
odtulumbedder | 1:6d4265aa4184 | 13 | #define DEBUG_MSG(x) |
odtulumbedder | 1:6d4265aa4184 | 14 | #endif |
odtulumbedder | 1:6d4265aa4184 | 15 | |
odtulumbedder | 0:6be57f391716 | 16 | extern "C" { |
odtulumbedder | 0:6be57f391716 | 17 | |
odtulumbedder | 0:6be57f391716 | 18 | static I2C *i2c_object = NULL; |
odtulumbedder | 0:6be57f391716 | 19 | |
odtulumbedder | 0:6be57f391716 | 20 | int lt_i2c_init(int sda, int scl) { |
odtulumbedder | 1:6d4265aa4184 | 21 | DEBUG_MSG(__PRETTY_FUNCTION__) |
odtulumbedder | 0:6be57f391716 | 22 | if (i2c_object == NULL) { |
odtulumbedder | 0:6be57f391716 | 23 | i2c_object = new I2C((PinName) sda, (PinName) scl); |
odtulumbedder | 0:6be57f391716 | 24 | return LT_I2C_INIT_FINE; |
odtulumbedder | 0:6be57f391716 | 25 | } |
odtulumbedder | 0:6be57f391716 | 26 | else { |
odtulumbedder | 0:6be57f391716 | 27 | return LT_I2C_INIT_QUIT_FIRST; |
odtulumbedder | 0:6be57f391716 | 28 | } |
odtulumbedder | 0:6be57f391716 | 29 | } |
odtulumbedder | 0:6be57f391716 | 30 | |
odtulumbedder | 0:6be57f391716 | 31 | int lt_i2c_quit() { |
odtulumbedder | 1:6d4265aa4184 | 32 | DEBUG_MSG(__PRETTY_FUNCTION__) |
odtulumbedder | 0:6be57f391716 | 33 | delete i2c_object; |
odtulumbedder | 0:6be57f391716 | 34 | return LT_I2C_SUCCESS; |
odtulumbedder | 0:6be57f391716 | 35 | } |
odtulumbedder | 0:6be57f391716 | 36 | |
odtulumbedder | 0:6be57f391716 | 37 | int lt_i2c_init_attach(void *p_i2c_object) { |
odtulumbedder | 1:6d4265aa4184 | 38 | DEBUG_MSG(__PRETTY_FUNCTION__) |
odtulumbedder | 0:6be57f391716 | 39 | if (i2c_object == NULL) { |
odtulumbedder | 0:6be57f391716 | 40 | i2c_object = (I2C *) p_i2c_object; |
odtulumbedder | 0:6be57f391716 | 41 | return LT_I2C_INIT_FINE; |
odtulumbedder | 0:6be57f391716 | 42 | } |
odtulumbedder | 0:6be57f391716 | 43 | else { |
odtulumbedder | 0:6be57f391716 | 44 | return LT_I2C_INIT_QUIT_FIRST; |
odtulumbedder | 0:6be57f391716 | 45 | } |
odtulumbedder | 0:6be57f391716 | 46 | } |
odtulumbedder | 0:6be57f391716 | 47 | |
odtulumbedder | 0:6be57f391716 | 48 | void *lt_i2c_get_i2c_object() { |
odtulumbedder | 1:6d4265aa4184 | 49 | DEBUG_MSG(__PRETTY_FUNCTION__) |
odtulumbedder | 0:6be57f391716 | 50 | return i2c_object; |
odtulumbedder | 0:6be57f391716 | 51 | } |
odtulumbedder | 0:6be57f391716 | 52 | |
odtulumbedder | 0:6be57f391716 | 53 | #define CHECK_OBJECT if (i2c_object == NULL) return LT_I2C_FAILURE; |
odtulumbedder | 0:6be57f391716 | 54 | |
odtulumbedder | 0:6be57f391716 | 55 | int lt_i2c_start() { |
odtulumbedder | 1:6d4265aa4184 | 56 | DEBUG_MSG(__PRETTY_FUNCTION__) |
odtulumbedder | 0:6be57f391716 | 57 | CHECK_OBJECT |
odtulumbedder | 0:6be57f391716 | 58 | i2c_object->start(); |
odtulumbedder | 0:6be57f391716 | 59 | return LT_I2C_SUCCESS; |
odtulumbedder | 0:6be57f391716 | 60 | } |
odtulumbedder | 0:6be57f391716 | 61 | |
odtulumbedder | 0:6be57f391716 | 62 | int lt_i2c_stop() { |
odtulumbedder | 1:6d4265aa4184 | 63 | DEBUG_MSG(__PRETTY_FUNCTION__) |
odtulumbedder | 0:6be57f391716 | 64 | CHECK_OBJECT |
odtulumbedder | 0:6be57f391716 | 65 | i2c_object->stop(); |
odtulumbedder | 0:6be57f391716 | 66 | return LT_I2C_SUCCESS; |
odtulumbedder | 0:6be57f391716 | 67 | } |
odtulumbedder | 0:6be57f391716 | 68 | |
odtulumbedder | 0:6be57f391716 | 69 | uint8_t lt_i2c_write(uint8_t data) { |
odtulumbedder | 1:6d4265aa4184 | 70 | DEBUG_MSG(__PRETTY_FUNCTION__) |
odtulumbedder | 0:6be57f391716 | 71 | CHECK_OBJECT |
odtulumbedder | 0:6be57f391716 | 72 | return 1-i2c_object->write(data); // I wish casting will work fine |
odtulumbedder | 0:6be57f391716 | 73 | // must return 0 if ACK was received (1- is due that) |
odtulumbedder | 0:6be57f391716 | 74 | } |
odtulumbedder | 0:6be57f391716 | 75 | |
odtulumbedder | 0:6be57f391716 | 76 | uint8_t lt_i2c_read(int8_t ack) { |
odtulumbedder | 1:6d4265aa4184 | 77 | DEBUG_MSG(__PRETTY_FUNCTION__) |
odtulumbedder | 0:6be57f391716 | 78 | CHECK_OBJECT |
odtulumbedder | 0:6be57f391716 | 79 | return (uint8_t) (i2c_object->read(1-ack)); // act is different in mbed |
odtulumbedder | 0:6be57f391716 | 80 | } |
odtulumbedder | 0:6be57f391716 | 81 | |
odtulumbedder | 0:6be57f391716 | 82 | int8_t lt_i2c_write_byte_data( |
odtulumbedder | 0:6be57f391716 | 83 | uint8_t address, |
odtulumbedder | 0:6be57f391716 | 84 | uint8_t command, |
odtulumbedder | 0:6be57f391716 | 85 | uint8_t value) { |
odtulumbedder | 1:6d4265aa4184 | 86 | DEBUG_MSG(__PRETTY_FUNCTION__) |
odtulumbedder | 0:6be57f391716 | 87 | CHECK_OBJECT |
odtulumbedder | 0:6be57f391716 | 88 | int8_t ret = 0; |
odtulumbedder | 0:6be57f391716 | 89 | if (lt_i2c_start()!=0) //I2C START |
odtulumbedder | 1:6d4265aa4184 | 90 | { |
odtulumbedder | 1:6d4265aa4184 | 91 | DEBUG_MSG("START FAIL") |
odtulumbedder | 0:6be57f391716 | 92 | return(1); //Stop and return 0 if START fail |
odtulumbedder | 1:6d4265aa4184 | 93 | } |
odtulumbedder | 0:6be57f391716 | 94 | ret |= lt_i2c_write((address<<1)|LT_I2C_WRITE_BIT); |
odtulumbedder | 0:6be57f391716 | 95 | // Write 7 bit address with W bit |
odtulumbedder | 0:6be57f391716 | 96 | ret|= lt_i2c_write(command); // Set register to be read to command |
odtulumbedder | 0:6be57f391716 | 97 | ret|= lt_i2c_write(value); |
odtulumbedder | 0:6be57f391716 | 98 | lt_i2c_stop(); // I2C STOP |
odtulumbedder | 0:6be57f391716 | 99 | if (ret!=0) //If there was a NAK return 1 |
odtulumbedder | 1:6d4265aa4184 | 100 | { |
odtulumbedder | 1:6d4265aa4184 | 101 | DEBUG_MSG("NACK") |
odtulumbedder | 0:6be57f391716 | 102 | return(1); |
odtulumbedder | 1:6d4265aa4184 | 103 | } |
odtulumbedder | 1:6d4265aa4184 | 104 | DEBUG_MSG("OK") |
odtulumbedder | 0:6be57f391716 | 105 | return(0); // Return success |
odtulumbedder | 0:6be57f391716 | 106 | } |
odtulumbedder | 0:6be57f391716 | 107 | |
odtulumbedder | 0:6be57f391716 | 108 | int8_t lt_i2c_write_word_data( |
odtulumbedder | 0:6be57f391716 | 109 | uint8_t address, |
odtulumbedder | 0:6be57f391716 | 110 | uint8_t command, |
odtulumbedder | 0:6be57f391716 | 111 | uint16_t value) { |
odtulumbedder | 1:6d4265aa4184 | 112 | DEBUG_MSG(__PRETTY_FUNCTION__) |
odtulumbedder | 0:6be57f391716 | 113 | CHECK_OBJECT |
odtulumbedder | 0:6be57f391716 | 114 | int8_t ret=0; |
odtulumbedder | 0:6be57f391716 | 115 | union |
odtulumbedder | 0:6be57f391716 | 116 | { |
odtulumbedder | 0:6be57f391716 | 117 | uint8_t b[2]; |
odtulumbedder | 0:6be57f391716 | 118 | uint16_t w; |
odtulumbedder | 0:6be57f391716 | 119 | } data; |
odtulumbedder | 0:6be57f391716 | 120 | data.w = value; |
odtulumbedder | 0:6be57f391716 | 121 | if (lt_i2c_start()!=0) //I2C START |
odtulumbedder | 1:6d4265aa4184 | 122 | { |
odtulumbedder | 1:6d4265aa4184 | 123 | DEBUG_MSG("START FAIL") |
odtulumbedder | 0:6be57f391716 | 124 | return(1); //Stop and return 0 if START fail |
odtulumbedder | 1:6d4265aa4184 | 125 | } |
odtulumbedder | 0:6be57f391716 | 126 | ret |= lt_i2c_write((address<<1)|LT_I2C_WRITE_BIT); |
odtulumbedder | 0:6be57f391716 | 127 | // Write 7 bit address with W bit |
odtulumbedder | 0:6be57f391716 | 128 | ret|= lt_i2c_write(command); // Set register to be read to command |
odtulumbedder | 0:6be57f391716 | 129 | ret|= lt_i2c_write(data.b[1]); //Write MSB |
odtulumbedder | 0:6be57f391716 | 130 | ret|= lt_i2c_write(data.b[0]); //Write LSB; |
odtulumbedder | 0:6be57f391716 | 131 | lt_i2c_stop(); // I2C STOP |
odtulumbedder | 0:6be57f391716 | 132 | if (ret!=0) //If there was a NAK return 1 |
odtulumbedder | 1:6d4265aa4184 | 133 | { |
odtulumbedder | 1:6d4265aa4184 | 134 | DEBUG_MSG("NACK") |
odtulumbedder | 0:6be57f391716 | 135 | return(1); |
odtulumbedder | 1:6d4265aa4184 | 136 | } |
odtulumbedder | 1:6d4265aa4184 | 137 | DEBUG_MSG("OK") |
odtulumbedder | 0:6be57f391716 | 138 | return(0); |
odtulumbedder | 0:6be57f391716 | 139 | } |
odtulumbedder | 0:6be57f391716 | 140 | |
odtulumbedder | 0:6be57f391716 | 141 | uint8_t lt_i2c_write_block_data( |
odtulumbedder | 0:6be57f391716 | 142 | uint8_t address, |
odtulumbedder | 0:6be57f391716 | 143 | uint8_t command, |
odtulumbedder | 0:6be57f391716 | 144 | uint8_t length, |
odtulumbedder | 0:6be57f391716 | 145 | uint8_t *values) { |
odtulumbedder | 1:6d4265aa4184 | 146 | DEBUG_MSG(__PRETTY_FUNCTION__) |
odtulumbedder | 0:6be57f391716 | 147 | CHECK_OBJECT |
odtulumbedder | 0:6be57f391716 | 148 | int8_t i = length-1; |
odtulumbedder | 0:6be57f391716 | 149 | int8_t ret = 0; |
odtulumbedder | 0:6be57f391716 | 150 | if (lt_i2c_start()!=0) //I2C START |
odtulumbedder | 0:6be57f391716 | 151 | return(1); //Stop and return 0 if START fail |
odtulumbedder | 0:6be57f391716 | 152 | ret |= lt_i2c_write((address<<1)|LT_I2C_WRITE_BIT); |
odtulumbedder | 0:6be57f391716 | 153 | // Write 7 bit address with W bit |
odtulumbedder | 0:6be57f391716 | 154 | ret|= lt_i2c_write(command); // Set register to be read to command |
odtulumbedder | 0:6be57f391716 | 155 | while (i>=0) |
odtulumbedder | 0:6be57f391716 | 156 | { |
odtulumbedder | 0:6be57f391716 | 157 | ret|= lt_i2c_write(values[i]); //Write Value |
odtulumbedder | 0:6be57f391716 | 158 | i--; |
odtulumbedder | 0:6be57f391716 | 159 | } |
odtulumbedder | 0:6be57f391716 | 160 | lt_i2c_stop(); // I2C STOP |
odtulumbedder | 0:6be57f391716 | 161 | if (ret!=0) |
odtulumbedder | 1:6d4265aa4184 | 162 | { |
odtulumbedder | 1:6d4265aa4184 | 163 | DEBUG_MSG("ERROR") |
odtulumbedder | 0:6be57f391716 | 164 | return(1); |
odtulumbedder | 1:6d4265aa4184 | 165 | } |
odtulumbedder | 0:6be57f391716 | 166 | else |
odtulumbedder | 1:6d4265aa4184 | 167 | { |
odtulumbedder | 1:6d4265aa4184 | 168 | DEBUG_MSG("OK") |
odtulumbedder | 0:6be57f391716 | 169 | return(0); // Success! |
odtulumbedder | 1:6d4265aa4184 | 170 | } |
odtulumbedder | 0:6be57f391716 | 171 | } |
odtulumbedder | 0:6be57f391716 | 172 | |
odtulumbedder | 0:6be57f391716 | 173 | uint8_t lt_i2c_read_byte_data( |
odtulumbedder | 0:6be57f391716 | 174 | uint8_t address, |
odtulumbedder | 0:6be57f391716 | 175 | uint8_t command, |
odtulumbedder | 0:6be57f391716 | 176 | uint8_t *value) { |
odtulumbedder | 1:6d4265aa4184 | 177 | DEBUG_MSG(__PRETTY_FUNCTION__) |
odtulumbedder | 0:6be57f391716 | 178 | CHECK_OBJECT |
odtulumbedder | 0:6be57f391716 | 179 | int8_t ret = 0; |
odtulumbedder | 0:6be57f391716 | 180 | if (lt_i2c_start() != 0) // I2C START |
odtulumbedder | 0:6be57f391716 | 181 | { |
odtulumbedder | 1:6d4265aa4184 | 182 | DEBUG_MSG("START FAIL") |
odtulumbedder | 0:6be57f391716 | 183 | return (1); // Stop and return 0 if START fail |
odtulumbedder | 0:6be57f391716 | 184 | } |
odtulumbedder | 0:6be57f391716 | 185 | ret |= lt_i2c_write((address << 1) | LT_I2C_WRITE_BIT); // Write 7 bit address with W bit |
odtulumbedder | 0:6be57f391716 | 186 | if (ret != 0) // If NACK return 1 |
odtulumbedder | 0:6be57f391716 | 187 | { |
odtulumbedder | 1:6d4265aa4184 | 188 | DEBUG_MSG("NACK") |
odtulumbedder | 0:6be57f391716 | 189 | lt_i2c_stop(); // I2C STOP |
odtulumbedder | 0:6be57f391716 | 190 | return (1); |
odtulumbedder | 0:6be57f391716 | 191 | } |
odtulumbedder | 0:6be57f391716 | 192 | ret |= lt_i2c_write(command); // Set register to be read to command |
odtulumbedder | 0:6be57f391716 | 193 | if (ret != 0) // If NACK return 1 |
odtulumbedder | 0:6be57f391716 | 194 | { |
odtulumbedder | 1:6d4265aa4184 | 195 | DEBUG_MSG("NACK") |
odtulumbedder | 0:6be57f391716 | 196 | lt_i2c_stop(); // I2C STOP |
odtulumbedder | 0:6be57f391716 | 197 | return (1); |
odtulumbedder | 0:6be57f391716 | 198 | } |
odtulumbedder | 0:6be57f391716 | 199 | if (lt_i2c_start() != 0) // I2C repeated START |
odtulumbedder | 0:6be57f391716 | 200 | { |
odtulumbedder | 1:6d4265aa4184 | 201 | DEBUG_MSG("NACK") |
odtulumbedder | 0:6be57f391716 | 202 | lt_i2c_stop(); // Attempt to issue I2C STOP |
odtulumbedder | 0:6be57f391716 | 203 | return (1); // Stop and return 0 if START fail |
odtulumbedder | 0:6be57f391716 | 204 | } |
odtulumbedder | 0:6be57f391716 | 205 | ret |= lt_i2c_write((address << 1) | LT_I2C_READ_BIT); // Write 7 bit address with R bit |
odtulumbedder | 0:6be57f391716 | 206 | *value = lt_i2c_read(LT_I2C_WITH_NACK); // Read byte from buffer with NAK |
odtulumbedder | 0:6be57f391716 | 207 | lt_i2c_stop(); // I2C STOP |
odtulumbedder | 0:6be57f391716 | 208 | if (ret != 0) // If there was a NACK return 1 |
odtulumbedder | 0:6be57f391716 | 209 | { |
odtulumbedder | 1:6d4265aa4184 | 210 | DEBUG_MSG("NACK") |
odtulumbedder | 0:6be57f391716 | 211 | return (1); |
odtulumbedder | 0:6be57f391716 | 212 | } |
odtulumbedder | 1:6d4265aa4184 | 213 | DEBUG_MSG("OK") |
odtulumbedder | 0:6be57f391716 | 214 | return (0); // Return success |
odtulumbedder | 0:6be57f391716 | 215 | } |
odtulumbedder | 0:6be57f391716 | 216 | |
odtulumbedder | 0:6be57f391716 | 217 | uint8_t lt_i2c_read_word_data( |
odtulumbedder | 0:6be57f391716 | 218 | uint8_t address, |
odtulumbedder | 0:6be57f391716 | 219 | uint8_t command, |
odtulumbedder | 0:6be57f391716 | 220 | uint16_t *value) { |
odtulumbedder | 1:6d4265aa4184 | 221 | DEBUG_MSG(__PRETTY_FUNCTION__) |
odtulumbedder | 0:6be57f391716 | 222 | CHECK_OBJECT |
odtulumbedder | 0:6be57f391716 | 223 | int8_t ret = 0; |
odtulumbedder | 0:6be57f391716 | 224 | union { |
odtulumbedder | 0:6be57f391716 | 225 | uint8_t b[2]; |
odtulumbedder | 0:6be57f391716 | 226 | uint16_t w; |
odtulumbedder | 0:6be57f391716 | 227 | } data; |
odtulumbedder | 0:6be57f391716 | 228 | if (lt_i2c_start() != 0) //I2C START |
odtulumbedder | 1:6d4265aa4184 | 229 | { |
odtulumbedder | 1:6d4265aa4184 | 230 | DEBUG_MSG("START FAIL") |
odtulumbedder | 0:6be57f391716 | 231 | return (1); //Stop and return 0 if START fail |
odtulumbedder | 1:6d4265aa4184 | 232 | } |
odtulumbedder | 0:6be57f391716 | 233 | ret |= lt_i2c_write((address << 1) | LT_I2C_WRITE_BIT); // Write 7 bit address with W bit |
odtulumbedder | 0:6be57f391716 | 234 | if (ret != 0) //If NACK return 1 |
odtulumbedder | 0:6be57f391716 | 235 | { |
odtulumbedder | 1:6d4265aa4184 | 236 | DEBUG_MSG("NACK") |
odtulumbedder | 0:6be57f391716 | 237 | lt_i2c_stop(); //I2C STOP |
odtulumbedder | 0:6be57f391716 | 238 | return (1); |
odtulumbedder | 0:6be57f391716 | 239 | } |
odtulumbedder | 0:6be57f391716 | 240 | ret |= lt_i2c_write(command); // Set register to be read to command |
odtulumbedder | 0:6be57f391716 | 241 | if (ret != 0) //If NACK return 1 |
odtulumbedder | 0:6be57f391716 | 242 | { |
odtulumbedder | 1:6d4265aa4184 | 243 | DEBUG_MSG("NACK") |
odtulumbedder | 0:6be57f391716 | 244 | lt_i2c_stop(); //I2C STOP |
odtulumbedder | 0:6be57f391716 | 245 | return (1); |
odtulumbedder | 0:6be57f391716 | 246 | } |
odtulumbedder | 0:6be57f391716 | 247 | if (lt_i2c_start() != 0) //I2C START |
odtulumbedder | 0:6be57f391716 | 248 | { |
odtulumbedder | 1:6d4265aa4184 | 249 | DEBUG_MSG("START FAIL") |
odtulumbedder | 0:6be57f391716 | 250 | lt_i2c_stop(); //Attempt to issue I2C STOP |
odtulumbedder | 0:6be57f391716 | 251 | return (1); //Stop and return 0 if START fail |
odtulumbedder | 0:6be57f391716 | 252 | } |
odtulumbedder | 0:6be57f391716 | 253 | ret |= lt_i2c_write((address << 1) | LT_I2C_READ_BIT); // Write 7 bit address with R bit |
odtulumbedder | 0:6be57f391716 | 254 | data.b[1] = lt_i2c_read(LT_I2C_WITH_ACK); // Read MSB from buffer |
odtulumbedder | 0:6be57f391716 | 255 | data.b[0] = lt_i2c_read(LT_I2C_WITH_NACK); // Read LSB from buffer |
odtulumbedder | 0:6be57f391716 | 256 | lt_i2c_stop(); //I2C STOP |
odtulumbedder | 0:6be57f391716 | 257 | *value = data.w; |
odtulumbedder | 0:6be57f391716 | 258 | if (ret != 0) //If NAK |
odtulumbedder | 1:6d4265aa4184 | 259 | { |
odtulumbedder | 1:6d4265aa4184 | 260 | DEBUG_MSG("NACK") |
odtulumbedder | 0:6be57f391716 | 261 | return (1); //return 1 |
odtulumbedder | 1:6d4265aa4184 | 262 | } |
odtulumbedder | 1:6d4265aa4184 | 263 | DEBUG_MSG("OK") |
odtulumbedder | 0:6be57f391716 | 264 | return (0); // Return success |
odtulumbedder | 0:6be57f391716 | 265 | } |
odtulumbedder | 0:6be57f391716 | 266 | |
odtulumbedder | 0:6be57f391716 | 267 | uint8_t lt_i2c_read_block_data( |
odtulumbedder | 0:6be57f391716 | 268 | uint8_t address, |
odtulumbedder | 0:6be57f391716 | 269 | uint8_t command, |
odtulumbedder | 0:6be57f391716 | 270 | uint8_t length, |
odtulumbedder | 0:6be57f391716 | 271 | uint8_t *values) { |
odtulumbedder | 1:6d4265aa4184 | 272 | DEBUG_MSG(__PRETTY_FUNCTION__) |
odtulumbedder | 0:6be57f391716 | 273 | CHECK_OBJECT |
odtulumbedder | 0:6be57f391716 | 274 | uint8_t i = (length - 1); |
odtulumbedder | 0:6be57f391716 | 275 | int8_t ret = 0; |
odtulumbedder | 0:6be57f391716 | 276 | if (lt_i2c_start() != 0) //I2C START |
odtulumbedder | 1:6d4265aa4184 | 277 | { |
odtulumbedder | 1:6d4265aa4184 | 278 | DEBUG_MSG("START FAIL") |
odtulumbedder | 0:6be57f391716 | 279 | return (1); //Stop and return 0 if START fail |
odtulumbedder | 1:6d4265aa4184 | 280 | } |
odtulumbedder | 0:6be57f391716 | 281 | ret |= lt_i2c_write((address << 1) | LT_I2C_WRITE_BIT); //Write 7-bit address with W bit |
odtulumbedder | 0:6be57f391716 | 282 | if (ret != 0) //If NACK return 1 |
odtulumbedder | 0:6be57f391716 | 283 | { |
odtulumbedder | 1:6d4265aa4184 | 284 | DEBUG_MSG("NACK") |
odtulumbedder | 0:6be57f391716 | 285 | lt_i2c_stop(); //I2C STOP |
odtulumbedder | 0:6be57f391716 | 286 | return (1); |
odtulumbedder | 0:6be57f391716 | 287 | } |
odtulumbedder | 0:6be57f391716 | 288 | ret |= lt_i2c_write(command); //Write 8 bit command word |
odtulumbedder | 0:6be57f391716 | 289 | if (ret != 0) //If NACK return 1 |
odtulumbedder | 0:6be57f391716 | 290 | { |
odtulumbedder | 1:6d4265aa4184 | 291 | DEBUG_MSG("NACK") |
odtulumbedder | 0:6be57f391716 | 292 | lt_i2c_stop(); //I2C STOP |
odtulumbedder | 0:6be57f391716 | 293 | return (1); |
odtulumbedder | 0:6be57f391716 | 294 | } |
odtulumbedder | 0:6be57f391716 | 295 | if (lt_i2c_start() != 0) //I2C repeated START |
odtulumbedder | 0:6be57f391716 | 296 | { |
odtulumbedder | 1:6d4265aa4184 | 297 | DEBUG_MSG("START FAIL") |
odtulumbedder | 0:6be57f391716 | 298 | lt_i2c_stop(); //Attempt to issue I2C STOP |
odtulumbedder | 0:6be57f391716 | 299 | return (1); //Stop and return 0 if START fail |
odtulumbedder | 0:6be57f391716 | 300 | } |
odtulumbedder | 0:6be57f391716 | 301 | ret |= lt_i2c_write((address << 1) | LT_I2C_READ_BIT); //Write 7-bit address with R bit |
odtulumbedder | 0:6be57f391716 | 302 | if (ret != 0) //If NACK return 1 |
odtulumbedder | 0:6be57f391716 | 303 | { |
odtulumbedder | 1:6d4265aa4184 | 304 | DEBUG_MSG("NACK") |
odtulumbedder | 0:6be57f391716 | 305 | lt_i2c_stop(); //I2C STOP |
odtulumbedder | 0:6be57f391716 | 306 | return (1); |
odtulumbedder | 0:6be57f391716 | 307 | } |
odtulumbedder | 0:6be57f391716 | 308 | while (i > 0) //Begin read loop |
odtulumbedder | 0:6be57f391716 | 309 | { |
odtulumbedder | 0:6be57f391716 | 310 | values[i] = lt_i2c_read(LT_I2C_WITH_ACK); //Read from bus with ACK |
odtulumbedder | 0:6be57f391716 | 311 | i--; |
odtulumbedder | 0:6be57f391716 | 312 | } |
odtulumbedder | 0:6be57f391716 | 313 | values[0] = lt_i2c_read(LT_I2C_WITH_NACK); //Read from bus with NACK for the last one; |
odtulumbedder | 0:6be57f391716 | 314 | lt_i2c_stop(); //I2C STOP |
odtulumbedder | 1:6d4265aa4184 | 315 | DEBUG_MSG("OK") |
odtulumbedder | 0:6be57f391716 | 316 | return (0); // Success! |
odtulumbedder | 0:6be57f391716 | 317 | } |
odtulumbedder | 0:6be57f391716 | 318 | |
odtulumbedder | 0:6be57f391716 | 319 | } |